Skip to content
12 min read

Sign-out that really ends the session

Deleting a cookie signs a browser out of your app. It does not sign the user out of anything else. If your web app sits in front of an identity provider that issues its own sessions and tokens, those keep working after the cookie is gone, and a captured token stays valid for as long as it lives.

This runbook covers the pattern we use for the Fjord web app and the failures it is built to avoid. It applies to anyone running a web front end over OpenID Connect, including a custom sign-in in front of Forgejo.

What there is to tear down

A signed-in user usually holds four separate things, each with its own owner and its own lifetime.

ThingWhere it livesIf you leave it behind
The app’s session cookieThe browserNothing, once deleted. This is the only part most apps clear.
The identity provider’s sessionThe provider, as its own cookie in the browser and as a record on its sideThe next sign-in succeeds silently as the same account, without asking for credentials.
The access tokenShort lived, usually about an hourA captured token keeps calling the API until it expires, and the session still lists as active.
The refresh chainThe provider’s registry, with the current token stored by your app on the serverA captured refresh token can be exchanged for new access tokens long after sign-out.

A sign-out is complete when all four are gone. The rest of this page is how to get there without making sign-out fragile.

Keep the long-lived secret off the browser

The refresh token is the most valuable credential in the flow. Leaking it is far worse than leaking an access token that dies within the hour. So it never rides in the cookie.

  • The cookie carries a signed, opaque session id and the claims the app needs to render.
  • The refresh token, and the identity token used to log out, are stored on the server, keyed by that session id.
  • The server looks the refresh token up only when it has to renew an access token, and writes back the new one.

This is also what makes sign-out tractable. The server knows which refresh chain belongs to the session, so it can end exactly that chain.

Lifetimes that cannot drift apart

There are two clocks: how long the cookie lasts, and how long the server holds the refresh token. They are not independent.

  • The cookie uses a sliding idle window. Activity pushes it forward, so an active user is never ended mid-task, and an idle one is dropped after the window. Push it forward only at the moment you renew the credential behind it, in the same step that rewrites the refresh entry. Extending the cookie on every request, without renewing the refresh entry, lets the cookie’s deadline run past the credential that sustains it.
  • The refresh token has its own expiry on the server. Every renewal rewrites it.

The invariant: the refresh token must outlive the cookie. If the two clocks are equal, the cookie can slide past the refresh token’s death. The user then holds a session that looks signed in, but cannot renew its access token, so every call to the identity provider fails until the window finally runs out. Give the refresh entry a little slack beyond the cookie window, so that on genuine idleness the cookie always expires first and the user is simply asked to sign in again. Pin the ordering with a test, and test activity between renewals, not only a comparison of the two constants: the invariant is about deadlines at the moment the cookie is extended.

Renewals can also race: a navigation, its prefetch and a second tab all notice the same expiring token at once. If your provider rotates the refresh token on every use, the losers present an already-spent token, and the provider treats the duplicates as replay and revokes the whole chain. The user is signed out in the middle of a session for no reason. Let one request renew, have the others wait for it, and let them reuse the new access token it cached for a short time.

Handle renewal failures asymmetrically:

  • A terminal token error means the refresh token is expired, revoked or replayed. Read the OAuth error code (invalid_grant is the usual one) rather than the status alone, and clear the session so the user signs in cleanly.
  • Anything retryable keeps the session untouched: a 5xx, a network error, or a 429 from rate limiting. Back off and let a later request retry, or a brief provider outage or a burst of load signs everyone out.

Sign-out, step by step

Do these in this order. The order is not cosmetic.

  1. Build the provider’s logout address first. Provider-initiated logout wants the identity token as a hint. Your teardown deletes that token, so read it before anything else.
  2. Revoke the provider session, and only this one. Use the session id inside the access token. Signing out on a laptop must not sign the user out on their phone.
  3. Revoke the refresh family at the provider. Standard OAuth token revocation (RFC 7009) is the request to make, but the RFC leaves it to the provider whether revoking one token also ends the rest of its rotation chain. Confirm that your provider does, and test it. Deleting your stored copy only stops you from minting new tokens, while the chain stays alive at the provider until it expires.
  4. Delete the server-side entries once revocation is done: the refresh token, any cached renewed access token, and the identity token. If the revoke did not land, do not throw away the only copy of the refresh token. See When the revoke does not land.
  5. Delete the cookies.
  6. Send the browser through the provider’s end-session endpoint, with your client id, the identity token hint and a post-logout address registered with the provider. That severs the provider’s own browser cookie, so the next sign-in needs real credentials.

If there is no provider session to clear, such as a development deployment or an incomplete configuration, skip the hop and send the browser home yourself.

Fail open on the upstream, never on the local state

Sign-out must always end the local session, even if the provider is down. So the upstream calls are best effort and individually guarded:

  • Bound every upstream call. Give each call a short timeout and the whole teardown an overall budget well inside your request limit. A provider that accepts the connection and then stalls must not hold the sign-out until the request is killed with the cookie still in place. When the budget runs out, clear the local session and cookies, record what is still unfinished, and return.
  • A failure in one step must never skip the others. Wrap each call separately, not the whole block in one handler. A storage read that fails must still fall through to the next step.
  • A failed revoke must not block the cookie clear. The user is signed out of your app at once.

When the revoke does not land

