Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(login): submit the logout form to the page-relative route only
Elian Dorand75998d3fa00
diff --git a/apps/client/src/components/entrypoints.spec.ts b/apps/client/src/components/entrypoints.spec.tsindex c4244b4c75..276a26a2ce 100644--- a/apps/client/src/components/entrypoints.spec.ts+++ b/apps/client/src/components/entrypoints.spec.ts@@ -43,58 +43,30 @@ describe("logoutCommand", () => {window.glob = {...window.glob,baseApiUrl: "api/",- csrfToken: "csrf-test-token",- httpBaseUrl: undefined,- isElectron: false+ csrfToken: "csrf-test-token"};});- it("submits a CSRF-protected browser navigation for OIDC redirects", async () => {+ it("submits a CSRF-protected form navigation to the logout route", () => {const submit = vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});- await entrypoints.logoutCommand();+ entrypoints.logoutCommand();const form = document.body.querySelector("form");expect(form).not.toBeNull();expect(form?.method).toBe("POST");expect(form?.action).toBe("http://localhost:3000/logout");- expect(- form?.querySelector("input[name='x-csrf-token']")?.getAttribute("value")- ).toBe("csrf-test-token");+ expect(form?.querySelector("input[name='x-csrf-token']")?.getAttribute("value"))+ .toBe("csrf-test-token");expect(submit).toHaveBeenCalledOnce();- });-- it("selects the correct logout target for each runtime", async () => {- const submit = vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});- const actions: string[] = [];-- window.glob.httpBaseUrl = "http://127.0.0.1:37742";- await entrypoints.logoutCommand();- actions.push(document.body.querySelector("form")?.action ?? "");+ // The route resolves against the page like every other request, so a sub-path+ // deployment keeps its prefix.document.body.replaceChildren();- const happyDOM = (window as unknown as {- happyDOM: { setURL(url: string): void }- }).happyDOM;- happyDOM.setURL("trilium-app://app/");- window.glob.isElectron = true;- await entrypoints.logoutCommand();- actions.push(document.body.querySelector("form")?.action ?? "");-- document.body.replaceChildren();- window.glob.isElectron = false;- window.glob.httpBaseUrl = undefined;- happyDOM.setURL("http://localhost:3000/");window.history.replaceState({}, "", "/trilium/");- await entrypoints.logoutCommand();- actions.push(document.body.querySelector("form")?.action ?? "");- happyDOM.setURL("http://localhost:3000/");-- expect(actions).toEqual([- "http://127.0.0.1:37742/logout",- "trilium-app://app/logout",- "http://localhost:3000/trilium/logout"- ]);- expect(submit).toHaveBeenCalledTimes(3);+ entrypoints.logoutCommand();+ expect(document.body.querySelector("form")?.action)+ .toBe("http://localhost:3000/trilium/logout");+ window.history.replaceState({}, "", "/");});});diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.tsindex 8ce030a5e8..b5cfeee1ca 100644--- a/apps/client/src/components/entrypoints.ts+++ b/apps/client/src/components/entrypoints.ts@@ -99,19 +99,12 @@ export default class Entrypoints extends Component {utils.reloadFrontendApp();}- async logoutCommand() {- // A browser navigation avoids the cross-origin XHR preflight that OIDC providers reject.- // Electron keeps the request on its trusted custom protocol.+ logoutCommand() {+ // A form submission keeps the OIDC provider's redirect a top-level navigation; an XHR+ // follows it cross-origin and fails the provider's CORS preflight.const form = document.createElement("form");form.method = "POST";- const httpBaseUrl = window.glob.httpBaseUrl;- if (window.glob.isElectron) {- form.action = String(new URL(`${window.glob.baseApiUrl}../logout`, window.location.href).href);- } else if (httpBaseUrl) {- form.action = String(new URL("/logout", httpBaseUrl).href);- } else {- form.action = String(new URL(`${window.glob.baseApiUrl}../logout`, window.location.href).href);- }+ form.action = `${window.glob.baseApiUrl}../logout`;form.hidden = true;const csrfToken = document.createElement("input");diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.tsindex 81cfd98c73..572a7537e3 100644--- a/apps/desktop/src/services/web_contents_security.spec.ts+++ b/apps/desktop/src/services/web_contents_security.spec.ts@@ -579,17 +579,14 @@ describe("navigation guard", () => {setupWebContentsSecurity(SECURITY_OPTIONS);});- it("allows the app shell root and the logout endpoint", () => {+ it("only allows the app shell at its root path", () => {expect(isNavigationAllowed("trilium-app://app/")).toBe(true);// Root "/?" path is allowed.expect(isNavigationAllowed("trilium-app://app/?")).toBe(true);// The extra-window URL: root path with a query string and a hash.expect(isNavigationAllowed("trilium-app://app/?extraWindow=1#root/abc")).toBe(true);- // Logout is a form POST dispatched through the trusted custom protocol.- expect(isNavigationAllowed("trilium-app://app/logout")).toBe(true);-- // Other app paths are blocked (in-page SPA routing / hostile).+ // App shell but non-root path is blocked (in-page SPA routing / hostile).expect(isNavigationAllowed("trilium-app://app/somewhere")).toBe(false);// Our scheme but not the app host — only `trilium-app://app` is ever served.expect(isNavigationAllowed("trilium-app://evil/")).toBe(false);diff --git a/apps/desktop/src/services/web_contents_security.ts b/apps/desktop/src/services/web_contents_security.tsindex 74798f218f..2b22146e5f 100644--- a/apps/desktop/src/services/web_contents_security.ts+++ b/apps/desktop/src/services/web_contents_security.ts@@ -213,7 +213,9 @@ export function isPermissionAllowedForOrigin(kind: SessionKind, permission: stri* meta refresh) can proceed, and which `window.open` URLs become extra* windows. Only the app shell itself is allowed: the `trilium-app://app`* origin (the sole host ever served — see the loadURL call sites), and only- * at the root path or the renderer's logout endpoint.+ * at the root path; the query string (`?extraWindow=1`) and the hash are not+ * part of the check. Anything deeper is in-page SPA routing (which+ * `will-navigate` does not fire for) or hostile.** `localhost` was previously allowed too, from the era when the desktop* renderer was served over `http://127.0.0.1:<port>`. The custom-protocol@@ -225,9 +227,7 @@ export function isNavigationAllowed(targetUrl: string): boolean {const parsedUrl = url.parse(targetUrl);const isAppShell = parsedUrl.protocol === `${TRILIUM_APP_SCHEME}:` && parsedUrl.hostname === TRILIUM_APP_HOST;- return isAppShell && (!parsedUrl.pathname- || parsedUrl.pathname === "/"- || parsedUrl.pathname === "/logout");+ return isAppShell && (!parsedUrl.pathname || parsedUrl.pathname === "/");}/**