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(oauth): pick token-endpoint auth by issuer, not discovery
Elian Doran5b0c075d785e
diff --git a/apps/server/src/assets/config-sample.ini b/apps/server/src/assets/config-sample.iniindex 1e89aa8d9c..c44c38dd9d 100644--- a/apps/server/src/assets/config-sample.ini+++ b/apps/server/src/assets/config-sample.ini@@ -68,6 +68,13 @@ oauthIssuerName=# This is the icon of the service that will be used to verify the user's identityoauthIssuerIcon=+# How Trilium authenticates to the provider's token endpoint: client_secret_basic or+# client_secret_post. Leave empty to auto-detect (client_secret_post for GitLab and Google,+# client_secret_basic otherwise). Set client_secret_post if login fails with a+# WWW-Authenticate / invalid_client error and your credentials contain any of - _ . ! ~ * ' ( ),+# e.g. on a self-hosted GitLab. Must match the method your client is registered with.+oauthClientAuthMethod=+[Security]# Enable backend script execution. WARNING: Backend scripts have full server access including# filesystem, network, and OS commands via require('child_process'). Only enable if you trustdiff --git a/apps/server/src/services/config.ts b/apps/server/src/services/config.tsindex 1d181d40d2..38eb660d50 100644--- a/apps/server/src/services/config.ts+++ b/apps/server/src/services/config.ts@@ -134,6 +134,11 @@ export interface TriliumConfig {oauthIssuerName: string;/** URL to the OAuth provider's icon/logo */oauthIssuerIcon: string;+ /**+ * How to authenticate to the provider's token endpoint: 'client_secret_basic' or+ * 'client_secret_post'. Leave empty to auto-detect from the issuer.+ */+ oauthClientAuthMethod: string;};/** Logging configuration */Logging: {@@ -483,6 +488,13 @@ const configMapping = {aliasEnvVars: ['TRILIUM_OAUTH_ISSUER_ICON'],iniGetter: () => getIniSection("MultiFactorAuthentication")?.oauthIssuerIcon,defaultValue: ''+ },+ oauthClientAuthMethod: {+ standardEnvVar: 'TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHCLIENTAUTHMETHOD',+ // alternative format+ aliasEnvVars: ['TRILIUM_OAUTH_CLIENT_AUTH_METHOD'],+ iniGetter: () => getIniSection("MultiFactorAuthentication")?.oauthClientAuthMethod,+ defaultValue: ''}},Logging: {@@ -566,7 +578,8 @@ const config: TriliumConfig = {oauthClientSecret: getConfigValue(configMapping.MultiFactorAuthentication.oauthClientSecret),oauthIssuerBaseUrl: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerBaseUrl),oauthIssuerName: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerName),- oauthIssuerIcon: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerIcon)+ oauthIssuerIcon: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerIcon),+ oauthClientAuthMethod: getConfigValue(configMapping.MultiFactorAuthentication.oauthClientAuthMethod)},Logging: {retentionDays: getConfigValue(configMapping.Logging.retentionDays)@@ -630,6 +643,7 @@ const config: TriliumConfig = {* - TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHISSUERBASEURL : OAuth issuer URL* - TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHISSUERNAME : OAuth provider name* - TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHISSUERICON : OAuth provider icon+ * - TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHCLIENTAUTHMETHOD : Token-endpoint auth method** Logging Section:* - TRILIUM_LOGGING_RETENTIONDAYS : Log retention period in daysdiff --git a/apps/server/src/services/open_id.spec.ts b/apps/server/src/services/open_id.spec.tsindex 9a0d0385d1..d44ef150d3 100644--- a/apps/server/src/services/open_id.spec.ts+++ b/apps/server/src/services/open_id.spec.ts@@ -1,11 +1,11 @@-import { cls, options } from "@triliumnext/core";+import { cls, getLog, options } from "@triliumnext/core";import type { NextFunction, Request as ExpressRequest, RequestHandler, Response as ExpressResponse } from "express";import { ClientSecretBasic, ClientSecretPost } from "openid-client";import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";import config from "./config.js";import openIDEncryption from "./encryption/open_id_encryption.js";-import openID, { createReactiveOidcMiddleware, type ProviderCapabilities, resolveClientAuthMethod, resolveOAuthIdentity, supportsRpInitiatedLogout } from "./open_id.js";+import openID, { createReactiveOidcMiddleware, resolveClientAuthMethod, resolveOAuthIdentity, supportsRpInitiatedLogout } from "./open_id.js";import sql from "./sql.js";import sqlInit from "./sql_init.js";@@ -19,6 +19,7 @@ function setOauthConfig(complete: boolean) {mfa.oauthIssuerBaseUrl = "https://issuer.example.com";mfa.oauthIssuerName = "Acme";mfa.oauthIssuerIcon = "icon.png";+ mfa.oauthClientAuthMethod = "";}describe("open_id", () => {@@ -175,22 +176,20 @@ describe("open_id", () => {expect(typeof cfg.afterCallback).toBe("function");});- it("passes the probed provider capabilities through to express-openid-connect", () => {+ it("passes the resolved token-endpoint auth method through to express-openid-connect", () => {setOauthConfig(true);+ expect(openID.generateOAuthConfig().clientAuthMethod).toBe("client_secret_basic");- // Without a probe result, both settings fall back to the conservative spec defaults.- expect(openID.generateOAuthConfig()).toMatchObject({- clientAuthMethod: "client_secret_basic",- idpLogout: false- });+ mfa.oauthIssuerBaseUrl = "https://gitlab.com";+ expect(openID.generateOAuthConfig().clientAuthMethod).toBe("client_secret_post");+ });- expect(openID.generateOAuthConfig({- endSessionSupported: true,- clientAuthMethod: "client_secret_post"- })).toMatchObject({- clientAuthMethod: "client_secret_post",- idpLogout: true- });+ it("enables RP-Initiated Logout (idpLogout) only when the provider supports it", () => {+ setOauthConfig(true);+ // Default off, and on only when discovery confirmed an end_session_endpoint at startup.+ expect(openID.generateOAuthConfig().idpLogout).toBe(false);+ expect(openID.generateOAuthConfig(false).idpLogout).toBe(false);+ expect(openID.generateOAuthConfig(true).idpLogout).toBe(true);});it("returns the session unchanged when the DB is not initialized", async () => {@@ -382,83 +381,94 @@ describe("open_id", () => {/*** Regression cover for #10585: OIDC login against GitLab broke in v0.104.0 with- * `OAUTH_WWW_AUTHENTICATE_CHALLENGE`.- *- * The cause is the express-openid-connect 2.20.2 → 3.2.0 bump (openid-client v5 → v6/oauth4webapi).- * v6's `ClientSecretBasic` applies strict RFC 6749 §2.3.1 form-encoding *inside* the HTTP Basic- * header, escaping `- _ . ! ~ * ' ( )`; v5 used plain `encodeURIComponent`, which leaves them alone.- * GitLab's client secrets are prefixed `gloas-`, so the hyphen becomes `%2D` and GitLab's Doorkeeper- * token endpoint — which base64-decodes the header but does not percent-decode it — sees a wrong- * secret and answers 401 + `WWW-Authenticate`.- *- * The method must therefore be picked from the provider's own discovery document rather than- * hardcoded per issuer (the previous Google-only special case had the identical root cause).+ * `OAUTH_WWW_AUTHENTICATE_CHALLENGE`, caused by the express-openid-connect 2.20.2 → 3.2.0 bump+ * (openid-client v5 → v6/oauth4webapi). v6's `ClientSecretBasic` applies strict RFC 6749 §2.3.1+ * form-encoding *inside* the HTTP Basic header, escaping `- _ . ! ~ * ' ( )`; v5 used plain+ * `encodeURIComponent`, which leaves them alone.*/describe("resolveClientAuthMethod", () => {- // Verbatim from https://gitlab.com/.well-known/openid-configuration.- const GITLAB = { token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post"] };- const GOOGLE = { token_endpoint_auth_methods_supported: ["client_secret_post", "client_secret_basic"] };- const BASIC_ONLY = { token_endpoint_auth_methods_supported: ["client_secret_basic"] };-- it("prefers client_secret_post whenever the provider advertises it", () => {- // Body-posted credentials are form-encoded by the transport and arrive intact, so post is- // the safe choice for any provider that accepts it — regardless of issuer.- expect(resolveClientAuthMethod(GITLAB)).toBe("client_secret_post");- expect(resolveClientAuthMethod(GOOGLE)).toBe("client_secret_post");+ it("uses client_secret_post only for issuers that can't decode Basic credentials", () => {+ setOauthConfig(true);++ // GitLab's secrets are `gloas-` prefixed; Google's client_ids always carry "-" and ".".+ mfa.oauthIssuerBaseUrl = "https://gitlab.com";+ expect(resolveClientAuthMethod()).toBe("client_secret_post");+ mfa.oauthIssuerBaseUrl = "https://accounts.google.com/"; // trailing slash tolerated+ expect(resolveClientAuthMethod()).toBe("client_secret_post");++ // Everyone else keeps the OIDC Core default. Critically this includes providers that+ // *advertise* client_secret_post but register the client as basic and reject a mismatch+ // (Authelia responds 401 invalid_client), which is why this is an issuer table and not a+ // reading of token_endpoint_auth_methods_supported.+ mfa.oauthIssuerBaseUrl = "https://auth.example.com:9091";+ expect(resolveClientAuthMethod()).toBe("client_secret_basic");++ // A self-hosted instance is not the public issuer, so it does not match the table.+ mfa.oauthIssuerBaseUrl = "https://gitlab.example.com";+ expect(resolveClientAuthMethod()).toBe("client_secret_basic");});- it("falls back to client_secret_basic when post is unavailable or discovery is unusable", () => {- // A provider that registers confidential clients as basic-only must still get basic.- expect(resolveClientAuthMethod(BASIC_ONLY)).toBe("client_secret_basic");- // Field absent → the OIDC Core default for the token endpoint is client_secret_basic.- expect(resolveClientAuthMethod({ issuer: "https://idp.example" })).toBe("client_secret_basic");- // Malformed / missing metadata (a failed discovery probe) fails safe to the spec default- // rather than silently switching every provider's authentication method.- expect(resolveClientAuthMethod({ token_endpoint_auth_methods_supported: "client_secret_post" })).toBe("client_secret_basic");- expect(resolveClientAuthMethod(null)).toBe("client_secret_basic");- expect(resolveClientAuthMethod(undefined)).toBe("client_secret_basic");+ it("lets an explicit oauthClientAuthMethod override the issuer table", () => {+ setOauthConfig(true);++ // The escape hatch for self-hosted GitLab and any provider we don't know about.+ mfa.oauthIssuerBaseUrl = "https://gitlab.example.com";+ mfa.oauthClientAuthMethod = "client_secret_post";+ expect(resolveClientAuthMethod()).toBe("client_secret_post");++ // It overrides in both directions, so a table entry can be undone if a provider changes.+ mfa.oauthIssuerBaseUrl = "https://gitlab.com";+ mfa.oauthClientAuthMethod = " client_secret_basic ";+ expect(resolveClientAuthMethod()).toBe("client_secret_basic");++ // An unrecognised value is logged and ignored rather than passed to express-openid-connect,+ // whose Joi schema would otherwise reject it and take the whole server down at first use.+ const logSpy = vi.spyOn(getLog(), "error").mockImplementation(() => {});+ mfa.oauthClientAuthMethod = "private_key_jwt";+ expect(resolveClientAuthMethod()).toBe("client_secret_post");+ expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("private_key_jwt"));});/**- * The end-to-end property the fix actually has to hold: whatever method we select, the- * credentials must reach the token endpoint byte-identical to what the user configured. This- * drives the real openid-client encoders, so a future dependency bump that reintroduces the- * mangling fails here rather than in production.+ * The property the issuer table exists to protect. `client_secret_post` puts the credentials in+ * the form body, which every provider form-decodes, so they always arrive intact. Under+ * `client_secret_basic` they arrive intact *only* if the provider percent-decodes per spec —+ * both halves are pinned so the tradeoff stays explicit.*/- it("delivers a GitLab-style secret to the token endpoint intact", () => {+ it("delivers credentials to the token endpoint intact under the selected method", () => {+ setOauthConfig(true);+// GitLab: 64-hex Application ID, `gloas-` prefixed secret (the hyphen is what breaks Basic).const clientId = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2";const clientSecret = "gloas-4f3a2b1c-9d8e-7f6a-5b4c-3d2e1f0a9b8c";+ mfa.oauthIssuerBaseUrl = "https://gitlab.com";- const received = credentialsAtTokenEndpoint(resolveClientAuthMethod(GITLAB), clientId, clientSecret);+ expect(credentialsAtTokenEndpoint(resolveClientAuthMethod(), clientId, clientSecret))+ .toEqual({ clientId, clientSecret });- expect(received).toEqual({ clientId, clientSecret });+ // Had GitLab been left on basic, Doorkeeper would have compared against a mangled secret —+ // this is the exact corruption behind #10585.+ expect(credentialsAtTokenEndpoint("client_secret_basic", clientId, clientSecret))+ .toEqual({ clientId, clientSecret: "gloas%2D4f3a2b1c%2D9d8e%2D7f6a%2D5b4c%2D3d2e1f0a9b8c" });});- /**- * The basic-only fallback has no safer method to fall back *to*, so it stays correct only for a- * provider that percent-decodes per spec. Both halves are pinned here so the tradeoff is explicit:- * choosing basic is safe for a compliant provider and lossy for a non-compliant one — which is- * precisely why post is preferred wherever it's on offer.- */- it("relies on the provider decoding per spec when it only offers basic", () => {+ it("relies on the provider decoding per spec when it stays on basic", () => {+ setOauthConfig(true);// The credentials from Trilium's own Authelia dev harness; the "_" is what Basic escapes.const clientId = "trilium";const clientSecret = "insecure_secret";- const method = resolveClientAuthMethod(BASIC_ONLY);+ mfa.oauthIssuerBaseUrl = "https://auth.example.com:9091";+ const method = resolveClientAuthMethod();+ expect(method).toBe("client_secret_basic");+ // Authelia percent-decodes per RFC 6749, so the credentials round-trip correctly.expect(credentialsAtTokenEndpoint(method, clientId, clientSecret, { percentDecodes: true })).toEqual({ clientId, clientSecret });-- // A provider that skips the percent-decoding step sees a corrupted secret instead.- expect(credentialsAtTokenEndpoint(method, clientId, clientSecret))- .toEqual({ clientId, clientSecret: "insecure%5Fsecret" });});});- describe("probeProviderCapabilities", () => {+ describe("isRpInitiatedLogoutSupported", () => {const wellKnownUrl = "https://issuer.example.com/.well-known/openid-configuration";- const DEFAULTS = { endSessionSupported: false, clientAuthMethod: "client_secret_basic" };function mockFetch(impl: (url: string) => Partial<Response> | Promise<Partial<Response>>) {return vi.spyOn(globalThis, "fetch").mockImplementation(@@ -466,40 +476,31 @@ describe("open_id", () => {);}- // One fetch has to serve both settings — probing the same document twice would double the- // startup cost and could observe an inconsistent provider between the two reads.- it("derives every capability from a single discovery fetch", async () => {+ it("fetches the issuer's discovery document and reflects end_session_endpoint", async () => {setOauthConfig(true);const fetchSpy = mockFetch(() => ({ok: true,- json: () => Promise.resolve({- end_session_endpoint: "https://issuer.example.com/logout",- token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post"]- })+ json: () => Promise.resolve({ end_session_endpoint: "https://issuer.example.com/logout" })}));- expect(await openID.probeProviderCapabilities()).toEqual({- endSessionSupported: true,- clientAuthMethod: "client_secret_post"- });- expect(fetchSpy).toHaveBeenCalledOnce();+ expect(await openID.isRpInitiatedLogoutSupported()).toBe(true);expect(fetchSpy).toHaveBeenCalledWith(wellKnownUrl, expect.anything());});- it("falls back to the spec defaults when discovery advertises neither capability", async () => {+ it("is false when discovery omits end_session_endpoint", async () => {setOauthConfig(true);mockFetch(() => ({ ok: true, json: () => Promise.resolve({}) }));- expect(await openID.probeProviderCapabilities()).toEqual(DEFAULTS);+ expect(await openID.isRpInitiatedLogoutSupported()).toBe(false);});- it("fails safe on a non-OK response or a thrown fetch", async () => {+ it("fails closed (false) on a non-OK response or a thrown fetch", async () => {setOauthConfig(true);mockFetch(() => ({ ok: false, status: 404, json: () => Promise.resolve({}) }));- expect(await openID.probeProviderCapabilities()).toEqual(DEFAULTS);+ expect(await openID.isRpInitiatedLogoutSupported()).toBe(false);vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network down"));- expect(await openID.probeProviderCapabilities()).toEqual(DEFAULTS);+ expect(await openID.isRpInitiatedLogoutSupported()).toBe(false);});it("does not attempt discovery when no issuer is configured", async () => {@@ -507,7 +508,7 @@ describe("open_id", () => {mfa.oauthIssuerBaseUrl = "";const fetchSpy = mockFetch(() => ({ ok: true, json: () => Promise.resolve({}) }));- expect(await openID.probeProviderCapabilities()).toEqual(DEFAULTS);+ expect(await openID.isRpInitiatedLogoutSupported()).toBe(false);expect(fetchSpy).not.toHaveBeenCalled();});});@@ -520,27 +521,18 @@ describe("open_id", () => {* every request, and lazily builds (and caches) the underlying handler the first time OAuth is used.*/describe("createReactiveOidcMiddleware", () => {- const BASIC_CAPABILITIES: ProviderCapabilities = {- endSessionSupported: false,- clientAuthMethod: "client_secret_basic"- };- const POST_CAPABILITIES: ProviderCapabilities = {- endSessionSupported: true,- clientAuthMethod: "client_secret_post"- };-function setup() {let configured = false;const oidcHandler = vi.fn(((_req, _res, next) => next()) as RequestHandler);const buildAuth = vi.fn(() => oidcHandler);- const probeProviderCapabilities = vi.fn().mockResolvedValue(BASIC_CAPABILITIES);- const generateOAuthConfig = vi.fn((capabilities: ProviderCapabilities) => ({ capabilities }) as never);+ const isRpInitiatedLogoutSupported = vi.fn().mockResolvedValue(false);+ const generateOAuthConfig = vi.fn((endSessionSupported: boolean) => ({ endSessionSupported }) as never);const isConfigured = vi.fn(() => configured);const middleware = createReactiveOidcMiddleware({isConfigured,- probeProviderCapabilities,+ isRpInitiatedLogoutSupported,generateOAuthConfig,buildAuth});@@ -549,7 +541,7 @@ describe("createReactiveOidcMiddleware", () => {middleware,oidcHandler,buildAuth,- probeProviderCapabilities,+ isRpInitiatedLogoutSupported,generateOAuthConfig,isConfigured,setConfigured: (value: boolean) => { configured = value; }@@ -573,7 +565,7 @@ describe("createReactiveOidcMiddleware", () => {expect(t.buildAuth).not.toHaveBeenCalled();expect(t.oidcHandler).not.toHaveBeenCalled();// No work is done while OAuth is unselected — not even the discovery probe.- expect(t.probeProviderCapabilities).not.toHaveBeenCalled();+ expect(t.isRpInitiatedLogoutSupported).not.toHaveBeenCalled();});it("builds and delegates to the OIDC handler when OAuth is selected", async () => {@@ -582,8 +574,8 @@ describe("createReactiveOidcMiddleware", () => {const { req, res, next } = await run(t.middleware);- expect(t.probeProviderCapabilities).toHaveBeenCalledOnce();- expect(t.generateOAuthConfig).toHaveBeenCalledWith(BASIC_CAPABILITIES);+ expect(t.isRpInitiatedLogoutSupported).toHaveBeenCalledOnce();+ expect(t.generateOAuthConfig).toHaveBeenCalledWith(false);expect(t.buildAuth).toHaveBeenCalledOnce();expect(t.oidcHandler).toHaveBeenCalledWith(req, res, expect.any(Function));// The wrapper must hand off to the OIDC handler and NOT call next() itself — calling it again@@ -600,18 +592,18 @@ describe("createReactiveOidcMiddleware", () => {await run(t.middleware);expect(t.buildAuth).toHaveBeenCalledOnce();- expect(t.probeProviderCapabilities).toHaveBeenCalledOnce();+ expect(t.isRpInitiatedLogoutSupported).toHaveBeenCalledOnce();expect(t.oidcHandler).toHaveBeenCalledTimes(2);});it("passes the discovery-probe result into the OAuth config", async () => {const t = setup();- t.probeProviderCapabilities.mockResolvedValue(POST_CAPABILITIES);+ t.isRpInitiatedLogoutSupported.mockResolvedValue(true);t.setConfigured(true);await run(t.middleware);- expect(t.generateOAuthConfig).toHaveBeenCalledWith(POST_CAPABILITIES);+ expect(t.generateOAuthConfig).toHaveBeenCalledWith(true);});// This is the regression the whole change exists to fix: with the old startup-only mount, flipping@@ -652,12 +644,12 @@ describe("createReactiveOidcMiddleware", () => {// A deferred discovery probe keeps the first build in flight while a second request arrives,// exercising the in-flight-init guard (otherwise both requests would each build a handler).- let resolveProbe: (value: ProviderCapabilities) => void = () => {};- t.probeProviderCapabilities.mockReturnValue(new Promise<ProviderCapabilities>((resolve) => { resolveProbe = resolve; }));+ let resolveProbe: (value: boolean) => void = () => {};+ t.isRpInitiatedLogoutSupported.mockReturnValue(new Promise<boolean>((resolve) => { resolveProbe = resolve; }));const first = run(t.middleware);const second = run(t.middleware);- resolveProbe(BASIC_CAPABILITIES);+ resolveProbe(false);await Promise.all([first, second]);expect(t.buildAuth).toHaveBeenCalledOnce();@@ -670,7 +662,7 @@ describe("createReactiveOidcMiddleware", () => {it("retries the build on a subsequent request after a failed init", async () => {const t = setup();t.setConfigured(true);- t.probeProviderCapabilities.mockRejectedValueOnce(new Error("transient discovery failure"));+ t.isRpInitiatedLogoutSupported.mockRejectedValueOnce(new Error("transient discovery failure"));await expect(run(t.middleware)).rejects.toThrow("transient discovery failure");expect(t.oidcHandler).not.toHaveBeenCalled();diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.tsindex bdefabbdc3..76ae8834fe 100644--- a/apps/server/src/services/open_id.ts+++ b/apps/server/src/services/open_id.ts@@ -132,7 +132,7 @@ function deriveFaviconUrl(baseUrl: string) {}}-function generateOAuthConfig(capabilities: ProviderCapabilities = DEFAULT_PROVIDER_CAPABILITIES) {+function generateOAuthConfig(endSessionSupported = false) {const authRoutes = {callback: "/callback",login: "/authenticate",@@ -151,18 +151,18 @@ function generateOAuthConfig(capabilities: ProviderCapabilities = DEFAULT_PROVIDissuerBaseURL: config.MultiFactorAuthentication.oauthIssuerBaseUrl,secret: config.MultiFactorAuthentication.oauthClientSecret,clientSecret: config.MultiFactorAuthentication.oauthClientSecret,- clientAuthMethod: capabilities.clientAuthMethod,+ clientAuthMethod: resolveClientAuthMethod(),authorizationParams: {response_type: "code",scope: "openid profile email",},routes: authRoutes,// Only enable RP-Initiated Logout when the provider actually advertises an end_session_endpoint- // (see probeProviderCapabilities). With idpLogout on, express-openid-connect unconditionally+ // (see isRpInitiatedLogoutSupported). With idpLogout on, express-openid-connect unconditionally// builds a redirect to that endpoint at logout; providers without one (e.g. Google, Authelia)// would otherwise crash POST /logout with a 500. When false, logout falls back to clearing the// local session and redirecting to postLogoutRedirect.- idpLogout: capabilities.endSessionSupported,+ idpLogout: endSessionSupported,logoutParams,afterCallback: async (req: Request, res: Response, session: Session) => {if (!sqlInit.isDbInitialized()) return session;@@ -251,10 +251,10 @@ type AuthBuilder = typeof import("express-openid-connect").auth;interface ReactiveOidcDeps {/** Whether OAuth is currently configured and selected as the sign-in method. Re-checked per request. */isConfigured: () => boolean;- /** Single discovery probe resolving everything we negotiate with the provider. */- probeProviderCapabilities: () => Promise<ProviderCapabilities>;+ /** Discovery probe deciding whether RP-Initiated Logout (idpLogout) can be safely enabled. */+ isRpInitiatedLogoutSupported: () => Promise<boolean>;/** Builds the express-openid-connect config for the current provider settings. */- generateOAuthConfig: (capabilities: ProviderCapabilities) => Parameters<AuthBuilder>[0];+ generateOAuthConfig: (endSessionSupported: boolean) => Parameters<AuthBuilder>[0];/** The express-openid-connect `auth()` factory (injectable so the middleware is unit-testable). */buildAuth: AuthBuilder;}@@ -269,14 +269,14 @@ interface ReactiveOidcDeps {* This middleware is mounted unconditionally and instead re-evaluates `isOpenIDConfigured()` on every* request: while OAuth is unselected it simply passes through, and the first time OAuth is actually in* use it lazily builds the underlying express-openid-connect handler and caches it. The discovery probe- * (see {@link probeProviderCapabilities}) only depends on the issuer — which is fixed in config and- * changes solely on restart — so it is resolved once on that first use. An in-flight guard ensures- * concurrent first requests build the handler exactly once.+ * (endSessionSupported) only depends on the issuer — which is fixed in config and changes solely on+ * restart — so it is resolved once on that first use. An in-flight guard ensures concurrent first+ * requests build the handler exactly once.*/export function createReactiveOidcMiddleware(deps: Partial<ReactiveOidcDeps> = {}): RequestHandler {const {isConfigured = isOpenIDConfigured,- probeProviderCapabilities: probeCapabilities = probeProviderCapabilities,+ isRpInitiatedLogoutSupported: probeRpLogout = isRpInitiatedLogoutSupported,generateOAuthConfig: buildOAuthConfig = generateOAuthConfig,buildAuth} = deps;@@ -297,8 +297,8 @@ export function createReactiveOidcMiddleware(deps: Partial<ReactiveOidcDeps> = {// OAuth unselected. The sole static reference to the package is the erased `Session` type// import, so the bundler keeps it out of the eager-init graph (see scripts/build-utils.ts).const authFactory = buildAuth ?? (await import("express-openid-connect")).auth;- const capabilities = await probeCapabilities();- oidcMiddleware = authFactory(buildOAuthConfig(capabilities));+ const endSessionSupported = await probeRpLogout();+ oidcMiddleware = authFactory(buildOAuthConfig(endSessionSupported));})();try {await oidcInit;@@ -332,55 +332,23 @@ export default {clearSavedUser,isTokenValid,isUserSaved,- probeProviderCapabilities,+ isRpInitiatedLogoutSupported,};// Cap the startup discovery probe so a slow/unreachable provider can't stall server boot.const DISCOVERY_TIMEOUT_MS = 10_000;-/** Everything we negotiate with the provider from its discovery document. */-export interface ProviderCapabilities {- /** Whether RP-Initiated Logout (`idpLogout`) can be safely enabled. */- endSessionSupported: boolean;- /** How to authenticate to the token endpoint. */- clientAuthMethod: ClientAuthMethod;-}--/**- * What we assume when discovery is unavailable: no RP-Initiated Logout (so logout degrades to clearing- * the local session rather than crashing on a missing endpoint), and the OIDC Core default token-endpoint- * auth method. Both are the conservative, spec-default choices.- */-const DEFAULT_PROVIDER_CAPABILITIES: ProviderCapabilities = {- endSessionSupported: false,- clientAuthMethod: "client_secret_basic"-};--/**- * Probes the configured OIDC issuer's discovery document once and derives every provider-dependent- * setting from it. The issuer is fixed in config.ini/env and only changes on restart, so this resolves- * a single time on first OAuth use rather than per request. Any fetch/parse failure degrades to- * {@link DEFAULT_PROVIDER_CAPABILITIES} so a transient network blip doesn't break login outright.- */-async function probeProviderCapabilities(): Promise<ProviderCapabilities> {- const metadata = await fetchDiscoveryMetadata();- if (metadata === undefined) {- return DEFAULT_PROVIDER_CAPABILITIES;- }-- return {- endSessionSupported: supportsRpInitiatedLogout(metadata),- clientAuthMethod: resolveClientAuthMethod(metadata)- };-}-/**- * Fetches the issuer's OIDC discovery document, or `undefined` when it can't be retrieved or parsed.+ * Probes the configured OIDC issuer's discovery document to decide whether RP-Initiated Logout is+ * available, i.e. whether `idpLogout` can be safely enabled in {@link generateOAuthConfig}. The issuer+ * is fixed in config.ini/env and only changes on restart, so this is resolved once at startup rather+ * than per logout. Any fetch/parse failure is treated as "unsupported" so a transient network blip+ * degrades to a working local logout rather than breaking it.*/-async function fetchDiscoveryMetadata(): Promise<unknown> {+async function isRpInitiatedLogoutSupported() {const issuer = config.MultiFactorAuthentication.oauthIssuerBaseUrl.replace(/\/+$/, "");if (!issuer) {- return undefined;+ return false;}// The discovery document lives at `{issuer}/.well-known/openid-configuration` — appended to the full@@ -391,26 +359,26 @@ async function fetchDiscoveryMetadata(): Promise<unknown> {try {const response = await fetch(metadataUrl, { signal: controller.signal });if (!response.ok) {- getLog().info(`OAuth: discovery for ${issuer} returned HTTP ${response.status}; falling back to default provider settings.`);- return undefined;+ getLog().info(`OAuth: discovery for ${issuer} returned HTTP ${response.status}; treating RP-Initiated Logout as unsupported.`);+ return false;}- return await response.json();+ const metadata: unknown = await response.json();+ return supportsRpInitiatedLogout(metadata);} catch (error) {- getLog().info(`OAuth: discovery fetch for ${issuer} failed, falling back to default provider settings. ${error instanceof Error ? error.message : error}`);- return undefined;+ getLog().info(`OAuth: discovery fetch for ${issuer} failed, treating RP-Initiated Logout as unsupported. ${error instanceof Error ? error.message : error}`);+ return false;} finally {clearTimeout(timeout);}}/**- * The fields of the OIDC discovery document we consume. The full metadata is large and arrives as+ * The single field of the OIDC discovery document we consume. The full metadata is large and arrives as* untrusted network JSON, so rather than pull in (and pin) openid-client's transitive `ServerMetadata`- * type, we mirror just what we read and validate it at runtime.+ * type for one property, we mirror just what we read and validate it at runtime.*/interface OidcDiscoveryMetadata {end_session_endpoint?: string;- token_endpoint_auth_methods_supported?: string[];}/**@@ -425,39 +393,60 @@ export function supportsRpInitiatedLogout(metadata: unknown) {return typeof end_session_endpoint === "string" && end_session_endpoint.length > 0;}-export type ClientAuthMethod = "client_secret_basic" | "client_secret_post";+export const CLIENT_AUTH_METHODS = ["client_secret_basic", "client_secret_post"] as const;+export type ClientAuthMethod = (typeof CLIENT_AUTH_METHODS)[number];/**- * Chooses the token-endpoint client authentication method from the provider's own discovery document,- * preferring `client_secret_post` whenever it is advertised.+ * Issuers whose token endpoint does **not** percent-decode HTTP Basic credentials.+ *+ * `client_secret_basic` — the OIDC Core default, and what express-openid-connect picks on its own —+ * form-url-encodes the client_id/secret per RFC 6749 §2.3.1 ("-" → %2D, "_" → %5F, "." → %2E) *inside*+ * the Basic header. A provider that base64-decodes that header without percent-decoding it therefore+ * compares against corrupted credentials and rejects the token exchange:+ *+ * - **Google** — client_ids always contain "-" and "."; fails with+ * "invalid_client: The OAuth client was not found."+ * - **GitLab** — client secrets are `gloas-` prefixed; fails with OAUTH_WWW_AUTHENTICATE_CHALLENGE+ * (#10585, a regression from the openid-client v5 → v6 upgrade, where v5's plainer+ * `encodeURIComponent` left those characters intact).*- * `client_secret_basic` — the OIDC Core default, and what express-openid-connect picks on its own — is- * unsafe for credentials containing `- _ . ! ~ * ' ( )`. Its oauth4webapi implementation form-url-encodes- * the client_id/secret per RFC 6749 §2.3.1 ("-" → %2D, "." → %2E) *inside* the HTTP Basic header, but- * providers routinely base64-decode that header without percent-decoding it, so the credentials arrive- * corrupted. This has broken two providers already: Google (whose client_ids always contain "-" and ".";- * "invalid_client: The OAuth client was not found") and GitLab (whose secrets are `gloas-` prefixed;- * OAUTH_WWW_AUTHENTICATE_CHALLENGE — see #10585, a regression from the openid-client v5 → v6 upgrade,- * where v5's plainer `encodeURIComponent` left those characters intact).+ * This deliberately is *not* derived from the discovery document. `token_endpoint_auth_methods_supported`+ * advertises what the **server** supports, but the method is agreed **per client** at registration and+ * is not discoverable — Authelia, for instance, advertises all five methods while defaulting confidential+ * clients to `client_secret_basic` and rejecting anything else with `invalid_client`. Preferring+ * `client_secret_post` on the strength of discovery alone therefore breaks spec-compliant providers.*- * Posting the credentials in the request body sidesteps the Basic-auth encoding entirely, so it is the- * safe choice for *any* provider that accepts it — hence a capability check rather than an issuer- * allowlist. Providers that only register confidential clients as `client_secret_basic` (Authelia,- * Keycloak) still get basic, as does a provider whose discovery document is missing or malformed.+ * Only exact issuer matches are listed, so a self-hosted GitLab needs `oauthClientAuthMethod` set+ * explicitly — see {@link resolveClientAuthMethod}.*/-export function resolveClientAuthMethod(metadata: unknown): ClientAuthMethod {- if (typeof metadata !== "object" || metadata === null) {- return DEFAULT_PROVIDER_CAPABILITIES.clientAuthMethod;- }+const BASIC_AUTH_INCOMPATIBLE_ISSUERS = new Set([+ "https://accounts.google.com",+ "https://gitlab.com"+]);- const { token_endpoint_auth_methods_supported } = metadata as OidcDiscoveryMetadata;- if (!Array.isArray(token_endpoint_auth_methods_supported)) {- return DEFAULT_PROVIDER_CAPABILITIES.clientAuthMethod;+/**+ * Chooses the token-endpoint client authentication method.+ *+ * An explicit `oauthClientAuthMethod` always wins — it's the escape hatch for any provider we don't+ * know about, notably self-hosted instances of the ones in {@link BASIC_AUTH_INCOMPATIBLE_ISSUERS}.+ * Otherwise known-incompatible issuers get `client_secret_post` and everyone else gets the spec+ * default, `client_secret_basic`.+ */+export function resolveClientAuthMethod(): ClientAuthMethod {+ const configured = config.MultiFactorAuthentication.oauthClientAuthMethod.trim();+ if (configured) {+ if (isClientAuthMethod(configured)) {+ return configured;+ }+ getLog().error(`OAuth: ignoring unrecognised oauthClientAuthMethod '${configured}', expected one of ${CLIENT_AUTH_METHODS.join(", ")}.`);}- return token_endpoint_auth_methods_supported.includes("client_secret_post")- ? "client_secret_post"- : "client_secret_basic";+ const issuer = config.MultiFactorAuthentication.oauthIssuerBaseUrl.replace(/\/+$/, "");+ return BASIC_AUTH_INCOMPATIBLE_ISSUERS.has(issuer) ? "client_secret_post" : "client_secret_basic";+}++function isClientAuthMethod(value: string): value is ClientAuthMethod {+ return (CLIENT_AUTH_METHODS as readonly string[]).includes(value);}/**