Electron contextIsolation by default and safe Node API exposure
Electron 17+ enables context isolation by default, but migrating existing renderers requires refactoring direct Node.js access toward contextBridge. This post walks the configuration, a concrete exposure pattern, and how to verify the setup.
15 Apr 2026, 02:05 UTC

The renderer privilege problem
Electron apps have long walked a security trade-off: by default, renderer processes have unrestricted access to Node.js globals like process and require, which opens the door for DOM-based attacks if user input is compromised. Conversely, disabling nodeIntegration protects the app but breaks legitimate native calls, forcing teams into an all-or-nothing choice.
What context isolation means
When webPreferences.contextIsolation=true is set, Electron forces the renderer to use a separate JavaScript context, isolating it from the main world where Node.js objects would otherwise be available. This means the renderer cannot directly access process, require, or other Node.js globals unless they are explicitly exposed. The feature mitigates DOM-based attacks while still allowing controlled API exposure through contextBridge.
Enabling contextIsolation in BrowserWindow
Electron 17+ enables contextIsolation by default for new projects. For older projects, you must set both webPreferences.contextIsolation=true and webPreferences.nodeIntegration=false to avoid the security risk of keeping node integration on.
Here's the minimal BrowserWindow configuration:
const { BrowserWindow } = require('electron') function createWindow() { const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { contextIsolation: true, nodeIntegration: false, }, }) mainWindow.loadFile('index.html') } createWindow()Exposing Node.js APIs selectively with contextBridge
Instead of re-enabling global node integration, use
contextBridge.exposeInMainWorldto make specific APIs available in the renderer's isolated context:const { contextBridge } = require('electron') contextBridge.exposeInMainWorld('electronAPI', { getCurrentPath: () => process.cwd(), openExternalUrl: (url) => require('open').url(url), }) // In the renderer console.log(electronAPI.getCurrentPath()) // works within isolated contextcontextBridge only works when contextIsolation is enabled. If you try to access process directly in the renderer, it will be undefined.
Verification and limitations
- Open your app with
electron .- Open DevTools (F12) and switch to the renderer process console
- Run
window.isContextIsolated— expected result:trueif isolation is active- Alternatively, from the main process you can check
mainWindow.webContents.getWindowContextIsolation()— it returns the isolation stateIf the value is false, double-check that webPreferences.contextIsolation is set to true and that no other configuration is overriding it.
Limitation: context isolation is not a silver bullet. Any API you expose via contextBridge becomes a potential attack surface if that API performs privileged operations without its own validation. Review every exposed method and ensure it sanitizes inputs and enforces permissions.
Closing checklist
- Set webPreferences.contextIsolation=true and nodeIntegration=false (or rely on Electron 17+ defaults)
- Use contextBridge.exposeInMainWorld for any Node.js calls the renderer needs
- Run the verification steps above and confirm window.isContextIsolated === true
- Audit exposed APIs for input validation and least-privilege principles
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.