Skip to content

Commit 97a8a17

Browse files
authored
fix(transport): apply refreshed bearer token after cross-host redirect (#2337)
When an http.Client-level redirect sends a request to a host that differs from bt.registry, the sendRequest() closure skips setting the Authorization header (its matchesHost guard returns false). The previous code called sendRequest() again after bt.refresh(), so the freshly fetched token was never applied to the retried request and the client received a second 401 instead of succeeding. Fix by reading the token directly from bt.bearer after a successful refresh and setting the Authorization header unconditionally before retrying, bypassing the matchesHost guard that is only needed to prevent header forwarding on client-visible redirects. Closes #2333
1 parent e963497 commit 97a8a17

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,16 @@ func (bt *bearerTransport) RoundTrip(in *http.Request) (*http.Response, error) {
247247
if err = bt.refresh(in.Context()); err != nil {
248248
return nil, err
249249
}
250-
return sendRequest()
250+
// Apply the freshly fetched token directly rather than calling
251+
// sendRequest(), which only sets Authorization when in.URL.Host
252+
// matches bt.registry. When the request followed a cross-host
253+
// redirect the hosts differ and sendRequest() would omit the
254+
// header, causing an unnecessary second 401.
255+
bt.mx.RLock()
256+
tok := bt.bearer.RegistryToken
257+
bt.mx.RUnlock()
258+
in.Header.Set("Authorization", fmt.Sprintf("Bearer %s", tok))
259+
return bt.inner.RoundTrip(in)
251260
}
252261

253262
return res, err

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,72 @@ func TestValidateRealmURLSameHost(t *testing.T) {
697697
})
698698
}
699699
}
700+
701+
// TestBearerTokenAppliedAfterCrossHostRedirect is a regression test for
702+
// https://github.com/google/go-containerregistry/issues/2333.
703+
//
704+
// When an HTTP redirect sends the request to a host that differs from
705+
// bt.registry, the sendRequest() closure omits the Authorization header
706+
// (its matchesHost guard returns false). Previously, the post-401 retry
707+
// also went through sendRequest(), so the freshly refreshed token was
708+
// never applied and the request failed with a second 401.
709+
//
710+
// The fix skips sendRequest() after a successful refresh and sets the
711+
// Authorization header directly before retrying.
712+
func TestBearerTokenAppliedAfterCrossHostRedirect(t *testing.T) {
713+
const (
714+
staleToken = "stale-token"
715+
freshToken = "fresh-token"
716+
)
717+
718+
attempts := 0
719+
// registryServer simulates a registry reachable after a cross-host redirect.
720+
// Its host intentionally differs from bt.registry ("registry.example.com").
721+
registryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
722+
attempts++
723+
if r.Header.Get("Authorization") == "Bearer "+freshToken {
724+
w.WriteHeader(http.StatusOK)
725+
return
726+
}
727+
w.Header().Set("WWW-Authenticate", `Bearer realm="https://token.example.com/token",service="registry.example.com"`)
728+
w.WriteHeader(http.StatusUnauthorized)
729+
}))
730+
defer registryServer.Close()
731+
732+
// bt.registry is "registry.example.com", which does NOT match registryServer's host.
733+
// This replicates what bearerTransport sees after an http.Client-level redirect:
734+
// in.URL.Host is the redirected host, not bt.registry.
735+
registry, err := name.NewRegistry("registry.example.com", name.WeakValidation)
736+
if err != nil {
737+
t.Fatal(err)
738+
}
739+
740+
bt := &bearerTransport{
741+
inner: http.DefaultTransport,
742+
bearer: authn.AuthConfig{RegistryToken: staleToken},
743+
// authn.Bearer causes refresh() to set bearer.RegistryToken directly from
744+
// the credential without a network call, so no SSRF guard is triggered.
745+
basic: &authn.Bearer{Token: freshToken},
746+
registry: registry,
747+
realm: "https://token.example.com/token",
748+
scopes: []string{"repo:example/image:pull"},
749+
service: "registry.example.com",
750+
scheme: "http",
751+
}
752+
753+
res, err := (&http.Client{Transport: bt}).Get(registryServer.URL + "/v2/example/image/manifests/latest")
754+
if err != nil {
755+
t.Fatalf("unexpected error: %v", err)
756+
}
757+
defer res.Body.Close()
758+
759+
if res.StatusCode != http.StatusOK {
760+
t.Errorf("got status %d, want %d: fresh bearer token was not applied on cross-host retry", res.StatusCode, http.StatusOK)
761+
}
762+
if got := bt.bearer.RegistryToken; got != freshToken {
763+
t.Errorf("bearer.RegistryToken = %q, want %q", got, freshToken)
764+
}
765+
if attempts != 2 {
766+
t.Errorf("registry received %d request(s), want 2 (401 then 200)", attempts)
767+
}
768+
}

0 commit comments

Comments
 (0)