CSE Roadmap

Core tier · about 9 hours · 17 parts

Part 4 of 17 · 2 min read

A stolen code is worthless without the secret that never left the client

A code that leaked in a log or a shared machine still can't be redeemed by whoever found it, as long as redeeming it needs a secret only the real client ever held.

Think of a coat-check ticket that only works alongside a photo of the person who checked the coat in. Anyone who finds a dropped ticket still cannot collect the coat. They do not have the thing the ticket was paired with.

In plain words: PKCE pairs a one-time secret (the verifier) with its hash (the challenge) sent earlier in the flow, so a stolen authorization code is useless without the verifier that never left the client -- and because PKCE alone can't stop a mix-up attack, where a code meant for one authorization server is sent to a different, hostile one, the client also records the issuer before redirecting and compares it against the iss value in the response.

A worked example. Against the authorization server, a stolen code with no verifier and with a guessed verifier are both refused (invalid_grant); the same code redeemed twice is refused; a code redeemed 61 seconds after issue is refused (the example's limit is 60 seconds); a code redeemed with a different redirect address is refused. Remove PKCE entirely (no challenge required, no verifier sent) and the stolen code is exchanged for a token successfully. Separately, a response naming a different issuer is rejected, and a missing iss from a server that promised one is rejected -- with the checks switched off, the client continues and accepts the token both times.

The key lines. From code/mini-auth/oauth_client.py:

# from code/mini-auth/oauth_client.py
def check_iss(response_iss: str | None, recorded: str, metadata: dict) -> str | None:
    supported = bool(metadata.get("authorization_response_iss_parameter_supported"))
    if supported and response_iss is None:
        return "the server promised an iss parameter and did not send one"
    if response_iss is not None and response_iss != recorded:               # simple string comparison, no normalisation
        return f"iss {response_iss!r} is not the issuer {recorded!r} we sent the user to"

Read it as: the comment on the comparison line says it directly. It is plain string equality, with no normalisation. A trailing slash is treated as a genuine mismatch. Loosening the comparison is exactly what would let a mix-up attack slip through disguised as a formatting difference.

PKCE, mix-up attack

PKCE pairs a one-time secret (the verifier) with its hash (the challenge, sent earlier), so a stolen code is useless without the verifier. A mix-up attack tricks a client talking to several authorization servers into sending a code meant for an honest one to a hostile one; PKCE does not stop it, so the client also checks the iss value against the issuer it recorded before redirecting.

Example: a stolen code with a guessed verifier failing is PKCE doing its job; a forged response naming the wrong issuer being rejected is the iss check doing a different job -- confusing the two is the common mistake, since each defends against an attack the other does not touch.