Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): gate app-session permissions by request origin

Elian Doran536751c5e360
diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.ts
index d0a9d8908a..fe5fbf23fa 100644
--- a/apps/desktop/src/services/web_contents_security.spec.ts
+++ b/apps/desktop/src/services/web_contents_security.spec.ts
@@ -11,7 +11,7 @@ const state = vi.hoisted(() => {
callback: (granted: boolean) => void,
details: { requestingUrl: string }
) => void;
- type PermissionCheckHandler = (webContents: unknown, permission: string) => boolean;
+ type PermissionCheckHandler = (webContents: unknown, permission: string, requestingOrigin?: string) => boolean;
/** Fake Electron session capturing the permission handlers installed on it. */
function makeFakeSession() {
@@ -80,7 +80,7 @@ vi.mock("electron", () => ({
}
}));
-const { hardenWebviewPreferences, isNavigationAllowed, isPermissionAllowed, setupWebContentsSecurity } = await import("./web_contents_security.js");
+const { hardenWebviewPreferences, isNavigationAllowed, isPermissionAllowed, isPermissionAllowedForOrigin, setupWebContentsSecurity } = await import("./web_contents_security.js");
interface WindowOpenResult {
action: "allow" | "deny";
@@ -281,6 +281,35 @@ describe("isPermissionAllowed", () => {
});
});
+describe("isPermissionAllowedForOrigin", () => {
+ it("gates non-fullscreen app permissions on the trilium-app://app origin", () => {
+ // The app shell itself keeps its grants.
+ expect(isPermissionAllowedForOrigin("app", "notifications", "trilium-app://app")).toBe(true);
+ expect(isPermissionAllowedForOrigin("app", "clipboard-sanitized-write", "trilium-app://app/some/path")).toBe(true);
+
+ // A remote <iframe> sharing the default session does not.
+ expect(isPermissionAllowedForOrigin("app", "notifications", "https://www.youtube-nocookie.com/embed/x")).toBe(false);
+ expect(isPermissionAllowedForOrigin("app", "clipboard-sanitized-write", "https://evil.example")).toBe(false);
+ // Another host under our own scheme is not the app shell.
+ expect(isPermissionAllowedForOrigin("app", "notifications", "trilium-app://evil")).toBe(false);
+ // A missing / unparseable origin is treated as untrusted.
+ expect(isPermissionAllowedForOrigin("app", "notifications", null)).toBe(false);
+ expect(isPermissionAllowedForOrigin("app", "notifications", "not a url")).toBe(false);
+ });
+
+ it("allows fullscreen for any origin, in both sessions", () => {
+ expect(isPermissionAllowedForOrigin("app", "fullscreen", "https://www.youtube-nocookie.com/embed/x")).toBe(true);
+ expect(isPermissionAllowedForOrigin("guest", "fullscreen", "https://example.com")).toBe(true);
+ expect(isPermissionAllowedForOrigin("app", "fullscreen", null)).toBe(true);
+ });
+
+ it("never grants a permission outside the session allowlist, even from the app shell", () => {
+ expect(isPermissionAllowedForOrigin("app", "geolocation", "trilium-app://app")).toBe(false);
+ expect(isPermissionAllowedForOrigin("app", "clipboard-read", "trilium-app://app")).toBe(false);
+ expect(isPermissionAllowedForOrigin("guest", "notifications", "trilium-app://app")).toBe(false);
+ });
+});
+
describe("permission handlers", () => {
beforeEach(async () => {
resetState();
@@ -299,12 +328,37 @@ describe("permission handlers", () => {
expect(state.partitionSessions.size).toBe(1);
});
- it("grants allowlisted permission requests without logging", () => {
+ it("grants allowlisted permission requests from the app shell without logging", () => {
+ const handler = state.defaultSession.requestHandler;
+ if (!handler) throw new Error("request handler not installed");
+
+ const callback = vi.fn();
+ handler({}, "notifications", callback, { requestingUrl: "trilium-app://app/" });
+
+ expect(callback).toHaveBeenCalledWith(true);
+ expect(state.errors).toEqual([]);
+ });
+
+ it("denies an allowlisted permission requested by a foreign embedded iframe", () => {
+ const handler = state.defaultSession.requestHandler;
+ if (!handler) throw new Error("request handler not installed");
+
+ // A YouTube embed (link_embed.tsx) runs in the default session but must
+ // not inherit the app shell's notification / clipboard grants.
+ const callback = vi.fn();
+ handler({}, "notifications", callback, { requestingUrl: "https://www.youtube-nocookie.com/embed/x" });
+
+ expect(callback).toHaveBeenCalledWith(false);
+ expect(state.errors[0]).toContain("notifications");
+ expect(state.errors[0]).toContain("youtube-nocookie.com");
+ });
+
+ it("grants fullscreen to any origin in the app session (embedded video players)", () => {
const handler = state.defaultSession.requestHandler;
if (!handler) throw new Error("request handler not installed");
const callback = vi.fn();
- handler({}, "fullscreen", callback, { requestingUrl: "trilium-app://main/" });
+ handler({}, "fullscreen", callback, { requestingUrl: "https://www.youtube-nocookie.com/embed/x" });
expect(callback).toHaveBeenCalledWith(true);
expect(state.errors).toEqual([]);
@@ -330,10 +384,12 @@ describe("permission handlers", () => {
const guestCheck = state.partitionSessions.get("persist:webview")?.checkHandler;
if (!appCheck || !guestCheck) throw new Error("check handlers not installed");
- expect(appCheck({}, "clipboard-sanitized-write")).toBe(true);
- expect(appCheck({}, "geolocation")).toBe(false);
- expect(guestCheck({}, "fullscreen")).toBe(true);
- expect(guestCheck({}, "clipboard-sanitized-write")).toBe(false);
+ expect(appCheck({}, "clipboard-sanitized-write", "trilium-app://app")).toBe(true);
+ expect(appCheck({}, "clipboard-sanitized-write", "https://www.youtube-nocookie.com")).toBe(false);
+ expect(appCheck({}, "geolocation", "trilium-app://app")).toBe(false);
+ // Fullscreen is origin-independent.
+ expect(guestCheck({}, "fullscreen", "https://www.youtube-nocookie.com")).toBe(true);
+ expect(guestCheck({}, "clipboard-sanitized-write", "https://www.youtube-nocookie.com")).toBe(false);
});
});
diff --git a/apps/desktop/src/services/web_contents_security.ts b/apps/desktop/src/services/web_contents_security.ts
index 9b7f8af165..2b8f1e8c7b 100644
--- a/apps/desktop/src/services/web_contents_security.ts
+++ b/apps/desktop/src/services/web_contents_security.ts
@@ -136,6 +136,11 @@ export function hardenWebviewPreferences(webPreferences: Electron.WebPreferences
* - `guest`: the `<webview>` partition hosting arbitrary remote pages from
* Web View notes. Fullscreen only (embedded video players) — a remote page
* must not show OS notifications that appear to come from Trilium.
+ *
+ * The `app` allowlist is gated by request origin (see
+ * {@link isPermissionAllowedForOrigin}): only the `trilium-app://app` shell
+ * gets clipboard-write / notifications, not remote `<iframe>` embeds that share
+ * the default session. Fullscreen is the exception — granted for any origin.
*/
const PERMISSION_ALLOWLIST = {
app: new Set(["clipboard-sanitized-write", "fullscreen", "notifications"]),
@@ -149,6 +154,43 @@ export function isPermissionAllowed(kind: SessionKind, permission: string): bool
return PERMISSION_ALLOWLIST[kind].has(permission);
}
+/**
+ * Full permission decision: the per-session allowlist {@link isPermissionAllowed}
+ * AND an origin check. The default (`app`) session hosts not only the trusted
+ * app shell but also remote `<iframe>` embeds (e.g. the YouTube preview in
+ * `link_embed.tsx`) — those run with the app session's permissions unless the
+ * request origin is checked. So every allowlisted permission additionally
+ * requires the request to come from the `trilium-app://app` shell, which keeps
+ * a remote embed from inheriting the renderer's clipboard / notification grants.
+ *
+ * `fullscreen` is the deliberate exception: it is allowed for any origin
+ * (embedded video players — both the YouTube `<iframe>` and `<webview>` guests —
+ * legitimately request it, and it carries no exfiltration / spoofing risk that
+ * an origin check would mitigate).
+ */
+export function isPermissionAllowedForOrigin(kind: SessionKind, permission: string, requestingUrl: string | null | undefined): boolean {
+ if (!isPermissionAllowed(kind, permission)) {
+ return false;
+ }
+ if (permission === "fullscreen") {
+ return true;
+ }
+ return isAppShellOrigin(requestingUrl);
+}
+
+/** True only for the app shell's own origin (`trilium-app://app`). */
+function isAppShellOrigin(requestingUrl: string | null | undefined): boolean {
+ if (!requestingUrl) {
+ return false;
+ }
+ try {
+ const parsed = new URL(requestingUrl);
+ return parsed.protocol === "trilium-app:" && parsed.host === "app";
+ } catch {
+ return false;
+ }
+}
+
/**
* Decides whether a renderer-initiated navigation (link click, drag & drop,
* meta refresh) may proceed. Only the app shell itself is allowed: the
@@ -217,7 +259,7 @@ function installNavigationGuard(webContents: Electron.WebContents) {
function installPermissionPolicy(session: Electron.Session, kind: SessionKind) {
// Asynchronous requests (e.g. getUserMedia prompting for the camera).
session.setPermissionRequestHandler((_webContents, permission, callback, details) => {
- const allowed = isPermissionAllowed(kind, permission);
+ const allowed = isPermissionAllowedForOrigin(kind, permission, details.requestingUrl);
if (!allowed) {
getLog().error(`Denied '${permission}' permission request from ${details.requestingUrl} (${kind} session)`);
}
@@ -225,5 +267,6 @@ function installPermissionPolicy(session: Electron.Session, kind: SessionKind) {
});
// Synchronous checks (e.g. navigator.permissions.query, push subscription
// state) — without this handler Electron reports everything as granted.
- session.setPermissionCheckHandler((_webContents, permission) => isPermissionAllowed(kind, permission));
+ session.setPermissionCheckHandler((_webContents, permission, requestingOrigin) =>
+ isPermissionAllowedForOrigin(kind, permission, requestingOrigin));
}