fix: OIDC 502 on sign-in from oversized id_token cookie (#12986)

Storing the full OIDC id_token in the `oidcIdToken` cookie inflated the
sign-in response headers enough to exceed reverse proxy buffers, causing a
502 Bad Gateway. Store the id_token server-side in Redis keyed by a short
session identifier and keep only that identifier in the cookie, preserving
spec-compliant RP-initiated logout via `id_token_hint`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom MoorandClaude Opus 4.8 committed 2026-07-13 18:29:05 -04:00
1 parent 8170639085
commit b6786f7ac8
3 files changed
+58 -10

No files matched your search

+16 -2
View File
@@ -1,3 +1,5 @@
import Redis from "@server/storage/redis";
import { RedisPrefixHelper } from "@server/utils/RedisPrefixHelper";
import { getTestServer } from "@server/test/support";
const server = getTestServer();
@@ -32,10 +34,16 @@ describe("oidc", () => {
});
it("should include the id_token_hint when present", async () => {
const sessionId = "test-session-id";
await Redis.defaultClient.set(
RedisPrefixHelper.getLogoutTokenKey(sessionId),
"fake-id-token"
);
const res = await server.get("/auth/oidc.logout", {
redirect: "manual",
headers: {
Cookie: "oidcIdToken=fake-id-token",
Cookie: `oidcSession=${sessionId}`,
},
});
expect(res.status).toEqual(302);
@@ -44,8 +52,14 @@ describe("oidc", () => {
"fake-id-token"
);
expect(res.headers.get("set-cookie")).toContain(
"oidcIdToken=; path=/auth/oidc.logout;"
"oidcSession=; path=/auth/oidc.logout;"
);
// The token is consumed from the server-side store on logout.
expect(
await Redis.defaultClient.get(
RedisPrefixHelper.getLogoutTokenKey(sessionId)
)
).toBeNull();
});
});
});
+31 -8
View File
@@ -1,5 +1,6 @@
import crypto from "node:crypto";
import passport from "@outlinewiki/koa-passport";
import { addMonths, subMinutes } from "date-fns";
import { addSeconds, subMinutes } from "date-fns";
import JWT from "jsonwebtoken";
import type { Context, Request } from "koa";
import type Router from "koa-router";
@@ -17,7 +18,9 @@ import Logger from "@server/logging/Logger";
import passportMiddleware from "@server/middlewares/passport";
import type { User } from "@server/models";
import { AuthenticationProvider } from "@server/models";
import Redis from "@server/storage/redis";
import type { AuthenticationResult } from "@server/types";
import { RedisPrefixHelper } from "@server/utils/RedisPrefixHelper";
import {
StateStore,
getTeamFromContext,
@@ -32,6 +35,10 @@ import { OIDCStrategy } from "./OIDCStrategy";
import { createContext } from "@server/context";
const OIDC_LOGOUT_PATH = "/auth/oidc.logout";
const OIDC_SESSION_COOKIE = "oidcSession";
// Shared by the server-side token and the cookie referencing it so the hint
// is available for as long as the cookie can be sent back.
const OIDC_LOGOUT_TOKEN_TTL_SECONDS = 90 * 24 * 60 * 60;
export interface OIDCEndpoints {
authorizationURL: string;
@@ -238,9 +245,20 @@ export function createOIDCRouter(
});
// Persist the id_token so a later RP-initiated logout can pass it as
// the `id_token_hint`, allowing the provider to scope the logout to
// this session rather than terminating its global SSO session.
// this session rather than terminating its global SSO session. The
// token is stored server-side keyed by a short session identifier;
// only that identifier goes in the cookie. id_tokens can be large and
// storing one directly in the cookie inflates response headers enough
// to exceed reverse proxy buffers, resulting in a 502 on sign-in.
if (endpoints.logoutURL && params.id_token) {
context.cookies.set("oidcIdToken", params.id_token, {
const sessionId = crypto.randomBytes(16).toString("hex");
await Redis.defaultClient.set(
RedisPrefixHelper.getLogoutTokenKey(sessionId),
params.id_token,
"EX",
OIDC_LOGOUT_TOKEN_TTL_SECONDS
);
context.cookies.set(OIDC_SESSION_COOKIE, sessionId, {
httpOnly: true,
sameSite: "lax",
secure: env.isProduction,
@@ -249,7 +267,7 @@ export function createOIDCRouter(
context.request.hostname,
env.isCloudHosted
),
expires: addMonths(new Date(), 3),
expires: addSeconds(new Date(), OIDC_LOGOUT_TOKEN_TTL_SECONDS),
});
}
@@ -270,11 +288,16 @@ export function createOIDCRouter(
// ended so the provider can scope the logout and skip a confirmation prompt,
// while `post_logout_redirect_uri` returns the user to Outline afterwards.
// https://openid.net/specs/openid-connect-rpinitiated-1_0.html
router.get(`${config.id}.logout`, (ctx: Context) => {
const idToken = ctx.cookies.get("oidcIdToken");
router.get(`${config.id}.logout`, async (ctx: Context) => {
const sessionId = ctx.cookies.get(OIDC_SESSION_COOKIE);
const idToken = sessionId
? await Redis.defaultClient.getdel(
RedisPrefixHelper.getLogoutTokenKey(sessionId)
)
: null;
// Always discard our copy of the id_token, regardless of where we redirect.
ctx.cookies.set("oidcIdToken", "", {
// Always discard our session identifier, regardless of where we redirect.
ctx.cookies.set(OIDC_SESSION_COOKIE, "", {
httpOnly: true,
sameSite: "lax",
secure: env.isProduction,
+11
View File
@@ -69,4 +69,15 @@ export class RedisPrefixHelper {
) {
return `count:${modelName}:${relationName}:${id}`;
}
/**
* Gets key for storing an OIDC id_token used as the `id_token_hint` during
* RP-initiated logout, referenced by a short session identifier.
*
* @param sessionId The logout session identifier to generate a key for.
* @returns the cache key string.
*/
public static getLogoutTokenKey(sessionId: string) {
return `oidc:logout:${sessionId}`;
}
}