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

feat: add preloadInWorker API #28923

Closed
wants to merge 2 commits into from

Conversation

hamst
Copy link

@hamst hamst commented Apr 29, 2021

Description of Change

Add preloadInWorker for webPreferences in BrowserWindow to define a script to be loaded first before any other scripts in worker.

cc @codebytere

Checklist

Release Notes

Notes: Added preloadInWorker to define a script to be loaded first before any other scripts in worker.

@welcome
Copy link

welcome bot commented Apr 29, 2021

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Apr 29, 2021
@hamst hamst changed the title Preload in worker script feat: add preloadInWorker Apr 29, 2021
@hamst hamst changed the title feat: add preloadInWorker feat: add preloadInWorker API Apr 29, 2021
@nornagon nornagon added the semver/minor backwards-compatible functionality label Apr 29, 2021
Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the document it is unclear how this option works with contextIsolation, can you make it clear and add related tests?

And I think this option should either respect the contextIsolation option of the window, or just always run the preload script in isolated context. /cc @electron/wg-api

const preloadScript = parseOption('preload-in-worker');

if (nodeIntegration) {
// Export node bindings to global.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shares lots of code with lib/renderer/init.ts, can you move the shared code into a utility function? Like what windowSetup and webViewInit do.

@zcbenz zcbenz requested a review from a team May 5, 2021 07:27
@hamst
Copy link
Author

hamst commented May 5, 2021

@zcbenz It seems that contextIsolation is not supported in WebWorkers, so adding preloadInWorker doesn't affect it in any ways. I will add some details in documents, as far as having tests with contextIsolation, will they be useful?

@zcbenz
Copy link
Member

zcbenz commented May 6, 2021

The contextIsolation controls whether preload scripts should run in the web page's context or in a separate context, currently it is not related to WebWorker because WebWorker does not have preload scripts.

But since this PR is adding preloadInWorker, we have to define how contextIsolation affects preload scripts in WebWorker. We can either:

  1. Make preloadInWorker respect the contextIsolation option that, contextIsolation should be able to control whether preload scripts should run in the worker script's context or in a separate context ;
  2. Or ignore the contextIsolation option and always run the preload scripts in separate context in WebWorker.

From current implementation, the preload scripts specified by preloadInWorker are running in the same context of the worker script, which is against the "secure by default" principle that we have in Electron's APIs.

Copy link
Member

@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without solving the ctx isolation issue:

This script will always have access to node APIs no matter whether node integration in worker is turned on or off.

is fundamentally a massive security vulnerability that we can't / won't land into this project. Putting a hard block on this pending a proposal for how to deal with ctx isolation

@hamst
Copy link
Author

hamst commented May 6, 2021

Right now we have an option nodeIntegrationInWorker but don't support contextIsolation in worker. Being enabled nodeIntegrationInWorker allows access to powerful API without any limitation for all web worker scripts loaded in webpage, and there is no way to limit this. Very unlikely that this option without contextIsolation support follows "secure by default" principle, and that's what we have today. So contextIsolation for worker must be supported no matter if we have preloadInWorker or not.
Option preloadInWorker makes things way better: you define the script with all access to API, and you can make some API visible for all workers, but it requires additional steps and unlikely could be done unintentionally. By default, nothing will be exposed. That's a big difference from security prospective.
So getting contextIsolation support for workers should be a separate task which must be done to be "secure by defaults" regardless preloadInWorker. As contextIsolation heavily relies on contextBridge module, some work needs to be done to relax the requirement to avoid using electron modules in workers https://www.electronjs.org/docs/tutorial/multithreading#available-apis and make the implementation thread-safe.

@zcbenz
Copy link
Member

zcbenz commented May 7, 2021

@hamst Thanks for explaining, I see your point now. I'm good with this API, since it is an improvement over nodeIntegrationInWorker on security, before contextIsolation is supported in worker.

@hamst
Copy link
Author

hamst commented May 7, 2021

Would be good to link the issue #28620 to this PR.

@MarshallOfSound
Copy link
Member

since it is an improvement over nodeIntegrationInWorker on security, before contextIsolation is supported in worker.

I disagree with this from a security perspective. Previously, enabling nodeIntegrationInWorker very clearly gave all workers Node.js access. Now providing preloadInWorker transparently provides node.js access to the worker context. This is massively unsafe, isn't clear from docs and people won't realize that's what it does.

Without context isolation this concept of "run a preload next to a worker script" is massively unsafe and IMO isn't something that belongs in core as it is antithetical to our secure-by-default policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review/requested 🗳 new-pr 🌱 PR opened in the last 24 hours no-backport semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants