Browse Source

Merge pull request #8377 from electron/sandbox-open-crash

Destroy web contents if new-window event is prevented
Kevin Sawicki 8 years ago
parent
commit
9ff1f6bbde
3 changed files with 25 additions and 2 deletions
  1. 4 2
      atom/browser/api/atom_api_web_contents.cc
  2. 17 0
      spec/api-browser-window-spec.js
  3. 4 0
      spec/static/main.js

+ 4 - 2
atom/browser/api/atom_api_web_contents.cc

@@ -419,9 +419,11 @@ void WebContents::AddNewContents(content::WebContents* source,
   v8::Locker locker(isolate());
   v8::HandleScope handle_scope(isolate());
   auto api_web_contents = CreateFrom(isolate(), new_contents);
-  Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
+  if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
       initial_rect.x(), initial_rect.y(), initial_rect.width(),
-      initial_rect.height());
+      initial_rect.height())) {
+    api_web_contents->DestroyWebContents();
+  }
 }
 
 content::WebContents* WebContents::OpenURLFromTab(

+ 17 - 0
spec/api-browser-window-spec.js

@@ -1012,6 +1012,23 @@ describe('BrowserWindow module', function () {
           })
         })
       })
+
+      it('supports calling preventDefault on new-window events', (done) => {
+        w.destroy()
+        w = new BrowserWindow({
+          show: false,
+          webPreferences: {
+            sandbox: true
+          }
+        })
+        const initialWebContents = webContents.getAllWebContents()
+        ipcRenderer.send('prevent-next-new-window', w.webContents.id)
+        w.webContents.once('new-window', () => {
+          assert.deepEqual(webContents.getAllWebContents(), initialWebContents)
+          done()
+        })
+        w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html'))
+      })
     })
   })
 

+ 4 - 0
spec/static/main.js

@@ -245,3 +245,7 @@ ipcMain.on('create-window-with-options-cycle', (event) => {
   const window = new BrowserWindow({show: false, foo: foo})
   event.returnValue = window.id
 })
+
+ipcMain.on('prevent-next-new-window', (event, id) => {
+  webContents.fromId(id).once('new-window', event => event.preventDefault())
+})