SOLFIND
Web Lens
Portal home

Context Isolation | Electron

https://www.electronjs.org/de/docs/latest/tutorial/context-isolation • 49 KB fetched
Open original page


Context Isolation | Electron Zum Hauptteil springen Electron Dokumentation API Blog Tools * Electron Forge * Elektro-Fiddle Community * Kontrolle * Showcase * Ressourcen Veröffentlichungen Deutsch * English * Deutsch * Español * Français * 日本語 * Português * Русский * 中文 Suche * Loslegen * Prozesse in Electron * Prozessmodell * Context Isolation * Inter-Process Communication * Process-Sandboxing * Nachrichtenanschlüsse in Electron * Bewährte Verfahren * Beispiele * Entwicklung * Native Node Modules * Distribution * Testen und Debuggen * References * Beitragen * * Prozesse in Electron * Context Isolation Auf dieser Seite 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 // preload with contextIsolation disabled window . myAPI = { doAThing : ( ) => { } } The doAThing() function could then be used directly in the renderer process: renderer.js // use the exposed API in the renderer 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 // preload with contextIsolation enabled const { contextBridge } = require ( 'electron' ) contextBridge . exposeInMainWorld ( 'myAPI' , { doAThing : ( ) => { } } ) renderer.js // use the exposed API in the renderer 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 // ❌ Bad code contextBridge . exposeInMainWorld ( 'myAPI' , { send : ipcRenderer . send } ) 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 // ✅ Good code contextBridge . exposeInMainWorld ( 'myAPI' , { loadPreferences : ( ) => ipcRenderer . invoke ( 'load-prefs' ) } ) 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 contextBridge . exposeInMainWorld ( 'electronAPI' , { loadPreferences : ( ) => ipcRenderer . invoke ( 'load-prefs' ) } ) You can create a interface.d.ts declaration file and globally augment the Window interface: interface.d.ts export interface IElectronAPI { loadPreferences : ( ) => Promise < void > , } declare global { interface Window { electronAPI : IElectronAPI } } 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 window . electronAPI . loadPreferences ( ) Diese Seite bearbeiten Vorherige Prozessmodell Nächste Inter-Process Communication * What is it? * Migration * Before: context isolation disabled * After: context isolation enabled * Security considerations * Usage with TypeScript Dokumentation * Erste Schritte * API-Referenz Checklisten * Performance * Sicherheit Tools * Electron Forge * Elektro-Fiddle Community * Kontrolle * Ressourcen * Discord * Bluesky * X * Mastodon * Stack Overflow Mehr * GitHub * Open Collective * Infrastruktur Dashboard Copyright OpenJS Foundation und Electron Mitwirkenden. Alle Rechte vorbehalten. Die OpenJS Foundation hat eingetragene Marken und verwendet Marken. Für eine Liste der Marken der OpenJS Foundation , lesen Sie bitte unsere Markenrichtlinie und Markenliste . Marken und Logos nicht auf der Liste der Marken der OpenJS Foundation sind Marken™ oder eingetragene® Marken ihrer jeweiligen Inhaber. Der Gebrauch von ihnen impliziert keine Zugehörigkeit oder Zustimmung von ihnen. Die OpenJS Foundation | Nutzungsbedingungen | Datenschutzrichtlinie | Satzung | | | Markenrichtlinie | Markenliste | Cookie Policy Hosting and infrastructure graciously provided by

Links found on this page

  1. Zum Hauptteil springen [direct]
  2. Electron [direct]
  3. Dokumentation [direct]
  4. API [direct]
  5. Blog [direct]
  6. Electron Forge [direct]
  7. Elektro-Fiddle [direct]
  8. Kontrolle [direct]
  9. Showcase [direct]
  10. Ressourcen [direct]
  11. Veröffentlichungen [direct]
  12. English [direct]
  13. Español [direct]
  14. Français [direct]
  15. 日本語 [direct]
  16. Português [direct]
  17. Русский [direct]
  18. 中文 [direct]
  19. Prozesse in Electron [direct]
  20. Inter-Process Communication [direct]
  21. Process-Sandboxing [direct]
  22. Nachrichtenanschlüsse in Electron [direct]
  23. Bewährte Verfahren [direct]
  24. Beispiele [direct]
  25. Entwicklung [direct]
  26. Native Node Modules [direct]
  27. Distribution [direct]
  28. Testen und Debuggen [direct]
  29. References [direct]
  30. Beitragen [direct]
  31. webContents [direct]
  32. contextBridge [direct]
  33. declaration file [direct]
  34. Diese Seite bearbeiten [direct]
  35. Sicherheit [direct]
  36. Discord [direct]
  37. Bluesky [direct]
  38. X [direct]
  39. Mastodon [direct]
  40. Stack Overflow [direct]
  41. GitHub [direct]
  42. Open Collective [direct]
  43. Infrastruktur Dashboard [direct]
  44. OpenJS Foundation [direct]
  45. Markenrichtlinie [direct]
  46. Markenliste [direct]
  47. Nutzungsbedingungen [direct]
  48. Datenschutzrichtlinie [direct]
  49. Satzung [direct]
  50. | [direct]
  51. Cookie Policy [direct]