Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The handler of "setWindowOpenHandler" not fires on Electron 12.0.0 #27967

Closed
3 tasks done
PurpleHorrorRus opened this issue Mar 2, 2021 · 9 comments · Fixed by #28498
Closed
3 tasks done

The handler of "setWindowOpenHandler" not fires on Electron 12.0.0 #27967

PurpleHorrorRus opened this issue Mar 2, 2021 · 9 comments · Fixed by #28498

Comments

@PurpleHorrorRus
Copy link

PurpleHorrorRus commented Mar 2, 2021

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 12.0.0
  • Operating System:
    • Windows 10 20H2

Expected Behavior

Handler fires and no new window opens

Actual Behavior

Handler fails and a new window opens

To Reproduce

  1. Write this code:
window.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
  1. Debug or run the application.
  2. Press Alt + LMB (or Scroll) on any link in app.
@PurpleHorrorRus PurpleHorrorRus changed the title The handler of "setWindowOpenHandler" not fires The handler of "setWindowOpenHandler" not fires on Electron 12.0.0 Mar 2, 2021
@idanwork
Copy link

idanwork commented Mar 4, 2021

Hi,
This issue happens on macOS 10.15.6 as well.

Looks like the setWindowOpenHandler doesnt handle <a target="_blank"... elements and only handles window.open(...).

I feel like I've missed something in the documentation about it https://www.electronjs.org/docs/api/window-open
But I cant see reference to cases when the app is not sandboxed and has an anchor element.
BTW the webContents.on("new-window"...) works fine in v12 and previous electron version but it's deprecated.

If an example is required:
https://gist.github.com/idanwork/ecbd1a902250d4a5d2d58d76b069849f

Example output - first click on the anchor, then on the button
image

@PurpleHorrorRus
Copy link
Author

Hi. Yes, I knew about it and used it before Electron 12.0.0. I read the documentation and noticed that a new handler is recommended, but since it doesn't work, I decided to create an issue.

@andreasdj
Copy link
Contributor

I would expect that the given code in the reproduction step:
window.webContents.setWindowOpenHandler(() => ({ action: "deny" }));

also would prevent a new window from being opened if having navigateOnDragDrop enabled and dragging/dropping a url onto the window. But the handler is not called and a new window is indeed opened.

It feels like the same underlying issue so I wont create a new issue for it at this point.

@WildXBird
Copy link

12.0.2 still has this problem

@CoryDAsana
Copy link

Just stumbled upon this issue on my end. We previously relied on "new-window" as an opportunity to apply an allowlist that guaranteed only trusted content was loaded in a BrowserWindow. Paired with the same check on "will-navigate" and "will-redirect", we had confidence we were covering all possible page navigation.

Seeing "new-window" deprecated in favor of setWindowOpenHandler, I transitioned our code to the new standard, but am now seeing our allowlist isn't covering (ex:) shift-clicks on an anchor tag.

Given the deprecated standard and the new standard have different coverage over a potentially security-critical surface area,

@phosphore
Copy link

phosphore commented Apr 3, 2021

As a temporary fix before #28498, for anyone looking to prevent navigations to untrusted origins it should still possible to use the web-contents-created event emitted when a new webContents is created and attach then your new-window, will-navigate, and setWindowOpenHandler handlers:

app.on('web-contents-created', (createEvent, contents) => {
  
  contents.on('new-window', newEvent => {
    console.log("Blocked by 'new-window'")
    newEvent.preventDefault();
  });
  
  contents.on('will-navigate', newEvent => {
    console.log("Blocked by 'will-navigate'")
    newEvent.preventDefault()
  });
  
  contents.setWindowOpenHandler(({ url }) => {
    if (url.startsWith("https://doyensec.com/")) {
      setImmediate(() => {
        shell.openExternal(url);
        });
      return { action: 'allow' }
    } else {
      console.log("Blocked by 'setWindowOpenHandler'")
      return { action: 'deny' }
    }
  })
  
});

Link to the Fiddle gist used for testing. This works from 12.0.0 to the latest 12.0.2 as for now.

== 2022 Edit
For a better defense in depth, it's possible to add will-attach-webview to the list of blocked events.

@Delagen
Copy link

Delagen commented Apr 14, 2021

@codebytere Why this issue closed? I don't see this commit neither in release branch of 12.0.4 nor in changelog.

@VerteDinde
Copy link
Member

@Delagen This is now being backported to 12 - it should be in the next 12 release, thanks for the catch! 👍

@ikkisoft
Copy link
Contributor

Just tested on Electron V13 and will-navigate is not protected with the default implementation of setWindowOpenHandler. #27967 (comment) mitigation is still recommended. Stay safe out there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.