Skip to content

Commit bf87c3b

Browse files
authored
transport: allow bearer realm at same host:port as registry (#2302)
Fixes #2258. The realm-URL validation introduced in #2243 rejects realms whose host resolves to a private, loopback, or link-local IP. This is the right default for the cross-host SSRF case (a malicious registry pointing the token endpoint at 169.254.169.254 or a sister internal service), but it breaks legitimate setups where a user explicitly targets a private/loopback registry that returns a same-host token endpoint — e.g. test suites that run an authenticated registry on 127.0.0.1, or on-prem deployments that colocate the registry and its token server. Add a same-host:port exception: if the realm URL host:port equals the registry host:port the user is already talking to, skip the private/ link-local check. The realm is not crossing a trust boundary the user did not already trust by passing the reference. The match is strict (host AND port) so that "different port on the same loopback host" attacks — e.g. a realm pointed at a local database or metadata-service port — remain blocked. Mirrors the same-host exception pattern already used by the blob upload Location-header check in pkg/v1/remote/write.go. Signed-off-by: iahsanGill <[email protected]>
1 parent c55facd commit bf87c3b

2 files changed

Lines changed: 83 additions & 9 deletions

File tree

‎pkg/v1/remote/transport/bearer.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func fromChallenge(reg name.Registry, auth authn.Authenticator, t http.RoundTrip
9090
// registry can supply a realm pointing at an internal service or cloud
9191
// metadata endpoint (e.g. 169.254.169.254), causing SSRF when the client
9292
// subsequently fetches a token.
93-
if err := validateRealmURL(realm, pr.Insecure); err != nil {
93+
if err := validateRealmURL(realm, reg.RegistryStr(), pr.Insecure); err != nil {
9494
return nil, fmt.Errorf("invalid realm in www-authenticate: %w", err)
9595
}
9696
service := pr.Parameters["service"]
@@ -111,22 +111,22 @@ func fromChallenge(reg name.Registry, auth authn.Authenticator, t http.RoundTrip
111111

112112
// realmRedirectCheck mimics the default http.Client redirect policy but also
113113
// validates each redirect URL with validateRealmURL.
114-
func realmRedirectCheck(insecure bool) func(*http.Request, []*http.Request) error {
114+
func realmRedirectCheck(registryHost string, insecure bool) func(*http.Request, []*http.Request) error {
115115
return func(req *http.Request, via []*http.Request) error {
116116
if len(via) >= 10 {
117117
return fmt.Errorf("stopped after 10 redirects")
118118
}
119-
if err := validateRealmURL(req.URL.String(), insecure); err != nil {
119+
if err := validateRealmURL(req.URL.String(), registryHost, insecure); err != nil {
120120
return fmt.Errorf("refusing token-server redirect to %q: %w", req.URL, err)
121121
}
122122
return nil
123123
}
124124
}
125125

126126
// validateRealmURL returns an error if the realm URL uses a disallowed scheme
127-
// or resolves to a private / link-local IP address. This prevents a crafted
128-
// WWW-Authenticate header from redirecting token fetches to internal services.
129-
func validateRealmURL(realm string, insecure bool) error {
127+
// or resolves to a private / link-local IP address. Realm URLs matching the
128+
// registry host:port are always allowed. See #2258.
129+
func validateRealmURL(realm, registryHost string, insecure bool) error {
130130
u, err := url.Parse(realm)
131131
if err != nil {
132132
return fmt.Errorf("parsing realm %q: %w", realm, err)
@@ -141,6 +141,10 @@ func validateRealmURL(realm string, insecure bool) error {
141141
default:
142142
return fmt.Errorf("realm scheme %q not allowed; must be https (or http for insecure registries)", u.Scheme)
143143
}
144+
// Always allow realms matching the registry host:port.
145+
if registryHost != "" && u.Host == registryHost {
146+
return nil
147+
}
144148
// Reject IP literals that resolve to private or link-local ranges.
145149
// This blocks direct references to RFC 1918 addresses, loopback, and
146150
// link-local ranges including the cloud instance metadata service
@@ -393,7 +397,7 @@ func (bt *bearerTransport) refreshOauth(ctx context.Context) ([]byte, error) {
393397
}
394398

395399
allowInsecure := bt.scheme == "http"
396-
client := http.Client{Transport: bt.inner, CheckRedirect: realmRedirectCheck(allowInsecure)}
400+
client := http.Client{Transport: bt.inner, CheckRedirect: realmRedirectCheck(bt.registry.RegistryStr(), allowInsecure)}
397401
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(v.Encode()))
398402
if err != nil {
399403
return nil, err
@@ -431,7 +435,7 @@ func (bt *bearerTransport) refreshBasic(ctx context.Context) ([]byte, error) {
431435
target: u.Host,
432436
}
433437
allowInsecure := bt.scheme == "http"
434-
client := http.Client{Transport: b, CheckRedirect: realmRedirectCheck(allowInsecure)}
438+
client := http.Client{Transport: b, CheckRedirect: realmRedirectCheck(bt.registry.RegistryStr(), allowInsecure)}
435439

436440
v := u.Query()
437441
bt.mx.RLock()

‎pkg/v1/remote/transport/bearer_test.go‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ func TestValidateRealmURLUnspecified(t *testing.T) {
618618
{"https://registry.example.com/token", false},
619619
}
620620
for _, tt := range tests {
621-
err := validateRealmURL(tt.realm, false)
621+
err := validateRealmURL(tt.realm, "", false)
622622
if tt.wantErr && err == nil {
623623
t.Errorf("validateRealmURL(%q) should have been rejected", tt.realm)
624624
}
@@ -627,3 +627,73 @@ func TestValidateRealmURLUnspecified(t *testing.T) {
627627
}
628628
}
629629
}
630+
631+
// TestValidateRealmURLSameHost verifies the same-host exception: a realm URL
632+
// pointing at the same host the user is already talking to is allowed even
633+
// when that host is private/loopback/link-local. See
634+
// https://github.com/google/go-containerregistry/issues/2258.
635+
func TestValidateRealmURLSameHost(t *testing.T) {
636+
tests := []struct {
637+
name string
638+
realm string
639+
registryHost string
640+
insecure bool
641+
wantErr bool
642+
}{
643+
{
644+
name: "same loopback host:port allowed",
645+
realm: "http://127.0.0.1:5000/auth",
646+
registryHost: "127.0.0.1:5000",
647+
insecure: true,
648+
},
649+
{
650+
name: "same loopback host but different port still blocked",
651+
realm: "http://127.0.0.1:8080/auth",
652+
registryHost: "127.0.0.1:5000",
653+
insecure: true,
654+
wantErr: true,
655+
},
656+
{
657+
name: "same private host (no port) allowed",
658+
realm: "https://10.0.0.1/auth",
659+
registryHost: "10.0.0.1",
660+
},
661+
{
662+
name: "same IPv6 loopback host:port allowed",
663+
realm: "http://[::1]:5000/auth",
664+
registryHost: "[::1]:5000",
665+
insecure: true,
666+
},
667+
{
668+
name: "cross-host loopback redirect still blocked",
669+
realm: "http://127.0.0.1:5000/auth",
670+
registryHost: "registry.example.com",
671+
insecure: true,
672+
wantErr: true,
673+
},
674+
{
675+
name: "cross-host metadata redirect still blocked",
676+
realm: "https://169.254.169.254/auth",
677+
registryHost: "registry.example.com",
678+
wantErr: true,
679+
},
680+
{
681+
name: "scheme check still applies on same host",
682+
realm: "http://127.0.0.1:5000/auth",
683+
registryHost: "127.0.0.1:5000",
684+
insecure: false,
685+
wantErr: true,
686+
},
687+
}
688+
for _, tt := range tests {
689+
t.Run(tt.name, func(t *testing.T) {
690+
err := validateRealmURL(tt.realm, tt.registryHost, tt.insecure)
691+
if tt.wantErr && err == nil {
692+
t.Errorf("validateRealmURL(%q, %q) should have returned an error", tt.realm, tt.registryHost)
693+
}
694+
if !tt.wantErr && err != nil {
695+
t.Errorf("validateRealmURL(%q, %q) unexpected error: %v", tt.realm, tt.registryHost, err)
696+
}
697+
})
698+
}
699+
}

0 commit comments

Comments
 (0)