Browse Source

Always use guest contents for canAccessWindow check

Kevin Sawicki 8 years ago
parent
commit
13d2fc9c81
1 changed files with 18 additions and 13 deletions
  1. 18 13
      lib/browser/guest-window-manager.js

+ 18 - 13
lib/browser/guest-window-manager.js

@@ -87,10 +87,7 @@ const createGuest = function (embedder, url, frameName, options) {
   return guestId
 }
 
-const getGuestWindow = function (guestId) {
-  const guestContents = webContents.fromId(guestId)
-  if (guestContents == null) return
-
+const getGuestWindow = function (guestContents) {
   let guestWindow = BrowserWindow.fromWebContents(guestContents)
   if (guestWindow == null) {
     const hostContents = guestContents.hostWebContents
@@ -129,27 +126,35 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', function (event, url, fr
 })
 
 ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', function (event, guestId) {
-  const guestWindow = getGuestWindow(guestId)
-  if (guestWindow == null) return
+  const guestContents = webContents.fromId(guestId)
+  if (guestContents == null) return
 
-  if (canAccessWindow(event.sender, guestWindow.webContents)) {
-    guestWindow.destroy()
-  } else {
+  if (!canAccessWindow(event.sender, guestContents)) {
     console.error(`Blocked ${event.sender.getURL()} from closing its opener.`)
+    return
   }
+
+  const guestWindow = getGuestWindow(guestContents)
+  if (guestWindow != null) guestWindow.destroy()
 })
 
 ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guestId, method, ...args) {
-  const guestWindow = getGuestWindow(guestId)
-  if (guestWindow == null) {
+  const guestContents = webContents.fromId(guestId)
+  if (guestContents == null) {
+    event.returnValue = null
+    return
+  }
+
+  if (!canAccessWindow(event.sender, guestContents)) {
+    console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
     event.returnValue = null
     return
   }
 
-  if (canAccessWindow(event.sender, guestWindow.webContents)) {
+  const guestWindow = getGuestWindow(guestContents)
+  if (guestWindow != null) {
     event.returnValue = guestWindow[method].apply(guestWindow, args)
   } else {
-    console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
     event.returnValue = null
   }
 })