Lesson 4 of 9 · 23m
contextIsolation, sandbox, and the Death of nodeIntegration
The three switches that decide whether renderer compromise is embarrassing or catastrophic — and how to structure code so you can leave them enabled.
If you remember one operational rule from this course: a renderer that can reach Node is one XSS away from arbitrary code execution. Electron gives you the controls to prevent that — contextIsolation, sandbox, and nodeIntegration: false — but they only help if you resist turning them off the first time integration becomes inconvenient.
The defaults you should ship
webPreferences: {
contextIsolation: true,
sandbox: true,
nodeIntegration: false,
nodeIntegrationInWorker: false,
preload: path.join(__dirname, "preload.js"),
}nodeIntegration puts require directly into the page. Do not do this for application UI. Even local HTML can become attacker-controlled through injected markup, compromised dependencies, or a feature that renders user content. contextIsolation keeps preload and page JavaScript in separate worlds, so a page cannot simply overwrite ipcRenderer or prototype-pollute exposed objects. sandbox applies Chromium's renderer sandbox, reducing what a compromised renderer can do before any application logic runs.
Why teams disable them — and what to do instead
The usual complaint is "I need filesystem access in React." You don't. The UI needs to request a file operation. Move that operation behind an IPC capability:
// renderer wants this...
const result = await window.desktop.readCertificate("/etc/ssl/cert.pem");
// main decides whether the request is allowed
ipcMain.handle("cert:read", async (event, filePath) => {
requireTrustedSender(event);
if (!isAllowedCertificatePath(filePath)) throw new Error("path denied");
return fs.promises.readFile(filePath, "utf8");
});Yes, this is more code. That is the cost of owning a privileged desktop runtime. The alternative is shipping a browser where one injected <img> or dependency exploit becomes remote command execution.
Preload code is privileged code
Preload scripts can use a limited Electron API surface even in sandboxed renderers. Keep them tiny, pinned, audited, and free of network fetches. Every function exposed through contextBridge should have a name, argument shape, and threat model. If the preload script starts importing application frameworks, you have recreated nodeIntegration with worse ergonomics.
Escape hatches accumulate risk
Sometimes legacy software truly cannot migrate in one release. Isolate the exception rather than weakening the whole app: one legacy window with sandbox disabled, never shared with modern windows, no remote content, no generic IPC bridge, and a removal ticket with an owner. Security debt without an expiration mechanism becomes architecture.
Field notes
- Verify the built app, not only development config; boilerplates sometimes change
webPreferencesper environment. webSecurity: falseis not an escape hatch — it is a confession. Treat it as a release blocker.- Prototype pollution across isolated worlds is still possible through exposed data patterns; expose functions, not mutable objects.
- Review dependency injection in preload code. Preload supply-chain compromise bypasses much of the model.
Was this lesson helpful?