Skip to content

Build your personal knowledge base with Trilium Notes

fix(oauth): re-probe discovery instead of caching a fallback algorithm

Elian Doranb9a09b135fd6
diff --git a/apps/server/src/services/open_id.spec.ts b/apps/server/src/services/open_id.spec.ts
index 3e76c48570..e0eba94569 100644
--- a/apps/server/src/services/open_id.spec.ts
+++ b/apps/server/src/services/open_id.spec.ts
@@ -492,7 +492,8 @@ describe("open_id", () => {
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: true,
issuerBaseUrl: "https://issuer.example.com",
- idTokenSigningAlg: "RS256"
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: true
});
expect(fetchSpy).toHaveBeenCalledWith(wellKnownUrl, expect.anything());
});
@@ -525,14 +526,16 @@ describe("open_id", () => {
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: false,
issuerBaseUrl: "https://issuer.example.com",
- idTokenSigningAlg: "RS256"
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: false
});
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network down"));
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: false,
issuerBaseUrl: "https://issuer.example.com",
- idTokenSigningAlg: "RS256"
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: false
});
});
@@ -544,7 +547,8 @@ describe("open_id", () => {
expect(await openID.probeDiscovery()).toEqual({
endSessionSupported: false,
issuerBaseUrl: "",
- idTokenSigningAlg: "RS256"
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: false
});
expect(fetchSpy).not.toHaveBeenCalled();
});
@@ -801,7 +805,8 @@ describe("createReactiveOidcMiddleware", () => {
const probeDiscovery = vi.fn().mockResolvedValue({
endSessionSupported: false,
issuerBaseUrl: PROBED_ISSUER,
- idTokenSigningAlg: "RS256"
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: true
});
const generateOAuthConfig = vi.fn((endSessionSupported: boolean) => ({ endSessionSupported }) as never);
const isConfigured = vi.fn(() => configured);
@@ -864,6 +869,47 @@ describe("createReactiveOidcMiddleware", () => {
expect(next).toHaveBeenCalledOnce();
});
+ /**
+ * A probe that couldn't reach the issuer returns fallbacks, not answers — and `auth()` performs no
+ * discovery of its own (it only validates config and builds a router), so the build succeeds and
+ * nothing downstream notices. Caching that would freeze the RS256 fallback into the config object
+ * for the life of the process: a provider signing EdDSA would then reject every valid ID token until
+ * a restart, re-creating the very failure this resolution exists to prevent. The library recovers
+ * from the same blip by itself, so the handler must be rebuilt rather than pinned.
+ */
+ it("does not cache a handler built without the provider's discovery document", async () => {
+ const t = setup();
+ t.probeDiscovery.mockResolvedValueOnce({
+ endSessionSupported: false,
+ issuerBaseUrl: PROBED_ISSUER,
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: false
+ });
+ t.probeDiscovery.mockResolvedValue({
+ endSessionSupported: true,
+ issuerBaseUrl: PROBED_ISSUER,
+ idTokenSigningAlg: "EdDSA",
+ metadataAvailable: true
+ });
+ t.setConfigured(true);
+
+ // The degraded build still serves its own request rather than failing it — a blip must not break
+ // a sign-in the library itself may well complete.
+ await run(t.middleware);
+ expect(t.oidcHandler).toHaveBeenCalledOnce();
+ expect(t.generateOAuthConfig).toHaveBeenLastCalledWith(false, PROBED_ISSUER, "RS256");
+
+ // Once discovery recovers, the real algorithm takes effect without a restart...
+ await run(t.middleware);
+ expect(t.probeDiscovery).toHaveBeenCalledTimes(2);
+ expect(t.generateOAuthConfig).toHaveBeenLastCalledWith(true, PROBED_ISSUER, "EdDSA");
+
+ // ...and that build, made from a document that was actually read, is the one that gets cached.
+ await run(t.middleware);
+ expect(t.probeDiscovery).toHaveBeenCalledTimes(2);
+ expect(t.buildAuth).toHaveBeenCalledTimes(2);
+ });
+
it("builds the underlying handler only once across requests (cached)", async () => {
const t = setup();
t.setConfigured(true);
@@ -884,7 +930,8 @@ describe("createReactiveOidcMiddleware", () => {
t.probeDiscovery.mockResolvedValue({
endSessionSupported: true,
issuerBaseUrl: "https://sso.example.com/application/o/trilium-app/",
- idTokenSigningAlg: "EdDSA"
+ idTokenSigningAlg: "EdDSA",
+ metadataAvailable: true
});
t.setConfigured(true);
@@ -931,13 +978,23 @@ 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; idTokenSigningAlg: string };
+ type Probe = {
+ endSessionSupported: boolean;
+ issuerBaseUrl: string;
+ idTokenSigningAlg: string;
+ metadataAvailable: boolean;
+ };
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, idTokenSigningAlg: "RS256" });
+ resolveProbe({
+ endSessionSupported: false,
+ issuerBaseUrl: PROBED_ISSUER,
+ idTokenSigningAlg: "RS256",
+ metadataAvailable: true
+ });
await Promise.all([first, second]);
expect(t.buildAuth).toHaveBeenCalledOnce();
diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.ts
index 4b4b46f907..991a1bca8b 100644
--- a/apps/server/src/services/open_id.ts
+++ b/apps/server/src/services/open_id.ts
@@ -298,7 +298,7 @@ export function createReactiveOidcMiddleware(deps: Partial<ReactiveOidcDeps> = {
} = deps;
let oidcMiddleware: RequestHandler | null = null;
- let oidcInit: Promise<void> | null = null;
+ let oidcInit: Promise<RequestHandler> | null = null;
return async (req: Request, res: Response, next: NextFunction) => {
// OAuth not selected as the sign-in method → behave as if the middleware were never mounted.
@@ -306,39 +306,66 @@ export function createReactiveOidcMiddleware(deps: Partial<ReactiveOidcDeps> = {
return next();
}
- if (!oidcMiddleware) {
- oidcInit ??= (async () => {
+ let handler = oidcMiddleware;
+ if (!handler) {
+ const init = (oidcInit ??= (async () => {
// Load express-openid-connect lazily so the (heavy) library and its transitive deps are
// only evaluated the first time OAuth is actually used, never on a server that runs with
// 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, idTokenSigningAlg } = await probe();
- oidcMiddleware = authFactory(buildOAuthConfig(endSessionSupported, issuerBaseUrl, idTokenSigningAlg));
- })();
+ const { endSessionSupported, issuerBaseUrl, idTokenSigningAlg, metadataAvailable } = await probe();
+ const built = authFactory(buildOAuthConfig(endSessionSupported, issuerBaseUrl, idTokenSigningAlg));
+
+ // Only commit to the handler when the probe actually read the provider's document.
+ //
+ // A probe that couldn't reach the issuer degrades to fallbacks rather than throwing (see
+ // probeDiscovery), and `auth()` does not itself perform discovery — it only validates
+ // config and builds a router — so the build succeeds and nothing here would notice. The
+ // library recovers from the same blip on its own (it discovers per request, behind a
+ // cache it drops on failure), but these values are frozen into the config object handed
+ // to `auth()`. Caching them would outlive the blip: a provider that signs EdDSA would be
+ // pinned to the RS256 fallback and reject every valid ID token until a restart — exactly
+ // the failure this resolution exists to prevent.
+ if (metadataAvailable) {
+ oidcMiddleware = built;
+ }
+ return built;
+ })());
+
try {
- await oidcInit;
+ handler = await init;
} catch (error) {
- // Reset so the next request can retry — otherwise a single failed init (transient
- // discovery-probe failure, malformed config, etc.) would leave the rejected promise
+ // Reset so the next request can retry — otherwise a single failed init (a malformed
+ // config `auth()` rejects, an import failure, etc.) would leave the rejected promise
// cached and break every subsequent OAuth request until a server restart.
- oidcInit = null;
+ if (oidcInit === init) {
+ oidcInit = null;
+ }
return failRoundTrip(req, res, next, error);
}
+
+ // Settled, so drop the in-flight guard — its only job is to make concurrent first requests
+ // share one build. When the probe read the document `oidcMiddleware` now holds the handler
+ // and this is never consulted again; when it didn't, the next request re-probes. The identity
+ // check keeps a request from clearing a *newer* build's guard.
+ if (oidcInit === init) {
+ oidcInit = null;
+ }
}
// Hand off entirely to the express-openid-connect handler: it drives the request by side effect
// (sends a response/redirect or calls next() itself) and returns undefined. We must NOT call
// next() ourselves afterwards — doing so double-invokes the downstream pipeline and triggers
// "Cannot set headers after they are sent". The guard only covers a failed build.
- if (!oidcMiddleware) {
+ if (!handler) {
return next();
}
// The library reports a broken round-trip by calling next(err) rather than by rejecting, so the
// interception has to happen on `next` — a try/catch around this call would never see it. A bare
// next() (the pass-through for non-OIDC routes) must still propagate untouched.
- return oidcMiddleware(req, res, (error?: unknown) => {
+ return handler(req, res, (error?: unknown) => {
if (!error) {
return next();
}
@@ -428,6 +455,12 @@ interface DiscoveryProbe {
issuerBaseUrl: string;
/** The ID token signature algorithm to expect — see {@link resolveIdTokenSigningAlg}. */
idTokenSigningAlg: string;
+ /**
+ * Whether the provider's discovery document was actually read. False means every field above is a
+ * fallback rather than an answer, which is what stops {@link createReactiveOidcMiddleware} from
+ * caching a handler built from it.
+ */
+ metadataAvailable: boolean;
}
/**
@@ -440,7 +473,8 @@ interface DiscoveryProbe {
* 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).
+ * clearing the local session). Such a result is flagged `metadataAvailable: false` so the caller can
+ * tell a fallback from an answer and decline to cache it — see {@link createReactiveOidcMiddleware}.
*/
async function probeDiscovery(): Promise<DiscoveryProbe> {
const configuredIssuer = config.MultiFactorAuthentication.oauthIssuerBaseUrl;
@@ -449,7 +483,8 @@ async function probeDiscovery(): Promise<DiscoveryProbe> {
return {
endSessionSupported: supportsRpInitiatedLogout(metadata),
issuerBaseUrl: reconcileIssuerBaseUrl(configuredIssuer, readAdvertisedIssuer(metadata)),
- idTokenSigningAlg: resolveIdTokenSigningAlg(metadata)
+ idTokenSigningAlg: resolveIdTokenSigningAlg(metadata),
+ metadataAvailable: metadata !== null
};
}