Context Isolation

What is it?

Context Isolation is a feature that ensures that both your preload scripts and Electron’s internal logic run in a separate context to the website you load in a webContents. This is important for security purposes as it helps prevent the website from accessing Electron internals or the powerful APIs your preload script has access to.

This means that the window object that your preload script has access to is actually a different object than the website would have access to. For example, if you set window.hello = 'wave' in your preload script and context isolation is enabled, window.hello will be undefined if the website tries to access it.

Context isolation has been enabled by default since Electron 12, and it is a recommended security setting for all applications.

Migration

Without context isolation, I used to provide APIs from my preload script using window.X = apiObject. Now what?

Before: context isolation disabled

Exposing APIs from your preload script to a loaded website in the renderer process is a common use-case. With context isolation disabled, your preload script would share a common global window object with the renderer. You could then attach arbitrary properties to a preload script:

preload.js

  1. // preload with contextIsolation disabled
  2. window.myAPI = {
  3. doAThing: () => {}
  4. }

The doAThing() function could then be used directly in the renderer process:

renderer.js

  1. // use the exposed API in the renderer
  2. window.myAPI.doAThing()

After: context isolation enabled

There is a dedicated module in Electron to help you do this in a painless way. The contextBridge module can be used to safely expose APIs from your preload script’s isolated context to the context the website is running in. The API will also be accessible from the website on window.myAPI just like it was before.

preload.js

  1. // preload with contextIsolation enabled
  2. const { contextBridge } = require('electron')
  3. contextBridge.exposeInMainWorld('myAPI', {
  4. doAThing: () => {}
  5. })

renderer.js

  1. // use the exposed API in the renderer
  2. window.myAPI.doAThing()

Please read the contextBridge documentation linked above to fully understand its limitations. For instance, you can’t send custom prototypes or symbols over the bridge.

Security considerations

Just enabling contextIsolation and using contextBridge does not automatically mean that everything you do is safe. For instance, this code is unsafe.

preload.js

  1. // ❌ Bad code
  2. contextBridge.exposeInMainWorld('myAPI', {
  3. send: ipcRenderer.send
  4. })

It directly exposes a powerful API without any kind of argument filtering. This would allow any website to send arbitrary IPC messages, which you do not want to be possible. The correct way to expose IPC-based APIs would instead be to provide one method per IPC message.

preload.js

  1. // ✅ Good code
  2. contextBridge.exposeInMainWorld('myAPI', {
  3. loadPreferences: () => ipcRenderer.invoke('load-prefs')
  4. })

Usage with TypeScript

If you’re building your Electron app with TypeScript, you’ll want to add types to your APIs exposed over the context bridge. The renderer’s window object won’t have the correct typings unless you extend the types with a declaration file.

For example, given this preload.ts script:

preload.ts

  1. contextBridge.exposeInMainWorld('electronAPI', {
  2. loadPreferences: () => ipcRenderer.invoke('load-prefs')
  3. })

You can create a renderer.d.ts declaration file and globally augment the Window interface:

renderer.d.ts

  1. export interface IElectronAPI {
  2. loadPreferences: () => Promise<void>,
  3. }
  4. declare global {
  5. interface Window {
  6. electronAPI: IElectronAPI
  7. }
  8. }

Doing so will ensure that the TypeScript compiler will know about the electronAPI property on your global window object when writing scripts in your renderer process:

renderer.ts

  1. window.electronAPI.loadPreferences()