Skip to content

fix(mcp): a revoked OAuth token stops working within a minute - #128

Merged
Ooscaar merged 1 commit into
developfrom
t3/oauth-revocation-cache-ttl
Nov 24, 2026
Merged

Ooscaar merged 1 commit into
developfrom
t3/oauth-revocation-cache-ttl

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 24, 2026

Copy link
Copy Markdown
Member

Why

ZernioTokenVerifier caches a confirmed bearer for 600s. Now that zernio.com has an RFC 7009 revocation endpoint (Schedule-Posts-API #10347, advertised as revocation_endpoint in the live AS metadata), a revoked OAuth access token still worked on /mcp and /chatgpt for up to 10 minutes.

What

  • OAuth access tokens (prefix late_at_, from app/api/oauth/token/route.ts generateToken("late_at_")) are re-verified after _OAUTH_FRESH_TTL_SECONDS = 60.
  • API keys keep _FRESH_TTL_SECONDS = 600.
  • The 1-hour outage grace, in-flight de-duplication, negative handling (401/403 evicts, never cached) and the per-call resolved_by / waited_ms claims (logged as auth / auth_wait_ms on mcp_tool_call) are unchanged.
  • Audience check and tool filtering are not touched.

The cache is per process and production runs 4 workers (worker field in Axiom), so the bound is still 60s: each worker re-asks on its own.

Also worth knowing: every tool call already sends the bearer to the API, and _ApiTransport evicts the cache entry on a 401. So during the 60s window a revoked token could not read or write data. It could still reach the surfaces that don't call the API (initialize, tools/list, docs search).

Extra load on /v1/auth/verify

The repo does have telemetry for this: the zernio-mcp Axiom dataset gets one mcp_tool_call per tool call with auth (cache|upstream|joined|grace) and auth_wait_ms. Last 7 days (2026-11-17 to 2026-11-24):

value
tool calls 6.00M
verify calls caused by tool calls (upstream + joined) 796k (about 1.3/s)
upstream verify wait p50 / p95 / p99 117 / 341 / 1189 ms
grace grants 1

Caveat: token_scope is empty on every event, so the telemetry can't split OAuth from API keys. I estimated the upper bound by counting distinct (caller, worker, time bucket) groups:

  • Worst case (every token is OAuth): 1.68M distinct 60s buckets vs 0.99M distinct 600s buckets. Fixed buckets overcount the real 600s verifies (796k) by about 24%. Corrected, that is roughly 1.35M/week (about 2.2/s), or 1.7x today. That is well short of 10x, because most callers don't call continuously.
  • Likely OAuth clients only (Claude-User, Cursor, openai-mcp): their verifies go from 520k to at most about 1.0M/week, about +0.8/s on average.

Verifies from initialize and tools/list are not in this telemetry, so real totals are somewhat higher. /v1/auth/verify only authenticates and returns, so the extra cost per call is small.

Tests

tests/test_mcp_auth_cache.py:

  • an OAuth token is served from cache at 50s and re-verified at 61s
  • an API key is cached at 61s and re-verified at 601s
  • a revoked OAuth token (verify answers 401) is refused on the first call after its 60s window, and its cache entry is dropped
  • an OAuth token past its 60s keeps the outage grace (timeout and 503); past 3600s it gets the 400 AuthenticationError

pytest for the auth, middleware, HTTP auth, PRM and telemetry tests: 116 passed. Full suite: 3501 passed, 14 skipped.

Local verification

Local server: MCP_PUBLIC_URL=http://127.0.0.1:8765 zernio-mcp-http, pointed at production zernio.com discovery.

  • 401 challenge, no token: Bearer resource_metadata="http://127.0.0.1:8765/.well-known/oauth-protected-resource/mcp" for /mcp, and .../chatgpt for /chatgpt.
  • 401 challenge, bad token: adds error="invalid_token" and error_description.
  • PRM documents: resource is http://127.0.0.1:8765/mcp and http://127.0.0.1:8765/chatgpt, exactly the URLs the client connected to. authorization_servers is ["https://zernio.com"], matching the live issuer byte for byte. /chatgpt advertises its narrowed scopes. The legacy root /.well-known/oauth-protected-resource returns 308 to /mcp's document.
  • Official MCP Python SDK (mcp 2.3.0, OAuthClientProvider, which by default rejects a resource mismatch per RFC 8707), run against both surfaces: 401, then PRM, then the AS metadata from zernio.com, then DCR against prod, then the authorize redirect with resource=<surface URL>, the surface's scopes, S256 and response_type=code. I stopped at the consent screen, since no human login was available.
  • Revocation timing, official SDK client with a static bearer, MCP_VERIFY_URL pointed at a local stub that answers 401 once a token is revoked. tools/list ran every 5s:
    • late_at_ token: verified at t=0, revoked at t=5.3s, still served from cache through t=55.8s, refused at t=60.9s. The refusal was a 401 with error="invalid_token" and resource_metadata.
    • sk_ key revoked at the same moment: still served at t=76s, as expected with its 600s cache.
    • The stub saw one verify per token inside the first window.

I could not test a real production OAuth token end to end, because getting one needs browser consent.

Differences vs Sentry MCP and Cloudflare MCP

Reference PRM documents, fetched live:

  • mcp.sentry.dev: {resource, authorization_servers:[self], scopes_supported, bearer_methods_supported}
  • observability.mcp.cloudflare.com: {resource, authorization_servers:[self], bearer_methods_supported}
Difference Ours Theirs Reason / verdict
Authorization server Separate origin (https://zernio.com) Same origin (each server is its own AS via workers-oauth-provider) By design: we are a pure resource server. RFC 9728 allows this, and the SDK client follows it fine.
realm in challenge None realm="OAuth" realm is optional in RFC 6750 and RFC 9728, and clients ignore it. Keep as is.
resource_name, resource_documentation in PRM Present Absent Optional RFC 9728 fields that help consent screens. Keep.
scopes_supported in PRM Present (narrowed on /chatgpt) Sentry: present. Cloudflare: absent. Needed: ChatGPT and Cursor request exactly the advertised scopes. Keep.
scope in the 401 challenge None None Same as both references. The MCP spec recommends but doesn't require it, and clients fall back to PRM scopes_supported. Could add later; out of scope here.
error_description on invalid_token Long, tells users to clear tokens and re-register Sentry and Cloudflare send error="invalid_token" with no description Comes from FastMCP's default. Harmless for spec compliance. Left alone here, since the verifier deliberately avoids it for outages already.
Root /.well-known/oauth-protected-resource 308 to the /mcp document Path-inserted documents only Kept for clients that saved the old URL.
Revocation latency ≤60s (this PR), enforced per call by the API regardless Token lookup per request in Workers KV, so edge-cached reads can lag briefly. Cloudflare also caches API-token identity for 30 days and relies on the downstream API to enforce revocation. Same model as ours: the cache only skips re-authentication, and the API rejects a revoked token on every tool call.

No changes to metadata or challenges in this PR.

🤖 Generated with Claude Code

OAuth access tokens (late_at_) are re-verified after 60s instead of 600s,
so an RFC 7009 revocation at zernio.com reaches the MCP within a minute.
API keys keep the 10-minute cache; the 1-hour outage grace and in-flight
de-duplication are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit d077193 into develop Nov 24, 2026
4 checks passed
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