Skip to content

Build your personal knowledge base with Trilium Notes

fix: keep OIDC logout navigation in the app (closes #11183)

dajiaohuangb8b41cadbb12
diff --git a/apps/client/src/components/entrypoints.spec.ts b/apps/client/src/components/entrypoints.spec.ts
index dee4ab5e42..88e5cf160f 100644
--- a/apps/client/src/components/entrypoints.spec.ts
+++ b/apps/client/src/components/entrypoints.spec.ts
@@ -44,7 +44,8 @@ describe("logoutCommand", () => {
...window.glob,
baseApiUrl: "api/",
csrfToken: "csrf-test-token",
- httpBaseUrl: undefined
+ httpBaseUrl: undefined,
+ isElectron: false
};
});
@@ -57,7 +58,9 @@ describe("logoutCommand", () => {
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();
});
@@ -70,6 +73,21 @@ describe("logoutCommand", () => {
expect(document.body.querySelector("form")?.action).toBe("http://127.0.0.1:37742/logout");
});
+ it("keeps Electron logout on the trusted app protocol", async () => {
+ const happyDOM = (window as unknown as {
+ happyDOM: { setURL(url: string): void }
+ }).happyDOM;
+ happyDOM.setURL("trilium-app://app/");
+ window.glob.isElectron = true;
+ 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("trilium-app://app/logout");
+ happyDOM.setURL("http://localhost:3000/");
+ });
+
it("keeps the browser deployment path", async () => {
window.history.replaceState({}, "", "/trilium/");
vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 3891fa3d71..1b09f1851e 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -100,14 +100,15 @@ export default class Entrypoints extends Component {
}
async logoutCommand() {
- // 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.
+ // A browser navigation avoids the cross-origin XHR preflight that OIDC providers reject.
+ // Electron keeps the request on its trusted custom protocol.
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.action = window.glob.isElectron
+ ? new URL(`${window.glob.baseApiUrl}../logout`, window.location.href).href
+ : 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");
diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.ts
index 572a7537e3..81cfd98c73 100644
--- a/apps/desktop/src/services/web_contents_security.spec.ts
+++ b/apps/desktop/src/services/web_contents_security.spec.ts
@@ -579,14 +579,17 @@ describe("navigation guard", () => {
setupWebContentsSecurity(SECURITY_OPTIONS);
});
- it("only allows the app shell at its root path", () => {
+ it("allows the app shell root and the logout endpoint", () => {
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);
- // App shell but non-root path is blocked (in-page SPA routing / hostile).
+ // 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).
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.ts
index 2b22146e5f..74798f218f 100644
--- a/apps/desktop/src/services/web_contents_security.ts
+++ b/apps/desktop/src/services/web_contents_security.ts
@@ -213,9 +213,7 @@ 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; 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.
+ * at the root path or the renderer's logout endpoint.
*
* `localhost` was previously allowed too, from the era when the desktop
* renderer was served over `http://127.0.0.1:<port>`. The custom-protocol
@@ -227,7 +225,9 @@ 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 === "/");
+ return isAppShell && (!parsedUrl.pathname
+ || parsedUrl.pathname === "/"
+ || parsedUrl.pathname === "/logout");
}
/**
diff --git a/apps/server/src/routes/error_handlers.spec.ts b/apps/server/src/routes/error_handlers.spec.ts
index 271f4d5a58..7d319fe241 100644
--- a/apps/server/src/routes/error_handlers.spec.ts
+++ b/apps/server/src/routes/error_handlers.spec.ts
@@ -1,6 +1,34 @@
import { describe, expect, it } from "vitest";
-import { describeError, extractOAuthErrorDetail } from "./error_handlers.js";
+import {
+ describeError,
+ extractOAuthErrorDetail,
+ isLogoutFormNavigation
+} from "./error_handlers.js";
+
+describe("isLogoutFormNavigation", () => {
+ it("recognizes HTML form posts to logout", () => {
+ const req = {
+ method: "POST",
+ path: "/logout",
+ headers: { accept: "text/html,application/xhtml+xml" },
+ is: () => "application/x-www-form-urlencoded"
+ };
+
+ expect(isLogoutFormNavigation(req as never)).toBe(true);
+ });
+
+ it("does not classify API requests as navigations", () => {
+ const req = {
+ method: "POST",
+ path: "/logout",
+ headers: { accept: "application/json" },
+ is: () => "application/x-www-form-urlencoded"
+ };
+
+ expect(isLogoutFormNavigation(req as never)).toBe(false);
+ });
+});
describe("extractOAuthErrorDetail", () => {
it("combines OAuth error code and description", () => {
diff --git a/apps/server/src/routes/error_handlers.ts b/apps/server/src/routes/error_handlers.ts
index 8f21a3c47e..6ce725b51b 100644
--- a/apps/server/src/routes/error_handlers.ts
+++ b/apps/server/src/routes/error_handlers.ts
@@ -50,6 +50,13 @@ function register(app: Application) {
getLog().error(`Error on ${req.method} ${req.url}: ${detail}`);
}
+ // A logout form is a top-level navigation so OIDC redirects can leave the origin.
+ // Keep validation and provider failures inside the app instead of rendering an error body.
+ if (isLogoutFormNavigation(req)) {
+ res.redirect("..");
+ return;
+ }
+
res.status(statusCode).send({
message: err instanceof Error ? err.message : "Unknown Error"
});
@@ -57,6 +64,14 @@ function register(app: Application) {
});
}
+/** Whether a failed logout came from the browser form navigation. */
+export function isLogoutFormNavigation(req: Request): boolean {
+ return req.method === "POST"
+ && req.path === "/logout"
+ && req.is("application/x-www-form-urlencoded")
+ && req.headers.accept?.includes("text/html") === true;
+}
+
/**
* Pulls the OAuth/OIDC error code and description off an error, if present. Returns `null` when the
* error carries neither, so callers can skip logging for ordinary errors.