Repository navigation
fix(auth): recover and clear stale session cookies on page loads - #7832
Merged
Merged
Conversation
Server-rendered pages refreshed WorkOS sessions without sharing the rotation through session_refreshes, so other copies of the cookie (the AdCP bridge copy, in-flight API requests) could no longer refresh. The page then rendered logged out while keeping the dead cookie, and every optional-auth API call from it answered 401. - Resolve page sessions in auth middleware: reuse the session cache, store refreshes, and adopt a session another request rotated. - Clear the cookie only when WorkOS terminally rejects the refresh; provider outages and unknown failures keep it. - On adcontextprotocol.org, bridge once more after clearing so the page picks up the current AgenticAdvertising.org session. - Give optionalAuth the same shared-refresh retry requireAuth has. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… outage WorkOS answers a refresh token another request already used with the same terminal invalid_grant as a dead session, and getRefreshedSession reported database failures as "nothing stored". Either could clear a valid cookie on a page load. Wait longer for a concurrent rotation to be stored, and keep the cookie when the lookup itself fails. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — server-side auth fix (shared session refreshes on page-render path, terminal-rejection cookie clearing with re-bridge on adcontextprotocol.org).
Gate checks:
- No no-auto-approve team match.
- gated_paths: false — row 2 does not apply despite review_decision being REVIEW_REQUIRED.
- high_risk: false; no deletions in sensitive paths.
- App-only change (server/), correctly carries no changeset; no protocol surface (static/schemas/source/, docs/reference/, dist/) touched.
Reviewer confirmed cookie clearing is fail-closed (terminal-only, outages/errors preserve the cookie, lost-race retries), re-bridge loop protection holds via the _bridge_checked marker + bridge-checked cookie, and clear/set cookie flags are symmetric. New test coverage validates the fixed path.
No Critical/High/Medium findings. None of rows 1–8 fire; falls through to row 9 → approve.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Server-side follow-up to #7825 (the empty Agents registry for signed-in users).
Since #7462,
optionalAuthreturns 401 for a present-but-unusablewos-sessioncookie. The server-rendered page path (getUserFromRequest) had two gaps that could leave the browser holding such a cookie indefinitely:session_refreshesso other copies of the cookie can adopt it; the page path didn't. Page navigations are often the first request after the access token expires, so the adcontextprotocol.org copy of the session (made by/auth/bridge) and in-flight API requests were left with a dead refresh token.Changes
resolvePageSession(server/src/middleware/auth.ts) replaces the page path's own WorkOS handling:sessionCacheand awaitsstoreRefreshedSessionafter a refreshretryable: false:invalid_grant,invalid_session_cookie)session_refresheslookups keep the cookieinvalid_grantas a dead session, so before clearing, the page path retries the shared lookup at 200ms and again at 800msserver/src/http.ts): after clearing, the page redirects through/auth/bridgeto pick up the current AgenticAdvertising.org session. The existing_bridge_checked=1return marker prevents a loop when that session is dead too.optionalAuthnow uses the same shared-refresh retry asrequireAuth(one retry at 200ms) through a sharedfindSharedRefreshedSessionhelper. It doesn't fall back to anonymous access, so fix(auth): hydrate primary authorization snapshots per request #7462's policy is unchanged.getRefreshedSessiontakes{ throwOnError }so the page path can tell "lookup failed" from "nothing stored". Existing callers keep their behavior.setSessionCookie, which respectsALLOW_INSECURE_COOKIES.Review notes
A security review found no auth grant, open redirect, or redirect loop. It raised one medium issue: a lost refresh race or a DB outage could clear a valid cookie. The second commit fixes it. Remaining low-severity notes, left as-is:
deadSessionCache. A cookierequireAuthjust marked dead still gets one WorkOS check on a page load (and then gets cleared or kept according to the rules above).requireAuthin that same window.Test plan
server/tests/unit/page-session.test.ts(new): no cookie, valid session, shared refresh, adopting a rotation after one retry, clearing oninvalid_grant/invalid_session_cookie, keeping the cookie on retryable/unknown/lookup failuresserver/tests/unit/cross-domain-session-bridge.test.ts: re-bridge after clear, no loop with the return marker, no bridge for AAO or non-cleared sessionsmiddleware/authorhttp.ts(78 files, 1581 tests)🤖 Generated with Claude Code
Open workspace in Conductor