OAuth sign-in with a BFF: exact redirect URIs (and the nonce)
In the previous articles we saw how state stops login CSRF and how PKCE stops a stolen authorization code from being used. This one covers the last piece of the sign-in: exact redirect URIs, which stop an authorization code from being sent to the wrong place.
At the end, a word on the nonce, which you'll see in every OpenID Connect sign-in. The app is the same as before: a BFF (backend for frontend) runs the sign-in and keeps the tokens on the server.
The attacks so far
| Attack | What the attacker does | What catches it | Article |
|---|---|---|---|
| Login CSRF | Gets your browser to make the callback request with their authorization code and their state | state: your browser never saved their state first |
first |
| Authorization code injection | Makes the callback request in their browser, with their state and your stolen authorization code | PKCE: your authorization code is tied to your challenge, not theirs | second |
Words used here
- Authorization code: the one-time ticket the identity provider gives after you log in, which the BFF exchanges for tokens.
- Redirect URI: the app's address the provider sends the browser back to after the login, e.g.
https://app.com/callback. The provider adds the authorization code and the state to it, so the browser lands onhttps://app.com/callback?code=ABC&state=X.
The authorization code sent to the wrong place โ exact redirect URIs
The app tells the provider where to send the authorization code back: the redirect_uri parameter in the /authorize URL (RFC 6749, ยง4.1.1):
https://provider.com/authorize?client_id=app&redirect_uri=https://app.com/callback&state=โฆ&code_challenge=โฆ
Here the redirect URI is https://app.com/callback. The provider sends the authorization code to whatever redirect URI that URL names, as long as it matches what the app registered.
If the provider accepts loose matches, an attacker can change it:
- The app registered
https://*.app.com/callback("any subdomain of app.com"), and the provider accepts any URL that fits. - The attacker controls one subdomain: say
blog.app.comstill points to an old hosting account that someone else has since taken over (a "subdomain takeover"). - The attacker builds a sign-in link that looks normal, but with
redirect_uri=https://blog.app.com/callback, and gets you to click it. - You see the provider's real login page and log in as usual (or you're already logged in there, and nothing even shows).
- The provider sends your authorization code to
blog.app.com, which is the attacker's server.
Notice what the attacker never needed: your password. You typed it yourself, on the real page. The attacker only bent the address the result goes to.
Without a BFF, a client with no secret, they can often exchange that code for your tokens right away; with a BFF it's harder, but an authorization code should never reach a stranger in the first place. This is "insufficient redirect URI validation" (RFC 9700, ยง4.1).
The fix is simple: the app registers its exact callback URLs, and the provider compares them character by character (RFC 9700, ยง2.1). If only https://app.com/auth/callback is registered, then https://blog.app.com/callback, https://app.com/other/page, and even https://app.com/auth/callback/ with a trailing slash are all refused.
And the nonce?
If you've read about OpenID Connect, you've seen a nonce ("number used once") in the /authorize URL. It's a random value the app creates for each sign-in; the provider copies it into the ID token (the token that tells the app who logged in), and the app checks that the ID token carries the nonce of this sign-in (OpenID Connect Core, ยง3.1.2.1 and ยง3.1.3.7).
Its main job today is stopping the authorization code injection from the previous article: your stolen authorization code brings back an ID token with your sign-in's nonce, which doesn't match the attacker's sign-in, so the app throws away all the tokens.
PKCE already stops that attack, so with PKCE the nonce adds little. RFC 9700 treats the two as alternatives, not layers (ยง2.1.1).
So why does it exist? History. OpenID Connect (2014) came before PKCE (2015), and PKCE was first meant for mobile apps; web apps with a server used the nonce. The nonce also came from older flows that put the ID token straight in the redirect URL, where it could be swapped.
And there is one real difference: the app checks the nonce by itself, while PKCE only works if the provider supports and enforces it. A provider that doesn't know PKCE silently ignores the code_challenge.
Today the advice is PKCE for every client; most libraries send the nonce too, since it costs nothing.
Summary: all three attacks
| Attack | What the attacker does | What catches it | Article |
|---|---|---|---|
| Login CSRF | Gets your browser to make the callback request with their authorization code and their state | state: your browser never saved their state first |
first |
| Authorization code injection | Makes the callback request in their browser, with their state and your stolen authorization code | PKCE: your authorization code is tied to your challenge, not theirs | second |
| Authorization code sent to the wrong place | Gets the provider to send your authorization code to an address they control, through a loosely matched redirect URI | Exact redirect URIs: the provider only sends authorization codes to addresses registered character by character | this one |
References
- OpenID Connect Core 1.0, ยง3.1.2.1 and ยง3.1.3.7 (
nonce). - RFC 9700, Best Current Practice for OAuth 2.0 Security, ยง2.1, ยง2.1.1, ยง4.1.
Comments
No comments yet. Start the discussion.