Skip to content

Commit 7dfcb5e

Browse files
authored
fix: block custom window.open when nativeWindowOpen is true (electron#23188) (electron#23224)
* fix: block custom window.open when nativeWindowOpen is true (electron#23188) * fix lint * Update chromium-spec.ts
1 parent 0b3bf1e commit 7dfcb5e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/browser/guest-window-manager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ const canAccessWindow = function (sender, target) {
188188

189189
// Routed window.open messages with raw options
190190
ipcMainInternal.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, url, frameName, features) => {
191+
// This should only be allowed for senders that have nativeWindowOpen: false
192+
{
193+
const webPreferences = event.sender.getLastWebPreferences();
194+
if (webPreferences.nativeWindowOpen || webPreferences.sandbox) {
195+
event.returnValue = null;
196+
throw new Error('GUEST_WINDOW_MANAGER_WINDOW_OPEN denied: expected native window.open');
197+
}
198+
}
191199
if (url == null || url === '') url = 'about:blank';
192200
if (frameName == null) frameName = '';
193201
if (features == null) features = '';

spec-main/chromium-spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ describe('reporting api', () => {
7272
server.close()
7373
}
7474
})
75+
76+
describe('window.open', () => {
77+
it('denies custom open when nativeWindowOpen: true', async () => {
78+
const w = new BrowserWindow({
79+
show: false,
80+
webPreferences: {
81+
contextIsolation: false,
82+
nodeIntegration: true,
83+
nativeWindowOpen: true
84+
}
85+
});
86+
w.loadURL('about:blank');
87+
88+
const previousListeners = process.listeners('uncaughtException');
89+
process.removeAllListeners('uncaughtException');
90+
try {
91+
const uncaughtException = new Promise<Error>(resolve => {
92+
process.once('uncaughtException', resolve);
93+
});
94+
expect(await w.webContents.executeJavaScript(`(${function () {
95+
const ipc = process.electronBinding('ipc').ipc;
96+
return ipc.sendSync(true, 'ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', ['', '', ''])[0];
97+
}})()`)).to.be.null('null');
98+
const exception = await uncaughtException;
99+
expect(exception.message).to.match(/denied: expected native window\.open/);
100+
} finally {
101+
previousListeners.forEach(l => process.on('uncaughtException', l));
102+
}
103+
});
104+
});
75105
})
76106

77107
describe('window.postMessage', () => {

0 commit comments

Comments
 (0)