SOLFIND
Web Lens
Portal home

Process Sandboxing | Electron

https://www.electronjs.org/docs/latest/tutorial/sandbox • 51 KB fetched
Open original page


Process Sandboxing | Electron Skip to main content Electron Docs API Blog Tools * Electron Forge * Electron Fiddle Community * Governance * Showcase * Resources Releases English * English * Deutsch * Español * Français * 日本語 * Português * Русский * 中文 Search * Get Started * Processes in Electron * Process Model * Context Isolation * Inter-Process Communication * Process Sandboxing * MessagePorts in Electron * Best Practices * Examples * Development * Native Node Modules * Distribution * Testing And Debugging * References * Contributing * * Processes in Electron * Process Sandboxing On this page Process Sandboxing One key security feature in Chromium is that processes can be executed within a sandbox. The sandbox limits the harm that malicious code can cause by limiting access to most system resources — sandboxed processes can only freely use CPU cycles and memory. In order to perform operations requiring additional privilege, sandboxed processes use dedicated communication channels to delegate tasks to more privileged processes. In Chromium, sandboxing is applied to most processes other than the main process. This includes renderer processes, as well as utility processes such as the audio service, the GPU service and the network service. See Chromium's Sandbox design document for more information. Starting from Electron 20, the sandbox is enabled for renderer processes without any further configuration. Sandboxing is tied to Node.js integration. Enabling Node.js integration for a renderer process by setting nodeIntegration: true disables the sandbox for the process. If you want to disable the sandbox for a process, see the Disabling the sandbox for a single process section. Sandbox behavior in Electron ​ Sandboxed processes in Electron behave mostly in the same way as Chromium's do, but Electron has a few additional concepts to consider because it interfaces with Node.js. Renderer processes ​ When renderer processes in Electron are sandboxed, they behave in the same way as a regular Chromium renderer would. A sandboxed renderer won't have a Node.js environment initialized. Therefore, when the sandbox is enabled, renderer processes can only perform privileged tasks (such as interacting with the filesystem, making changes to the system, or spawning subprocesses) by delegating these tasks to the main process via inter-process communication (IPC). note For more info on inter-process communication, check out our IPC guide . Preload scripts ​ In order to allow renderer processes to communicate with the main process, preload scripts attached to sandboxed renderers will still have a polyfilled subset of Node.js APIs available. A require function similar to Node's require module is exposed, but can only import a subset of Electron and Node's built-in modules: * electron (following renderer process modules: contextBridge , crashReporter , ipcRenderer , nativeImage , webFrame , webUtils ) * events * timers * url node: imports are supported as well: * node:events * node:timers * node:url In addition, the preload script also polyfills certain Node.js primitives as globals: * Buffer * process * clearImmediate * setImmediate Because the require function is a polyfill with limited functionality, you will not be able to use CommonJS modules to separate your preload script into multiple files. If you need to split your preload code, use a bundler such as webpack or Parcel . Note that because the environment presented to the preload script is substantially more privileged than that of a sandboxed renderer, it is still possible to leak privileged APIs to untrusted code running in the renderer process unless contextIsolation is enabled. Configuring the sandbox ​ For most apps, sandboxing is the best choice. In certain use cases that are incompatible with the sandbox (for instance, when using native node modules in the renderer), it is possible to disable the sandbox for specific processes. This comes with security risks, especially if any untrusted code or content is present in the unsandboxed process. Disabling the sandbox for a single process ​ In Electron, renderer sandboxing can be disabled on a per-process basis with the sandbox: false preference in the BrowserWindow constructor. main.js app . whenReady ( ) . then ( ( ) => { const win = new BrowserWindow ( { webPreferences : { sandbox : false } } ) win . loadURL ( 'https://google.com' ) } ) Sandboxing is also disabled whenever Node.js integration is enabled in the renderer. This can be done through the BrowserWindow constructor with the nodeIntegration: true flag or by providing the respective HTML boolean attribute for a webview . main.js app . whenReady ( ) . then ( ( ) => { const win = new BrowserWindow ( { webPreferences : { nodeIntegration : true } } ) win . loadURL ( 'https://google.com' ) } ) index.html (Renderer Process) < webview nodeIntegration src = " page.html " > </ webview > Enabling the sandbox globally ​ If you want to force sandboxing for all renderers, you can also use the app.enableSandbox API. Note that this API has to be called before the app's ready event. main.js app . enableSandbox ( ) app . whenReady ( ) . then ( ( ) => { // any sandbox:false calls are overridden since `app.enableSandbox()` was called. const win = new BrowserWindow ( ) win . loadURL ( 'https://google.com' ) } ) Disabling Chromium's sandbox (testing only) ​ You can also disable Chromium's sandbox entirely with the --no-sandbox CLI flag, which will disable the sandbox for all processes (including utility processes). We highly recommend that you only use this flag for testing purposes, and never in production. Note that the sandbox: true option will still disable the renderer's Node.js environment. A note on rendering untrusted content ​ Rendering untrusted content in Electron is still somewhat uncharted territory, though some apps are finding success (e.g. Beaker Browser ). Our goal is to get as close to Chrome as we can in terms of the security of sandboxed content, but ultimately we will always be behind due to a few fundamental issues: * We do not have the dedicated resources or expertise that Chromium has to apply to the security of its product. We do our best to make use of what we have, to inherit everything we can from Chromium, and to respond quickly to security issues, but Electron cannot be as secure as Chromium without the resources that Chromium is able to dedicate. * Some security features in Chrome (such as Safe Browsing and Certificate Transparency) require a centralized authority and dedicated servers, both of which run counter to the goals of the Electron project. As such, we disable those features in Electron, at the cost of the associated security they would otherwise bring. * There is only one Chromium, whereas there are many thousands of apps built on Electron, all of which behave slightly differently. Accounting for those differences can yield a huge possibility space, and make it challenging to ensure the security of the platform in unusual use cases. * We can't push security updates to users directly, so we rely on app vendors to upgrade the version of Electron underlying their app in order for security updates to reach users. While we make our best effort to backport Chromium security fixes to older versions of Electron, we do not make a guarantee that every fix will be backported. Your best chance at staying secure is to be on the latest stable version of Electron. Edit this page Previous Inter-Process Communication Next MessagePorts in Electron * Sandbox behavior in Electron * Renderer processes * Preload scripts * Configuring the sandbox * Disabling the sandbox for a single process * Enabling the sandbox globally * Disabling Chromium's sandbox (testing only) * A note on rendering untrusted content Docs * Getting Started * API Reference Checklists * Performance * Security Tools * Electron Forge * Electron Fiddle Community * Governance * Resources * Discord * Bluesky * X * Mastodon * Stack Overflow More * GitHub * Open Collective * Infrastructure Dashboard Copyright OpenJS Foundation and Electron contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation , please see our Trademark Policy and Trademark List . Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. The OpenJS Foundation | Terms of Use | Privacy Policy | Bylaws | Code of Conduct | Trademark Policy | Trademark List | Cookie Policy Hosting and infrastructure graciously provided by

