Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/acr/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/client/acr/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/gcr/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/gcr/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions pkg/client/selfhosted/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions pkg/client/selfhosted/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions pkg/client/util/host.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions pkg/client/util/host_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading