Building AI-powered desktop clients on Electron introduces a specific security nightmare: you are rendering untrusted content from a language model inside a process that often has direct filesystem access. In a standard web app, a cross-site scripting (XSS) bug allows an attacker to run JavaScript in a browser tab. In a misconfigured Electron app, that same bug can allow an attacker to read the user's home directory. The source material, published by Ganesh Joshi on DEV.to, outlines the critical settings and coding patterns required to prevent this escalation of privilege.
The Three Settings That Decide Everything
The foundation of Electron security rests on three specific webPreferences flags that tutorials often disable for convenience. Developers must ensure contextIsolation remains true, nodeIntegration stays false, and sandbox is enabled unless there is a specific, justified reason to turn it off. Enabling nodeIntegration gives renderer JavaScript direct access to require, meaning any injected script can call child_process.exec. Disabling contextIsolation allows page scripts to reach into the preload script's context, potentially stealing credentials. If a dependency requires these settings to be disabled, the dependency itself is the security flaw.
Expose Functions, Not Capabilities
The preload script acts as the bridge between the renderer and the main process, but it must not expose raw capabilities. A common mistake is exposing a generic readFile function that accepts a path parameter. This allows injected scripts to read sensitive files like /etc/passwd or SSH keys. Instead, expose specific actions like openDocument where the main process handles the file picker and user selection. By removing the path parameter from the renderer's control, you keep authority with the user. Every exposed function should be a narrow operation with decision-making logic residing in the main process.
Validate IPC as a Public API
Inter-process communication (IPC) handlers must be treated as public APIs because any code in the renderer can invoke them with arbitrary arguments. Validation requires three steps: confirming the sender is a trusted frame, validating the payload shape using a schema, and verifying the resolved file path. The path check is frequently overlooked; a filename like ../../.ssh/authorized_keys can resolve outside the intended data directory. Developers must resolve the path first and then verify it still starts with the allowed directory prefix. String concatenation alone is insufficient to prevent directory traversal attacks.
Treat Model Output as Untrusted Input
AI-specific risks arise when rendering model outputs that may contain HTML, links, or code blocks. Never use innerHTML on model output; instead, use a markdown parser that escapes HTML by default and sanitizes the result. Prompt injection can deliver payloads that look like normal answers. Additionally, handle external links by intercepting window.open and will-navigate events. Force all external URLs to open in the system browser using shell.openExternal, and prevent the app window from navigating away. This ensures that a malicious link cannot replace your trusted interface with an attacker's page.
Key Takeaways
- Keep
nodeIntegration: falseandcontextIsolation: trueto prevent injected scripts from accessing Node.js APIs. - Expose specific functions in the preload script rather than generic file system capabilities.
- Validate IPC payloads and resolve file paths to prevent directory traversal attacks.
- Sanitize AI model output and force external links to open in the system browser.
- Disable remote debugging and permissive web security in production builds.
The Bottom Line
Electron's convenience is a security trap for AI apps. If you don't lock down IPC and sanitize model output, you're just building a remote code execution vector with a nice UI.