Skip to content

Commit 443660e

Browse files
committed
docs(security): add guide for ipc with contextIsolation
1 parent 0739358 commit 443660e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/guide/security.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ window.ipcRenderer = ipcRenderer
2525
```
2626

2727
Now, you can access `ipcRenderer` with `window.ipcRenderer` in your Vue app.
28+
29+
If you want to use IPC without `nodeIntegration` and with `contextIsolation`, use this:
30+
31+
```js
32+
import { contextBridge, ipcRenderer } from 'electron'
33+
34+
// Expose protected methods that allow the renderer process to use
35+
// the ipcRenderer without exposing the entire object
36+
contextBridge.exposeInMainWorld('ipcRenderer', {
37+
send: (channel, data) => {
38+
// whitelist channels
39+
let validChannels = ['toMain']
40+
if (validChannels.includes(channel)) {
41+
ipcRenderer.send(channel, data)
42+
}
43+
},
44+
receive: (channel, func) => {
45+
let validChannels = ['fromMain']
46+
if (validChannels.includes(channel)) {
47+
// Deliberately strip event as it includes `sender`
48+
ipcRenderer.on(channel, (event, ...args) => func(...args))
49+
}
50+
}
51+
})
52+
```
53+
54+
Then, you can use `window.ipcRenderer.(send/receive)` in the renderer process.
55+
56+
(Solution from [this StackOverflow answer](https://stackoverflow.com/questions/55164360/with-contextisolation-true-is-it-possible-to-use-ipcrenderer/59675116#59675116))
57+
2858
:::
2959

3060
## Loading Local Images/Resources

0 commit comments

Comments
 (0)