Links found on this page

  1. Skip to main content [direct]
  2. Electron [direct]
  3. Docs [direct]
  4. API [direct]
  5. Blog [direct]
  6. Electron Forge [direct]
  7. Electron Fiddle [direct]
  8. Governance [direct]
  9. Showcase [direct]
  10. Resources [direct]
  11. Releases [direct]
  12. Deutsch [direct]
  13. Español [direct]
  14. Français [direct]
  15. 日本語 [direct]
  16. Português [direct]
  17. Русский [direct]
  18. 中文 [direct]
  19. Processes in Electron [direct]
  20. Context Isolation [direct]
  21. Inter-Process Communication [direct]
  22. MessagePorts in Electron [direct]
  23. Best Practices [direct]
  24. Examples [direct]
  25. Development [direct]
  26. Native Node Modules [direct]
  27. Distribution [direct]
  28. Testing And Debugging [direct]
  29. References [direct]
  30. Contributing [direct]
  31. Sandbox design document [direct]
  32. events [direct]
  33. timers [direct]
  34. url [direct]
  35. node: imports [direct]
  36. Buffer [direct]
  37. process [direct]
  38. CommonJS modules [direct]
  39. webpack [direct]
  40. Parcel [direct]
  41. BrowserWindow [direct]
  42. --no-sandbox [direct]
  43. Beaker Browser [direct]
  44. Edit this page [direct]
  45. Security [direct]
  46. Discord [direct]
  47. Bluesky [direct]
  48. X [direct]
  49. Mastodon [direct]
  50. Stack Overflow [direct]
  51. GitHub [direct]
  52. Open Collective [direct]
  53. Infrastructure Dashboard [direct]
  54. OpenJS Foundation [direct]
  55. Trademark Policy [direct]
  56. Trademark List [direct]
  57. Terms of Use [direct]
  58. Privacy Policy [direct]
  59. Bylaws [direct]
  60. Code of Conduct [direct]
  61. Cookie Policy [direct]