Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

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 c

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
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 on https://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:

  1. The app registered https://*.app.com/callback ("any subdomain of app.com"), and the provider accepts any URL that fits.
  2. The attacker controls one subdomain: say blog.app.com still points to an old hosting account that someone else has since taken over (a "subdomain takeover").
  3. 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.
  4. You see the provider's real login page and log in as usual (or you're already logged in there, and nothing even shows).
  5. 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
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.
πŸ“° 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.