electron-trpc
electron-trpc copied to clipboard
Not compatible with default sandbox mode of Electron 20+
Since Electron 20, the sandbox
option of window webPreferences
has been set to true by default. This means that preload scripts can only use a small subset of the Electron and Node APIs. Specifically, require
is polyfilled with a version that will only allow requiring a small subset of node core modules.
This causes a problem for this lib, since we need to require electron-trpc/main
in the preload script, and then call exposeElectronTRPC()
.
Disabling the sandbox of course resolves this issue, but at the cost of losing the security benefits of sandboxing.
After a quick think about this I have two potential solutions:
- Advise users to process and bundle their preload script. This would inline the import of the constants, leaving only the allowed
contextBridge
code. This should be a documentation only change, perhaps based on a samplevite-plugin-electron
config. - Remove
ELECTRON_TRPC_CHANNEL
and make it up to the user to configure an appropriate and consistent channel across main, preload, and renderer. Then supply a copy/paste snippet for the preload file that only uses thecontextBridge
API. It would be helpful to export theRendererGlobalElectronTRPC
type so consumers could at least match up with some sort of package API.
There may be other options I haven't considered.
Thanks for creating such a useful lib!
Hey, thanks for the thorough detail here and the considerations for fixing this. It seems to me that both of these solutions could be available going forward, where users pre-processing their preload file could use the current export, and users that are authoring a preload file directly could use a snippet.
I'm going to work on adding an example that's non-vite to the examples which should demonstrate the issue, and make the requisite changes.
Thanks again!
Hello, any update on this? :)