Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 8 min read

OAuth sign-in with a BFF: redirects and PKCE explained

In the previous article we saw how CSRF works and how OAuth's state parameter stops an attacker from signing you into their account. But state is not the only thing that can go wrong in an OAuth sign-in. This article cov

In the previous article we saw how CSRF works and how OAuth's state parameter stops an attacker from signing you into their account. But state is not the only thing that can go wrong in an OAuth sign-in. This article covers the next one: a stolen authorization code, and the tool that stops it, PKCE, with its verifier and challenge. Along the way we'll answer two questions that confuse almost everyone the first time: what a 302 really does, and why the server can't simply call the identity provider itself.

This time the app has a BFF (backend for frontend): a small server that sits between the browser and everything else. A BFF is not required for the sign-in to work: a single-page app can run the whole sign-in by itself, with the same state and PKCE. But it makes it safer: the secrets stay on the server, the browser only gets an HttpOnly session cookie holding a random id, and as a server the BFF can hold a client secret, which a browser can't. Why keeping tokens away from JavaScript matters is the subject of a later article. In OAuth terms the BFF is the client.

Words used here

"Code" and "token" mean many things in this area, so here is exactly what each word means in this article:

  • Authorization code: a one-time ticket the identity provider gives after you log in. It is not a token and grants nothing by itself; it can only be exchanged for tokens, once, within a few minutes (RFC 6749 recommends at most 10, ยง4.1.2). Whenever this article says "code" alone, it means this.
  • Access token: a short-lived key (usually minutes to an hour) that lets whoever holds it call the API in your name, limited to what you allowed. The API checks it on every request and doesn't care who presents it, which is why it must not leak.
  • Not covered here: the code the provider may email or text you to log in. That is part of how you log in at the provider and has nothing to do with OAuth.

What a 302 is

A server can't move your browser anywhere. It can only answer the request the browser sent. When it wants the browser to go somewhere else, it answers with status 302 Found and a Location header holding the new URL (RFC 9110, ยง15.4.3). The browser reads that answer and immediately sends a new request to that URL, on its own, without the user doing anything. That is all a "redirect" is: an answer that says "ask over there instead".

So in an OAuth sign-in, when we say "the BFF sends you to the identity provider", what really happens is: the browser asks the BFF, the BFF answers 302 with the provider's URL, and the browser goes there by itself. The BFF never talks to the provider in that step.

Why the BFF can't just call the identity provider

If the BFF is a server and the identity provider is a server, why not have them talk directly and skip the browser?

Because the whole point of OAuth is that your credentials go only to the identity provider. You type your password (or a one-time login code, or use a passkey) on the provider's own page, in your own browser. The app never sees them (RFC 6749, ยง1). The provider also needs your browser for things only it can do: show its login page, ask for a second factor, ask for your consent, and set its own session cookie so you don't have to log in again on the next app (single sign-on).

There is an old way that skips the browser: the app collects your username and password and sends them to the provider's token endpoint (the "resource owner password credentials" grant). It hands your password to the app, so the OAuth Security Best Current Practice says it must not be used (RFC 9700, ยง2.4).

So the sign-in always has two kinds of trips:

  • Front channel: through the browser, with redirects. Anything here travels in URLs, so it can end up in browser history, server logs, or a malicious extension.
  • Back channel: a direct server-to-server call from the BFF to the provider. The browser doesn't see it.

Keep that split in mind: PKCE is built on it.

The flow, step by step

  1. The browser asks the BFF to sign in: GET /auth/signin. The request carries nothing special.
  2. The BFF creates two random values: state (from the previous article) and a PKCE code_verifier. It computes code_challenge = SHA-256(code_verifier), keeps state and the verifier in server memory, and sets a short-lived cookie in the browser so it can find them again later.
  3. The BFF answers 302 to the provider's /authorize URL, with state and code_challenge in it. The verifier is not in the URL.
  4. The browser follows the redirect. You log in on the provider's page. The provider remembers the code_challenge, and redirects the browser back to the BFF's callback with a one-time authorization code: /callback?code=ABC&state=โ€ฆ.
  5. The BFF checks state, then calls the provider's token endpoint directly (back channel) with the authorization code, the verifier and its client secret.
  6. The provider checks everything and answers with the tokens. The BFF stores them on the server and gives the browser its session cookie.

Steps 3 and 4 are the front channel; step 5 is the back channel. Now let's see what PKCE is for.

A stolen authorization code โ†’ PKCE

