From 788cb54a180291a7f2ec78dccc0e310b935efea8 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 21 Nov 2026 11:47:45 +0100 Subject: [PATCH 1/4] Reject unknown upstream.auth types and warn about entries without credentials Fixes #767. An entry with a mistyped type, or whose ${VAR} is unset, sent nothing and still won the longest-prefix match for its scope. --- docs/configuration.md | 2 + internal/config/auth_validate_test.go | 79 +++++++++++++++++++++++++++ internal/config/config.go | 38 +++++++++++-- internal/config/mirror.go | 3 + 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 internal/config/auth_validate_test.go diff --git a/docs/configuration.md b/docs/configuration.md index 70c9a516..88348822 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1212,6 +1212,8 @@ This field has no environment variable, as with the other named upstream maps. Configure authentication for private upstream registries. The same authentication-aware client is used for metadata and artifact downloads, and credentials can reference environment variables using `${VAR_NAME}` syntax. +The `type` is `bearer`, `basic`, `header` or `ecr`; any other value fails the configuration check. An entry that ends up with nothing to send, such as a token whose `${VAR_NAME}` is unset, is still the match for its scope, so requests under it go out without credentials. The proxy logs a warning naming each such entry at startup. + OCI registries that return a Bearer challenge from a `/v2/{repository}/…` endpoint are handled automatically. The proxy discovers the token realm from `WWW-Authenticate`, applies any configured credentials for the token URL, and reuses the token until shortly before it expires. The token is always requested for pull access to the repository being fetched (`repository:{repository}:pull`), whatever scope the challenge names. A token fetched with credentials configured for the token URL is only fetched for, and sent to, a registry that is trusted with it: one on the same host and port as the token service, one configured with the same credentials as the token URL, or Docker Hub (`registry-1.docker.io` and `index.docker.io` with `auth.docker.io`) and GitLab.com (`registry.gitlab.com` with `gitlab.com`). Sharing a parent domain is not enough, so `team-a.jfrog.io` is not trusted with tokens from `team-b.jfrog.io`. Any other host that answers with a challenge naming that token service gets its 401 back, so it cannot obtain a token minted from your credentials. Tokens from a token service with no configured credentials are anonymous and are fetched for any registry, as before. If your registry and its token service are on different hosts, such as a self-hosted GitLab with its registry on its own hostname, configure the same credentials for both URLs: diff --git a/internal/config/auth_validate_test.go b/internal/config/auth_validate_test.go new file mode 100644 index 00000000..c3241647 --- /dev/null +++ b/internal/config/auth_validate_test.go @@ -0,0 +1,79 @@ +package config + +import ( + "strings" + "testing" +) + +func TestValidateRejectsUnknownUpstreamAuthType(t *testing.T) { + for _, typ := range []string{"token", "", "bearer "} { + cfg := Default() + cfg.Upstream.Auth = map[string]AuthConfig{ + "https://registry.example.com/private": {Type: typ, Token: "t"}, + } + err := cfg.Validate() + if err == nil { + t.Fatalf("type %q: Validate() error = nil, want an unknown type error", typ) + } + for _, want := range []string{"upstream.auth", "https://registry.example.com/private", `"` + typ + `"`, "bearer, basic, header or ecr"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("type %q: Validate() error = %q, want it to contain %q", typ, err, want) + } + } + } +} + +func TestValidateAcceptsEveryUpstreamAuthType(t *testing.T) { + for _, typ := range []string{"bearer", "basic", "header", "ecr", "Bearer", "ECR"} { + cfg := Default() + cfg.Upstream.Auth = map[string]AuthConfig{ + "https://registry.example.com": {Type: typ}, + } + if err := cfg.Validate(); err != nil { + t.Errorf("type %q: Validate() error = %v", typ, err) + } + } +} + +func TestWarningsUpstreamAuthWithoutCredentials(t *testing.T) { + t.Setenv("AUTH_TEST_SET", "secret") + t.Setenv("AUTH_TEST_EMPTY", "") + + cfg := Default() + cfg.Upstream.Auth = map[string]AuthConfig{ + "https://ok-bearer.example.com": {Type: "bearer", Token: "${AUTH_TEST_SET}"}, + "https://ok-basic.example.com": {Type: "basic", Username: "u"}, + "https://ok-header.example.com": {Type: "header", HeaderName: "X-Key", HeaderValue: "${AUTH_TEST_SET}"}, + "https://ok-ecr.example.com": {Type: "ecr"}, + "https://no-token.example.com": {Type: "bearer", Token: "${AUTH_TEST_EMPTY}"}, + "https://no-user.example.com": {Type: "basic", Password: "p"}, + "https://no-name.example.com": {Type: "header", HeaderValue: "v"}, + "https://no-value.example.com": {Type: "header", HeaderName: "X-Key", HeaderValue: "${AUTH_TEST_EMPTY}"}, + "https://a-no-token.example.com/p": {Type: "Bearer"}, + } + + var got []string + for _, w := range cfg.Warnings() { + if strings.Contains(w, "upstream.auth") { + got = append(got, w) + } + } + want := []string{ + "https://a-no-token.example.com/p", + "https://no-name.example.com", + "https://no-token.example.com", + "https://no-user.example.com", + "https://no-value.example.com", + } + if len(got) != len(want) { + t.Fatalf("Warnings() = %q, want one for each of %q", got, want) + } + for i, scope := range want { + if !strings.Contains(got[i], `"`+scope+`"`) || !strings.Contains(got[i], "without credentials") { + t.Errorf("warning %d = %q, want it to name %q and say requests go out without credentials", i, got[i], scope) + } + if strings.Contains(got[i], "secret") { + t.Errorf("warning %d = %q leaks a credential", i, got[i]) + } + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 837213c8..81e2604a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -961,10 +961,8 @@ func (u *UpstreamConfig) AuthForURL(url string) *AuthConfig { // Validate checks upstream authentication URL scopes. func (u *UpstreamConfig) Validate() error { - for pattern := range u.Auth { - if _, err := parseAuthURL(pattern); err != nil { - return fmt.Errorf("invalid upstream.auth URL %q: %w", pattern, err) - } + if err := u.validateAuth(); err != nil { + return err } if err := validateNamedUpstreams("upstream.helm", u.Helm); err != nil { return err @@ -2341,6 +2339,38 @@ func (a *AuthConfig) Header() (name, value string) { } } +// validateAuth checks the URL scope and the type of every upstream.auth +// entry. +func (u *UpstreamConfig) validateAuth() error { + for pattern, auth := range u.Auth { + if _, err := parseAuthURL(pattern); err != nil { + return fmt.Errorf("invalid upstream.auth URL %q: %w", pattern, err) + } + switch strings.ToLower(auth.Type) { + case "bearer", "basic", "header", "ecr": + default: + return fmt.Errorf("invalid upstream.auth %q: unknown type %q, must be bearer, basic, header or ecr", pattern, auth.Type) + } + } + return nil +} + +// authWithoutCredentials returns the upstream.auth scopes whose entry has +// nothing to send, sorted. An ECR entry gets its credentials from AWS. +func (u *UpstreamConfig) authWithoutCredentials() []string { + var scopes []string + for pattern, auth := range u.Auth { + if strings.EqualFold(auth.Type, "ecr") { + continue + } + if name, value := auth.Header(); name == "" || value == "" { + scopes = append(scopes, pattern) + } + } + slices.Sort(scopes) + return scopes +} + // expandEnv expands ${VAR_NAME} references in a string. func expandEnv(s string) string { return os.Expand(s, os.Getenv) diff --git a/internal/config/mirror.go b/internal/config/mirror.go index 3b56fd87..790dfe0d 100644 --- a/internal/config/mirror.go +++ b/internal/config/mirror.go @@ -144,6 +144,9 @@ func (c *Config) Warnings() []string { if c.Mirror.Refresh != nil && !c.CacheMetadata { warnings = append(warnings, "mirror.refresh without cache_metadata does not refresh Debian, Alpine or RPM packages: their indexes are found through the metadata cache") } + for _, scope := range c.Upstream.authWithoutCredentials() { + warnings = append(warnings, fmt.Sprintf("upstream.auth %q has nothing to send, so requests under it go out without credentials: its token, username, header name or header value is empty, after expanding ${VAR} references", scope)) + } return warnings } From 19d85026c90d05e436b332e1a61fb53b691dbc89 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sat, 21 Nov 2026 12:56:22 +0100 Subject: [PATCH 2/4] Leave a scope's own credentials out of upstream.auth messages --- internal/config/auth_validate_test.go | 33 +++++++++++++++++++++++++++ internal/config/config.go | 16 ++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/internal/config/auth_validate_test.go b/internal/config/auth_validate_test.go index c3241647..9e65a750 100644 --- a/internal/config/auth_validate_test.go +++ b/internal/config/auth_validate_test.go @@ -77,3 +77,36 @@ func TestWarningsUpstreamAuthWithoutCredentials(t *testing.T) { } } } + +// A scope can carry a credential of its own, which the messages naming the +// scope leave out. +func TestUpstreamAuthMessagesLeaveOutScopeCredentials(t *testing.T) { + const scope = "https://user:scope-pass@registry.example.com/private?api_key=scope-secret" + const shown = `"https://registry.example.com/private"` + + cfg := Default() + cfg.Upstream.Auth = map[string]AuthConfig{scope: {Type: "bearer"}} + var warning string + for _, w := range cfg.Warnings() { + if strings.Contains(w, "upstream.auth") { + warning = w + } + } + + cfg.Upstream.Auth = map[string]AuthConfig{scope: {Type: "token"}} + err := cfg.Validate() + if err == nil { + t.Fatal("Validate() error = nil, want an unknown type error") + } + + for name, message := range map[string]string{"warning": warning, "error": err.Error()} { + if !strings.Contains(message, shown) { + t.Errorf("%s = %q, want it to name %s", name, message, shown) + } + for _, secret := range []string{"scope-pass", "scope-secret", "user:", "api_key"} { + if strings.Contains(message, secret) { + t.Errorf("%s = %q, want it without %q", name, message, secret) + } + } + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 81e2604a..10629cc3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2349,14 +2349,24 @@ func (u *UpstreamConfig) validateAuth() error { switch strings.ToLower(auth.Type) { case "bearer", "basic", "header", "ecr": default: - return fmt.Errorf("invalid upstream.auth %q: unknown type %q, must be bearer, basic, header or ecr", pattern, auth.Type) + return fmt.Errorf("invalid upstream.auth %q: unknown type %q, must be bearer, basic, header or ecr", authScopeName(pattern), auth.Type) } } return nil } +// authScopeName returns an upstream.auth scope for a message, without the +// user information and query a scope can carry credentials in. +func authScopeName(pattern string) string { + parsed, err := parseAuthURL(pattern) + if err != nil { + return "(invalid URL)" + } + return (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host, Path: parsed.Path, RawPath: parsed.RawPath}).String() +} + // authWithoutCredentials returns the upstream.auth scopes whose entry has -// nothing to send, sorted. An ECR entry gets its credentials from AWS. +// nothing to send, as authScopeName gives them, sorted. An ECR entry gets its credentials from AWS. func (u *UpstreamConfig) authWithoutCredentials() []string { var scopes []string for pattern, auth := range u.Auth { @@ -2364,7 +2374,7 @@ func (u *UpstreamConfig) authWithoutCredentials() []string { continue } if name, value := auth.Header(); name == "" || value == "" { - scopes = append(scopes, pattern) + scopes = append(scopes, authScopeName(pattern)) } } slices.Sort(scopes) From 1cf3fa2104c63915978e5dada602b06bbcd4da59 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 22 Nov 2026 07:20:15 +0100 Subject: [PATCH 3/4] Leave an invalid scope's credentials out of its error too Also say that the startup warning doesn't cover ecr entries, whose token is fetched, and a failure logged, when a request needs one. --- docs/configuration.md | 2 +- internal/config/auth_validate_test.go | 23 +++++++++++++++++++++++ internal/config/config.go | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index c48b1a71..b907cf58 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1212,7 +1212,7 @@ This field has no environment variable, as with the other named upstream maps. Configure authentication for private upstream registries. The same authentication-aware client is used for metadata and artifact downloads, and credentials can reference environment variables using `${VAR_NAME}` syntax. -The `type` is `bearer`, `basic`, `header` or `ecr`; any other value fails the configuration check. An entry that ends up with nothing to send, such as a token whose `${VAR_NAME}` is unset, is still the match for its scope, so requests under it go out without credentials. The proxy logs a warning naming each such entry at startup. +The `type` is `bearer`, `basic`, `header` or `ecr`; any other value fails the configuration check. An entry that ends up with nothing to send, such as a token whose `${VAR_NAME}` is unset, is still the match for its scope, so requests under it go out without credentials. The proxy logs a warning naming each such entry at startup, except an `ecr` entry: it gets its token from AWS when a request needs one, and a failed fetch is logged as an error then. OCI registries that return a Bearer challenge from a `/v2/{repository}/…` endpoint are handled automatically. The proxy discovers the token realm from `WWW-Authenticate`, applies any configured credentials for the token URL, and reuses the token until shortly before it expires. The token is always requested for pull access to the repository being fetched (`repository:{repository}:pull`), whatever scope the challenge names. If the token URL redirects, the redirected request only carries credentials when the redirect target is configured with the same ones as the token URL, so a token URL that redirects to a token service with its own credentials needs those credentials configured for both. diff --git a/internal/config/auth_validate_test.go b/internal/config/auth_validate_test.go index 9e65a750..013371de 100644 --- a/internal/config/auth_validate_test.go +++ b/internal/config/auth_validate_test.go @@ -110,3 +110,26 @@ func TestUpstreamAuthMessagesLeaveOutScopeCredentials(t *testing.T) { } } } + +// An invalid scope is named without its credentials too. +func TestInvalidUpstreamAuthScopeLeavesOutItsCredentials(t *testing.T) { + for scope, shown := range map[string]string{ + "https://user:scope-pass@/private?api_key=scope-secret": `"https:///private"`, + "https://user:scope-pass@host/%zz?api_key=scope-secret": `"(invalid URL)"`, + } { + cfg := Default() + cfg.Upstream.Auth = map[string]AuthConfig{scope: {Type: "bearer", Token: "t"}} + err := cfg.Validate() + if err == nil { + t.Fatalf("%s: Validate() error = nil, want an invalid URL error", shown) + } + if !strings.Contains(err.Error(), shown) { + t.Errorf("Validate() error = %q, want it to name %s", err, shown) + } + for _, secret := range []string{"scope-pass", "scope-secret", "user:", "api_key"} { + if strings.Contains(err.Error(), secret) { + t.Errorf("Validate() error = %q, want it without %q", err, secret) + } + } + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 10629cc3..e3932db5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2344,7 +2344,7 @@ func (a *AuthConfig) Header() (name, value string) { func (u *UpstreamConfig) validateAuth() error { for pattern, auth := range u.Auth { if _, err := parseAuthURL(pattern); err != nil { - return fmt.Errorf("invalid upstream.auth URL %q: %w", pattern, err) + return fmt.Errorf("invalid upstream.auth URL %q: %w", authScopeName(pattern), err) } switch strings.ToLower(auth.Type) { case "bearer", "basic", "header", "ecr": @@ -2358,7 +2358,7 @@ func (u *UpstreamConfig) validateAuth() error { // authScopeName returns an upstream.auth scope for a message, without the // user information and query a scope can carry credentials in. func authScopeName(pattern string) string { - parsed, err := parseAuthURL(pattern) + parsed, err := url.Parse(pattern) if err != nil { return "(invalid URL)" } From ed0c885a8504257e820f079cfe3f0d913b15736c Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Sun, 22 Nov 2026 08:52:13 +0000 Subject: [PATCH 4/4] Trim upstream.auth helper comments --- internal/config/config.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index e3932db5..63dda7da 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2339,8 +2339,6 @@ func (a *AuthConfig) Header() (name, value string) { } } -// validateAuth checks the URL scope and the type of every upstream.auth -// entry. func (u *UpstreamConfig) validateAuth() error { for pattern, auth := range u.Auth { if _, err := parseAuthURL(pattern); err != nil { @@ -2365,8 +2363,7 @@ func authScopeName(pattern string) string { return (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host, Path: parsed.Path, RawPath: parsed.RawPath}).String() } -// authWithoutCredentials returns the upstream.auth scopes whose entry has -// nothing to send, as authScopeName gives them, sorted. An ECR entry gets its credentials from AWS. +// An ECR entry gets its credentials from AWS. func (u *UpstreamConfig) authWithoutCredentials() []string { var scopes []string for pattern, auth := range u.Auth {