Skip to content

Commit ee58d60

Browse files
fix: ensure no node globals passively leak when nodeIntegration is disabled (electron#21342)
1 parent 66035a2 commit ee58d60

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/renderer/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ if (nodeIntegration) {
192192
delete global.setImmediate
193193
delete global.clearImmediate
194194
delete global.global
195+
delete global.root
196+
delete global.GLOBAL
195197
})
196198
}
197199
}

spec-main/api-browser-window-spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,37 @@ describe('BrowserWindow module', () => {
15431543
sandbox: true,
15441544
contextIsolation: true
15451545
})
1546+
it('does not leak any node globals on the window object with nodeIntegration is disabled', async () => {
1547+
let w = new BrowserWindow({
1548+
webPreferences: {
1549+
contextIsolation: false,
1550+
nodeIntegration: false,
1551+
preload: path.resolve(fixtures, 'module', 'empty.js')
1552+
},
1553+
show: false
1554+
})
1555+
w.loadFile(path.join(fixtures, 'api', 'globals.html'))
1556+
const [, notIsolated] = await emittedOnce(ipcMain, 'leak-result')
1557+
expect(notIsolated).to.have.property('globals')
1558+
1559+
w.destroy()
1560+
w = new BrowserWindow({
1561+
webPreferences: {
1562+
contextIsolation: true,
1563+
nodeIntegration: false,
1564+
preload: path.resolve(fixtures, 'module', 'empty.js')
1565+
},
1566+
show: false
1567+
})
1568+
w.loadFile(path.join(fixtures, 'api', 'globals.html'))
1569+
const [, isolated] = await emittedOnce(ipcMain, 'leak-result')
1570+
expect(isolated).to.have.property('globals')
1571+
const notIsolatedGlobals = new Set(notIsolated.globals)
1572+
for (const isolatedGlobal of isolated.globals) {
1573+
notIsolatedGlobals.delete(isolatedGlobal)
1574+
}
1575+
expect([...notIsolatedGlobals]).to.deep.equal([], 'non-isoalted renderer should have no additional globals')
1576+
})
15461577

15471578
it('loads the script before other scripts in window', async () => {
15481579
const preload = path.join(fixtures, 'module', 'set-global.js')

spec/fixtures/api/globals.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Document</title>
5+
</head>
6+
<body>
7+
<script>
8+
window.postMessage({
9+
globals: Object.keys(Object.getOwnPropertyDescriptors(window))
10+
})
11+
</script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)