From e8a5dc3f3d517cd6d26447544c37363f3140b0ad Mon Sep 17 00:00:00 2001 From: David Collom Date: Wed, 18 Nov 2026 13:15:29 +0000 Subject: [PATCH] Restrict registry host matching so credentials only go to their registry Which registry client handles an image is decided by a regex over the host in the image reference, and that host is controlled by whoever authors the pod (including through the override-url annotation). Several of those regexes were loose enough to route a configured credential to another host: - ACR matched any host containing ".azurecr.io", so "x.azurecr.io.attacker.example" received the ACR credential. - GCR matched "*.k8s.io" and used ".*" prefixes, which also match URL metacharacters ("evil.com#.gcr.io" is requested as host evil.com). - Selfhosted used an unescaped, left-unanchored host, and a Host without a scheme collapsed the regex to match every host. Anchor the ACR and GCR patterns to hostname characters, escape and anchor the selfhosted host, and reject selfhosted hosts that are not http(s) URLs. Validate every selfhosted entry rather than only the first, and reject any image host that is not a bare hostname or IP literal (with optional port) before a client is chosen, so a request URL is built from the host that was matched. Signed-off-by: David Collom --- cmd/app/options.go | 4 +++- pkg/client/acr/path.go | 2 +- pkg/client/acr/path_test.go | 12 ++++++++++++ pkg/client/client.go | 7 +++++++ pkg/client/gcr/path.go | 2 +- pkg/client/gcr/path_test.go | 16 ++++++++++++++++ pkg/client/selfhosted/path.go | 10 ++++++++-- pkg/client/selfhosted/path_test.go | 17 +++++++++++++++++ pkg/client/util/host.go | 20 ++++++++++++++++++++ pkg/client/util/host_test.go | 23 +++++++++++++++++++++++ 10 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 pkg/client/util/host.go create mode 100644 pkg/client/util/host_test.go diff --git a/cmd/app/options.go b/cmd/app/options.go index 5df7b59a..3ef09bf2 100644 --- a/cmd/app/options.go +++ b/cmd/app/options.go @@ -472,7 +472,9 @@ func validSelfHostedOpts(opts *Options) bool { // opts set using env vars if opts.Client.Selfhosted != nil { for _, selfHostedOpts := range opts.Client.Selfhosted { - return isValidOption(selfHostedOpts.Host, "") + if !isValidOption(selfHostedOpts.Host, "") { + return false + } } } diff --git a/pkg/client/acr/path.go b/pkg/client/acr/path.go index a416c688..1667b3f4 100644 --- a/pkg/client/acr/path.go +++ b/pkg/client/acr/path.go @@ -6,7 +6,7 @@ import ( ) var ( - reg = regexp.MustCompile(`.*\.azurecr\.io|.*\.azurecr\.cn|.*\.azurecr\.de|.*\.azurecr\.us`) + reg = regexp.MustCompile(`^[a-zA-Z0-9-]+\.azurecr\.(io|cn|de|us)$`) ) func (c *Client) IsHost(host string) bool { diff --git a/pkg/client/acr/path_test.go b/pkg/client/acr/path_test.go index d16e2dd6..211edf7e 100644 --- a/pkg/client/acr/path_test.go +++ b/pkg/client/acr/path_test.go @@ -51,6 +51,18 @@ func TestIsHost(t *testing.T) { host: "azurecr.iofoo", expIs: false, }, + "credential-forwarding suffix host should be false": { + host: "x.azurecr.io.attacker.example", + expIs: false, + }, + "embedded host should be false": { + host: "attacker.example/.azurecr.io", + expIs: false, + }, + "URL fragment trick should be false": { + host: "evil.com#.azurecr.io", + expIs: false, + }, } handler := new(Client) diff --git a/pkg/client/client.go b/pkg/client/client.go index 219802d6..4519204c 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -18,6 +18,7 @@ import ( "github.com/jetstack/version-checker/pkg/client/oci" "github.com/jetstack/version-checker/pkg/client/quay" "github.com/jetstack/version-checker/pkg/client/selfhosted" + "github.com/jetstack/version-checker/pkg/client/util" ) // Used for testing/mocking purposes @@ -125,6 +126,12 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) // Tags returns the full list of image tags available, for a given image URL. func (c *Client) Tags(ctx context.Context, imageURL string) ([]api.ImageTag, error) { client, host, path := c.fromImageURL(imageURL) + // The host is pod-controlled and is interpolated into request URLs. + if host != "" { + if err := util.ValidHost(host); err != nil { + return nil, err + } + } c.log.Debugf("using client %q for image URL %q", client.Name(), imageURL) repo, image := client.RepoImageFromPath(path) diff --git a/pkg/client/gcr/path.go b/pkg/client/gcr/path.go index d84b59cd..3b2e4b6a 100644 --- a/pkg/client/gcr/path.go +++ b/pkg/client/gcr/path.go @@ -6,7 +6,7 @@ import ( ) var ( - reg = regexp.MustCompile(`(^(.*\.)?gcr\.io$|^(.*\.)?k8s\.io$|^(.+)-docker\.pkg\.dev$)`) + reg = regexp.MustCompile(`^(([a-zA-Z0-9-]+\.)?gcr\.io|([a-zA-Z0-9-]+\.)?k8s\.io|[a-zA-Z0-9-]+-docker\.pkg\.dev)$`) ) func (c *Client) IsHost(host string) bool { diff --git a/pkg/client/gcr/path_test.go b/pkg/client/gcr/path_test.go index 7bc80e47..ebcf44ca 100644 --- a/pkg/client/gcr/path_test.go +++ b/pkg/client/gcr/path_test.go @@ -59,6 +59,22 @@ func TestIsHost(t *testing.T) { host: "registry.k8s.io", expIs: true, }, + "URL fragment trick should be false": { + host: "evil.com#.gcr.io", + expIs: false, + }, + "URL query trick should be false": { + host: "evil.com?.k8s.io", + expIs: false, + }, + "look-alike k8s.io host should be false": { + host: "evilk8s.io", + expIs: false, + }, + "suffix host should be false": { + host: "gcr.io.attacker.example", + expIs: false, + }, } handler := new(Client) diff --git a/pkg/client/selfhosted/path.go b/pkg/client/selfhosted/path.go index 9d99b049..e4d0d966 100644 --- a/pkg/client/selfhosted/path.go +++ b/pkg/client/selfhosted/path.go @@ -9,7 +9,9 @@ import ( const ( // Regex template to be used to check "isHost". - hostRegTemplate = `^.*%s$` + // The optional prefix is restricted to hostname characters: `.*` would + // match URL metacharacters (#, ?, @) and route credentials to another host. + hostRegTemplate = `^([A-Za-z0-9-]+\.)*%s$` ) func (c *Client) IsHost(host string) bool { @@ -40,7 +42,11 @@ func parseURL(rawurl string) (*regexp.Regexp, string, error) { return nil, "", fmt.Errorf("failed parsing host %q: %s", rawurl, err) } - hostRegTemplate := fmt.Sprintf(hostRegTemplate, parsedURL.Host) + if parsedURL.Host == "" || (parsedURL.Scheme != "http" && parsedURL.Scheme != "https") { + return nil, "", fmt.Errorf("host %q must be a http(s) URL such as https://registry.example.com", rawurl) + } + + hostRegTemplate := fmt.Sprintf(hostRegTemplate, regexp.QuoteMeta(parsedURL.Host)) hostRegex, err := regexp.Compile(hostRegTemplate) if err != nil { return nil, "", fmt.Errorf("failed to parse regex: %s for host %q: %s", diff --git a/pkg/client/selfhosted/path_test.go b/pkg/client/selfhosted/path_test.go index 0df5684a..aa7414b9 100644 --- a/pkg/client/selfhosted/path_test.go +++ b/pkg/client/selfhosted/path_test.go @@ -65,6 +65,18 @@ func TestIsHost(t *testing.T) { host: "foo.docker.repositories.yourdomain.ext", expIs: true, }, + "URL fragment trick should be false": { + host: "evil.com#.docker.repositories.yourdomain.ext", + expIs: false, + }, + "URL query trick should be false": { + host: "evil.com?.docker.repositories.yourdomain.ext", + expIs: false, + }, + "URL userinfo trick should be false": { + host: "x@evil.com.docker.repositories.yourdomain.ext", + expIs: false, + }, } options := &Options{ @@ -135,3 +147,8 @@ func TestRepoImage(t *testing.T) { }) } } + +func TestNewRejectsSchemelessHost(t *testing.T) { + _, err := New(context.TODO(), logrus.NewEntry(logrus.New()), &Options{Host: "registry.example.com:5000"}) + assert.Error(t, err) +} diff --git a/pkg/client/util/host.go b/pkg/client/util/host.go new file mode 100644 index 00000000..49c90947 --- /dev/null +++ b/pkg/client/util/host.go @@ -0,0 +1,20 @@ +package util + +import ( + "fmt" + "regexp" +) + +// hostRe is a bare DNS name or bracketed IP literal with an optional port. It +// excludes every URL metacharacter (@ # ? / \ whitespace), so the host a +// check sees is the host a request is sent to. +var hostRe = regexp.MustCompile(`^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*|\[[0-9A-Fa-f:.]+\])(?::[0-9]{1,5})?$`) + +// ValidHost returns an error unless host is a bare hostname or IP literal with +// an optional port. Image hosts are pod-controlled and end up in request URLs. +func ValidHost(host string) error { + if len(host) > 255 || !hostRe.MatchString(host) { + return fmt.Errorf("invalid registry host %.64q", host) + } + return nil +} diff --git a/pkg/client/util/host_test.go b/pkg/client/util/host_test.go new file mode 100644 index 00000000..d066cb57 --- /dev/null +++ b/pkg/client/util/host_test.go @@ -0,0 +1,23 @@ +package util + +import ( + "strings" + "testing" +) + +func TestValidHost(t *testing.T) { + for _, ok := range []string{"ghcr.io", "registry.example.com:5000", "localhost:5000", "[::1]:5000", "10.0.0.1"} { + if err := ValidHost(ok); err != nil { + t.Errorf("%q should be valid: %v", ok, err) + } + } + for _, bad := range []string{ + "", "evil.com#.registry.example.com", "evil.com?.registry.example.com", + "x@169.254.169.254", "127.0.0.1?x", "a b", "a\\b", "a/b", "-a.com", "a..com", "host:99999999", + strings.Repeat("a", 300), + } { + if ValidHost(bad) == nil { + t.Errorf("%q should be invalid", bad) + } + } +}