From 574eec3e748e153b15adceb262871b17d4f4ff25 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 14:55:48 -0800 Subject: [PATCH 1/8] Make window.opener a BrowserWindowProxy --- atom/renderer/lib/override.coffee | 7 ++++--- spec/chromium-spec.coffee | 5 ++++- spec/fixtures/pages/window-open-postMessage.html | 7 ++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/atom/renderer/lib/override.coffee b/atom/renderer/lib/override.coffee index 333acc82739d..3b741e004d3e 100644 --- a/atom/renderer/lib/override.coffee +++ b/atom/renderer/lib/override.coffee @@ -94,9 +94,10 @@ window.prompt = -> # Implement window.postMessage if current window is a guest window. guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID' if guestId? - window.opener = - postMessage: (message, targetOrigin='*') -> - ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin + window.opener = BrowserWindowProxy.getOrCreate(guestId) + Object.setPrototypeOf(window.opener, null) + window.opener.postMessage = (message, targetOrigin='*') -> + ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, guestId, message, sourceOrigin) -> # Manually dispatch event instead of using postMessage because we also need to diff --git a/spec/chromium-spec.coffee b/spec/chromium-spec.coffee index 2cc2237385be..e6ae8a732f9d 100644 --- a/spec/chromium-spec.coffee +++ b/spec/chromium-spec.coffee @@ -120,7 +120,10 @@ describe 'chromium feature', -> listener = (event) -> window.removeEventListener 'message', listener b.close() - assert.equal event.data, 'file://testing' + message = JSON.parse(event.data) + assert.equal message.data, 'testing' + assert.equal message.origin, 'file://' + assert.equal message.sourceEqualsOpener, true assert.equal event.origin, 'file://' done() window.addEventListener 'message', listener diff --git a/spec/fixtures/pages/window-open-postMessage.html b/spec/fixtures/pages/window-open-postMessage.html index e547fa2a6090..550b61b27592 100644 --- a/spec/fixtures/pages/window-open-postMessage.html +++ b/spec/fixtures/pages/window-open-postMessage.html @@ -2,7 +2,12 @@ From 0ef0ce73451c34fd8b6153132b6890b7ef47d6b4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 14:57:38 -0800 Subject: [PATCH 2/8] Add comment about window.opener tweaks --- atom/renderer/lib/override.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/renderer/lib/override.coffee b/atom/renderer/lib/override.coffee index 3b741e004d3e..213dc497d0ce 100644 --- a/atom/renderer/lib/override.coffee +++ b/atom/renderer/lib/override.coffee @@ -91,10 +91,10 @@ window.confirm = (message, title='') -> window.prompt = -> throw new Error('prompt() is and will not be supported.') -# Implement window.postMessage if current window is a guest window. guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID' if guestId? window.opener = BrowserWindowProxy.getOrCreate(guestId) + # Remove BrowserWindowProxy API and give it a custom postMessage method Object.setPrototypeOf(window.opener, null) window.opener.postMessage = (message, targetOrigin='*') -> ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin From 184b11be4caeeea454ee937a13eabb6cdc1df79b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 16:47:33 -0800 Subject: [PATCH 3/8] Use id from source window when building proxy for event source --- atom/browser/lib/guest-window-manager.coffee | 22 ++++++++++++-------- atom/renderer/lib/override.coffee | 14 ++++++------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/atom/browser/lib/guest-window-manager.coffee b/atom/browser/lib/guest-window-manager.coffee index 9ed75c225f54..97c285df0770 100644 --- a/atom/browser/lib/guest-window-manager.coffee +++ b/atom/browser/lib/guest-window-manager.coffee @@ -74,23 +74,27 @@ ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, met BrowserWindow.fromId(guestId)?[method] args... ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) -> + sourceId = BrowserWindow.fromWebContents(event.sender)?.id + return unless sourceId? + guestContents = BrowserWindow.fromId(guestId)?.webContents if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*' - guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, message, sourceOrigin + guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin + +ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, message, targetOrigin, sourceOrigin) -> + sourceId = BrowserWindow.fromWebContents(event.sender)?.id + return unless sourceId? -ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) -> embedder = v8Util.getHiddenValue event.sender, 'embedder' if embedder?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*' - embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, message, sourceOrigin + embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) -> BrowserWindow.fromId(guestId)?.webContents?[method] args... -ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID', (event) -> +ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID', (event) -> embedder = v8Util.getHiddenValue event.sender, 'embedder' + openerId = null if embedder? - guest = BrowserWindow.fromWebContents event.sender - if guest? - event.returnValue = guest.id - return - event.returnValue = null + openerId = BrowserWindow.fromWebContents(embedder)?.id + event.returnValue = openerId diff --git a/atom/renderer/lib/override.coffee b/atom/renderer/lib/override.coffee index 213dc497d0ce..8b33c6e2237c 100644 --- a/atom/renderer/lib/override.coffee +++ b/atom/renderer/lib/override.coffee @@ -91,22 +91,20 @@ window.confirm = (message, title='') -> window.prompt = -> throw new Error('prompt() is and will not be supported.') -guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID' -if guestId? - window.opener = BrowserWindowProxy.getOrCreate(guestId) - # Remove BrowserWindowProxy API and give it a custom postMessage method - Object.setPrototypeOf(window.opener, null) +openerId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID' +if openerId? + window.opener = BrowserWindowProxy.getOrCreate(openerId) window.opener.postMessage = (message, targetOrigin='*') -> - ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin + ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', message, targetOrigin, location.origin -ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, guestId, message, sourceOrigin) -> +ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, sourceId, message, sourceOrigin) -> # Manually dispatch event instead of using postMessage because we also need to # set event.source. event = document.createEvent 'Event' event.initEvent 'message', false, false event.data = message event.origin = sourceOrigin - event.source = BrowserWindowProxy.getOrCreate(guestId) + event.source = BrowserWindowProxy.getOrCreate(sourceId) window.dispatchEvent event # Forward history operations to browser. From c5936a024dbeaafb42430d616c4f2421d9eebc98 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 16:54:26 -0800 Subject: [PATCH 4/8] Remove unneeded custom postMessage on window.opener --- atom/renderer/lib/override.coffee | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/atom/renderer/lib/override.coffee b/atom/renderer/lib/override.coffee index 8b33c6e2237c..ba15c7b3f61a 100644 --- a/atom/renderer/lib/override.coffee +++ b/atom/renderer/lib/override.coffee @@ -92,10 +92,7 @@ window.prompt = -> throw new Error('prompt() is and will not be supported.') openerId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID' -if openerId? - window.opener = BrowserWindowProxy.getOrCreate(openerId) - window.opener.postMessage = (message, targetOrigin='*') -> - ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', message, targetOrigin, location.origin +window.opener = BrowserWindowProxy.getOrCreate(openerId) if openerId? ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, sourceId, message, sourceOrigin) -> # Manually dispatch event instead of using postMessage because we also need to From d133553c6a68386046f16859af154e8db5fa4efa Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 16:58:32 -0800 Subject: [PATCH 5/8] Remove unused ipc event handler --- atom/browser/lib/guest-window-manager.coffee | 8 -------- 1 file changed, 8 deletions(-) diff --git a/atom/browser/lib/guest-window-manager.coffee b/atom/browser/lib/guest-window-manager.coffee index 97c285df0770..1e9ee13c71d7 100644 --- a/atom/browser/lib/guest-window-manager.coffee +++ b/atom/browser/lib/guest-window-manager.coffee @@ -81,14 +81,6 @@ ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*' guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin -ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, message, targetOrigin, sourceOrigin) -> - sourceId = BrowserWindow.fromWebContents(event.sender)?.id - return unless sourceId? - - embedder = v8Util.getHiddenValue event.sender, 'embedder' - if embedder?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*' - embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin - ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) -> BrowserWindow.fromId(guestId)?.webContents?[method] args... From b8de1bd9de21b413408fdf6ba911fe5ed886a097 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 17:02:36 -0800 Subject: [PATCH 6/8] Assert that source id matches opener --- spec/chromium-spec.coffee | 2 ++ spec/fixtures/pages/window-open-postMessage.html | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/chromium-spec.coffee b/spec/chromium-spec.coffee index e6ae8a732f9d..f0e56a47ffe0 100644 --- a/spec/chromium-spec.coffee +++ b/spec/chromium-spec.coffee @@ -117,6 +117,7 @@ describe 'chromium feature', -> describe 'window.postMessage', -> it 'sets the origin correctly', (done) -> + sourceId = remote.getCurrentWindow().id listener = (event) -> window.removeEventListener 'message', listener b.close() @@ -124,6 +125,7 @@ describe 'chromium feature', -> assert.equal message.data, 'testing' assert.equal message.origin, 'file://' assert.equal message.sourceEqualsOpener, true + assert.equal message.sourceId, sourceId assert.equal event.origin, 'file://' done() window.addEventListener 'message', listener diff --git a/spec/fixtures/pages/window-open-postMessage.html b/spec/fixtures/pages/window-open-postMessage.html index 550b61b27592..401b7d645fdf 100644 --- a/spec/fixtures/pages/window-open-postMessage.html +++ b/spec/fixtures/pages/window-open-postMessage.html @@ -5,7 +5,8 @@ var reply = JSON.stringify({ origin: e.origin, data: e.data, - sourceEqualsOpener: e.source === window.opener + sourceEqualsOpener: e.source === window.opener, + sourceId: e.source.guestId }) window.opener.postMessage(reply, '*'); }); From d1989b3624ccb508640920c2123c7cd19a1d16c2 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 17:02:58 -0800 Subject: [PATCH 7/8] Mention source in spec description --- spec/chromium-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/chromium-spec.coffee b/spec/chromium-spec.coffee index f0e56a47ffe0..c456a6919b47 100644 --- a/spec/chromium-spec.coffee +++ b/spec/chromium-spec.coffee @@ -116,7 +116,7 @@ describe 'chromium feature', -> b = window.open url, '', 'show=no' describe 'window.postMessage', -> - it 'sets the origin correctly', (done) -> + it 'sets the source and origin correctly', (done) -> sourceId = remote.getCurrentWindow().id listener = (event) -> window.removeEventListener 'message', listener From aa9c7662a30da7374a03d80ec12a1375529400e1 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 14 Dec 2015 17:09:31 -0800 Subject: [PATCH 8/8] Inline JSON message response --- spec/fixtures/pages/window-open-postMessage.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/fixtures/pages/window-open-postMessage.html b/spec/fixtures/pages/window-open-postMessage.html index 401b7d645fdf..0e7e4d8cce0c 100644 --- a/spec/fixtures/pages/window-open-postMessage.html +++ b/spec/fixtures/pages/window-open-postMessage.html @@ -2,13 +2,12 @@