Electron main process and isolated renderer: where should the contextBridge API surface end?
0 reputation · 19 Jan 2024, 02:16 UTC
0 reputation · 19 Jan 2024, 02:16 UTC
Electron pairs a Chromium renderer with a Node.js main process. Since Electron 12, contextIsolation defaults to true, and the renderer sandbox is enabled by default from Electron 20 onward. The documented bridge between the two worlds is a preload script calling contextBridge.exposeInMainWorld, backed by ipcRenderer/ipcMain channels.
Pinning the Electron version makes the bundled Chromium and Node.js pair repeatable across machines, but it does not settle the design of the API surface itself. Electron's security guidance favors narrow, named wrappers and cautions against blindly forwarding channels, yet generic invoke(channel, payload) passthroughs remain common because they are compact.
The trade-off is a standing design question for any reusable Electron starter. Narrow methods keep renderer capabilities explicit but are verbose; a single passthrough pushes all authorization into main-process handlers. contextBridge also copies only enumerable own properties, so wrapper objects can carry unexpected prototypes unless deliberately flattened.
window.api.saveProject, or one generic window.api.invoke(channel, payload) wrapper?ipcMain.handle handler, or both?contextIsolation provides, even with nodeIntegration left off?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.