Skip to content

fix(mcp): prevent auth failures during concurrent token refresh - #4441

Open
benjaminshafii wants to merge 6 commits into
devfrom
fix/codex-mcp-auth
Open

benjaminshafii wants to merge 6 commits into
devfrom
fix/codex-mcp-auth

Conversation

@benjaminshafii

@benjaminshafii benjaminshafii commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Codex can successfully initialize OpenWork MCP and then report Auth required when overlapping workers refresh the same credential. The investigated client log contained invalid_grant: invalid refresh token; it does not establish the exact cause of that historical rejection. A new HTTP journey reproduced a concrete race in the pinned OAuth provider: four concurrent refreshes returned [400, 400, 200, 400] before the patch.

The provider revokes the original token before persisting the winning response. This patch lets both the atomic-update loser and a reader of that intermediate state briefly poll for the same encrypted replay response. It preserves the existing client, scope, resource, confirmation-binding, and grace checks and never mints a second successor. Polling is capped at 40 attempts with 25 ms between attempts; failures remain closed. The dependency patch needs review on the next Better Auth upgrade.

Validation: pnpm evals:pr specs/mcp-oauth-concurrent-refresh.test.ts (local API + isolated MySQL database; no reused Den). Covers PKCE sign-in/consent, three rounds of four concurrent refreshes, one shared successor per round, MCP initialization, rejection of another client/broader scopes/mismatched replay/foreign resource, and stale replay with family revocation after 31 seconds. Test evidence is published below. Spec boundary and channel ratchets also passed. The general CI lane explicitly skips this database journey when MySQL/Redis are absent; a dedicated Linux step provisions both services and requires the regression to execute. Missing services in that step fail the job.

The investigation and recovery guidance are in docs/mcp-oauth-refresh-race.md. Already expired or revoked credentials still require reauthorization; this does not claim to restore them or prove that the reproduced race caused the historical incident.

CI also exposed a shutdown-order assertion in the server unit suite: retirement marks a generation dead before the process exits. The test now waits (bounded to five seconds) for process exit, preserving the original assertion.

@vercel

vercel Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

4 Skipped Deployments
Project Deployment Actions Updated
openwork-app Skipped Skipped Sep 5, 2026 7:55pm UTC
openwork-den Skipped Skipped Sep 5, 2026 7:55pm UTC
openwork-diagnostics Skipped Skipped Sep 5, 2026 7:55pm UTC
openwork-landing Skipped Skipped Sep 5, 2026 7:55pm UTC

diff-warden[bot]
diff-warden Bot previously approved these changes Sep 4, 2026

@diff-warden diff-warden Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warden security clearance: clear. No blocking findings (security or desktop↔den sync) in this diff (33161db84f26a23ffe48940d0cc0991fc4efe3bd). Automated clearance satisfies the required-review gate only — a human still reviews and merges. Analysis run

@benjaminshafii
benjaminshafii enabled auto-merge (squash) September 4, 2026 20:31
@vercel
vercel Bot temporarily deployed to Preview – openwork-landing September 4, 2026 20:37 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-diagnostics September 4, 2026 20:37 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-den September 4, 2026 20:37 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-app September 4, 2026 20:37 Inactive
@benjaminshafii

Copy link
Copy Markdown
Member Author

@OmarMcAdam Please review the Better Auth dependency patch, especially refresh-token replay binding, revocation, grace limits, and polling. CI passes, but this changes the authentication boundary; the branch also currently has a merge conflict.

Holding for your review under the current merge instructions.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Deploying openwork with  Cloudflare Pages  Cloudflare Pages

Latest commit: af3dd12
Status: ✅  Deploy successful!
Preview URL: https://446db974.openwork.pages.dev
Branch Preview URL: https://fix-codex-mcp-auth.openwork.pages.dev

View logs

@benjaminshafii

Copy link
Copy Markdown
Member Author

Test evidence — overlapping-mcp-refreshes-reuse-one-successor-without-granting-another-client-or — ✅ passed

✅ passed · 6 expectations passed · 0 failed · 0 pending

verdict passed · 0 user observations · 0 probes · steps 0/0

ℹ️ ASSERTION — 1. Concurrent refresh round 1 succeeds

HTTP statuses: 200, 200, 200, 200

  • ✅ PASS Concurrent refresh round 1 succeeds — HTTP statuses: 200, 200, 200, 200

ℹ️ ASSERTION — 2. Concurrent refresh round 2 succeeds

HTTP statuses: 200, 200, 200, 200

  • ✅ PASS Concurrent refresh round 2 succeeds — HTTP statuses: 200, 200, 200, 200

ℹ️ ASSERTION — 3. Concurrent refresh round 3 succeeds

HTTP statuses: 200, 200, 200, 200

  • ✅ PASS Concurrent refresh round 3 succeeds — HTTP statuses: 200, 200, 200, 200

ℹ️ ASSERTION — 4. Replay remains usable later within the grace interval

After 20 seconds, the same refresh request returns HTTP 200 and the identical access and refresh tokens.

  • ✅ PASS Replay remains usable later within the grace interval — After 20 seconds, the same refresh request returns HTTP 200 and the identical access and refresh tokens.

ℹ️ ASSERTION — 5. Replay remains bound to the original request and grace interval

Narrower-scope replay and a foreign resource returned 400. After 31 seconds, stale replay and its revoked successor both returned invalid_grant.

  • ✅ PASS Replay remains bound to the original request and grace interval — Narrower-scope replay and a foreign resource returned 400. After 31 seconds, stale replay and its revoked successor both returned invalid_grant.

ℹ️ ASSERTION — 6. One successor remains usable and replay cannot cross clients or expand scopes

All overlapping responses shared one successor; wrong-client and wider-scope attempts returned 400; the final successor refreshed and initialized MCP with HTTP 200.

  • ✅ PASS One successor remains usable and replay cannot cross clients or expand scopes — All overlapping responses shared one successor; wrong-client and wider-scope attempts returned 400; the final successor refreshed and initialized MCP with HTTP 200.

Test run created 2026-09-04T22:04:30.158Z · Source: evals/results/test-runs/2026-09-04T22-04-30-157Z-overlapping-mcp-refreshes-reuse-one-successor-without-granting-another-client-or/test-run.json · Repro: pnpm --dir evals artifacts:publish -- --pr 4441 --test-run 2026-09-04T22-04-30-157Z-overlapping-mcp-refreshes-reuse-one-successor-without-granting-another-client-or

@vercel
vercel Bot temporarily deployed to Preview – openwork-app September 5, 2026 19:55 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-den September 5, 2026 19:55 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-diagnostics September 5, 2026 19:55 Inactive
@vercel
vercel Bot temporarily deployed to Preview – openwork-landing September 5, 2026 19:55 Inactive

This branch was previously deployed

4 inactive deployments
Preview – openwork-landing — af3dd123 Deployed Sep 5, 2026 by vercel[bot]
Preview – openwork-app — af3dd123 Deployed Sep 5, 2026 by vercel[bot]
Preview – openwork-den — af3dd123 Deployed Sep 5, 2026 by vercel[bot]
Preview – openwork-diagnostics — af3dd123 Deployed Sep 5, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant