Repository navigation
fix(oauthserver): accept loopback redirect URIs on any port - #2871
just-some-random-pal wants to merge 2 commits into
Conversation
|
@cemalkilic could you take a look when you have a moment? It makes the OAuth server accept a loopback redirect URI on any port, as RFC 8252 section 7.3 and OAuth 2.1 require for native clients, so CLI and desktop apps that bind an ephemeral port can complete the flow. The change is confined to |
|
@just-some-random-pal What's the use case behind this in your situation? I understand the spec requires this, and happy to relax it -- but not right now. |
The authorization endpoint matched redirect_uri against the registered URIs by exact string only. OAuth 2.1 section 2.3.1 and RFC 8252 section 7.3 require an exception for loopback redirects: a native client registers http://127.0.0.1/callback and binds a different ephemeral port on every request, so the port must not take part in the comparison. Without it, spec-following native clients, including MCP clients, cannot complete authorization. Add the exception after the exact match: both URIs must use http, the registered host must be a loopback IP literal and equal the requested host, and path, query, fragment and userinfo must match exactly. localhost is excluded on purpose per RFC 8252 section 8.3. The token endpoint compares against the stored authorize-time value, so it stays consistent, and the code is delivered to the port the client bound. Closes supabase#2703
8195f0c to
12a3ca0
Compare
|
No private use case on my side. I picked this up from #2703. The concrete case is MCP clients: their Client ID Metadata Document registers a portless loopback redirect and the client binds an ephemeral port on each run, so every one of them fails at |
The loopback match compared the decoded URL.Path, so a request for /callback%2Fadmin matched a registered /callback/admin. Compare the escaped path and fragment instead so the match stays exact.
Closes #2703.
The authorization endpoint compared
redirect_urito the registered URIs by exact string match only. OAuth 2.1 section 2.3.1 and RFC 8252 section 7.3 require one exception: a native client registers anhttploopback redirect such ashttp://127.0.0.1/callbackand binds a different ephemeral port on every authorization request, so the port must be ignored when comparing loopback redirects. Without it, every native client that follows the spec, including MCP clients whose Client ID Metadata Document registers a portless loopback redirect, fails at/oauth/authorizewith an invalidredirect_uri.isValidRedirectURIkeeps the exact match as the first path and adds the loopback exception as the second: both URIs must parse, both must usehttp, the registered host must be a loopback IP literal (127.0.0.1or[::1]) and equal the requested host, and path, query, fragment and userinfo must match exactly. Only the port may differ.localhostis left out on purpose: RFC 8252 section 8.3 recommends against it because it can resolve to a non-loopback address, and the specification's exception is scoped to loopback IP literals. Registration already restrictshttpredirects to loopback hosts, so nothing new becomes registrable.The token endpoint is unchanged. It compares the exchange-time
redirect_uriwith the value stored at authorization time, which is the ported URI the client actually used, so the two stay consistent, and the success redirect is built from that same stored value, so the code is delivered to the port the client bound.Tests: two suite tests drive
/oauth/authorizewith a client registered ashttp://127.0.0.1/callback: a request withhttp://127.0.0.1:51234/callbackis accepted and the stored authorization keeps the ported URI, and one with a different path is rejected before any redirect. A table test covers exact matches, IPv4 and IPv6 loopback with a port added, changed or dropped, query preserved, and rejection when the path, query, fragment, scheme, host, or userinfo differ, when the host islocalhostor a non-loopback address, and when the value does not parse. The endpoint test fails onmasterand passes with the change; the wholeoauthserverpackage passes against Postgres 15.