🧺 fix: Discard Stale Session Tokens at OpenID Login (#15668)

With `OPENID_REUSE_TOKENS=true`, `sendOpenIDAuthResponse` compares the
incoming token set against `req.session.openidTokens`. Passport runs with
`session: false`, so the Express session is never regenerated at login and a
token set from an earlier authentication survives there. Its refresh token
differs from the one the IdP just issued, which the advanced-session check
reads as "a concurrent refresh already advanced the session": the fresh IdP
tokens are discarded and the stale set is republished. When that stale
access token has already expired, the completed flight's TTL clamps to 1ms
and the publication assertion fails with "OpenID refresh result is no longer
available for publication", which fails every login for as long as the stale
session lives.

The advanced-session comparison exists for refresh races and has no business
at a fresh authorization-code login. The login controller now asks the
publication service to discard whatever the session holds, and the service
does so after its own session reload so the store cannot resurrect the
stale set. Refresh callers are unchanged.

Fixes #15648
This commit is contained in:
Danny Avila 2026-09-06 15:07:08 -04:00 • committed by GitHub
parent 8f6efaf6ed
commit f1a21764f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 0 deletions

View file

@ -104,4 +104,49 @@ describe('OpenID authentication publication settlement', () => {
expect(deps.completeOpenIDRefreshFlight).toHaveBeenCalledTimes(1);
expect(deps.failOpenIDRefreshFlight).not.toHaveBeenCalled();
});
describe('with a stale token set left in the Express session', () => {
function buildStaleSession() {
return {
accessToken: 'stale-access',
idToken: 'stale-id',
refreshToken: 'stale-refresh',
accessTokenExpiresAt: Math.floor(Date.now() / 1000) - 120,
};
}
it('publishes the IdP token set at login instead of the stale session set', async () => {
const { deps, service, input } = setup();
const req = { session: { openidTokens: buildStaleSession() } };
await expect(
service.sendOpenIDAuthResponse({ ...input, req, discardSessionTokens: true }),
).resolves.toBe('app-token');
expect(req.session.openidTokens).toBeUndefined();
expect(deps.completeOpenIDRefreshFlight).toHaveBeenCalledWith(
expect.objectContaining({
tokens: expect.objectContaining({
tokenset: expect.objectContaining({ access_token: 'access', refresh_token: 'refresh' }),
}),
}),
);
expect(deps.setOpenIDAuthTokens).toHaveBeenCalledWith(
expect.objectContaining({ access_token: 'access', refresh_token: 'refresh' }),
req,
input.res,
expect.objectContaining({ userId: 'user', existingRefreshToken: 'refresh' }),
);
});
it('keeps adopting an advanced session on the refresh path', async () => {
const { deps, service, input } = setup();
const req = { session: { openidTokens: buildStaleSession() } };
await expect(service.sendOpenIDAuthResponse({ ...input, req })).resolves.toBe('app-token');
expect(deps.setOpenIDAuthTokens).toHaveBeenCalledWith(
expect.objectContaining({ access_token: 'stale-access', refresh_token: 'stale-refresh' }),
req,
input.res,
expect.objectContaining({ existingRefreshToken: 'stale-refresh' }),
);
});
});
});

View file

@ -100,6 +100,12 @@ interface SendOpenIDAuthResponseInput {
},
) => Promise<void>;
preparePublication?: boolean;
/**
* A fresh authorization-code login supersedes whatever token set the Express session still
* holds from an earlier authentication. The advanced-session comparison exists for refresh
* races and must not republish that stale set in place of the tokens the IdP just issued.
*/
discardSessionTokens?: boolean;
}
export interface OpenIDRefreshRecoveryService {
@ -679,6 +685,7 @@ export function createOpenIDRefreshRecoveryService(
publicationGeneration,
commitPublication,
preparePublication = true,
discardSessionTokens = false,
}: SendOpenIDAuthResponseInput): Promise<string | undefined> {
const userId = user._id.toString();
const publicationIdentity = predecessorIdentity ?? {
@ -720,6 +727,7 @@ export function createOpenIDRefreshRecoveryService(
publicationGeneration: sharedGeneration,
commitPublication: async () => {},
preparePublication: false,
discardSessionTokens,
});
}
let completionStarted = false;
@ -745,6 +753,7 @@ export function createOpenIDRefreshRecoveryService(
? new Date(flight.flight.createdAt).getTime()
: Date.now(),
},
discardSessionTokens,
commitPublication: async (appAuthToken, publishedTokenset, metadata) => {
const completed = await completeOpenIDRefreshFlight({
key,
@ -797,6 +806,9 @@ export function createOpenIDRefreshRecoveryService(
reload((error?: Error | null) => (error ? reject(error) : resolve()));
});
}
if (discardSessionTokens && req?.session?.openidTokens) {
delete req.session.openidTokens;
}
let effectiveTokenset = tokenset;
let effectiveExistingRefreshToken = existingRefreshToken;
let usesAdvancedSession = false;