DO SQLite: Connecting… PKCE: WebCrypto S256 Identity: Unsigned DCR

Track 9 · Agent Bridges & Discovery · Cloudflare Worker + SQLite Durable Object

MCP OAuth 2.1 Handshake Lab

Connecting an AI agent to remote Model Context Protocol (MCP) tools should not require copying static API keys into config files. This lab runs the full OAuth 2.1 + PKCE (RFC 7636) handshake live—covering RFC 9728 Protected Resource Metadata, RFC 8414 discovery, Dynamic Client Registration (DCR) vs. Client ID Metadata Documents (CIMD), RFC 8707 audience binding, and SQLite refresh-token family revocation.

Why this matters

The MCP authorization flow chains six specifications together. Missing one check—such as failing to bind tokens to /mcp/docs vs. /mcp/billing, trusting unsigned DCR client names, or skipping refresh-token replay revocation—quietly turns into cross-server token replay or spoofed consent prompts.

What this opens up

Standard browser OAuth 2.1 + PKCE delegation for remote MCP tools: users approve scoped access to a specific server with one click, zero copied API keys, zero server-side client-registration bloat, and automatic token-family kill-switches if a refresh token is stolen.

How the tests work

Click Run Full 6-Step Handshake below (or step through 1–6), switch between Unsigned DCR, Signed DCR, and Verified CIMD, then fire all 5 Red-Team Attacks and inspect the live HTTP wire trace and SQLite ledger.

1. 6-Step Interactive MCP OAuth 2.1 Runner

Run every real OAuth 2.1 + WebCrypto S256 PKCE hop against the Cloudflare Worker and SQLite ledger.

Client Identity Mode
Session DO —
Active client_id Not registered
PKCE S256 Challenge Not generated
Token Family & Gen None
Select an identity mode and click "Run Full 6-Step Handshake" or execute Step 1 below.
Step 1 Idle

Unauthenticated MCP Challenge

Sends POST /mcp/docs without a Bearer header to trigger 401 + WWW-Authenticate: Bearer resource_metadata="...".

Step 2 Idle

RFC 9728 + RFC 8414 Discovery

