part 2OAuth 2.1 from scratch·lesson 2.5 of 10·2 min read
PKCE: the secret that protects the code
PKCE (Proof Key for Code Exchange, RFC 7636, pronounced "pixy") is what makes a stolen authorization code useless. This lesson shows how the code_verifier and code_challenge are generated, which attack PKCE prevents, and what MCP authorization requires about PKCE in each revision.
In this lesson
Generate a code_verifier and a code_challenge.
Explain which attack PKCE prevents, and watch it fail in the lab.
State what MCP requires about PKCE in each revision.
The idea
The client invents a secret at the start (code_verifier), sends only its hash (code_challenge) on the outbound trip, and reveals the secret during the exchange. The AS checks that the hash of the secret matches. Whoever steals the code during the redirect does not have the secret, so the code is useless to them.
How it is computed
code_verifier = random string of 43 to 128 characters [A-Z a-z 0-9 - . _ ~]
code_challenge = BASE64URL( SHA256( code_verifier ) ) ← method "S256"
There is also the plain method (challenge = verifier), which offers no protection if someone sees the outbound trip; use S256.
PKCE lab: generate, send, redeem… and attack
Generate a code_verifier, send its challenge to /authorize and receive a code. Then try to redeem it twice: once as the legitimate client with the real verifier, and once as an attacker who stole the code during the redirect and has to make up a verifier. The lab computes a real SHA-256.
Interactive exercise: it loads in your browser.
PKCE in MCP
Revision
Requirement
2025-03-26
PKCE mandatory for all clients
2025-06-18
Same
2025-11-25
In addition, the client must verify that the AS metadata includes code_challenge_methods_supported; if it is missing, the client must refuse to proceed
2026-07-28
Same as 2025-11-25
Summary
Secret at the start, hash on the outbound trip, secret at the exchange.
A stolen code is useless without the verifier. Always use S256.
MCP: always mandatory; since 2025-11-25, the client verifies that the AS advertises it.
quiz · 2 questions
1. An attacker sees in the browser both the code_challenge (outbound) and the code (return). Can they redeem the code?
SHA-256 cannot be inverted. Note that with plain the challenge is the verifier, which is why S256 is the method to use.
2. In MCP 2025-11-25, the AS metadata does not include code_challenge_methods_supported. What does the client do?
The spec requires the client to refuse: it cannot assume protection that the AS does not advertise.