Redirect URI validation
The redirect URI decides where an authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. lands. This lesson follows a code that a carelessly built authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. sends to the wrong place, the matching shortcuts that let it happen, and what the photo service and the printer each check so that Ava's codes reach only the printer.
Sending the code somewhere else
An authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. code goes wherever the authorization request asks for it, as long as the authorization server agrees. The printer writes that request, but anyone can write one. It is an ordinary URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark., and nothing stops an attacker from copying the printer's client ID into a request of their own and naming a different return address.
Redirects and authorization codes said the photo service compares the requested redirect URI with the registered one exactly, refusing even an added trailing slash. That strictness can look fussy until you see what happens to Ava Archer, who prints her albums with the printer, at a version of the photo service that compares less carefully.
All domains and codes in these examples are fictional. The attacker begins by building a link to the photo service's authorization endpoint:
https://auth.photos.example/authorize
?response_type=code
&client_id=photo-printer
&redirect_uri=https%3A%2F%2Fprinter.example.attacker.example%2Foauth%2Fcallback
&scope=photos.read
&state=demo-attacker-state-1
&code_challenge=13TUFxsJi_vyUQcynxTDC61Vw6HLxqvpa2jIzPC_fBc
&code_challenge_method=S256Everything except the redirect URI is what the printer itself would send. The challenge is one the attacker computed from a verifier of their own.
- The attacker sends Ava the link, dressed up as an offer: "Print your summer album free this week."
- Ava is already signed in at the photo service. The consent screen names Photo Printer, because the request carries the printer's client ID, and she approves.
- This careless version of the photo service checks the redirect URI with a shortcut: does it begin with
https://printer.example? It does, so the service sends Ava's browser, with a fresh code, toprinter.example.attacker.example, a name under the attacker's domain. - The attacker's server reads the code from the incoming request.
What the code is worth depends on the client it was issued to. Had the request named the printer's phone app, photo-printer-app, the attacker could redeem the code at once. A public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret. sends only its client ID to the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., and PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. offers no protection here, because the attacker wrote the request and holds the verifier behind its challenge. The printer's website is a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server., so the attacker cannot redeem the code without the printer's credentials. They can still try to make the printer redeem it for them.
A consent screen that shows the return domain gives Ava a slim chance to notice. The address begins with printer.example precisely so that it passes a glance.
How careless matching goes wrong
The prefix check in step 3 is one of several shortcuts that have let attackers through. In each row below, the printer registered only https://printer.example/oauth/callback, except where noted.
| Requested redirect URI | Shortcut that accepts it | What the shortcut missed |
|---|---|---|
https://printer.example.attacker.example/oauth/callback | Begins with https://printer.example. | The host name keeps going. This host belongs to attacker.example. |
https://printer.example/oauth/callback/../../go | Begins with the registered address. | The browser resolves each .. segment, also when written %2e%2e, and arrives at https://printer.example/go. |
https://printer.example/oauth/callback/preview | The registered address or anything below it. | A different page, built for a different purpose, receives the code. |
https://printer.example/oauth/callback?next=https://attacker.example/ | Compares the address without its query. | The callback receives an instruction the printer never registered, which a callback that follows next would obey. |
https://attacker.example\.printer.example/oauth/callback | The host, as read by the server's own parser, ends with .printer.example. | Browsers treat \ as / in web addresses, so the host is attacker.example. |
https://promo.printer.example/oauth/callback, with https://*.printer.example/oauth/callback registered | Any subdomain of printer.example. | The subdomain may no longer be under the printing company's control. |
http://printer.example:8080/oauth/callback | The port may vary and plain HTTP is allowed, rules meant for loopback addresses. | A different service on another port receives the code over an unencrypted connection. |
Begins with https://printer.example
- The registered address
- Accepted
- printer.example.attacker.example
- Accepted
- /oauth/callback/../../go
- Accepted
- PRINTER.example, upper case
- Refused
Ava's code can reach the attacker
The same sequence of characters
- The registered address
- Accepted
- printer.example.attacker.example
- Refused
- /oauth/callback/../../go
- Refused
- PRINTER.example, upper case
- Refused
Only the registered address gets a code
Refusing a harmless variant costs a developer one corrected setting.
The wildcard row needs one more idea. A subdomain takeover happens when a DNSThe Domain Name System, the internet's lookup service that turns a domain such as photos.example into the address of the computer serving it. Whoever controls a name's DNS records controls where that name points. record still points a name such as promo.printer.example at an outside hosting service the printing company has stopped using. Anyone who opens an account with that service under the old name can then serve pages at the printer's subdomain, and a wildcard registration would send them codes.
The backslash row belongs to a family of tricks that work because the validator and the browser read the same text differently. Percent-encodingWriting a character as % followed by a two-character code, such as %2F for /, so a value inside an address or form body is not mistaken for part of its structure. The receiving service decodes it before use., unusual characters, and letter case all give two parsers a chance to disagree. Exact comparison avoids the question. The requested value must be the same sequence of characters as a registered value, so even https://PRINTER.example/oauth/callback is refused, although it leads to the same place. Refusing a harmless variant costs a developer one corrected setting.
Ordinary web callback addresses are registered and compared exactly. Installed applications can use a literal loopback address, with the operating system choosing an available port, under the protocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols.'s installed-app rules. That port flexibility does not make a changed host or path equivalent. A private-use scheme has a different risk: another installed app can claim the same scheme, which is why PKCE is essential.
Leaks from the printer's own pages
Suppose the printer's site has a page at https://printer.example/go that sends the browser to whatever address appears in its to parameter, used for "continue to our partner" links. That is an open redirect, the kind of page Correlating requests and responses warned against.
If the photo service accepted any address on printer.example, the attacker could ask for the code to be returned to /go, with to pointing at attacker.example. The validation passes, because the address really is on the printer's domain, and the printer's own page then forwards the browser onward. Whether a code in the query survives the second hop depends on the redirector: some pass the remaining parameters along, and one that redirects with script can expose its full address in the Referer header if its referrer policy allows it. A response carried in the URL fragment, as the implicit grant carried access tokens, fares worse. When an HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. redirect names no fragment of its own, the browser carries the original fragment along to the new address. Exact matching closes this route, because /go is not a registered address, and current guidance goes further: clients and authorization servers must not run open redirectors at all.
Even with exact matching, the code still arrives at a page, and that page can leak it. Redirects and authorization codes asked for a callback without third-party resources, and the reasons are concrete. Any script running on the callback page can read the full address, so an analytics or advertising script there would receive every code. Requests the page makes to other sites carry a Referer header. Current browsers send only the site's origin to other sites by default, but a page can choose a policy that sends the full address. The printer's callback therefore loads nothing from other sites, sets its own referrer policy, exchanges the code, and moves the browser to a clean address straight away. A 303 See Other response is a redirect that tells the browser to load the new address with an ordinary GET request:
HTTP/1.1 303 See Other
Location: https://printer.example/photo-book
Referrer-Policy: no-referrer
Cache-Control: no-storeThe code's own properties help as well. It is single-use and short-lived, so a copy that reaches someone after the printer has redeemed it is worth very little.
Who checks what
Both sides share the work, and neither can do the other's part.
| Party | Responsibility |
|---|---|
| Authorization server | Compares the requested redirect URI with each registered value as an exact string, before showing anything or redirecting. The only exception is a loopback port for a native app. |
| Authorization server | On a mismatch or an unknown client, shows an error on its own page and never redirects, as Errors and denied access described. |
| Authorization server | Refuses redirect URIs that use plain http, apart from a native app's loopback address, and refuses wildcard registrations. A native app's private-use scheme remains allowed. |
| Authorization server | Stores the redirect URI with the code and checks the token request against it. |
| Client | Registers each exact callback, with test sites under their own registrations. |
| Client | Runs no open redirects on its domains and validates any return destination after the callback. |
| Client | Keeps the callback free of third-party content, sets a strict referrer policy, and moves to a clean address. |
| Client | Removes DNS records for services it no longer uses. |
The authorization server's check decides where a code can go, and the client's care decides whether it stays there. When both fail and a code for the printer's website escapes, the attacker still cannot redeem it without the printer's credentials, and an attempt to have the printer redeem it for them fails its PKCE check, because the code is bound to the challenge the attacker wrote, not to any attempt the printer started.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Probe exact redirect URI matching with near-miss addressesRegister one exact HTTPS callback, send authorization requests with the lesson's near-miss variants, confirm none redirects, and see the token endpoint check the address again.
Try a Capture the Flag challenge
Apply this lesson against an isolated, intentionally vulnerable application. Capture a flag, explain the mistake, then repair and retest it.