The authorization code travels in a URL (step 4), so it can leak: browser history, a proxy log, a malicious browser extension, a Referer header. PKCE (pronounced "pixy", RFC 7636) was created for this "authorization code interception attack" (ยง1). The name says what it does: Proof Key for Code Exchange. When the authorization code is exchanged for tokens, the client must show a proof key, a secret that only the one who started that sign-in has.

How it works:

  • The verifier is a long random string the client creates for each sign-in (43 to 128 characters, ยง4.1). It's a secret, and it stays with the client: with a BFF, in the server's memory, so it never reaches the browser; without a BFF, the app in the browser keeps it (in sessionStorage) and sends it to the token endpoint itself.
  • The challenge is the verifier's SHA-256 hash, base64url-encoded (ยง4.2). It goes to the provider in the front channel, in the /authorize URL (ยง4.3).
  • When the client exchanges the authorization code, it sends the verifier in the back channel (ยง4.5). The provider hashes it and compares the result with the challenge it received earlier (ยง4.6). No match, no tokens.

Why this is safe:

The challenge travels in the front channel, in the /authorize URL the browser opens, so others may see it. But a hash can't be reversed, so seeing it doesn't reveal the verifier, and the verifier never travels in the front channel at all. Someone who steals the authorization code from a URL has the authorization code, maybe the challenge, but not the verifier. Without a BFF, an attacker with access to the browser itself (such as a malicious extension) could read the verifier from sessionStorage; with a BFF, the verifier never reaches the browser, so not even that works.

What about the BFF's client secret? It stops a stranger from calling the token endpoint, but it doesn't stop authorization code injection (RFC 9700, ยง4.5), an attack that goes through the BFF. The key detail: when the provider issues an authorization code, it stores the challenge it received in that same sign-in together with the code (RFC 7636, ยง4.4). Each authorization code is tied to one challenge. Now follow both sign-ins in time:

  1. Your sign-in. The BFF creates verifier V1, and the browser takes challenge C1 = hash(V1) to the provider. You log in. The provider issues your authorization code and records "this code belongs to C1".
  2. The attacker steals your authorization code.
  3. The attacker's sign-in, on the same app, in their own browser. The BFF creates verifier V2, and challenge C2 = hash(V2) goes to the provider.
  4. When the provider redirects the attacker back to the BFF, they replace the authorization code in the callback URL with yours.
  5. The BFF now exchanges your authorization code together with V2, the verifier of the attacker's sign-in.
  6. The provider hashes V2 and gets C2, but your authorization code belongs to C1. No match, no tokens.

Without PKCE there is no challenge to compare: in step 5 the BFF would exchange your authorization code with its own secret, and the attacker would be signed in as you.

Why state doesn't catch this. It looks a lot like the login CSRF (Cross-Site Request Forgery) from the previous article, which state did stop. The difference, in two lines:

  • Login CSRF: the attacker starts a sign-in in their own browser, logs in with their account, keeps the callback URL with their authorization code, and gets your browser to open it (through a link, an image or a hidden iframe on another site). You end up inside their account.
  • Code injection: the attacker steals your authorization code, starts a sign-in in their own browser, and at the callback swaps in your stolen authorization code. They end up inside your account.

state asks one question: did this browser start this sign-in? In login CSRF the answer is no (your browser, their sign-in), so it fails. In code injection the answer is yes (their browser, their sign-in, their valid state), so it passes. PKCE asks a different question, does this authorization code belong to this sign-in?, and in code injection the answer is no.

In neither attack does the attacker handle a token: the app exchanges the authorization code itself. (RFC 6749 ยง10.12 also mentions injecting an "access token": that comes from the old implicit flow, which put the access token straight in the redirect URL. RFC 9700 advises against it (ยง2.1.2) and OAuth 2.1 drops it.)

That's why RFC 9700 recommends PKCE for every client, servers included (ยง2.1.1).

What's next

PKCE and state are not the whole story. Two more things can go wrong in this flow: a replayed ID token, stopped by the nonce, and an authorization code sent to the wrong place, stopped by exact redirect URIs. That's the next article.

References

  • RFC 6749, The OAuth 2.0 Authorization Framework, ยง1, ยง4.1.2.
  • RFC 7636, Proof Key for Code Exchange by OAuth Public Clients, ยง1, ยง4.1โ€“4.6.
  • RFC 9110, HTTP Semantics, ยง15.4.3 (302 Found).
  • RFC 9700, Best Current Practice for OAuth 2.0 Security, ยง2.1.1, ยง2.4, ยง4.5.
  • IETF draft OAuth 2.0 for Browser-Based Applications (the BFF pattern).
๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.