part 2OAuth 2.1 from scratch·lesson 2.7 of 10·2 min read
redirect_uri, state and the HTTPS rules
The redirect_uri and state parameters guard the round trip through the browser in the OAuth 2.1 authorization code flow. This lesson covers the redirect URI rules and why OAuth 2.1 demands exact matching, the CSRF attack that state prevents, and the HTTPS rules that MCP authorization imposes.
In this lesson
Apply the redirect_uri rules and explain why matching must be exact.
Describe the attack that state prevents.
List the HTTPS rules that MCP imposes.
redirect_uri: where the code goes back to
If an attacker could get the authorization server to send the code to their URL, they would have the code. That is why:
The redirect_uri is registered in advance (at registration time, in the client metadata document…).
OAuth 2.1 requires exact string matching (no wildcards, no prefixes).
Exception: for http://127.0.0.1 / http://[::1] the port may vary, because native apps open a free port on the fly.
MCP: it must be localhost or HTTPS.
The simulator below checks a requested redirect_uri against a list of registered ones. Try an exact match, one with an added query string, a different letter case, a loopback address with a port, a look-alike "trap" domain such as app.example.com.evil.io, and plain http.
Interactive exercise: it loads in your browser.
state: binding the return to the outbound request
Picture this login CSRF attack: the attacker starts a flow with their own account, obtains a code, and gets you to visit …/callback?code=ATTACKER_CODE. If your client accepts it, your app ends up connected to the attacker's account, and whatever you save will land in their hands.
With state, the client generates a random value before redirecting and rejects any return that does not carry it. The attacker does not know your state.
HTTPS
In every MCP revision that uses OAuth: all authorization endpoints must be served over HTTPS, and redirect_uri values must be localhost or HTTPS. Tokens are bearer tokens; without TLS, anyone on the network can see them.
Summary
redirect_uri: registered in advance, exact string match, localhost or HTTPS (loopback port may vary).
state: a random value that binds the return to the outbound request (anti-CSRF); PKCE offers equivalent protection when supported.
Everything over HTTPS.
quiz · 2 questions
1. The AS has https://app.example.com/cb registered and receives https://app.example.com/cb/../evil. Under OAuth 2.1, what does it do?
OAuth 2.1 requires exact string matching. Flexible comparisons (prefixes, normalization, wildcards) have historically been a source of code theft.
2. Which attack does state mainly mitigate?
state binds the authorization response to the request that you started; an attacker cannot forge it because they do not know the random value.