From 0c43361d18291556554eee83ab6d5866edde7827 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Mon, 20 Apr 2020 15:51:04 -0700 Subject: [PATCH] fix: block custom window.open when nativeWindowOpen is true --- lib/browser/guest-window-manager.js | 6 ++++++ spec-main/chromium-spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/browser/guest-window-manager.js b/lib/browser/guest-window-manager.js index bcef78ec48416..c8af4ef082292 100644 --- a/lib/browser/guest-window-manager.js +++ b/lib/browser/guest-window-manager.js @@ -219,6 +219,12 @@ const canAccessWindow = function (sender, target) { // Routed window.open messages with raw options ipcMainInternal.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, url, frameName, features) => { + // This should only be allowed for senders that have nativeWindowOpen: false + const webPreferences = event.sender.getLastWebPreferences(); + if (webPreferences.nativeWindowOpen || webPreferences.sandbox) { + event.returnValue = null; + throw new Error('GUEST_WINDOW_MANAGER_WINDOW_OPEN denied: expected native window.open'); + } if (url == null || url === '') url = 'about:blank'; if (frameName == null) frameName = ''; if (features == null) features = ''; diff --git a/spec-main/chromium-spec.ts b/spec-main/chromium-spec.ts index 327142fe24da7..2a4320d393427 100644 --- a/spec-main/chromium-spec.ts +++ b/spec-main/chromium-spec.ts @@ -654,6 +654,34 @@ describe('chromium features', () => { const [, window] = await emittedOnce(app, 'browser-window-created'); expect(window.getTitle()).to.equal('__proto__'); }); + + it('denies custom open when nativeWindowOpen: true', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + contextIsolation: false, + nodeIntegration: true, + nativeWindowOpen: true + } + }); + w.loadURL('about:blank'); + + const previousListeners = process.listeners('uncaughtException'); + process.removeAllListeners('uncaughtException'); + try { + const uncaughtException = new Promise(resolve => { + process.once('uncaughtException', resolve); + }); + expect(await w.webContents.executeJavaScript(`(${function () { + const ipc = process.electronBinding('ipc').ipc; + return ipc.sendSync(true, 'ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', ['', '', ''])[0]; + }})()`)).to.be.null(); + const exception = await uncaughtException; + expect(exception.message).to.match(/denied: expected native window\.open/); + } finally { + previousListeners.forEach(l => process.on('uncaughtException', l)); + } + }); }); describe('window.opener', () => {