Skip to content

fix(oauthserver): accept loopback redirect URIs on any port - #2871

Open
just-some-random-pal wants to merge 2 commits into
supabase:masterfrom
just-some-random-pal:fix/oauth-loopback-redirect-port
Open

just-some-random-pal wants to merge 2 commits into
supabase:masterfrom
just-some-random-pal:fix/oauth-loopback-redirect-port

Conversation

@just-some-random-pal

Copy link
Copy Markdown

Closes #2703.

The authorization endpoint compared redirect_uri to 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 an http loopback redirect such as http://127.0.0.1/callback and 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/authorize with an invalid redirect_uri.

isValidRedirectURI keeps the exact match as the first path and adds the loopback exception as the second: both URIs must parse, both must use http, the registered host must be a loopback IP literal (127.0.0.1 or [::1]) and equal the requested host, and path, query, fragment and userinfo must match exactly. Only the port may differ. localhost is 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 restricts http redirects to loopback hosts, so nothing new becomes registrable.

The token endpoint is unchanged. It compares the exchange-time redirect_uri with 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/authorize with a client registered as http://127.0.0.1/callback: a request with http://127.0.0.1:51234/callback is 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 is localhost or a non-loopback address, and when the value does not parse. The endpoint test fails on master and passes with the change; the whole oauthserver package passes against Postgres 15.

@just-some-random-pal
just-some-random-pal requested a review from a team as a code owner October 13, 2026 07:37
@just-some-random-pal

Copy link
Copy Markdown
Author

@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 authorize.go, with tests in authorize_test.go.

@hf

hf commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@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
@just-some-random-pal
just-some-random-pal force-pushed the fix/oauth-loopback-redirect-port branch from 8195f0c to 12a3ca0 Compare October 30, 2026 15:17
@just-some-random-pal

Copy link
Copy Markdown
Author

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 /oauth/authorize. It isn't urgent for me, so I'm happy to leave it open until the timing suits you. I rebased on master, which also clears the govulncheck failure.

Comment thread internal/api/oauthserver/authorize.go Resolved
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth 2.1 server: authorize rejects RFC 8252 loopback ephemeral-port redirect_uri (exact-match, no port exemption)

2 participants