Skip to content

Fix malformed API token auth handling - #393

Merged
mattzcarey merged 1 commit into
mainfrom
fix/api-token-error-handling
Jul 29, 2026
Merged

Fix malformed API token auth handling#393
mattzcarey merged 1 commit into
mainfrom
fix/api-token-error-handling

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Convert expected direct Cloudflare credential validation failures into structured OAuth resource responses instead of uncaught Worker exceptions.
  • Use credential ownership prefixes as optimization hints before probing identity:
    • cfat_: only query /accounts and require exactly one account.
    • cfut_ and Wrangler OAuth cfoat_: query /user and /accounts in parallel and require user identity.
    • unprefixed/legacy credentials: retain response-based inference for backward compatibility.
  • Treat the Cloudflare OAuth credential obtained during this server's authorization flow explicitly as user-owned.
  • Map malformed-token 400 responses to 401 invalid_token and return RFC 9728 WWW-Authenticate metadata with no-store headers.
  • Return identity-probe rate limits as structured 429 temporarily_unavailable responses and preserve upstream Retry-After (default 30 seconds).
  • Never infer account-token ownership from a transient /user failure; any simultaneous 5xx continues to take precedence as 502.
  • Add exported-worker, HTTP-router, call-count, Wrangler OAuth, and mixed-status regression coverage.

Root cause

The direct credential route inferred ownership entirely from /user and /accounts responses. Expected validation errors escaped as McpError, so malformed values could become Worker exception responses. It also sent both identity requests for account-owned credentials even though current account-token formats encode their owner.

The MCP OAuth token and the upstream Cloudflare credential are different layers: workers-oauth-provider validates its own token and restores the already-verified Cloudflare user context. Direct Cloudflare API and Wrangler OAuth credentials continue through the explicit credential-validation branch.

Validation

  • pnpm check:deps
  • pnpm check:turbo
  • pnpm check:format
  • pnpm test — 283 tests
  • pnpm turbo deploy -- -e staging --dry-run — 17 Workers
  • Local GraphQL Worker + current wrangler auth token --json OAuth credential — authenticated initialize 200
  • Production GraphQL endpoint + current Wrangler OAuth credential — authenticated initialize 200

@mattzcarey
mattzcarey force-pushed the fix/api-token-error-handling branch from ee9fa2e to d24d050 Compare June 8, 2026 17:38
@mattzcarey
mattzcarey force-pushed the fix/api-token-error-handling branch 2 times, most recently from d029b8a to f868b2d Compare July 29, 2026 12:47
Return structured OAuth resource errors for expected direct-token validation failures so malformed bearer values do not escape as Worker exceptions.

Use documented credential ownership prefixes to avoid unnecessary identity probes while retaining Wrangler OAuth and legacy token compatibility. Preserve Retry-After for rate-limited identity probes.
@mattzcarey
mattzcarey force-pushed the fix/api-token-error-handling branch from f868b2d to d1d3d66 Compare July 29, 2026 13:08
@mattzcarey
mattzcarey merged commit 4e1e6ab into main Jul 29, 2026
6 checks passed
@mattzcarey
mattzcarey deleted the fix/api-token-error-handling branch July 29, 2026 13:25
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