Signing the user out locally is not the same as ending the session upstream. If the provider could not be reached, two things are still alive: the access token, until it expires, and the refresh token, which is far more dangerous.

  • Disable local use immediately, so the session cannot be used through your app.
  • Keep the revocation work, not the session. Record a protected, expiring “revoke pending” entry holding what is needed to finish the job, and retry it until it succeeds or the token would have expired anyway. Retries use the same timeouts.
  • Alert on revocations that stay pending. An unfinished revoke is a captured refresh token waiting to be useful to someone.
  • Bound the access token. If your API validates self-contained access tokens locally, it will keep accepting them after the session is revoked, until they expire. Either keep them short enough to accept that, or check revocation at the API for sensitive calls.

Deleting the only stored copy of the refresh token after a failed revoke trades a recoverable outage for a quiet hole, because nothing is left to retry with.

Make sign-out un-triggerable from another site

Sign-out changes state, so it must be a POST. A GET that signs you out can be fired by any page that includes an image, link or prefetch to it. Return a 405 for GET, and send the sign-out control as a form post.

POST alone is not CSRF protection. A cross-site form can still post to a cookie-authenticated endpoint if the cookie is sent. So also:

  • Set the session cookie SameSite=Lax or stricter, never None.
  • Validate the request’s Origin (or Sec-Fetch-Site) on every sign-out entry point that accepts the cookie, and reject cross-site requests.
  • Do not treat a same-site sibling subdomain as trusted unless you control it.

There are two sign-out entry points worth having, with the same teardown:

  • Browser: clears cookies and ends the provider session by redirecting.
  • Programmatic: for native apps and command-line tools, which read the response themselves. It does the same revocation and returns a plain success, without redirecting.

The cross-origin hop and form-action

A server redirect from a sign-out form to the provider’s end-session address is a cross-origin hop inside a form submission. Browsers enforce a Content Security Policy form-action rule against the whole redirect chain, so a redirect that leaves your allowed origins can be silently blocked. The user lands home, looking signed out, while the provider session is still alive.

Have the sign-out control post with fetch, ask for JSON, receive the address in the response, and then navigate to it as an ordinary top-level navigation. A navigation is not governed by form-action. Keep the plain redirect as the fallback for browsers with scripting off.

Defence in depth: ask for credentials next time

Set a short-lived, single-use marker cookie at sign-out. The next sign-in initiator reads it, clears it, and asks the provider to prompt for login. If the end-session hop was skipped or failed, the user still gets a real login screen instead of being silently signed back in as the account they just left.

Rotating the signing secret

The key that signs the session cookie can be reissued at any time. Everyone is signed out: their next request fails verification and they are sent to sign in.

This is safe only because of the design above. The cookie is the only thing the key signs, and no stored state depends on its value. The refresh tokens live server-side and are keyed by session id, not by the secret. So rotation signs users out of your app, and does not by itself revoke the sessions or refresh chains at the provider. If the reason you are rotating is a suspected compromise, rotate and also revoke at the provider, or the old sessions keep working anywhere a token already left the building.

After rotating, confirm that sign-in works, not just that the old cookies stopped. A deployment that was not redeployed with the new secret can fail every sign-in with a server error until it is.

Failure modes

What you seeUsual causeWhat to do
The user signs out and signing back in does not ask for credentialsThe provider session was never ended. The cookie was cleared and nothing else.Add the end-session hop, and the prompt-login marker as a backstop.
Sign-out looks successful but the user is still signed in at the providerA server redirect off a form submission was blocked by form-action.Post with fetch, then navigate to the returned address.
A signed-out user’s token still works for a whileThe upstream revoke did not run or did not land, or the API validates tokens locally and does not check revocation.Revoke the session and the refresh family, retry pending revokes, and bound access tokens by a short lifetime or an API check.
A captured refresh token works after sign-out and the provider recoveredThe failed revoke was dropped along with the stored token, so nothing retried.Keep an expiring revoke-pending entry and alert on any that stay pending.
Another site signs people outSign-out is POST only but accepts any cross-site request with the cookie.Use SameSite=Lax, validate Origin, and test a cross-origin POST.
A session looks signed in but every API call failsThe cookie outlived the refresh token.Extend the cookie only when the refresh entry is renewed, keep the refresh lifetime longer than the cookie window, and test activity between renewals.
Sign-out hangs and the user stays signed inA stalled provider call had no timeout, so the cookie clear never ran.Bound each call and the whole teardown, and clear local state when the budget ends.
Spurious sign-outs under loadConcurrent renewals presented a spent, rotated refresh token and the provider revoked the chain as replay.Let one renewal win and have racers reuse its result.
Everyone signed out during a provider blip or a burst of loadRetryable errors (5xx, network failure, 429) were treated like a dead refresh token.Clear the session only on a terminal token error such as invalid_grant. Keep it, and back off, on everything retryable.
Signing out on one device signs out all of themThe revoke targeted every session for the account.Revoke only the current session. Offer sign-out everywhere as its own action.
A link or image somewhere signs people outSign-out accepts GET.Make it POST-only, then add the origin checks above.
After a secret rotation, sign-in returns a server errorThe deployment still holds the old secret.Redeploy so the new secret is bound, then retry.

Verify it is healthy

  • After sign-out, the refresh token from that session cannot be exchanged, and the whole rotation chain is dead, not just the token you presented.
  • After sign-out, the access token from that session is rejected by the API, or you know exactly how long it can still be used.
  • No revoke-pending entry has stayed pending past its alert threshold.
  • The provider no longer lists the session as active.
  • Signing in again shows a real login prompt.
  • A second device that was signed in before is still signed in.
  • A GET to the sign-out address returns 405, and a cross-origin POST is rejected.
  • The cookie is extended only when the refresh entry is renewed, and a test covers activity between renewals.
  • Sign-out completes locally within its budget when the provider is stalled.
Contributors
  • Stephen Way