Skip to content

Build your personal knowledge base with Trilium Notes

fix(oauth): accept ID tokens signed with an algorithm other than RS256

Elian Doran073be0cabed1
diff --git a/apps/server/src/assets/config-sample.ini b/apps/server/src/assets/config-sample.ini
index c44c38dd9d..30895ccbab 100644
--- a/apps/server/src/assets/config-sample.ini
+++ b/apps/server/src/assets/config-sample.ini
@@ -75,6 +75,12 @@ oauthIssuerIcon=
# e.g. on a self-hosted GitLab. Must match the method your client is registered with.
oauthClientAuthMethod=
+# The algorithm the provider signs ID tokens with, e.g. RS256, EdDSA or ES256. Leave empty to
+# detect it from the provider's discovery document (RS256 is used whenever the provider offers it).
+# Set this only if sign-in fails with "unexpected JWT alg received", which means the provider signs
+# with an algorithm other than the one it advertises first.
+oauthIdTokenSigningAlg=
+
[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 trust
diff --git a/apps/server/src/services/config.spec.ts b/apps/server/src/services/config.spec.ts
index afe1eaf50b..8c2df25144 100644
--- a/apps/server/src/services/config.spec.ts
+++ b/apps/server/src/services/config.spec.ts
@@ -397,6 +397,9 @@ corsAllowOrigin=https://ini-cors.com
expect(config.MultiFactorAuthentication.oauthIssuerBaseUrl).toBe("https://accounts.google.com");
expect(config.MultiFactorAuthentication.oauthIssuerName).toBe("Google");
expect(config.MultiFactorAuthentication.oauthIssuerIcon).toBe("");
+ // Empty means "detect from the provider's discovery document" for both of these.
+ expect(config.MultiFactorAuthentication.oauthClientAuthMethod).toBe("");
+ expect(config.MultiFactorAuthentication.oauthIdTokenSigningAlg).toBe("");
// Logging defaults
expect(config.Logging.retentionDays).toBe(90);
diff --git a/apps/server/src/services/config.ts b/apps/server/src/services/config.ts
index 38eb660d50..5808838825 100644
--- a/apps/server/src/services/config.ts
+++ b/apps/server/src/services/config.ts
@@ -139,6 +139,11 @@ export interface TriliumConfig {
* 'client_secret_post'. Leave empty to auto-detect from the issuer.
*/
oauthClientAuthMethod: string;
+ /**
+ * The JWS algorithm the provider signs ID tokens with (e.g. 'RS256', 'EdDSA', 'ES256').
+ * Leave empty to auto-detect from the issuer's discovery document.
+ */
+ oauthIdTokenSigningAlg: string;
};
/** Logging configuration */
Logging: {
@@ -495,6 +500,13 @@ const configMapping = {
aliasEnvVars: ['TRILIUM_OAUTH_CLIENT_AUTH_METHOD'],
iniGetter: () => getIniSection("MultiFactorAuthentication")?.oauthClientAuthMethod,
defaultValue: ''
+ },
+ oauthIdTokenSigningAlg: {
+ standardEnvVar: 'TRILIUM_MULTIFACTORAUTHENTICATION_OAUTHIDTOKENSIGNINGALG',
+ // alternative format
+ aliasEnvVars: ['TRILIUM_OAUTH_ID_TOKEN_SIGNING_ALG'],
+ iniGetter: () => getIniSection("MultiFactorAuthentication")?.oauthIdTokenSigningAlg,
+ defaultValue: ''
}
},
Logging: {
@@ -579,7 +591,8 @@ const config: TriliumConfig = {
oauthIssuerBaseUrl: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerBaseUrl),
oauthIssuerName: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerName),
oauthIssuerIcon: getConfigValue(configMapping.MultiFactorAuthentication.oauthIssuerIcon),
- oauthClientAuthMethod: getConfigValue(configMapping.MultiFactorAuthentication.oauthClientAuthMethod)
+ oauthClientAuthMethod: getConfigValue(configMapping.MultiFactorAuthentication.oauthClientAuthMethod),
+ oauthIdTokenSigningAlg: getConfigValue(configMapping.MultiFactorAuthentication.oauthIdTokenSigningAlg)
},
Logging: {
retentionDays: getConfigValue(configMapping.Logging.retentionDays)
diff --git a/apps/server/src/services/open_id.spec.ts b/apps/server/src/services/open_id.spec.ts
index 96eb1ef1e2..3e76c48570 100644
--- a/apps/server/src/services/open_id.spec.ts
+++ b/apps/server/src/services/open_id.spec.ts
@@ -1,11 +1,13 @@
import { cls, getLog, options } from "@triliumnext/core";
import type { NextFunction, Request as ExpressRequest, RequestHandler, Response as ExpressResponse } from "express";
-import { ClientSecretBasic, ClientSecretPost, type CustomFetch, customFetch, discovery } from "openid-client";
+import { generateKeyPairSync, type KeyObject, sign } from "node:crypto";
+import { authorizationCodeGrant, ClientSecretBasic, ClientSecretPost, type CustomFetch, customFetch, discovery } from "openid-client";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
+import { describeError } from "../routes/error_handlers.js";
import config from "./config.js";
import openIDEncryption from "./encryption/open_id_encryption.js";
-import openID, { createReactiveOidcMiddleware, reconcileIssuerBaseUrl, resolveClientAuthMethod, resolveOAuthIdentity, supportsRpInitiatedLogout } from "./open_id.js";
+import openID, { createReactiveOidcMiddleware, DEFAULT_ID_TOKEN_SIGNING_ALG, reconcileIssuerBaseUrl, resolveClientAuthMethod, resolveIdTokenSigningAlg, resolveOAuthIdentity, supportsRpInitiatedLogout } from "./open_id.js";
import sql from "./sql.js";
import sqlInit from "./sql_init.js";
@@ -20,6 +22,7 @@ function setOauthConfig(complete: boolean) {
mfa.oauthIssuerName = "Acme";
mfa.oauthIssuerIcon = "icon.png";
mfa.oauthClientAuthMethod = "";
+ mfa.oauthIdTokenSigningAlg = "";
}
describe("open_id", () => {
@@ -488,7 +491,8 @@ describe("open_id", () => {
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: true,
- issuerBaseUrl: "https://issuer.example.com"
+ issuerBaseUrl: "https://issuer.example.com",
+ idTokenSigningAlg: "RS256"
});
expect(fetchSpy).toHaveBeenCalledWith(wellKnownUrl, expect.anything());
});
@@ -520,13 +524,15 @@ describe("open_id", () => {
mockFetch(() => ({ ok: false, status: 404, json: () => Promise.resolve({}) }));
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: false,
- issuerBaseUrl: "https://issuer.example.com"
+ issuerBaseUrl: "https://issuer.example.com",
+ idTokenSigningAlg: "RS256"
});
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network down"));
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: false,
- issuerBaseUrl: "https://issuer.example.com"
+ issuerBaseUrl: "https://issuer.example.com",
+ idTokenSigningAlg: "RS256"
});
});
@@ -535,9 +541,126 @@ describe("open_id", () => {
mfa.oauthIssuerBaseUrl = "";
const fetchSpy = mockFetch(() => ({ ok: true, json: () => Promise.resolve({}) }));
- expect(await openID.probeDiscovery()).toEqual({ endSessionSupported: false, issuerBaseUrl: "" });
+ expect(await openID.probeDiscovery()).toEqual({
+ endSessionSupported: false,
+ issuerBaseUrl: "",
+ idTokenSigningAlg: "RS256"
+ });
expect(fetchSpy).not.toHaveBeenCalled();
});
+
+ it("reports the signing algorithm the issuer advertises", async () => {
+ // Pocket ID with an Ed25519 key (discussion 6318): RS256 is not on offer at all, so expecting it is
+ // what produced "unexpected JWT alg received, expected RS256, got: EdDSA".
+ setOauthConfig(true);
+ mockFetch(() => ({
+ ok: true,
+ json: () => Promise.resolve({
+ issuer: "https://issuer.example.com",
+ id_token_signing_alg_values_supported: ["EdDSA"]
+ })
+ }));
+
+ expect((await openID.probeDiscovery()).idTokenSigningAlg).toBe("EdDSA");
+ });
+ });
+
+ /**
+ * Cover for https://github.com/orgs/TriliumNext/discussions/6318: express-openid-connect defaults
+ * `idTokenSigningAlg` to RS256 and hands it to openid-client as the client's registered
+ * `id_token_signed_response_alg`, which is then enforced on every ID token. Trilium never set it, so
+ * any provider signing with something else — Pocket ID and Kanidm sign Ed25519 by default — failed
+ * the round-trip outright.
+ */
+ describe("resolveIdTokenSigningAlg", () => {
+ it("keeps RS256 unless the issuer rules it out", () => {
+ setOauthConfig(true);
+
+ // No document, an unusable one, or one that says nothing about signing: the OIDC Core
+ // default stands, so nothing changes for a deployment that works today.
+ expect(resolveIdTokenSigningAlg(null)).toBe("RS256");
+ expect(resolveIdTokenSigningAlg("not an object")).toBe("RS256");
+ expect(resolveIdTokenSigningAlg({})).toBe("RS256");
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: "RS256" })).toBe("RS256");
+
+ // Advertised alongside others, in any position — RS256 is preferred whenever it is on offer,
+ // so a provider that supports both keeps signing exactly as it does today.
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["RS256", "EdDSA"] })).toBe("RS256");
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["EdDSA", "ES256", "RS256"] })).toBe("RS256");
+ });
+
+ it("adopts the issuer's algorithm when RS256 is not offered", () => {
+ setOauthConfig(true);
+ const logSpy = vi.spyOn(getLog(), "info").mockImplementation(() => {});
+
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["EdDSA"] })).toBe("EdDSA");
+ expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("EdDSA"));
+ // First advertised wins among several; providers list their own preference first.
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["ES256", "EdDSA"] })).toBe("ES256");
+
+ // "none" would leave ID tokens unsigned, and express-openid-connect's schema rejects it —
+ // passing it through would take the whole OIDC round-trip down at build time.
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["none"] })).toBe("RS256");
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["none", "EdDSA"] })).toBe("EdDSA");
+ // Junk entries are ignored rather than handed on as an algorithm.
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: [null, "", 256, "EdDSA"] })).toBe("EdDSA");
+ });
+
+ it("lets an explicit oauthIdTokenSigningAlg override discovery", () => {
+ setOauthConfig(true);
+
+ // The escape hatch for a provider that misadvertises, in both directions.
+ mfa.oauthIdTokenSigningAlg = " EdDSA ";
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["RS256"] })).toBe("EdDSA");
+ mfa.oauthIdTokenSigningAlg = "RS512";
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["EdDSA"] })).toBe("RS512");
+
+ // ...but not into accepting unsigned ID tokens: that is logged and ignored, leaving
+ // discovery to answer, rather than reaching express-openid-connect's schema (which rejects
+ // "none" and would take the OIDC round-trip down at build time).
+ const logSpy = vi.spyOn(getLog(), "error").mockImplementation(() => {});
+ mfa.oauthIdTokenSigningAlg = "none";
+ expect(resolveIdTokenSigningAlg({ id_token_signing_alg_values_supported: ["EdDSA"] })).toBe("EdDSA");
+ expect(resolveIdTokenSigningAlg(null)).toBe("RS256");
+ expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("none"));
+ });
+
+ /**
+ * The other half of the loop. The cases above pin what Trilium *resolves*; these pin that the
+ * resolved value is what openid-client then accepts, by running a real authorization-code
+ * exchange against a provider that genuinely signs Ed25519 — the Pocket ID shape from the report.
+ */
+ describe("against a real Ed25519-signing provider", () => {
+ it("rejects the ID token while the client expects RS256", async () => {
+ // The reported failure, reproduced end to end: the token is validly signed and the
+ // signature verifies, but the client is registered as expecting an algorithm the issuer
+ // does not use, so the exchange is refused before any claim is read.
+ expect(await eddsaIdTokenOutcome(DEFAULT_ID_TOKEN_SIGNING_ALG))
+ .toContain('unexpected JWT "alg" header parameter');
+ });
+
+ it("accepts it once the expected algorithm is the issuer's", async () => {
+ expect(await eddsaIdTokenOutcome("EdDSA")).toBe("accepted");
+ });
+
+ it("resolves, from that provider's own discovery document, an algorithm that is accepted", async () => {
+ setOauthConfig(true);
+ const pocketId = { id_token_signing_alg_values_supported: ["EdDSA"] };
+ expect(await eddsaIdTokenOutcome(resolveIdTokenSigningAlg(pocketId))).toBe("accepted");
+ });
+ });
+
+ it("closes the loop: the resolved algorithm is what reaches express-openid-connect", () => {
+ setOauthConfig(true);
+
+ // Unset, the library would silently apply its RS256 default — the bug.
+ expect(openID.generateOAuthConfig().idTokenSigningAlg).toBe("RS256");
+ expect(openID.generateOAuthConfig(false, mfa.oauthIssuerBaseUrl, "EdDSA").idTokenSigningAlg).toBe("EdDSA");
+
+ // A configured value applies even to callers that never probed discovery.
+ mfa.oauthIdTokenSigningAlg = "ES256";
+ expect(openID.generateOAuthConfig().idTokenSigningAlg).toBe("ES256");
+ });
});
/**
@@ -677,7 +800,8 @@ describe("createReactiveOidcMiddleware", () => {
const buildAuth = vi.fn(() => oidcHandler);
const probeDiscovery = vi.fn().mockResolvedValue({
endSessionSupported: false,
- issuerBaseUrl: PROBED_ISSUER
+ issuerBaseUrl: PROBED_ISSUER,
+ idTokenSigningAlg: "RS256"
});
const generateOAuthConfig = vi.fn((endSessionSupported: boolean) => ({ endSessionSupported }) as never);
const isConfigured = vi.fn(() => configured);
@@ -731,7 +855,7 @@ describe("createReactiveOidcMiddleware", () => {
const { req, res, next } = await run(t.middleware);
expect(t.probeDiscovery).toHaveBeenCalledOnce();
- expect(t.generateOAuthConfig).toHaveBeenCalledWith(false, PROBED_ISSUER);
+ expect(t.generateOAuthConfig).toHaveBeenCalledWith(false, PROBED_ISSUER, "RS256");
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
@@ -752,19 +876,21 @@ describe("createReactiveOidcMiddleware", () => {
expect(t.oidcHandler).toHaveBeenCalledTimes(2);
});
- it("passes both discovery-probe results into the OAuth config", async () => {
+ it("passes every discovery-probe result into the OAuth config", async () => {
const t = setup();
// The issuer reaching express-openid-connect is the probe's, not whatever sits in config — that
- // indirection is what lets a trailing-slash difference be reconciled (#10695).
+ // indirection is what lets a trailing-slash difference be reconciled (#10695). The signing
+ // algorithm travels the same way, so a non-RS256 provider is honoured (discussion 6318).
t.probeDiscovery.mockResolvedValue({
endSessionSupported: true,
- issuerBaseUrl: "https://sso.example.com/application/o/trilium-app/"
+ issuerBaseUrl: "https://sso.example.com/application/o/trilium-app/",
+ idTokenSigningAlg: "EdDSA"
});
t.setConfigured(true);
await run(t.middleware);
- expect(t.generateOAuthConfig).toHaveBeenCalledWith(true, "https://sso.example.com/application/o/trilium-app/");
+ expect(t.generateOAuthConfig).toHaveBeenCalledWith(true, "https://sso.example.com/application/o/trilium-app/", "EdDSA");
});
// This is the regression the whole change exists to fix: with the old startup-only mount, flipping
@@ -805,13 +931,13 @@ 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).
- type Probe = { endSessionSupported: boolean; issuerBaseUrl: string };
+ type Probe = { endSessionSupported: boolean; issuerBaseUrl: string; idTokenSigningAlg: string };
let resolveProbe: (value: Probe) => void = () => {};
t.probeDiscovery.mockReturnValue(new Promise<Probe>((resolve) => { resolveProbe = resolve; }));
const first = run(t.middleware);
const second = run(t.middleware);
- resolveProbe({ endSessionSupported: false, issuerBaseUrl: PROBED_ISSUER });
+ resolveProbe({ endSessionSupported: false, issuerBaseUrl: PROBED_ISSUER, idTokenSigningAlg: "RS256" });
await Promise.all([first, second]);
expect(t.buildAuth).toHaveBeenCalledOnce();
@@ -1032,3 +1158,83 @@ async function discoveryOutcome(configuredIssuer: string, advertisedIssuer: stri
return (error as { code?: string })?.code ?? String(error);
}
}
+
+/** The issuer the {@link eddsaIdTokenOutcome} stub provider answers as. */
+const EDDSA_ISSUER = "https://issuer.example.com";
+const EDDSA_CLIENT_ID = "client-id";
+
+/**
+ * Runs a full authorization-code exchange against a stub provider that signs its ID token with Ed25519,
+ * with the client registered as expecting `expectedAlg` — which is precisely what express-openid-connect
+ * hands openid-client as `id_token_signed_response_alg`. Returns `"accepted"`, or the error message a
+ * user would see in the log.
+ *
+ * Like {@link discoveryOutcome} this drives the real openid-client over a `customFetch` transport, so the
+ * assertions are about the library's actual behaviour rather than a re-implementation of it — but here
+ * the token is genuinely signed and genuinely verified against the served JWKS, so a wrong `expectedAlg`
+ * fails for the real reason instead of a stubbed one. Ed25519 comes from node's own crypto, so no
+ * dependency (and no Pocket ID container) is needed to reproduce the report.
+ */
+async function eddsaIdTokenOutcome(expectedAlg: string) {
+ const { publicKey, privateKey } = generateKeyPairSync("ed25519");
+ const jwks = { keys: [{ ...publicKey.export({ format: "jwk" }), kid: "ed25519-1", alg: "EdDSA", use: "sig" }] };
+
+ const now = Math.floor(Date.now() / 1000);
+ const idToken = signEdDsaJwt(privateKey, {
+ iss: EDDSA_ISSUER,
+ aud: EDDSA_CLIENT_ID,
+ sub: "user-1",
+ iat: now,
+ exp: now + 300
+ });
+
+ const json = (body: unknown) => new Response(
+ JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } }
+ );
+ // One transport serves all three legs of the exchange: discovery, the JWKS the signature is verified
+ // against, and the token endpoint that returns the signed ID token.
+ const serveProvider: CustomFetch = async (url) => {
+ const path = new URL(url.toString()).pathname;
+ if (path.endsWith("/jwks")) {
+ return json(jwks);
+ }
+ if (path.endsWith("/token")) {
+ return json({ access_token: "access-token", token_type: "bearer", id_token: idToken });
+ }
+ return json({
+ issuer: EDDSA_ISSUER,
+ authorization_endpoint: `${EDDSA_ISSUER}/authorize`,
+ token_endpoint: `${EDDSA_ISSUER}/token`,
+ jwks_uri: `${EDDSA_ISSUER}/jwks`,
+ response_types_supported: ["code"],
+ // The Pocket ID shape once its key has been rotated to Ed25519: RS256 is not on offer at all.
+ id_token_signing_alg_values_supported: ["EdDSA"]
+ });
+ };
+
+ const configuration = await discovery(
+ new URL(EDDSA_ISSUER),
+ EDDSA_CLIENT_ID,
+ { id_token_signed_response_alg: expectedAlg },
+ ClientSecretPost("client-secret"),
+ { [customFetch]: serveProvider }
+ );
+
+ try {
+ await authorizationCodeGrant(configuration, new URL("https://app.example.com/callback?code=auth-code"), {});
+ return "accepted";
+ } catch (error) {
+ // openid-client reports this as a bare "invalid response encountered" and puts the actual reason
+ // on `.cause`. describeError is what unwraps that chain for the log line and the toast the user
+ // gets (see failRoundTrip), so asserting on its output pins the message they would actually see.
+ return describeError(error) ?? String(error);
+ }
+}
+
+/** Signs `claims` as a compact EdDSA JWS. Node signs Ed25519 with a null digest (RFC 8032). */
+function signEdDsaJwt(privateKey: KeyObject, claims: Record<string, unknown>) {
+ const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString("base64url");
+ const signingInput = `${encode({ alg: "EdDSA", typ: "JWT", kid: "ed25519-1" })}.${encode(claims)}`;
+ const signature = sign(null, Buffer.from(signingInput), privateKey).toString("base64url");
+ return `${signingInput}.${signature}`;
+}
diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.ts
index e78f9bb0e7..4b4b46f907 100644
--- a/apps/server/src/services/open_id.ts
+++ b/apps/server/src/services/open_id.ts
@@ -146,7 +146,8 @@ const OIDC_ROUTES = {
function generateOAuthConfig(
endSessionSupported = false,
- issuerBaseUrl = config.MultiFactorAuthentication.oauthIssuerBaseUrl
+ issuerBaseUrl = config.MultiFactorAuthentication.oauthIssuerBaseUrl,
+ idTokenSigningAlg = resolveIdTokenSigningAlg(null)
) {
const logoutParams = {
};
@@ -163,6 +164,10 @@ function generateOAuthConfig(
secret: config.MultiFactorAuthentication.oauthClientSecret,
clientSecret: config.MultiFactorAuthentication.oauthClientSecret,
clientAuthMethod: resolveClientAuthMethod(),
+ // Also not read straight from config: express-openid-connect hardcodes RS256 unless told
+ // otherwise, so this comes from the discovery probe via resolveIdTokenSigningAlg. Defaulted for
+ // callers that don't probe.
+ idTokenSigningAlg,
authorizationParams: {
response_type: "code",
scope: "openid profile email",
@@ -265,7 +270,7 @@ interface ReactiveOidcDeps {
/** Discovery probe supplying the issuer identifier and RP-Initiated Logout support. */
probeDiscovery: () => Promise<DiscoveryProbe>;
/** Builds the express-openid-connect config for the current provider settings. */
- generateOAuthConfig: (endSessionSupported: boolean, issuerBaseUrl: string) => Parameters<AuthBuilder>[0];
+ generateOAuthConfig: (endSessionSupported: boolean, issuerBaseUrl: string, idTokenSigningAlg: string) => Parameters<AuthBuilder>[0];
/** The express-openid-connect `auth()` factory (injectable so the middleware is unit-testable). */
buildAuth: AuthBuilder;
}
@@ -308,8 +313,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 { endSessionSupported, issuerBaseUrl } = await probe();
- oidcMiddleware = authFactory(buildOAuthConfig(endSessionSupported, issuerBaseUrl));
+ const { endSessionSupported, issuerBaseUrl, idTokenSigningAlg } = await probe();
+ oidcMiddleware = authFactory(buildOAuthConfig(endSessionSupported, issuerBaseUrl, idTokenSigningAlg));
})();
try {
await oidcInit;
@@ -421,17 +426,21 @@ interface DiscoveryProbe {
endSessionSupported: boolean;
/** The issuer to hand express-openid-connect — see {@link reconcileIssuerBaseUrl}. */
issuerBaseUrl: string;
+ /** The ID token signature algorithm to expect — see {@link resolveIdTokenSigningAlg}. */
+ idTokenSigningAlg: string;
}
/**
- * Probes the configured OIDC issuer's discovery document for the two facts that must be settled before
- * express-openid-connect can be built: whether RP-Initiated Logout is available, and which spelling of
- * the issuer identifier the provider actually advertises. The issuer is fixed in config.ini/env and only
- * changes on restart, so this is resolved once at first use rather than per request.
+ * Probes the configured OIDC issuer's discovery document for the facts that must be settled before
+ * express-openid-connect can be built: whether RP-Initiated Logout is available, which spelling of the
+ * issuer identifier the provider actually advertises, and which algorithm it signs ID tokens with. The
+ * issuer is fixed in config.ini/env and only changes on restart, so this is resolved once at first use
+ * rather than per request.
*
- * Any fetch/parse failure degrades to the configured issuer with logout support off, leaving a transient
- * network blip to break neither sign-in (express-openid-connect runs its own discovery, which either
- * succeeds or reports the real error) nor logout (which falls back to clearing the local session).
+ * Any fetch/parse failure degrades to the configured issuer with logout support off and the RS256
+ * default, leaving a transient network blip to break neither sign-in (express-openid-connect runs its
+ * own discovery, which either succeeds or reports the real error) nor logout (which falls back to
+ * clearing the local session).
*/
async function probeDiscovery(): Promise<DiscoveryProbe> {
const configuredIssuer = config.MultiFactorAuthentication.oauthIssuerBaseUrl;
@@ -439,7 +448,8 @@ async function probeDiscovery(): Promise<DiscoveryProbe> {
return {
endSessionSupported: supportsRpInitiatedLogout(metadata),
- issuerBaseUrl: reconcileIssuerBaseUrl(configuredIssuer, readAdvertisedIssuer(metadata))
+ issuerBaseUrl: reconcileIssuerBaseUrl(configuredIssuer, readAdvertisedIssuer(metadata)),
+ idTokenSigningAlg: resolveIdTokenSigningAlg(metadata)
};
}
@@ -479,6 +489,7 @@ async function fetchDiscoveryMetadata(configuredIssuer: string): Promise<unknown
interface OidcDiscoveryMetadata {
end_session_endpoint?: string;
issuer?: string;
+ id_token_signing_alg_values_supported?: unknown;
}
function readAdvertisedIssuer(metadata: unknown) {
@@ -548,6 +559,63 @@ export function supportsRpInitiatedLogout(metadata: unknown) {
return typeof end_session_endpoint === "string" && end_session_endpoint.length > 0;
}
+/**
+ * What express-openid-connect assumes when `idTokenSigningAlg` is left unset, and what OIDC Core
+ * requires every provider to support — so it stays the fallback whenever discovery can't tell us better.
+ */
+export const DEFAULT_ID_TOKEN_SIGNING_ALG = "RS256";
+
+/**
+ * Picks the JWS algorithm to expect on ID tokens.
+ *
+ * express-openid-connect defaults `idTokenSigningAlg` to RS256 and passes it to openid-client as the
+ * client's registered `id_token_signed_response_alg`, which is then enforced on every ID token. Trilium
+ * never set it, so a provider signing with anything else failed the round-trip with
+ * "unexpected JWT alg received, expected RS256, got: EdDSA"
+ * (https://github.com/orgs/TriliumNext/discussions/6318). RS256 is merely the *legacy* default: Pocket
+ * ID and Kanidm sign with Ed25519 by default, and others offer ES256.
+ *
+ * The provider already publishes the answer as `id_token_signing_alg_values_supported`, so it is read
+ * from the discovery probe rather than demanded from the user. RS256 wins whenever it's advertised —
+ * that keeps every currently-working deployment on exactly the algorithm it uses today, and the fallback
+ * only engages for the providers that don't offer RS256 at all, which are precisely the broken ones.
+ * `none` is skipped: unsigned ID tokens are not acceptable here (and express-openid-connect's own schema
+ * rejects the value outright, which would take the OIDC round-trip down at build time).
+ *
+ * `oauthIdTokenSigningAlg` overrides all of it, for a provider that misadvertises or signs with an
+ * algorithm we'd rank differently.
+ */
+export function resolveIdTokenSigningAlg(metadata: unknown): string {
+ const configured = config.MultiFactorAuthentication.oauthIdTokenSigningAlg.trim();
+ if (configured) {
+ if (configured.toLowerCase() !== "none") {
+ return configured;
+ }
+ getLog().error(`OAuth: ignoring oauthIdTokenSigningAlg '${configured}' — unsigned ID tokens are not supported.`);
+ }
+
+ const advertised = readAdvertisedSigningAlgs(metadata);
+ if (advertised.length === 0 || advertised.includes(DEFAULT_ID_TOKEN_SIGNING_ALG)) {
+ return DEFAULT_ID_TOKEN_SIGNING_ALG;
+ }
+
+ const [preferred] = advertised;
+ getLog().info(`OAuth: the issuer does not sign ID tokens with ${DEFAULT_ID_TOKEN_SIGNING_ALG}; expecting ${preferred} (advertised: ${advertised.join(", ")}).`);
+ return preferred;
+}
+
+/** The usable entries of `id_token_signing_alg_values_supported`, or an empty list if there are none. */
+function readAdvertisedSigningAlgs(metadata: unknown): string[] {
+ if (typeof metadata !== "object" || metadata === null) {
+ return [];
+ }
+ const advertised = (metadata as OidcDiscoveryMetadata).id_token_signing_alg_values_supported;
+ if (!Array.isArray(advertised)) {
+ return [];
+ }
+ return advertised.filter((alg): alg is string => typeof alg === "string" && alg !== "" && alg.toLowerCase() !== "none");
+}
+
export const CLIENT_AUTH_METHODS = ["client_secret_basic", "client_secret_post"] as const;
export type ClientAuthMethod = (typeof CLIENT_AUTH_METHODS)[number];