-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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: service worker preload scripts for improved extensions support #44411
Conversation
d13f9c5
to
044dccc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's talk more about evaluateInMainWorld
. Making new functions that allow evaluation of strings directly contravenes web security standards and the original purpose of the context bridge. It will expose a giant security footgun and I'm a strong -1 on exposing such a feature.
I'd love to understand the motivation / usecase and discuss with the Security WG (cc @electron/wg-security) how to solve those use-cases with an API that isn't such a footgun.
@MarshallOfSound the goal with For my particular use case, I'd like to overwrite extension APIs such as const { ipcRenderer, contextBridge } = require('electron');
// Expose setBadgeText API
contextBridge.exposeInMainWorld('electron', {
setBadgeText: (text) => ipcRenderer.send('action.setBadgeText', text)
});
// Overwrite extension API to provide custom functionality
contextBridge.evaluateInMainWorld(`(function () {
chrome.action.setBadgeText = (text) => {
electron.setBadgeText(text);
};
}());`); A potential alternative might be to accept functions. This is similar to what's offered by chrome.scripting APIs. function overrideActionApi () {
chrome.action.setBadgeText = (text) => {
electron.setBadgeText(text);
};
}
contextBridge.evaluateInMainWorld({
func: overrideActionApi,
args: []
}); If this method existing on |
webFrame.executeJavaScript is also a foot gun, it's an API that wouldn't land nowadays and if we could, we'd remove it. I wouldn't use it as an example It sounds like what you want is support for overriding existing APIs from contextBridge which is a thing it supports internally but isn't exposed via API |
@MarshallOfSound The example I provided is limited. However, I do have use cases which require additional logic where providing a complete function would be necessary. For example, some extension APIs require serializing arguments such as action.setIcon. Additionally, the v8::Context provided in this implementation is based on ShadowRealms. These lack most DOM APIs, but could be partially restored by evaluating a method. // Polyfill setTimeout in ShadowRealmGlobalScope
function setTimeoutAsync (delay) {
return contextBridge.evaluateInMainWorld({
func: function mainWorldFunc (delay) {
return new Promise((resolve) => setTimeout(resolve, delay));
},
args: [delay]
});
} |
In this case adding support for zero-copy context bridge transfer of
I don't think this is a good enough usecase to justify a security footgun, the web knows it, chrome knows it, passing strings around to be evalled is just a nightmare. Someones gonna do something silly like |
@MarshallOfSound If I refactor this API to accept a |
Ideally we find a way to avoid this APi surface entirely, does the |
My current constraints:
There's also the issue of future unknowns. JS execution will allow the flexibility to solve varied problems with a small API surface. I'm not sure I fully understand the footgun argument against JS execution (outside of eval strings). Electron provides application developers with full control over a chromium browser environment through JS APIs, and this seems to take away from that level of control. This is a fairly common API provided in projects such as Chrome DevTools Protocol, Chrome Extensions, Puppeteer/Playwright, Selenium, and node's vm module. |
Let me clarify my stance
To give a path forward given the constraints noted above (thanks for those, gives a clear picture of what is needed)
Docs for the function thing could be fun, but at least technically that's the way forward IMO |
4791bda
to
c6164aa
Compare
I've refactored Tests to guarantee return values go through the context bridge reuse the logic from our webFrame.executeJavaScript world safe test. The internals of chrome.scripting.executeScript internals
contextBridge.evaluateInMainWorld(script) typesThe types are currently using interface EvaluationScript {
/**
* A JavaScript function to evaluate. This function will be serialized which means
* that any bound parameters and execution context will be lost.
*/
func: (...args: any[]) => any;
/**
* The arguments to pass to the provided function. These arguments must be
* JSON-serializable.
*/
args?: any[];
}
interface ContextBridge {
evaluateInMainWorld(evaluationScript: EvaluationScript): any;
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Release Notes Persisted
|
I was unable to backport this PR to "35-x-y" cleanly; |
…44411) * feat: preload scripts for service workers * feat: service worker IPC * test: service worker preload scripts and ipc
@samuelmaddock has manually backported this PR to "35-x-y", please check out #45408 |
Description of Change
Implements RFC #8 which aims to allow Electron applications to support additional Chrome Extension APIs. Extension background service workers can be modified using preload scripts.
Tip
I recommend reviewing by commit. I've split up the work into logical commits to make reading it more manageable.
Todo
contextBridge.evaluateInMainWorld
to acceptFunction
andArgs[]
rather than astring
feat: service worker preload scripts for improved extensions support #44411 (comment)
evaluateInMainWorld
should use contextBridge value passing rather than JSON-serialization if possible.registerPreloadScript
to allowid
as an optional property.fromVersionID
could be better named to something likegetWorkerFromVersionID
context_bridge::ObjectCache
array-level consistencyfeat: service worker preload scripts for improved extensions support #44411 (comment)
IpcMainServiceWorker
docs explaining its relationship withIpcMain
feat: service worker preload scripts for improved extensions support #44411 (comment)
IPCRenderer
implementation into frame and service worker subclassesfeat: service worker preload scripts for improved extensions support #44411 (comment)
Backports
Post-merge
Follow up
contextBridge.executeInMainWorld
not leaking objects in service worker contextfeat: service worker preload scripts for improved extensions support #44411 (comment)
feat: e pr download-dist <number> build-tools#679
Documentation
ServiceWorkerVersion
andServiceWorkerHost
are relevant for understandingServiceWorkerMain
in this PR.Overview
v8::Context
in renderer worker threads to allow secure interaction with service worker contexts.ServiceWorkerMain
.Service worker IPCs
ServiceWorkerMain.ipc
matches the implementation ofIpcMain
to enable IPC with the renderer process service worker thread. IPCs sent from the render worker threads are dispatched onSession
; currently only handling service worker IPCs.ipcMainInternal
now handles IPCs from both web frames and service workers. To differentiate the two, an IPC event has atype
of either 'frame' or 'service-worker'.Architecture Flow
As a starting point for reviewing, consider some of the flows this feature implements.
Checklist
npm test
passesRelease Notes
Notes: