Code interception and injection
An authorization code issued for Ava's photos is worth something to whoever holds it. This lesson follows a code that escapes on its way to the printer, the injection that gets the printer to redeem it for someone else, and the two checks that stop both: PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. and single use.
How a code escapes
The forged callback in Request correlation and CSRF put the attacker's code into Ava's sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier.. Turn that attack around: a code issued for Ava's photos has reached someone else, and they want to turn it into access.
All domains, codes, and credentials in these examples are fictional. Codes travel through the browser, so they pass through places the client does not control.
Another app on the same phone. The printer's phone app can receive responses at its private-use scheme, example.printer:/oauth/callback. As Redirect URIs and client metadata explained, nothing stops a second app on the same device from registering that scheme too. When the operating system hands the response to the wrong app, the attacker holds the only copy of the code, and the real app never sees it. This is code interception: capturing a code on its way to the client.
Logs and history. A load balancer or web server that records full request addresses keeps the code in a line like this one, readable by anyone with access to the logs:
2026-10-01T09:00:41Z GET /oauth/callback?code=demo-code-7&state=demo-attempt-7&iss=https%3A%2F%2Fauth.photos.example 303Browser history on a shared computer does the same, and so does a callback page that leaks its address, as Redirect URI validation described. For these leaks, the printer still receives its own copy, so the attacker is racing a client that normally redeems the code moments after it arrives.
What the attacker does next depends on the client. A code issued to the phone app, photo-printer-app, can be sent straight to the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., because a public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret. needs only its client ID there. A code issued to the printer's website is harder to use. The website is a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server., and the attacker does not have its credentials. So the attacker gets the printer to redeem the code for them.
Injecting a stolen code
The attacker holds demo-code-7, issued to photo-printer for Ava's photos during Ava's attempt. What comes next is authorization code injection: planting the stolen code in the attacker's own session at the legitimate client, so that the client redeems it with its own credentials. Followed step by step, it looks like this:
- The attacker signs in to the printer with their own account and selects Connect photo account. The printer creates a pending attempt in the attacker's session, with a fresh verifier and the state
demo-attacker-state-3, and redirects their browser to the photo service. - Instead of continuing, the attacker opens the printer's callback with Ava's code and their own state:
https://printer.example/oauth/callback
?code=demo-code-7
&state=demo-attacker-state-3
&iss=https%3A%2F%2Fauth.photos.example- The printer finds the pending attempt. It belongs to the session that delivered the callback, so the state check passes, and the issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts. is the one it expected.
- The printer sends
demo-code-7to the tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. endpoint with its own client authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. and the verifier saved in the attacker's attempt.
Look at what the token endpoint sees: the printer, correctly authenticated, presenting a code that was issued to the printer for the redirect URI it names. Client authentication cannot help, however strong the method, because the legitimate client is making the request. Without PKCE the exchange succeeds, and the printer links Ava's photos to the attacker's printer account. The attacker can browse Ava's albums in the printer's photo picker and order prints of them.
How PKCE stops both
The check that notices is the one Proof Key for Code Exchange (PKCE) described. Here is the token request from step 4. Everything in it is genuine, including the verifier, which is the right one for the attempt the printer is finishing:
POST /token HTTP/1.1
Host: auth.photos.example
Authorization: Basic cGhvdG8tcHJpbnRlcjpkZW1vLW9ubHktbm90LWEtcmVhbC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=demo-code-7
&redirect_uri=https%3A%2F%2Fprinter.example%2Foauth%2Fcallback
&code_verifier=demo_printer_verifier_for_the_attackers_attempt_5The authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. transforms the verifier and compares it with the challenge it stored with demo-code-7 when it issued the code during Ava's attempt:
Challenge stored with demo-code-7:
qd-75t-gnweZAhIl6REjxxgFnPXGwvqYcxq3vlsaJYQ
BASE64URL(SHA256(demo_printer_verifier_for_the_attackers_attempt_5)):
B3RoGEA9YcBPFmS6QCvABccuXgS2gJ__hG2_VOQ3IWUThey differ, so the server answers invalid_grant, and the printer has nothing to attach to the attacker's session. The code belongs to one attempt and the verifier to another, and PKCE is exactly the check that notices.
Interception fails for a simpler reason. The app that captured the response has the code but not the verifier, which stayed inside the printer app and never travelled through the redirect. Ava's connection fails and she tries again, but the intercepting app cannot redeem what it caught.
PKCE has one important limit. It works because the attacker cannot choose the challenge in Ava's authorization request. When they can, the codes line up again: the attacker puts the challenge from an attempt of their own into Ava's request, and a code issued to Ava then matches their verifier. That happens when a careless redirect matcher lets the attacker write her request, as in Redirect URIThe address at the client where the authorization server sends the browser back with its response, such as an authorization code. It must exactly match an address registered for that client. validation, or when a malicious server writes it on the client's behalf. The token endpoint's redirect URI check catches some of those cases, since a code issued for a different return address does not match the printer's callback, but PKCE is not a substitute for keeping the request and the response out of the attacker's hands. Nor can it help once the client itself is compromised: an attacker who holds both the code and the verifier sends a token request the server cannot tell apart from the client's own.
A code presented twice
Errors and denied access gave the rule from the printer's side: the authorization server must refuse a code presented a second time and should also revokeCancelling a credential, token, certificate, or authenticator before it would expire, so that it is no longer accepted. Whether every copy stops working at once depends on how services check it. the tokens already issued from it, which is why a retry cannot recover a lost token response. From the attacker's side, the same rule is a defense of its own.
Suppose the attacker reads demo-code-7 from the log a few seconds too late, after the printer has redeemed it, and presents it again, whether directly or by injection. The server refuses. It cannot tell which presentation was legitimate, only that a copy of the code exists outside the client that asked for it, so it also revokes what the code produced. If the attacker was faster, that takes their access away. If the printer was faster, it costs Ava one reconnection. Either way, the attacker comes away with nothing that lasts.
To make this work, the authorization server marks a code as used in the same step that issues its tokens, so two simultaneous requests cannot both succeed, and remembers which tokens came from each code. It also keeps codes short-lived. The original specification recommends a lifetime of no more than ten minutes, and many servers choose much shorter ones.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Check that PKCE refuses a code delivered to another browser's sign-inLet one of Ava's codes escape unused, deliver it to another browser's own sign-in at the Token Decoder, see PKCE refuse it, finish it in the attempt it belongs to, then present a code twice.