Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: validate token auth realm host for registry fallback (#2128)
Kyle Mendelle7b62d0882fe
diff --git a/backend/pkg/utils/distribution/distribution.go b/backend/pkg/utils/distribution/distribution.goindex 258fb61f6..d2be2fada 100644--- a/backend/pkg/utils/distribution/distribution.go+++ b/backend/pkg/utils/distribution/distribution.go@@ -149,6 +149,9 @@ func fetchWithTokenAuthInternal(ctx context.Context, httpClient *http.Client, reif realm == "" {return "", fmt.Errorf("no auth realm found")}+ if err := validateAuthRealmInternal(registryHost, realm); err != nil {+ return "", err+ }token, err := fetchRegistryTokenInternal(ctx, httpClient, realm, service, repository, credential)if err != nil {@@ -402,6 +405,35 @@ func serviceNameFromAuthURLInternal(authURL string) string {return host}+func validateAuthRealmInternal(registryHost, realm string) error {+ parsedRealm, err := url.Parse(strings.TrimSpace(realm))+ if err != nil {+ return fmt.Errorf("invalid auth realm: %w", err)+ }++ if !strings.EqualFold(parsedRealm.Scheme, "https") {+ return fmt.Errorf("auth realm must use HTTPS, got %q", parsedRealm.Scheme)+ }++ realmHost := normalizeRegistryForComparisonInternal(parsedRealm.Hostname())+ registry := normalizeRegistryForComparisonInternal(registryHost)++ if realmHost == "" {+ return fmt.Errorf("invalid auth realm host")+ }+ if realmHost == registry {+ return nil+ }++ // Docker Hub serves token auth from auth.docker.io while image pulls are made+ // against index.docker.io / registry-1.docker.io.+ if registry == "docker.io" && realmHost == "auth.docker.io" {+ return nil+ }++ return fmt.Errorf("untrusted auth realm host %q for registry %q", realmHost, registry)+}+func normalizeRegistryForComparisonInternal(raw string) string {value := strings.TrimSpace(strings.ToLower(raw))value = strings.TrimPrefix(value, "https://")diff --git a/backend/pkg/utils/distribution/distribution_test.go b/backend/pkg/utils/distribution/distribution_test.goindex 7d6a72109..25b4729f8 100644--- a/backend/pkg/utils/distribution/distribution_test.go+++ b/backend/pkg/utils/distribution/distribution_test.go@@ -95,3 +95,98 @@ func TestParseWWWAuthInternal_AllowsCommasInsideQuotedRealm(t *testing.T) {assert.Equal(t, "https://auth.example.com/token?a=1,b=2", realm)assert.Equal(t, "registry.example.com", service)}++func TestFetchDigestWithHTTPClient_RejectsUntrustedTokenRealm(t *testing.T) {+ registry := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("WWW-Authenticate", `Bearer realm="https://169.254.169.254/token",service="registry.example.com"`)+ w.WriteHeader(http.StatusUnauthorized)+ }))+ defer registry.Close()++ digest, err := FetchDigestWithHTTPClient(+ context.Background(),+ registry.URL,+ "team/app",+ "latest",+ nil,+ registry.Client(),+ )++ require.Error(t, err)+ assert.Empty(t, digest)+ assert.Contains(t, err.Error(), "untrusted auth realm host")+}++func TestValidateAuthRealmInternal(t *testing.T) {+ tests := []struct {+ name string+ registryHost string+ realm string+ wantErr bool+ errContains string+ }{+ {+ name: "same host allowed",+ registryHost: "registry.example.com",+ realm: "https://registry.example.com/token",+ },+ {+ name: "same host with explicit default https port allowed",+ registryHost: "registry.example.com",+ realm: "https://registry.example.com:443/token",+ },+ {+ name: "docker hub auth host allowed",+ registryHost: "registry-1.docker.io",+ realm: "https://auth.docker.io/token",+ },+ {+ name: "non https realm rejected",+ registryHost: "registry.example.com",+ realm: "http://registry.example.com/token",+ wantErr: true,+ errContains: "auth realm must use HTTPS",+ },+ {+ name: "empty realm rejected",+ registryHost: "registry.example.com",+ realm: "",+ wantErr: true,+ errContains: "auth realm must use HTTPS",+ },+ {+ name: "malformed realm rejected",+ registryHost: "registry.example.com",+ realm: "https://%zz",+ wantErr: true,+ errContains: "invalid auth realm",+ },+ {+ name: "relative realm rejected",+ registryHost: "registry.example.com",+ realm: "/token",+ wantErr: true,+ errContains: "auth realm must use HTTPS",+ },+ {+ name: "untrusted realm rejected",+ registryHost: "registry.example.com",+ realm: "https://evil.com/token",+ wantErr: true,+ errContains: "untrusted auth realm host",+ },+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ err := validateAuthRealmInternal(tc.registryHost, tc.realm)+ if tc.wantErr {+ require.Error(t, err)+ assert.Contains(t, err.Error(), tc.errContains)+ return+ }++ require.NoError(t, err)+ })+ }+}