Electron Desktop Security Basics
🎯 Introduction
An Electron page can potentially combine browser and desktop capabilities. Greater permissions increase the risk when malicious content reaches the page, so give it a key that opens only the rooms it needs.
By the end of this article, you will be able to:
- Explain
contextIsolation,sandbox, andnodeIntegration. - Expose small, specific APIs through a preload script.
- Validate IPC arguments and restrict navigation and new windows.
- Add a basic CSP to the page.
🧱 Three important settings
Start with these window settings for a beginner project:
const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
sandbox: true,
nodeIntegration: false,
},
});
| Setting | Purpose |
|---|---|
contextIsolation | Give page scripts and the preload script separate JavaScript contexts |
sandbox | Restrict direct access to system capabilities from the renderer |
nodeIntegration: false | Prevent the page from directly using Node.js modules |
These settings define boundaries for IPC rather than replacing it. The page can still use selected capabilities through the preload script.
🛡 Expose only purpose-specific APIs
Do not expose the entire ipcRenderer to the page:
const { contextBridge, ipcRenderer } = require('electron/renderer');
contextBridge.exposeInMainWorld('electronAPI', {
getAppVersion: () => ipcRenderer.invoke('app:get-version'),
});
The page can call only getAppVersion, without constructing arbitrary IPC channel names. The main process must also validate arguments rather than trusting data from the page.
ipcMain.handle('file:read-text', async (_event, filePath) => {
if (typeof filePath !== 'string' || !filePath.endsWith('.txt')) {
throw new Error('Only text files can be read');
}
return readFile(filePath, 'utf8');
});
🚫 Restrict navigation and new windows
Electron pages may display server-provided content, imported Markdown, or rich text. Links in this content are not necessarily controlled directly by the desktop app's code.
Navigating to an external website replaces the application interface. External websites should not run directly in the desktop app's window environment either.
Start with a simple rule: Electron windows display only the app's own pages; external links do not open inside Electron.
The following example assumes the packaged app loads local pages with loadFile:
win.webContents.on('will-navigate', (event, url) => {
if (!url.startsWith('file:')) {
event.preventDefault();
}
});
will-navigate fires when the current window is about to navigate. Cancel navigation whenever the destination is not one of the app's own file: pages.
Pages can also request new windows through window.open() or target="_blank". For example:
<a href="https://www.electronjs.org/" target="_blank"> View the Electron documentation </a>
Clicking this link invokes setWindowOpenHandler. You can open HTTPS pages in the system's default browser and prevent Electron from creating another window:
const { shell } = require('electron/main');
win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https://')) {
shell.openExternal(url);
}
return { action: 'deny' };
});
Here is the sequence:
- The user clicks a
target="_blank"link. setWindowOpenHandlerreceives the proposed new window URL.shell.openExternal(url)opens the HTTPS page in the system's default browser.{ action: 'deny' }tells Electron not to create a new Electron window.
shell.openExternal() does not run automatically. We explicitly call it to open the link in the system browser after checking that the address meets our requirements.
When using Vite in development, allowed addresses must also include http://localhost:5173. This is a development address; do not depend on it in the packaged app.
🧾 Summary
- Pages should not directly use Node.js or Electron modules by default.
- Preload scripts should expose methods for specific tasks only.
- Validate IPC arguments in the main process.
- Restrict page navigation and sources of new windows.
- CSP can reduce the chance of loading unknown scripts.
❓ Questions and answers
Q1: Can preload scripts still be used with contextIsolation enabled?
Yes. Exposing a limited API through contextBridge is the standard preload pattern for isolated contexts.
Q2: Why not expose all of ipcRenderer?
The page could invoke arbitrary channels, undermining API boundaries and making argument validation harder.
🧪 Practice
Change the following API so it can invoke only the fixed app:get-version channel:
contextBridge.exposeInMainWorld('electronAPI', {
// Write your code here
});
🎉 You now know how to establish basic security boundaries for Electron pages.
