fix(auth): fetch a fresh id token hint at sign-out #410

Sammanfogat
supernaut sammanfogade 2 incheckningar från fix/398-signout-confirm in i main 2026-10-25 19:34:26 +00:00
Ägare

Closes #398.

Cause

The portal stored the login id token in gb_id_token for 8 hours and sent it as id_token_hint at sign-out. Its sid names the Keycloak SSO session from portal login. Keycloak's default SSO idle timeout is 30 minutes, so that session often ends while the portal session lives on. A later sign-in elsewhere (for example on Forgejo) starts a new SSO session. Keycloak's end-session endpoint then sees a hint for one session and a browser cookie for another, and shows "Do you want to log out?".

#404 dropped hints from another issuer or client. It could not catch a stale sid, because the portal cannot see the browser's Keycloak session.

Change

  • POST /auth/logout clears the portal session, then starts a silent prompt=none authorization round trip.
  • The gb_oidc_logout transaction cookie holds logout:<state>. /auth/callback takes the sign-out branch only when it matches the returned state (constant-time). A sign-out in another tab cannot turn an in-flight login into a sign-out.
  • In that branch the callback exchanges the code and redirects to the end-session endpoint with the new id token as hint. That token names the browser's current SSO session.
    • error=login_required: no Keycloak session left, go home.
    • Any other failure, such as a failed token exchange: end-session without a hint (confirm page, as before).
    • The branch skips verifyIdToken and the nonce check. The token comes straight from the token endpoint, is PKCE-bound and is used only as an end-session hint. Noted in docs/design/oidc-security-review.md.
  • The transaction cookie setup moved from login.ts to src/lib/auth/authorize.ts, shared by login and sign-out.
  • The gb_id_token cookie is no longer set. Sign-out still deletes it for older browsers. idTokenMatches and setIdTokenHint are removed.
  • Version 1.45.1.

Tests

src/test/logout.test.ts covers the silent round trip start, the callback with a fresh hint, login_required, a failed token exchange, a forged state, and a sign-out flag left by another tab. src/lib/auth/authorize.test.ts covers the flag, prompt and return-path cookies for sign-out and login. pnpm check, pnpm lint and pnpm vitest run pass.

Staging check

  • Sign in on www.stg. Wait past the Keycloak SSO idle time. Sign in on git.stg (new SSO session). Sign out on www.stg. Expect no confirm page and a return to https://www.stg.bitborg.se/.
  • Sign in and sign out at once. Expect no confirm page.
  • Sign in, end the Keycloak session (sign out elsewhere), then sign out on www.stg. Expect a return home with no Keycloak page.
Closes #398. ## Cause The portal stored the login id token in `gb_id_token` for 8 hours and sent it as `id_token_hint` at sign-out. Its `sid` names the Keycloak SSO session from portal login. Keycloak's default SSO idle timeout is 30 minutes, so that session often ends while the portal session lives on. A later sign-in elsewhere (for example on Forgejo) starts a new SSO session. Keycloak's end-session endpoint then sees a hint for one session and a browser cookie for another, and shows "Do you want to log out?". #404 dropped hints from another issuer or client. It could not catch a stale `sid`, because the portal cannot see the browser's Keycloak session. ## Change - `POST /auth/logout` clears the portal session, then starts a silent `prompt=none` authorization round trip. - The `gb_oidc_logout` transaction cookie holds `logout:<state>`. `/auth/callback` takes the sign-out branch only when it matches the returned state (constant-time). A sign-out in another tab cannot turn an in-flight login into a sign-out. - In that branch the callback exchanges the code and redirects to the end-session endpoint with the new id token as hint. That token names the browser's current SSO session. - `error=login_required`: no Keycloak session left, go home. - Any other failure, such as a failed token exchange: end-session without a hint (confirm page, as before). - The branch skips `verifyIdToken` and the nonce check. The token comes straight from the token endpoint, is PKCE-bound and is used only as an end-session hint. Noted in `docs/design/oidc-security-review.md`. - The transaction cookie setup moved from `login.ts` to `src/lib/auth/authorize.ts`, shared by login and sign-out. - The `gb_id_token` cookie is no longer set. Sign-out still deletes it for older browsers. `idTokenMatches` and `setIdTokenHint` are removed. - Version 1.45.1. ## Tests `src/test/logout.test.ts` covers the silent round trip start, the callback with a fresh hint, `login_required`, a failed token exchange, a forged state, and a sign-out flag left by another tab. `src/lib/auth/authorize.test.ts` covers the flag, `prompt` and return-path cookies for sign-out and login. `pnpm check`, `pnpm lint` and `pnpm vitest run` pass. ## Staging check - [ ] Sign in on www.stg. Wait past the Keycloak SSO idle time. Sign in on git.stg (new SSO session). Sign out on www.stg. Expect no confirm page and a return to `https://www.stg.bitborg.se/`. - [ ] Sign in and sign out at once. Expect no confirm page. - [ ] Sign in, end the Keycloak session (sign out elsewhere), then sign out on www.stg. Expect a return home with no Keycloak page.
supernaut lade till 2 incheckningar 2026-10-25 19:29:16 +00:00
The stored id token named the SSO session from portal login. Keycloak
asks to confirm when that session differs from the browser's current
one. Sign-out now runs a silent prompt=none round trip and sends the
new id token as the hint.

Closes #398
fix(auth): bind the sign-out flag to its state
Alla kontroller lyckades
ci / ci (pull_request) Successful in 4m40s
e949fd7330
A sign-out in another tab could turn an in-flight login into a
sign-out. The flag now names its state and the callback takes the
sign-out branch only on a match. Adds tests and a security-review note.
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-25 19:30:05 +00:00
supernaut sammanfogade incheckning 5cc2d63c01 till main 2026-10-25 19:34:26 +00:00
supernaut tog bort grenen fix/398-signout-confirm 2026-10-25 19:34:27 +00:00
supernaut refererade denna ändringsförfrågan från en incheckning 2026-10-25 19:34:27 +00:00
Logga in för att delta i denna konversation.
Inga granskare
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Förfallodatumet är ogiltigt eller utanför gränserna. Använd formatet "åååå-mm-dd".

Inget förfallodatum satt.

Beroenden

Inga beroenden satta

Referens
bitborg/bitborg-web!410
Ingen beskrivning angiven.