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: navigate through OIDC logout redirect (closes #11183)
dajiaohuangb9a684bc6c61
diff --git a/apps/client/src/components/entrypoints.spec.ts b/apps/client/src/components/entrypoints.spec.tsindex c53381088c..dee4ab5e42 100644--- a/apps/client/src/components/entrypoints.spec.ts+++ b/apps/client/src/components/entrypoints.spec.ts@@ -32,3 +32,50 @@ describe("openInWindowCommand", () => {expect(String(url)).toMatch(/#root\/abc123$/);});});++describe("logoutCommand", () => {+ const entrypoints = new Entrypoints();++ beforeEach(() => {+ vi.restoreAllMocks();+ document.body.replaceChildren();+ window.history.replaceState({}, "", "/");+ window.glob = {+ ...window.glob,+ baseApiUrl: "api/",+ csrfToken: "csrf-test-token",+ httpBaseUrl: undefined+ };+ });++ it("submits a CSRF-protected browser navigation for OIDC redirects", async () => {+ const submit = vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});++ await 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(submit).toHaveBeenCalledOnce();+ });++ it("uses the server origin supplied to the Electron renderer", async () => {+ window.glob.httpBaseUrl = "http://127.0.0.1:37742";+ vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});++ await entrypoints.logoutCommand();++ expect(document.body.querySelector("form")?.action).toBe("http://127.0.0.1:37742/logout");+ });++ it("keeps the browser deployment path", async () => {+ window.history.replaceState({}, "", "/trilium/");+ vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});++ await entrypoints.logoutCommand();++ expect(document.body.querySelector("form")?.action).toBe("http://localhost:3000/trilium/logout");+ });+});diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.tsindex 58d5d64ac6..3891fa3d71 100644--- a/apps/client/src/components/entrypoints.ts+++ b/apps/client/src/components/entrypoints.ts@@ -100,8 +100,24 @@ export default class Entrypoints extends Component {}async logoutCommand() {- await server.post("../logout");- window.location.replace(`/login`);+ // A fetch/XHR follows an OIDC logout redirect as a cross-origin XHR, which+ // providers reject during CORS preflight. Submit the CSRF-protected POST as+ // a browser navigation so the provider redirect remains a top-level request.+ const form = document.createElement("form");+ form.method = "POST";+ form.action = window.glob.httpBaseUrl+ ? new URL("/logout", window.glob.httpBaseUrl).href+ : new URL(`${window.glob.baseApiUrl}../logout`, window.location.href).href;+ form.hidden = true;++ const csrfToken = document.createElement("input");+ csrfToken.type = "hidden";+ csrfToken.name = "x-csrf-token";+ csrfToken.value = window.glob.csrfToken;+ form.append(csrfToken);++ document.body.append(form);+ form.submit();}backInNoteHistoryCommand() {diff --git a/apps/server/src/routes/csrf_protection.ts b/apps/server/src/routes/csrf_protection.tsindex fb1e22fa28..63c392e9aa 100644--- a/apps/server/src/routes/csrf_protection.ts+++ b/apps/server/src/routes/csrf_protection.ts@@ -17,7 +17,23 @@ const doubleCsrfUtilities = doubleCsrf({httpOnly: true},cookieName: CSRF_COOKIE_NAME,- getSessionIdentifier: (req) => req.session.id+ getSessionIdentifier: (req) => req.session.id,+ // Browser form submissions cannot set custom headers. Keep the existing header+ // transport for XHR/API calls, and accept the same token in a URL-encoded form+ // body so state-changing navigations can follow an OIDC redirect without CORS.+ getCsrfTokenFromRequest: (req) => {+ const headerToken = req.headers["x-csrf-token"];+ if (typeof headerToken === "string") {+ return headerToken;+ }++ if (req.is("application/x-www-form-urlencoded")) {+ const bodyToken = req.body?.["x-csrf-token"];+ return typeof bodyToken === "string" ? bodyToken : undefined;+ }++ return undefined;+ }});export const { generateCsrfToken } = doubleCsrfUtilities;diff --git a/apps/server/src/routes/transport.spec.ts b/apps/server/src/routes/transport.spec.tsindex 1177ec8c90..cacd86745c 100644--- a/apps/server/src/routes/transport.spec.ts+++ b/apps/server/src/routes/transport.spec.ts@@ -68,6 +68,17 @@ describe("Route transport & middleware", () => {.expect(403);});+ it("accepts a valid CSRF token in a browser form body", async () => {+ const agent = supertest.agent(app);+ await agent.post("/login").send({ password: "demo1234" }).expect(302);+ const csrfToken = (await agent.get("/bootstrap").expect(200)).body.csrfToken;++ await agent.post("/logout")+ .type("form")+ .send({ "x-csrf-token": csrfToken })+ .expect(302);+ });+it("returns a 404 body for an unknown route", async () => {const res = await supertest(app).get("/this-route-does-not-exist").expect(404);expect(res.body.message).toBeTruthy();