Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 96ddcf6

Browse files
committed
refactor(repl): block window functions that do not work in headless mode
1 parent 0819657 commit 96ddcf6

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

bin/repl.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ async function evaluate (cmd, ctx, file, callback) {
230230
for (const node of root) {
231231
if (node.id?.name) {
232232
names.push(node.id.name)
233+
} else if (node.declarations) {
234+
for (const declaration of node.declarations) {
235+
if (declaration.id?.name) {
236+
names.push(declaration.id.name)
237+
}
238+
}
233239
} else {
234240
names.push(null)
235241
}
@@ -245,7 +251,10 @@ async function evaluate (cmd, ctx, file, callback) {
245251
} else if (lastName) {
246252
cmd = `${cmd}; io.util.format(${lastName});`
247253
} else if (!/^\s*((throw\s)|(with\s*\()|(try\s*{)|(const\s)|(let\s)|(var\s)|(if\s*\()|(for\s*\()|(while\s*\()|(do\s*{)|(return\s)|(import\s*\())/.test(cmd)) {
248-
cmd = `io.util.format(${cmd});`
254+
cmd = cmd.split(';').map((c) => {
255+
if (/^\s*\{/.test(c)) { return c }
256+
return `io.util.format(${c});`
257+
}).join('\n')
249258
}
250259
}
251260

repl/context.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,30 @@ export function init (opts) {
3030

3131
didInit = true
3232

33+
const disabledFunctions = [
34+
'alert',
35+
'blur',
36+
'close',
37+
'confirm',
38+
'focus',
39+
'moveBy',
40+
'moveTo',
41+
'openDialog',
42+
'print',
43+
'prompt',
44+
'resizeBy',
45+
'resizeTo',
46+
'scrollTo',
47+
'scroll',
48+
'stop'
49+
]
50+
3351
window.io = io
3452

53+
for (const fn of disabledFunctions) {
54+
window[fn] = () => console.warn(`WARN: ${fn}() is not available in the REPL context`)
55+
}
56+
3557
for (const key in io) {
3658
if (window[key] !== undefined) { continue }
3759
Object.defineProperty(window, key, {
@@ -89,15 +111,17 @@ function makeError (err) {
89111
export async function evaluate ({ cmd, id }) {
90112
try {
91113
if (/\s*await\s*import\s*\(/.test(cmd)) {
92-
const value = await new AsyncFunction(`return (${cmd})`)()
114+
cmd = cmd.replace(/^\s*(let|const|var)\s+/, '')
115+
const value = await new AsyncFunction(`(${cmd})`)()
93116
await ipc.send('repl.eval.result', {
94117
id,
95118
error: false,
96119
value: JSON.stringify({ data: io.util.format(value) })
97120
})
98121
return
99122
} else if (/\s*import\s*\(/.test(cmd)) {
100-
const value = new AsyncFunction(`return (${cmd})`)()
123+
cmd = cmd.replace(/^\s*(let|const|var)\s+/, '')
124+
const value = new AsyncFunction(`(${cmd})`)()
101125
await ipc.send('repl.eval.result', {
102126
id,
103127
error: false,

0 commit comments

Comments
 (0)