Privilege Escalation via ContextBridge Exposure in Electron
0 reputation · 24 Apr 2021, 06:35 UTC
0 reputation · 24 Apr 2021, 06:35 UTC
Electron's security model relies on contextIsolation to separate the renderer process from the main process. To facilitate communication, the contextBridge module allows developers to expose a limited API from a preload script to the renderer's global window object.
A critical design concern arises when complex objects or entire Electron modules are passed through exposeInMainWorld. If a function exposes a privileged module or a mutable object that can be manipulated by the renderer, the security boundary between the unprivileged renderer and the privileged main process may be compromised.
Given these constraints, what are the specific risks of exposing functions that return internal Electron objects to the renderer? How can a developer ensure that the contextBridge does not inadvertently allow a renderer process to execute arbitrary main-process commands?
31275 reputation · 24 Apr 2021, 10:58 UTC
When a function exposed through contextBridge.exposeInMainWorld returns an Electron module or a mutable object (e.g., require('electron'), BrowserWindow, webContents, session), the renderer gains direct access to privileged main‑process capabilities. This bypasses the isolation intended by contextIsolation and can lead to:
shell.openExternal or child_process.win.close(), win.setBounds) or web contents.require('electron') gives the renderer the same API surface as the main process.BrowserWindow allows calls like win.webContents.send or session.clearStorageData.require('electron') or any main‑process module directly.Object.freeze on it and on any nested objects.Object.getOwnPropertyDescriptors to confirm they are non‑writable, non‑configurable, and lack dangerous methods.window.electron.shell.openExternal) and verify that the calls throw or are blocked.--enable-sandbox flag and observe that no renderer‑initiated main‑process action succeeds.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.