Fetches /.well-known/oauth-protected-resource/* then /.well-known/oauth-authorization-server to verify S256 and CIMD support.

Step 3 Idle

Client Identity (DCR or CIMD)

Calls POST /oauth/register for stateless c_/cs_ IDs, or fetches and verifies /cimd/valid-client.json against its HTTPS URL.

Step 4 Idle

PKCE S256 & Consent Code

Computes browser WebCrypto SHA-256 challenge, inspects GET /oauth/authorize (frame-ancestors 'none'), and submits approval for a single-use code.

Step 5 Idle

Token Exchange + RFC 8707 Bind

Posts code, code_verifier, and resource to POST /oauth/token; DO burns code and mints a resource-bound token family.

Step 6 Idle

Authorized MCP Tool Call

Invokes MCP JSON-RPC tools/call on the bound resource with Authorization: Bearer ... and verifies 200 OK.

2. 5 Live Red-Team Attacks

Trigger real OAuth replay, bad-PKCE, cross-resource, DNS-rebinding, and client-spoofing attacks against the Worker and SQLite ledger.

Ready

Red-Team Execution Proof

Run any of the 5 red-team attacks above to inspect live fail-closed responses and SQLite Durable Object state transitions.

Attack #4 Live Opaque-Origin Probe (<iframe sandbox="allow-scripts" src="/probe/rebind-frame">)

Live HTTP Wire Trace

Click any recorded request to inspect exact headers, status codes, CSP, and JSON payloads.

0 exchanges
Wire trace details appear here as soon as you run a handshake step or attack.

OAuthLedger SQLite Durable Object

Live snapshot from GET /api/ledger/state (hashes only; raw tokens are never stored).

Active Families0
Revoked Families0
Burned Codes0
Revoked Tokens0

Token Families & Refresh Generation State

Family Mode Resource Status / Reason

Auth Code Burn Log (Burn-Before-Check)

SHA-256 Prefix Mode Outcome

Recent Security Audit Events

Event HTTP Detail
Key terms & further reading
MCP Authorization (2025-11-25) ↗
Model Context Protocol specification defining how HTTP MCP clients discover authorization servers, prove identity, and call scoped tools.
RFC 9728 Protected Resource Metadata ↗
Standard /.well-known/oauth-protected-resource document returned via WWW-Authenticate so clients find the right OAuth server per path.
RFC 8414 Authorization Server Metadata ↗
Discovery document listing authorize, token, and registration endpoints plus supported PKCE methods like S256.
OAuth 2.1 + PKCE (RFC 7636) ↗
Proof Key for Code Exchange using a SHA-256 challenge so an intercepted authorization code cannot be redeemed without the original verifier.
RFC 8707 Resource Indicators ↗
OAuth extension binding access tokens to one target audience URL so a /mcp/docs token is rejected on /mcp/billing.
DCR (RFC 7591) & CIMD ↗
Dynamic Client Registration lets agents register on the fly, whereas Client ID Metadata Documents tie client_id directly to a verifiable HTTPS JSON document.
Architecture MCP OAuth 2.1 Discovery, Identity Verification, PKCE S256 & SQLite Family Revocation
Real Cloudflare Worker + SQLite Durable Object Protocol Topology
Client + Red-Team

Browser Runner & Opaque Probe

WebCrypto S256 PKCE + fetch()
  • Generates 32-byte code_verifier and SHA-256 base64url challenge locally
  • Switches between Unsigned DCR (c_), Signed DCR (cs_), and HTTPS CIMD URLs
  • Opaque <iframe sandbox="allow-scripts"> sends real Origin: null POST to test DNS-rebinding defense
Edge Gate

Worker OAuth 2.1 + MCP src/router.ts

  1. /.well-known/oauth-protected-resource/* (RFC 9728) & /.well-known/oauth-authorization-server (RFC 8414)
  2. POST /oauth/register (stateless DCR) & /cimd/*.json URL-match verification
  3. GET/POST /oauth/authorize with frame-ancestors 'none' consent CSP
  4. POST /mcp/* validates Origin/Host (403) before Bearer + RFC 8707 audience check (401)

Zero static registration table rows written for DCR; every token stored only as a SHA-256 digest.

SQLite DO

OAuthLedger Durable Object

auth_codes · token_families · refresh_tokens · access_tokens
Burn-Before-Check S256 PKCE RFC 8707 aud
400 / 401 / 403

Fail-Closed Revocation & Gates

REFRESH_TOKEN_REUSE_FAMILY_REVOKED

Spent refresh replay kills the entire token family; bad PKCE burns the code; cross-resource tokens fail with 401; foreign Origin/Host fails with 403.

Every OAuth 2.1 endpoint and MCP resource runs inside src/worker.ts backed by the OAuthLedger SQLite Durable Object. Plaintext authorization codes, access tokens, and refresh tokens are hashed with SHA-256 before persistence.
How This Is Set Up

Unsigned Stateless DCR vs. Origin-Bound CIMD & SQLite Family Revocation

Adapted from ExtensibleWebApps/src/server/auth/mcp-oauth.ts and extended with MCP 2025-11-25 Client ID Metadata Documents (CIMD), SQLite Durable Object family revocation, and DNS-rebinding Origin/Host enforcement.

Unsigned stateless DCR (consent-name spoof risk)
// Any caller can forge c_<b64url> offline without calling /oauth/register:
const forged = "c_" + b64url(JSON.stringify({
  n: "Trusted Enterprise Agent",
  r: ["https://attacker.example/callback"]
}));
// Zero DB rows stored, but client_name is unverified on /oauth/authorize!
HTTPS CIMD + Burn-Before-Check + Family Kill-Switch
// 1. CIMD enforces doc.client_id === fetched HTTPS URL:
if (doc.client_id !== clientId) throw new Error("CIMD_METADATA_MISMATCH");

// 2. Burn auth code in SQLite BEFORE verifying PKCE S256:
sql.exec("UPDATE auth_codes SET consumed_at = ? WHERE code_hash = ?", now, hash);

// 3. Spent refresh replay revokes entire token_families lineage:
if (row.spent_at !== null) {
  sql.exec("UPDATE access_tokens SET revoked_at = ? WHERE family_id = ?", now, fam);
}