Skip to content

Commit 47994c9

Browse files
chore: update ref to docs (🤖)
1 parent ce756e4 commit 47994c9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

‎docs/latest/.sha

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a1816f3587aa63f2eebbda4f38c1a194e286e281
1+
c538aa8e6cf2f34e74f6860138536bab84b74a02

‎docs/latest/tutorial/security.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ You should at least follow these steps to improve the security of your applicati
122122
17. [Validate the `sender` of all IPC messages](#17-validate-the-sender-of-all-ipc-messages)
123123
18. [Avoid usage of the `file://` protocol and prefer usage of custom protocols](#18-avoid-usage-of-the-file-protocol-and-prefer-usage-of-custom-protocols)
124124
19. [Check which fuses you can change](#19-check-which-fuses-you-can-change)
125+
20. [Do not expose Electron APIs to untrusted web content](#20-do-not-expose-electron-apis-to-untrusted-web-content)
125126

126127
To automate the detection of misconfigurations and insecure patterns, it is
127128
possible to use
@@ -238,7 +239,7 @@ API to remotely loaded content via the [contextBridge API](../api/context-bridge
238239

239240
:::info
240241

241-
This recommendation is the default behavior in Electron since 12.0.0.
242+
Context Isolation is the default behavior in Electron since 12.0.0.
242243

243244
:::
244245

@@ -828,6 +829,50 @@ flipping these fuses easy. Check out the README of that module for more details
828829
potential error cases, and refer to
829830
[How do I flip the fuses?](./fuses.md#how-do-i-flip-the-fuses) in our documentation.
830831

832+
### 20. Do not expose Electron APIs to untrusted web content
833+
834+
You should not directly expose Electron's APIs, especially IPC, to untrusted web content in your
835+
preload scripts.
836+
837+
### Why?
838+
839+
Exposing raw APIs like `ipcRenderer.on` is dangerous because it gives renderer processes direct
840+
access to the entire IPC event system, allowing them to listen for any IPC events, not just the ones
841+
intended for them.
842+
843+
To avoid that exposure, we also cannot pass callbacks directly through: The first
844+
argument to IPC event callbacks is an `IpcRendererEvent` object, which includes properties like `sender`
845+
that provide access to the underlying `ipcRenderer` instance. Even if you only listen for specific
846+
events, passing the callback directly means the renderer gets access to this event object.
847+
848+
In short, we want the untrusted web content to only have access to necessary information and APIs.
849+
850+
### How?
851+
852+
```js title='preload'.js'
853+
// Bad
854+
contextBridge.exposeInMainWorld('electronAPI', {
855+
on: ipcRenderer.on
856+
})
857+
858+
// Also bad
859+
contextBridge.exposeInMainWorld('electronAPI', {
860+
onUpdateCounter: (callback) => ipcRenderer.on('update-counter', callback)
861+
})
862+
863+
// Good
864+
contextBridge.exposeInMainWorld('electronAPI', {
865+
onUpdateCounter: (callback) => ipcRenderer.on('update-counter', (_event, value) => callback(value))
866+
})
867+
```
868+
869+
:::info
870+
871+
For more information on what `contextIsolation` is and how to use it to secure your app,
872+
please see the [Context Isolation](context-isolation.md) document.
873+
874+
:::
875+
831876
[breaking-changes]: ../breaking-changes.md
832877
[browser-window]: ../api/browser-window.md
833878
[webview-tag]: ../api/webview-tag.md

0 commit comments

Comments
 (0)