Lesson 5 of 9 · 25m
Remote Content, CSP, and Permission Gates
Remote pages, media, fonts, and embeds bring the web's threat model into your shell; control origins, CSP, session permissions, and navigation explicitly.
The dangerous Electron fantasy is "we only load our own site." Then marketing adds a documentation iframe, support asks for a status page embed, a dependency renders external avatars, and suddenly your desktop shell is executing a rotating cast of remote JavaScript. Remote content is sometimes appropriate — but it has to be treated as untrusted web content inside a privileged application.
Separate trusted shell from untrusted web
The strongest pattern is local packaged UI in the privileged shell, remote services accessed as APIs. When remote UI is unavoidable, isolate it:
window A: packaged app UI full application bridge
window B: remote help/docs no application bridge, hardened session, external links deniedDo not reuse one session casually across trust levels. Cookies, cache, service workers, permission decisions, and storage are partitioned by session. Use session.fromPartition("persist:docs") for a dedicated remote-content compartment when isolation is required.
CSP is your second renderer boundary
A Content Security Policy limits what injected content can do after a bug occurs. A packaged app should start restrictive and loosen only for measured needs:
default-src 'self';
script-src 'self';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
connect-src 'self' https://api.sudosecurity.org;
font-src 'self';
object-src 'none';
base-uri 'none';
frame-ancestors 'none''unsafe-inline' in script-src largely defeats the point. If your bundler or framework requires inline bootstrap scripts, use nonces or hashes rather than opening the door. Test CSP in release builds; dev servers often inject different scripts than production.
Permission requests should default to no
Electron renderers can request camera, microphone, geolocation, notifications, clipboard, MIDI, serial, and more depending on configuration. Handle permission requests centrally:
session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => {
const url = webContents.getURL();
const allowed =
permission === "media" &&
url.startsWith("https://meet.sudosecurity.org/") &&
userMediaConsent.has(webContents.id);
callback(Boolean(allowed));
});Also review setPermissionCheckHandler; request and check are separate paths. Silent default-allow is common in sample code and unacceptable in production.
Service workers and third-party embeds
Service workers persist beyond page lifetime and can complicate updates, offline behavior, and incident response. Disable them for remote embeds unless you explicitly need them. Third-party iframes can observe embedding context and consume permissions; use <webview> only when you understand its process model, and prefer ordinary BrowserWindows for first-class isolation.
Field notes
- CSP belongs in HTTP response headers or
<meta>only when headers are impossible; headers win for remote content. - Test what happens when the network is hostile: captive portals rewrite remote pages.
- Separate sessions do not make malicious content safe — they merely reduce blast radius.
- Permission names are Chromium-specific strings; log unknown requests during development so new capabilities do not silently pass.
Was this lesson helpful?