Allow ephemeral ports for loopback redirect URIs
Native OAuth clients such as Claude Code publish loopback redirect URIs without a port (http://localhost/callback) and pick an ephemeral port at request time, as described in RFC 8252 section 7.3. The authorize flow matched the requested redirect URI against the registered set with an exact string comparison, so http://localhost:3118/callback was rejected with invalid_redirect_uri even for a trusted, allow-listed client. Make OAuth2Client.IsRedirectURIAllowed the single source of truth for redirect matching: it keeps exact matching and adds loopback-aware matching that ignores the port when scheme, host, path, and query agree. The redundant document-level check and its duplicate loopback helper in the CIMD resolver are removed, so both the registered-client and CIMD paths now rely on one matcher. Also add a pkg/netx package for the loopback helper. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/url"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
@@ -26,6 +27,7 @@ import (
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/iam/policy"
|
||||
"go.probo.inc/probo/pkg/netx"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.probo.inc/probo/pkg/uri"
|
||||
)
|
||||
@@ -54,7 +56,42 @@ type (
|
||||
)
|
||||
|
||||
func (c *OAuth2Client) IsRedirectURIAllowed(rawURI string) bool {
|
||||
return slices.Contains(c.RedirectURIs, uri.URI(rawURI))
|
||||
if slices.Contains(c.RedirectURIs, uri.URI(rawURI)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// RFC 8252 §7.3: native apps using a loopback redirect URI choose an
|
||||
// ephemeral port at request time, so the port must be ignored when
|
||||
// matching against the registered loopback redirect URIs.
|
||||
requested, err := url.Parse(rawURI)
|
||||
if err != nil || !netx.IsLoopback(requested.Hostname()) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, registered := range c.RedirectURIs {
|
||||
candidate, err := url.Parse(registered.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if candidate.Scheme != requested.Scheme {
|
||||
continue
|
||||
}
|
||||
|
||||
if !netx.IsLoopback(candidate.Hostname()) {
|
||||
continue
|
||||
}
|
||||
|
||||
if candidate.Hostname() != requested.Hostname() {
|
||||
continue
|
||||
}
|
||||
|
||||
if candidate.Path == requested.Path && candidate.RawQuery == requested.RawQuery {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *OAuth2Client) HasGrantType(grantType OAuth2GrantType) bool {
|
||||
|
||||
Reference in New Issue
Block a user