contextBridge

Create a safe, bi-directional, synchronous bridge across isolated contexts

Process: Renderer

An example of exposing an API to a renderer from an isolated preload script is given below:

  1. // Preload (Isolated World)
  2. const { contextBridge, ipcRenderer } = require('electron')
  3. contextBridge.exposeInMainWorld(
  4. 'electron',
  5. {
  6. doThing: () => ipcRenderer.send('do-a-thing')
  7. }
  8. )
  1. // Renderer (Main World)
  2. window.electron.doThing()

Glossary

Main World

The “Main World” is the JavaScript context that your main renderer code runs in. By default, the page you load in your renderer executes code in this world.

Isolated World

When contextIsolation is enabled in your webPreferences (this is the default behavior since Electron 12.0.0), your preload scripts run in an “Isolated World”. You can read more about context isolation and what it affects in the security docs.

Methods

The contextBridge module has the following methods:

contextBridge.exposeInMainWorld(apiKey, api)

  • apiKey string - The key to inject the API onto window with. The API will be accessible on window[apiKey].
  • api any - Your API, more information on what this API can be and how it works is available below.

contextBridge.exposeInIsolatedWorld(worldId, apiKey, api)

  • worldId Integer - The ID of the world to inject the API into. 0 is the default world, 999 is the world used by Electron’s contextIsolation feature. Using 999 would expose the object for preload context. We recommend using 1000+ while creating isolated world.
  • apiKey string - The key to inject the API onto window with. The API will be accessible on window[apiKey].
  • api any - Your API, more information on what this API can be and how it works is available below.

Usage

API

The api provided to exposeInMainWorld must be a Function, string, number, Array, boolean, or an object whose keys are strings and values are a Function, string, number, Array, boolean, or another nested object that meets the same conditions.

Function values are proxied to the other context and all other values are copied and frozen. Any data / primitives sent in the API become immutable and updates on either side of the bridge do not result in an update on the other side.

An example of a complex API is shown below:

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld(
  3. 'electron',
  4. {
  5. doThing: () => ipcRenderer.send('do-a-thing'),
  6. myPromises: [Promise.resolve(), Promise.reject(new Error('whoops'))],
  7. anAsyncFunction: async () => 123,
  8. data: {
  9. myFlags: ['a', 'b', 'c'],
  10. bootTime: 1234
  11. },
  12. nestedAPI: {
  13. evenDeeper: {
  14. youCanDoThisAsMuchAsYouWant: {
  15. fn: () => ({
  16. returnData: 123
  17. })
  18. }
  19. }
  20. }
  21. }
  22. )

An example of exposeInIsolatedWorld is shown below:

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInIsolatedWorld(
  3. 1004,
  4. 'electron',
  5. {
  6. doThing: () => ipcRenderer.send('do-a-thing')
  7. }
  8. )
  1. // Renderer (In isolated world id1004)
  2. window.electron.doThing()

API Functions

Function values that you bind through the contextBridge are proxied through Electron to ensure that contexts remain isolated. This results in some key limitations that we’ve outlined below.

Parameter / Error / Return Type support

Because parameters, errors and return values are copied when they are sent over the bridge, there are only certain types that can be used. At a high level, if the type you want to use can be serialized and deserialized into the same object it will work. A table of type support has been included below for completeness:

TypeComplexityParameter SupportReturn Value SupportLimitations
stringSimpleN/A
numberSimpleN/A
booleanSimpleN/A
ObjectComplexKeys must be supported using only “Simple” types in this table. Values must be supported in this table. Prototype modifications are dropped. Sending custom classes will copy values but not the prototype.
ArrayComplexSame limitations as the Object type
ErrorComplexErrors that are thrown are also copied, this can result in the message and stack trace of the error changing slightly due to being thrown in a different context, and any custom properties on the Error object will be lost
PromiseComplexN/A
FunctionComplexPrototype modifications are dropped. Sending classes or constructors will not work.
Cloneable TypesSimpleSee the linked document on cloneable types
ElementComplexPrototype modifications are dropped. Sending custom elements will not work.
BlobComplexN/A
SymbolN/ASymbols cannot be copied across contexts so they are dropped

If the type you care about is not in the above table, it is probably not supported.

Exposing Node Global Symbols

The contextBridge can be used by the preload script to give your renderer access to Node APIs. The table of supported types described above also applies to Node APIs that you expose through contextBridge. Please note that many Node APIs grant access to local system resources. Be very cautious about which globals and APIs you expose to untrusted remote content.

  1. const { contextBridge } = require('electron')
  2. const crypto = require('node:crypto')
  3. contextBridge.exposeInMainWorld('nodeCrypto', {
  4. sha256sum (data) {
  5. const hash = crypto.createHash('sha256')
  6. hash.update(data)
  7. return hash.digest('hex')
  8. }
  9. })