Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Security Model

The Electron Security Checklist

Understand why Electron apps need a security model and apply the essentials: context isolation, sandbox, disabled Node integration, safe preload APIs and loading only trusted content.

An Electron app is a web browser and a Node.js runtime in one box. That combination is the whole appeal, and also the whole danger. A web page that can run require('child_process') can run any program on the user's computer. One cross-site scripting bug, one badly loaded remote page, one compromised dependency in the renderer, and the attacker is not "in a tab": they are on the user's machine with the user's rights.

Electron's defaults have been getting stricter for years, and today the safe configuration is the default one. Your job is mostly to not turn the protections off and to design the bridge between page and system carefully. This lesson explains the three big switches and then gives you a checklist you can run against any app.

The threat in one picture

flowchart LR
  X["Untrusted input: remote page, user content, a dependency"] --> R["Renderer process (web content)"]
  R -- "preload API only" --> B["contextBridge"]
  B --> M["Main process: files, shell, OS"]
  R -. "blocked when isolated and sandboxed" .-> M

The goal: whatever happens in the renderer, the only path to the system is a small, validated API.

Assume the renderer will be compromised at some point, and design so that this is survivable.

Switch 1: nodeIntegration off

nodeIntegration: true gives page scripts require, process and the whole of Node. It has been false by default since Electron 5, and it should stay that way.

JavaScript
new BrowserWindow({
  webPreferences: {
    nodeIntegration: false,   // default
  },
});

With it on, an injected <img src=x onerror="require('child_process').exec('calc')"> runs a program. With it off, the same injection fails with require is not defined.

Switch 2: contextIsolation on

Your preload script runs with access to both Electron APIs and the page's DOM. Context isolation runs the preload in a separate JavaScript world from the page, so the page cannot reach into preload internals or tamper with built-ins the preload relies on. It is on by default. contextBridge.exposeInMainWorld is the only door between the two worlds.

JavaScript
// preload.js
import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('api', {
  version: () => ipcRenderer.invoke('app:version'),
});

Everything exposed is copied (structured clone), not shared by reference, so the page cannot get hold of ipcRenderer itself.

Switch 3: sandbox on

The sandbox puts the renderer process in the operating system's low-privilege container, like a Chrome tab. Even a full memory-corruption exploit in Chromium then lands in a process that cannot open files or start programs. It is on by default for renderers since Electron 20. A sandboxed preload can use only a small subset of Node (events, timers, url) plus Electron's renderer modules. That is fine: your heavy work belongs in the main process.

JavaScript
new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, 'preload.js'),
    contextIsolation: true,
    sandbox: true,
    nodeIntegration: false,
  },
});

Stating the defaults explicitly is not noise. It documents the intent and makes a later accidental change obvious in review.

Anti-patterns that undo everything

Never expose a generic pipe, even though it looks convenient:

JavaScript
// DANGEROUS: hands the whole IPC surface to the page
contextBridge.exposeInMainWorld('electron', { ipcRenderer });

// DANGEROUS: lets the page send any channel and any arguments
contextBridge.exposeInMainWorld('api', {
  invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
});

Expose one function per capability, each with a fixed channel:

JavaScript
contextBridge.exposeInMainWorld('api', {
  readSettings: () => ipcRenderer.invoke('settings:read'),
  saveNote: (text) => ipcRenderer.invoke('notes:save', String(text)),
});

Other settings that are red flags in a code review: webSecurity: false, allowRunningInsecureContent: true, nodeIntegrationInSubFrames: true, enableRemoteModule, and experimentalFeatures: true.

Only load content you trust

Load your own bundled files with loadFile or loadURL('file://...'). If you must load a remote site, use https, pin the origins you expect, and never give that window a powerful preload. A page you do not control plus an API that reads files is a vulnerability by design.

A config auditor stand-in

The runnable example is an auditor: it takes the webPreferences of a window and reports the problems. Real teams use the same idea through tools such as Electronegativity, or a unit test that checks the options passed to BrowserWindow.

Node.js Auditing BrowserWindow settings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Run it and notice how a single flag from the "quick hack" window brings several problems at once. Most real-world Electron vulnerabilities come from exactly such combinations.

What the isolated bridge really copies

contextBridge works with data, not live objects. Functions you expose are callable from the page, but their arguments and return values are cloned. This tiny stand-in shows the effect of cloning with structuredClone: the page gets a copy, so changing it cannot change the original in the main world.

Node.js Why cloning at the bridge protects you

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The checklist

Use this list for every app, every release:

  1. contextIsolation: true, sandbox: true, nodeIntegration: false on every window.
  2. Preload exposes named capabilities, never ipcRenderer or a generic invoke.
  3. All IPC handlers validate their arguments and the sender (next chapters).
  4. Content loaded from the network is https, from known origins, with a strict Content Security Policy.
  5. Navigation and new windows are controlled; external links go through an allowlist.
  6. Permission requests (camera, notifications) are denied unless you expect them.
  7. Secrets are not stored in plain files; use safeStorage.
  8. Fuses disable features you do not use; dependencies are updated; Electron is kept current.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does contextIsolation protect against?

    Isolation runs the preload in a separate world, so page code cannot modify built-ins or objects the preload depends on. Only contextBridge crosses over.
  2. 2. Which preload exposure is the safest?

    A function per capability with a fixed channel limits what a compromised page can do. The first two hand over the whole IPC surface and the last gives Node access.
  3. 3. With the sandbox enabled, what happens if an attacker exploits a Chromium bug in the renderer?

    The sandbox is a second wall. A renderer exploit still needs another bug to escape the OS-level restrictions.
  4. 4. When is it acceptable to set webSecurity to false?

    webSecurity false removes the same-origin policy. Work around CORS with proper headers or by making the request from the main process.

Summary

  • Treat the renderer as untrusted: assume a bug will eventually run attacker script there.
  • Keep nodeIntegration off, contextIsolation and sandbox on, and state them explicitly.
  • Expose one narrow function per capability from the preload script; clone data across the bridge.
  • Load local or https content only; review flags like webSecurity and nodeIntegrationInSubFrames.
  • Audit with the checklist (and a test) on every release.

Next: Navigation, CSP and Permissions