@@ -698,41 +698,36 @@ func TestValidateRealmURLSameHost(t *testing.T) {
698698 }
699699}
700700
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 ) {
701+ // TestBearerTokenReappliedOnSameHostChallenge is the regression test for
702+ // https://github.com/google/go-containerregistry/issues/2333: when the
703+ // registry we authenticated against answers with a 401 Bearer challenge
704+ // (e.g. token expiry mid-session), the freshly refreshed token must be
705+ // applied to the retry so it succeeds rather than failing with a second 401.
706+ func TestBearerTokenReappliedOnSameHostChallenge (t * testing.T ) {
713707 const (
714708 staleToken = "stale-token"
715709 freshToken = "fresh-token"
716710 )
717711
718712 attempts := 0
719- // registryServer simulates a registry reachable after a cross-host redirect.
720- // Its host intentionally differs from bt.registry ("registry.example.com").
721713 registryServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
722714 attempts ++
723715 if r .Header .Get ("Authorization" ) == "Bearer " + freshToken {
724716 w .WriteHeader (http .StatusOK )
725717 return
726718 }
727- w .Header ().Set ("WWW-Authenticate" , `Bearer realm="https://token.example.com/token",service="registry.example.com "` )
719+ w .Header ().Set ("WWW-Authenticate" , `Bearer realm="https://token.example.com/token",service="registry"` )
728720 w .WriteHeader (http .StatusUnauthorized )
729721 }))
730722 defer registryServer .Close ()
731723
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 )
724+ // bt.registry IS the server's host, so matchesHost is true: this is the
725+ // registry we authenticated against, and re-attaching the token is safe.
726+ u , err := url .Parse (registryServer .URL )
727+ if err != nil {
728+ t .Fatal (err )
729+ }
730+ registry , err := name .NewRegistry (u .Host , name .WeakValidation )
736731 if err != nil {
737732 t .Fatal (err )
738733 }
@@ -746,7 +741,7 @@ func TestBearerTokenAppliedAfterCrossHostRedirect(t *testing.T) {
746741 registry : registry ,
747742 realm : "https://token.example.com/token" ,
748743 scopes : []string {"repo:example/image:pull" },
749- service : "registry.example.com " ,
744+ service : "registry" ,
750745 scheme : "http" ,
751746 }
752747
@@ -757,7 +752,7 @@ func TestBearerTokenAppliedAfterCrossHostRedirect(t *testing.T) {
757752 defer res .Body .Close ()
758753
759754 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 )
755+ t .Errorf ("got status %d, want %d: fresh bearer token was not applied on same -host retry" , res .StatusCode , http .StatusOK )
761756 }
762757 if got := bt .bearer .RegistryToken ; got != freshToken {
763758 t .Errorf ("bearer.RegistryToken = %q, want %q" , got , freshToken )
@@ -766,3 +761,55 @@ func TestBearerTokenAppliedAfterCrossHostRedirect(t *testing.T) {
766761 t .Errorf ("registry received %d request(s), want 2 (401 then 200)" , attempts )
767762 }
768763}
764+
765+ // TestBearerTokenNotLeakedOnCrossHostChallenge verifies that when a request
766+ // has been redirected to a host that differs from the registry we logged in
767+ // to, the refreshed token is NOT re-attached. A malicious or compromised
768+ // registry must not be able to 302 the request to an attacker host, answer
769+ // with a Bearer challenge, and harvest the operator's registry credential.
770+ func TestBearerTokenNotLeakedOnCrossHostChallenge (t * testing.T ) {
771+ const (
772+ staleToken = "stale-token"
773+ freshToken = "fresh-token"
774+ )
775+
776+ var gotAuth string
777+ // attackerServer is NOT bt.registry. It issues a Bearer challenge and
778+ // records any Authorization header it receives.
779+ attackerServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
780+ if a := r .Header .Get ("Authorization" ); a != "" {
781+ gotAuth = a
782+ w .WriteHeader (http .StatusOK )
783+ return
784+ }
785+ w .Header ().Set ("WWW-Authenticate" , `Bearer realm="https://token.example.com/token",service="attacker"` )
786+ w .WriteHeader (http .StatusUnauthorized )
787+ }))
788+ defer attackerServer .Close ()
789+
790+ registry , err := name .NewRegistry ("registry.example.com" , name .WeakValidation )
791+ if err != nil {
792+ t .Fatal (err )
793+ }
794+
795+ bt := & bearerTransport {
796+ inner : http .DefaultTransport ,
797+ bearer : authn.AuthConfig {RegistryToken : staleToken },
798+ basic : & authn.Bearer {Token : freshToken },
799+ registry : registry , // does NOT match attackerServer's host
800+ realm : "https://token.example.com/token" ,
801+ scopes : []string {"repo:example/image:pull" },
802+ service : "registry.example.com" ,
803+ scheme : "http" ,
804+ }
805+
806+ res , err := (& http.Client {Transport : bt }).Get (attackerServer .URL + "/v2/example/image/manifests/latest" )
807+ if err != nil {
808+ t .Fatalf ("unexpected error: %v" , err )
809+ }
810+ defer res .Body .Close ()
811+
812+ if gotAuth != "" {
813+ t .Fatalf ("credential leaked to cross-host challenger: Authorization=%q" , gotAuth )
814+ }
815+ }
0 commit comments