diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 71a9af275d94..44bce9f604de 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -24,6 +24,24 @@ describe('BrowserWindow module', () => { let server let postData + const defaultOptions = { + show: false, + width: 400, + height: 400, + webPreferences: { + backgroundThrottling: false + } + } + + const openTheWindow = async (options = defaultOptions) => { + // The `afterEach` hook isn't called if a test fails, + // we should make sure that the window is closed ourselves. + await closeTheWindow() + + w = new BrowserWindow(options) + return w + } + const closeTheWindow = function () { return closeWindow(w).then(() => { w = null }) } @@ -1292,43 +1310,43 @@ describe('BrowserWindow module', () => { }) }) - it('should open windows in another domain with cross-scripting disabled', (done) => { - w.destroy() - w = new BrowserWindow({ + it('should open windows in another domain with cross-scripting disabled', async () => { + const w = await openTheWindow({ show: false, webPreferences: { sandbox: true, - preload: preload + preload } }) + ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open-external') - const pageUrl = 'file://' + htmlPath - let popupWindow - w.loadURL(pageUrl) - w.webContents.once('new-window', (e, url, frameName, disposition, options) => { - assert.equal(url, 'http://www.google.com/#q=electron') - assert.equal(options.width, 505) - assert.equal(options.height, 605) - ipcMain.once('child-loaded', function (event, openerIsNull, html) { - assert(openerIsNull) - assert.equal(html, '

http://www.google.com/#q=electron

') - ipcMain.once('answer', function (event, exceptionMessage) { - assert(/Blocked a frame with origin/.test(exceptionMessage)) + w.loadURL(`file://${htmlPath}`) + const expectedPopupUrl = 'http://www.google.com/#q=electron' // Set in the "sandbox.html". - // FIXME this popup window should be closed in sandbox.html - closeWindow(popupWindow, {assertSingleWindow: false}).then(() => { - popupWindow = null - done() - }) - }) - w.webContents.send('child-loaded') - }) - }) + // The page is going to open a popup that it won't be able to close. + // We have to close it from here later. + // XXX(alexeykuzmin): It will leak if the test fails too soon. + const [, popupWindow] = await emittedOnce(app, 'browser-window-created') - app.once('browser-window-created', function (event, window) { - popupWindow = window - }) + // Wait for a message from the popup's preload script. + const [, openerIsNull, html, locationHref] = await emittedOnce(ipcMain, 'child-loaded') + expect(openerIsNull).to.be.true('window.opener is not null') + expect(html).to.equal(`

${expectedPopupUrl}

`, + 'looks like a http: request has not been intercepted locally') + expect(locationHref).to.equal(expectedPopupUrl) + + // Ask the page to access the popup. + w.webContents.send('touch-the-popup') + const [, exceptionMessage] = await emittedOnce(ipcMain, 'answer') + + // We don't need the popup anymore, and its parent page can't close it, + // so let's close it from here before we run any checks. + await closeWindow(popupWindow, { assertSingleWindow: false }) + + expect(exceptionMessage).to.be.a('string', + `child's .document is accessible from its parent window`) + expect(exceptionMessage).to.match(/^Blocked a frame with origin/) }) it('should inherit the sandbox setting in opened windows', (done) => { diff --git a/spec/fixtures/api/sandbox.html b/spec/fixtures/api/sandbox.html index af796ebc988b..db6765c6816b 100644 --- a/spec/fixtures/api/sandbox.html +++ b/spec/fixtures/api/sandbox.html @@ -80,12 +80,14 @@ }, 'window-open-external': () => { addEventListener('load', () => { - ipcRenderer.once('child-loaded', (e) => { + ipcRenderer.once('touch-the-popup', () => { + let errorMessage = null try { - let childDoc = popup.document - } catch (e) { - ipcRenderer.send('answer', e.message) + const childDoc = popup.document + } catch (error) { + errorMessage = error.message } + ipcRenderer.send('answer', errorMessage) }) popup = open('http://www.google.com/#q=electron', '', 'top=65,left=55,width=505,height=605') }) diff --git a/spec/fixtures/module/preload-sandbox.js b/spec/fixtures/module/preload-sandbox.js index 38d14a8be50c..b9042ec93c0c 100644 --- a/spec/fixtures/module/preload-sandbox.js +++ b/spec/fixtures/module/preload-sandbox.js @@ -16,7 +16,7 @@ } } else if (location.href !== 'about:blank') { addEventListener('DOMContentLoaded', () => { - ipcRenderer.send('child-loaded', window.opener == null, document.body.innerHTML) + ipcRenderer.send('child-loaded', window.opener == null, document.body.innerHTML, location.href) }, false) } })()