Electron Hardening for AI-Powered Desktop Clients
This post was created with AI assistance and reviewed for accuracy before publishing. In a web app, cross-site scripting means an attacker runs JavaScript in a browser tab. In an Electron app with the wrong settings, th
This post was created with AI assistance and reviewed for accuracy before publishing.
In a web app, cross-site scripting means an attacker runs JavaScript in a browser tab. In an Electron app with the wrong settings, the same bug means an attacker reads the user's home directory.
AI desktop clients sit in the worst part of this space. They exist precisely because they can see local files, they render text produced by a model rather than by you, and that text frequently contains markup and code blocks. You are rendering untrusted content in a process that may have filesystem access.
The three settings that decide everything
Modern Electron defaults are safe. The danger is in tutorials and older codebases that turn them off to make something work quickly:
const win = new BrowserWindow({
webPreferences: {
contextIsolation: true, // must stay true
nodeIntegration: false, // must stay false
sandbox: true, // keep unless you have a specific reason
preload: path.join(__dirname, 'preload.js'),
},
});
nodeIntegration: true gives renderer JavaScript direct access to require, which means any injected script can call child_process.exec. contextIsolation: false lets page scripts reach into the preload script's context and take whatever it holds. Together they turn a rendering bug into full compromise.
If a dependency only works with these disabled, the dependency is the problem.
Expose functions, not capabilities
The preload script is the bridge, and the temptation is to make it general. Do not pass through primitives that can do anything:
// Wrong: this is filesystem access with extra steps.
contextBridge.exposeInMainWorld('api', {
readFile: (p) => fs.readFileSync(p, 'utf8'),
});
// Right: one narrow operation the app actually needs.
contextBridge.exposeInMainWorld('api', {
openDocument: () => ipcRenderer.invoke('document:open'),
});
The first version means any injected script reads /etc/passwd or the user's SSH keys. The second gives the renderer no path parameter at all: the main process shows the file picker, the user chooses, and only the chosen file's contents come back. The authority stays with the user rather than with the caller.
That is the general principle. Every function you expose should be a specific action with the decision-making on the main side, not a capability the renderer can point wherever it likes.
Validate IPC as if it were a public API
Because it is one. Any code running in the renderer can invoke your handlers with whatever arguments it likes.
ipcMain.handle('document:save', async (event, payload) => {
// Confirm the sender is a window you created.
if (!isTrustedFrame(event.senderFrame)) return { ok: false };
const parsed = SaveSchema.safeParse(payload); // shape and type
if (!parsed.success) return { ok: false };
const target = path.resolve(userDataDir, parsed.data.name);
// Confirm the resolved path did not escape the directory.
if (!target.startsWith(userDataDir + path.sep)) return { ok: false };
await fs.writeFile(target, parsed.data.contents);
return { ok: true };
});
The path check is the one people miss. A name of ../../.ssh/authorized_keys resolves outside your data directory, and string concatenation will not catch it. Resolve first, then verify the result is still inside the boundary.
Model output is untrusted input
This is the part specific to AI clients. A response may contain HTML, a link, an image reference, or a code block, and your renderer decides what to do with each.
Never use innerHTML on model output. Render markdown through a parser that escapes HTML by default, and sanitise the result before it reaches the DOM. A model reproducing an attacker's prompt injection is a realistic delivery path for a payload, and it arrives looking exactly like a normal answer.
Links deserve their own handling. An unhandled window.open or a target of _blank can navigate your app window to an arbitrary URL, replacing your interface with someone else's page inside a trusted frame:
win.webContents.setWindowOpenHandler(({ url }) => {
if (/^https?:\/\//.test(url)) shell.openExternal(url); // hand it to the OS browser
return { action: 'deny' }; // never open in-app
});
win.webContents.on('will-navigate', (event) => event.preventDefault());
Everything external goes to the system browser. The app window navigates nowhere.
Turn off the debugging conveniences before shipping
Two things commonly ship enabled by accident. The remote debugging port, which lets any local process attach to your renderer and execute code in it. And permissive webSecurity, which teams disable during development to load local files and then forget.
Both should be off in production builds, and worth asserting in the packaging step rather than trusting memory.
Update Electron on a schedule, not on demand
Electron bundles Chromium, so your app carries whatever browser vulnerabilities that version has, and unlike a website you do not get fixes when the user updates their browser. Every published Chromium security fix applies to you until you ship a new Electron.
Treat the upgrade as routine maintenance with a slot in the release cycle. An AI client that reads local files running a year-old Chromium is a meaningful risk regardless of how careful the rest of the code is.
Electron publishes a security checklist that changes between major versions. Re-reading it at each upgrade takes ten minutes and is the highest-value ten minutes in the process.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.