Skip to content

Build your personal knowledge base with Trilium Notes

8.4 KB
Raw Blame

trilium-app:// protocol + WebContents security triage

When the failure isn't a missing IPC channel but the renderer page itself misbehaving (white screen, crash, blocked request, non-streaming SSE, a rejected <webview> / permission / download), the cause is one of two files:

In packaged Electron the renderer loads the entire UI from trilium-app://app/ — every page load, bootstrap, API call, and asset arrives at protocol.handle as a Web Fetch Request and is dispatched through the real Express stack (session, CSRF, body-parser, multer, error middleware). It is not HTTP, which is why HTTP-shaped headers and origins break in surprising ways.

Symptom → cause → fix

Symptom Cause File / fix
Renderer aborts with STATUS_BREAKPOINT (often a white screen on launch) An http-only / HSTS / COOP / CORP / origin-agent-cluster header leaked onto the trilium-app:// response. Those headers run during renderer-process setup and trip internal Chromium asserts when the scheme isn't http(s). Add the header name to STRIPPED_HEADERS (protocol.ts:485). normalizeResponseHeaders (protocol.ts:500) drops everything in that set before building the Fetch Response. The rationale comment sits just above the set.
Navigation aborts with (blocked:origin) registerSchemesAsPrivileged did not run synchronously before app.ready, so Chromium treats the scheme as non-standard with an opaque origin. registerTriliumAppScheme() (protocol.ts:26) must be called at the very top of main() (it is, at the top of main()) — before any await. The "must be called before app.ready" note is in its docstring (its docstring); privileges set: standard, secure, supportFetchAPI, corsEnabled, codeCache.
SSE / LLM chat stream buffers until the request ends (no incremental tokens) The handler never called res.flushHeaders(), or the streaming bridge shim is missing/broken. Non-streaming handlers resolve via the buffered res.on("end") path; only flushHeaders() commits a ReadableStream body. installStreamingBridge (protocol.ts:284) maps res.flushHeaders() → commit(), which resolves the Fetch Response with a ReadableStream and forwards later res.write(chunk) into the controller. The handler must call flushHeaders() to opt in. The bridge also shims res.flush to a no-op to avoid an Express prototype-swap crash (in the same bridge).
API call from an iframe returns 403 / is cancelled The frame-origin guard rejected the request: a non-mainFrame request whose frame chain contains a non-app (foreign) frame is denied before the protocol handler runs. installFrameOriginGuard (protocol.ts:120) registers a webRequest.onBeforeRequest filter; the pure policy is isRequestorChainTrusted(resourceType, frameUrls) (protocol.ts:143) — mainFrame always allowed; otherwise every committed frame must be the app shell or devtools:. A second, weaker check isDispatchOriginAllowed (protocol.ts:173) 403s any request that positively attests a foreign Origin (only navigation-style requests carry one).
<webview> won't attach / log shows Blocked <webview> attach requesting [...] The attach requested a banned capability (preload, nodeIntegration, nodeIntegrationInSubFrames, webSecurity:false, allowRunningInsecureContent) or the wrong/empty session partition. hardenWebviewPreferences (web_contents_security.ts:79), called from the will-attach-webview hook (:50), returns the violation list; a non-empty list preventDefault()s the attach. The partition must exactly equal WEBVIEW_SESSION_PARTITION ("persist:webview") — an unset/empty/different partition is a violation (:111-115). The legit Web View note always declares it.
Permission denied (camera, mic, clipboard-read, notifications or geolocation from a remote embed, …) Not in PERMISSION_ALLOWLIST for that session kind, or the request origin isn't the app shell. Unset handlers would GRANT everything, so deny-by-default is enforced explicitly. PERMISSION_ALLOWLIST (web_contents_security.ts:150): app = clipboard-sanitized-write, fileSystem, fullscreen, geolocation, local-fonts, notifications; guest = fullscreen only. isPermissionAllowedForOrigin (:176) additionally requires the request to come from trilium-app://app (except fullscreen, allowed for any origin). Installed on both the default and WEBVIEW_SESSION_PARTITION sessions (:63-66).
window.open / target=_blank does nothing, or a note link opens in the OS browser By design: only the app shell's root URL (isNavigationAllowed, query string and hash allowed) is allowed — that is the client's own "open in new window", and Chromium creates it in the opener's renderer process; every other window-open is denied, and for app windows the URL is re-validated through the same scheme allowlist and routed to the OS browser. installWindowOpenPolicy (web_contents_security.ts) returns allow + outlivesOpener + the extraWindowOptions() that setupWindowing() supplies; otherwise it reuses validateOpenExternalUrl from shell.ts, so a note-content link can't bypass the Follina/smb: allowlist. <webview> guests are denied outright (no external dispatch, not even for the app URL).
A window opened from the client is blank, or has no window.electronApi The window.open child was built without a preload — children inherit only the security-related webPreferences (sandbox, nodeIntegration, …), never preload — or its initial navigation was blocked by the guard. getExtraWindowOptions() (window.ts) must include webPreferences.preload; setupWindowing() passes it as extraWindowOptions. did-create-window on the opener runs adoptExtraWindow() (menu bar, spellcheck, focus tracking). The --new-window command line still uses main-process createExtraWindow(), which has no opener.
A link/drag navigates an app window away from Trilium and gets blocked The navigation guard only allows the app shell root. isNavigationAllowed(targetUrl) (web_contents_security.ts:199): allowed only for trilium-app: + the app host at root path (/, /?, or empty). <webview> guests are exempt (they're embedded browsers). The old localhost carve-out was removed as dangerous.
YouTube embed shows "video player configuration error" On desktop the renderer origin is trilium-app://app (not http(s)), so the embed request goes out with no Referer. installYouTubeEmbedReferer (web_contents_security.ts:293) injects Referer: https://triliumnotes.org/ (a non-youtube.com origin) for the embed hosts, never overwriting an existing header.

Trust model, in one paragraph

Chromium stamps no reliable identifying headers on requests to a privileged custom scheme (protocol.ts:79-95 documents this empirically for Electron 41): same-origin renderer requests carry no Origin, foreign fetch() calls also carry none, and corsEnabled isn't enforced for reads. The only forgeable-proof signal is the requesting frame chain, which is main-process-side state (details.frame + .parent). So the real gate is installFrameOriginGuard (frame-chain trust), with isDispatchOriginAllowed as a weaker backstop for the rare navigation-style request that does carry an Origin. <webview> guests are out of scope by construction — they live in WEBVIEW_SESSION_PARTITION, where the trilium-app:// handler is never registered.

Tests

Both files expose their policy as pure exported functions for unit testing without spinning up Electron:

  • protocol.ts: isRequestorChainTrusted, isDispatchOriginAllowed, plus dispatch exercised via node-mocks-http.
  • web_contents_security.ts: hardenWebviewPreferences, isPermissionAllowed, isPermissionAllowedForOrigin, isNavigationAllowed, withYouTubeEmbedReferer.

Run with pnpm --filter desktop test (e.g. apps/desktop/src/services/web_contents_security.spec.ts). See the writing-unit-tests skill for the vi.mock("electron") pattern.