Isolate Electron Renderers with contextIsolation and contextBridge
Replace nodeIntegration:true with a preload script and contextBridge so renderer code cannot reach Node APIs directly.
08 Aug 2025, 11:50 UTC

The problem: a renderer with Node access is an XSS amplifier
If your Electron renderer has nodeIntegration: true, any script running in that window can call require and reach the file system, child processes, and native modules. A cross-site scripting flaw or compromised dependency is no longer limited to stealing cookies; it can execute arbitrary code on the user's machine. Many older tutorials still enable this option because it simplifies direct access to Electron and Node APIs, but the security cost is high.
The supported alternative is to isolate the renderer and expose only a small, intentional API through a preload script.
Set the trust boundary in BrowserWindow options
Create the renderer window with contextIsolation: true and nodeIntegration: false. Point preload at a script that runs in an isolated context before the renderer loads.
const { BrowserWindow } = require('electron')
const path = require('path')
const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
})
win.loadFile('index.html')
Run this in your Electron main process during application startup. No special operating-system permissions are required, but an incorrect preload path will prevent the window from loading your bridge API. Confirm the path resolves at build time, especially if you bundle or move the preload file.
Version assumption: default values for contextIsolation and nodeIntegration changed across Electron major releases. Do not assume they are secure in your installed version; check the Electron release notes for your target release.
Expose a minimal API from the preload script
The preload script can use Node and Electron APIs, but the renderer cannot. Use contextBridge.exposeInMainWorld to deliver only the functions you need.
// preload.js — runs in the isolated preload context
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
readConfig: () => ipcRenderer.invoke('config:read')
})
In the renderer, call the exposed function like an ordinary global:
// renderer.js — no require, no ipcRenderer
async function loadSettings() {
try {
const config = await window.electronAPI.readConfig()
console.log(config)
} catch (err) {
console.error('Failed to read config', err)
}
}
Handle the request in the main process and validate every argument. Treat the renderer as untrusted.
// main.js — main process
const { ipcMain } = require('electron')
const fs = require('fs')
const path = require('path')
ipcMain.handle('config:read', async (event) => {
// event.sender contains information about the originating renderer frame
const configPath = path.join(__dirname, 'config.json')
return JSON.parse(fs.readFileSync(configPath, 'utf8'))
})
Verify the isolation in DevTools
- Open the renderer's Developer Tools.
- In the console, confirm
window.electronAPIexists. - Confirm
requireandipcRendererare undefined in the renderer context. - Call
window.electronAPI.readConfig()and check the main process log for the matchingipcMain.handleinvocation.
If require is available, contextIsolation or nodeIntegration is not configured as intended. If window.electronAPI is missing, check the preload path and confirm the bridge was exposed before the renderer script ran.
Limitations and what to review next
- Any script in the renderer can call exposed functions, including injected scripts from XSS. Validate all inputs in the
ipcMainhandler; never assume the caller is your own code. contextBridgeworks best with plain functions and serializable values. Classes, prototypes, and complex objects may behave unexpectedly across the boundary.- The preload script has privileged access, so review and bundle it with the same care as main-process code.
- Changing
webPreferenceson an existing application can break renderer code that relied onrequireor direct Electron access. Test all renderer features after the change.
Closing checklist
- Set
contextIsolation: trueandnodeIntegration: falseinwebPreferences. - Provide a preload script and expose only specific functions through
contextBridge. - Move file-system, native, and sensitive logic into
ipcMain.handlehandlers with argument validation. - Verify with DevTools that the renderer cannot
requiremodules and that exposed calls reach the main process.
This pattern keeps the renderer able to render web content while treating the preload script as the trust boundary between the page and privileged system APIs.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.