iamops.dev Search
Argo on X RSS feed
ENEnglish

Knowledge Explainer · 5 min Beginners

This is for anyone new to login protocols who wonders why a phone app or a command-line tool takes an extra step. Security leads can skip to the last section.

Proof Key for Code Exchange (PKCE) explained: a stolen login code is useless

PKCE, or Proof Key for Code Exchange, makes a stolen login code useless: the app proves it started the login with a secret only it holds. How it works.

Three actors: a mobile app, the authorization server and an attacker. The attacker's stolen code is refused because it has no verifier.
TL;DRA login code that someone intercepts is useless without the secret the…

When you sign in through a website, the site does not give the app your password. It sends the app a short code, and the app trades that code for a token. The code travels through your browser, so anything on that path can read it. PKCE is the extra check that makes a copied code worthless. It is defined by the IETF, and it sits inside the OAuth login flow.

The problem: a code on the road#

A web server can keep a secret, because only the server reads its configuration. A phone app or a desktop tool cannot. Anyone who installs the app can take it apart and read whatever is inside. So the usual secret check does not protect these apps.

The attack is simple. A second app on the same phone registers for the same redirect address, catches the code as it goes past, and trades it for tokens itself. The authorization server sees a valid code and a valid request, so it issues the tokens.

Two strings: the verifier and the challenge#

PKCE adds two values that the app makes up for each login. The first is the code verifier, a random string of 43 to 128 characters that the app keeps in memory and never sends at the start. The second is the code challenge, which is the verifier run through a SHA-256 hash and encoded in a URL-safe form.

Because the hash cannot be reversed, the challenge can travel in the open. Anyone who sees it still does not know the verifier.

The two requests#

The login request carries the challenge. The token request carries the verifier. Between them, the code comes back through the browser, where an attacker could grab it.

Three actors: a mobile app, the authorization server and an attacker. The app sends a code challenge, gets a code back, and gets tokens only with the verifier. The attacker's stolen code is refused.
A stolen code meets a missing verifier. The attacker has the code but not the secret the app kept back.

A stolen code meets a missing verifier#

The attacker takes the code and sends it to the token endpoint. The attacker has no verifier, so the request has nothing to match. The server refuses it, and the attacker ends with no tokens.

The app itself does not change how it works. It simply adds one value to each of its two requests.

The check at the token endpoint#

When the server issues the code, it stores the challenge next to it. When the app sends the code and the verifier, the server hashes the verifier and compares the result with the stored challenge. Only a match gets tokens. A failed match returns an error, not a token.

The RFC also allows a method called plain, where the challenge is the verifier itself. It exists for clients that cannot compute a hash. Use S256, the hashed method, whenever the app can, because plain sends the secret in the open on the first request.

Where this is used#

PKCE is for public clients: single-page web apps, phone apps, desktop apps and command-line tools. A tool that an agent runs on a laptop falls in this group, because the laptop belongs to someone who can read the tool’s files.

A server that can keep a secret still uses a client secret. Many of those servers also add PKCE, because it costs little and closes the code-theft path.

For the flows that PKCE belongs to, read OAuth 2.0 flows explained. For what the code turns into, read access, refresh and ID tokens. A device with no browser uses a different route, covered in the OAuth device flow.

What to check#

  • The app uses S256, not plain.
  • The verifier is random, long enough and never written to a log.
  • The server rejects a token request whose verifier does not match the stored challenge.
  • The redirect address is fixed, so no other app can register the same one.

Sources#

Discuss this on X or LinkedIn

Follow on X

New posts by email

One email a week with new posts and labs. Unsubscribe anytime.

Your address is stored with Kit, which sends the emails. Details

Related research and labs

Type to search every page. Use the arrow keys to move through the results.

    Open the full search page