diff --git a/cmd/app/app.go b/cmd/app/app.go index 9cced0d9..db376bac 100644 --- a/cmd/app/app.go +++ b/cmd/app/app.go @@ -21,6 +21,7 @@ import ( "github.com/jetstack/version-checker/pkg/api" "github.com/jetstack/version-checker/pkg/client" + "github.com/jetstack/version-checker/pkg/client/util" "github.com/jetstack/version-checker/pkg/controller" "github.com/jetstack/version-checker/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/metrics/server" @@ -100,8 +101,12 @@ func NewCommand(ctx context.Context) *cobra.Command { metricsServer := metrics.New(log, ctrmetrics.Registry, mgr.GetCache()) + // util.DialContext lets individual requests (fallback lookups) opt in + // to refusing internal addresses at connect time. + baseTransport := cleanhttp.DefaultTransport() + baseTransport.DialContext = util.DialContext opts.Client.Transport = transport.Chain( - cleanhttp.DefaultTransport(), + baseTransport, metricsServer.RoundTripper, ) diff --git a/cmd/app/options.go b/cmd/app/options.go index f81ad9bd..9d0b7f12 100644 --- a/cmd/app/options.go +++ b/cmd/app/options.go @@ -125,6 +125,10 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) { "pprof-serving-address", "", "", "Address to serve pprof on for profiling.") + fs.BoolVar(&o.Client.FallbackAllowPrivateHosts, + "fallback-allow-private-hosts", false, + "Allow anonymous fallback registry lookups against loopback/private/link-local addresses. Disabled by default to prevent SSRF via pod image hosts.") + fs.BoolVar(&o.Client.ECR.AllowCrossAccount, "ecr-allow-cross-account", false, "Allow ECR lookups for registries outside the credentials' own AWS account. Disabled by default.") diff --git a/pkg/client/client.go b/pkg/client/client.go index 0929b972..60470c7e 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -51,6 +51,9 @@ type Options struct { Quay quay.Options Selfhosted map[string]*selfhosted.Options + // FallbackAllowPrivateHosts allows anonymous fallback lookups against internal addresses. + FallbackAllowPrivateHosts bool + Transport http.RoundTripper } @@ -85,11 +88,12 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) } // Create some of the fallback clients + opts.OCI.DenyInternal = !opts.FallbackAllowPrivateHosts ociclient, err := oci.New(&opts.OCI, log) if err != nil { return nil, fmt.Errorf("failed to create OCI client: %w", err) } - anonSelfHosted, err := selfhosted.New(ctx, log, &selfhosted.Options{Transporter: opts.Transport}) + anonSelfHosted, err := selfhosted.New(ctx, log, &selfhosted.Options{Transporter: opts.Transport, DenyInternal: !opts.FallbackAllowPrivateHosts}) if err != nil { return nil, fmt.Errorf("failed to create anonymous Selfhosted client: %w", err) } @@ -106,6 +110,8 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) return nil, fmt.Errorf("failed to create fallback client: %w", err) } + fallbackClient.AllowPrivateHosts = opts.FallbackAllowPrivateHosts + c := &Client{ // Append all the clients in order of which we want to check against clients: append( diff --git a/pkg/client/fallback/fallback.go b/pkg/client/fallback/fallback.go index 253f9b5a..023f0646 100644 --- a/pkg/client/fallback/fallback.go +++ b/pkg/client/fallback/fallback.go @@ -6,6 +6,7 @@ import ( "time" "github.com/jetstack/version-checker/pkg/api" + "github.com/jetstack/version-checker/pkg/client/util" "github.com/patrickmn/go-cache" @@ -15,10 +16,17 @@ import ( // Ensure that we are an ImageClient var _ api.ImageClient = (*Client)(nil) +// maxCachedHosts bounds the host -> client cache. +const maxCachedHosts = 1000 + type Client struct { log *logrus.Entry hostCache *cache.Cache clients []api.ImageClient + + // AllowPrivateHosts permits anonymous lookups against loopback, private + // and link-local addresses. Off by default to prevent pod-driven SSRF. + AllowPrivateHosts bool } func New(ctx context.Context, log *logrus.Entry, clients []api.ImageClient) (*Client, error) { @@ -34,6 +42,11 @@ func (c *Client) Name() string { } func (c *Client) Tags(ctx context.Context, host, repo, image string) (tags []api.ImageTag, err error) { + if !c.AllowPrivateHosts { + if err := util.CheckPublicHost(ctx, host); err != nil { + return nil, fmt.Errorf("refusing fallback lookup (set --fallback-allow-private-hosts to permit): %w", err) + } + } // Check if we have a cached client for the host if client, found := c.hostCache.Get(host); found { c.log.Infof("Found client for host %s in cache", host) @@ -50,7 +63,10 @@ func (c *Client) Tags(ctx context.Context, host, repo, image string) (tags []api // Try clients, one by one until we have none left.. for i, client := range c.clients { if tags, err := client.Tags(ctx, host, repo, image); err == nil { - c.hostCache.SetDefault(host, client) + // Hosts are pod-controlled: bound how many are remembered. + if c.hostCache.ItemCount() < maxCachedHosts { + c.hostCache.SetDefault(host, client) + } return tags, nil } diff --git a/pkg/client/oci/oci.go b/pkg/client/oci/oci.go index a2071db0..d984843c 100644 --- a/pkg/client/oci/oci.go +++ b/pkg/client/oci/oci.go @@ -16,6 +16,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/types" "github.com/jetstack/version-checker/pkg/api" + "github.com/jetstack/version-checker/pkg/client/util" ) var numWorkers = runtime.NumCPU() * 5 @@ -23,6 +24,10 @@ var numWorkers = runtime.NumCPU() * 5 type Options struct { Transporter http.RoundTripper Auth *authn.AuthConfig + + // DenyInternal refuses connections to loopback, private and link-local + // addresses, including via redirects, and caps response sizes. + DenyInternal bool } // maxTags bounds the per-tag manifest requests made for one lookup. @@ -52,8 +57,21 @@ func New(opts *Options, log *logrus.Entry) (*Client, error) { remote.WithUserAgent("version-checker/oci"), remote.WithAuth(opts), } - if opts.Transporter != nil { - pullOpts = append(pullOpts, remote.WithTransport(opts.Transporter)) + rt := opts.Transporter + if opts.DenyInternal { + if rt == nil { + if t, ok := remote.DefaultTransport.(*http.Transport); ok { + t = t.Clone() + t.DialContext = util.DialContext + rt = t + } + } + if rt != nil { + rt = util.PublicOnly(rt) + } + } + if rt != nil { + pullOpts = append(pullOpts, remote.WithTransport(rt)) } puller, err := remote.NewPuller(pullOpts...) diff --git a/pkg/client/selfhosted/selfhosted.go b/pkg/client/selfhosted/selfhosted.go index 6761523e..3bea0a90 100644 --- a/pkg/client/selfhosted/selfhosted.go +++ b/pkg/client/selfhosted/selfhosted.go @@ -51,6 +51,10 @@ type Options struct { TokenPath string CAPath string Insecure bool + + // DenyInternal refuses connections to loopback, private and link-local + // addresses, including via redirects. Used by the anonymous fallback client. + DenyInternal bool } type Client struct { *http.Client @@ -152,7 +156,12 @@ func configureTransport(client *Client, opts *Options) error { baseTransport.TLSClientConfig = tlsConfig } + baseTransport.DialContext = util.DialContext + + // PublicOnly is first so it is the outermost layer and survives the + // Transporter middleware below, which replaces everything inside it. client.Transport = transport.Chain(baseTransport, + transport.If(opts.DenyInternal, util.PublicOnly), transport.If(logrus.IsLevelEnabled(logrus.DebugLevel), transport.LogRequests(transport.LogOptions{Concise: true})), transport.If(opts.Transporter != nil, func(rt http.RoundTripper) http.RoundTripper { return opts.Transporter })) return nil diff --git a/pkg/client/util/hostcheck.go b/pkg/client/util/hostcheck.go new file mode 100644 index 00000000..ee902f96 --- /dev/null +++ b/pkg/client/util/hostcheck.go @@ -0,0 +1,125 @@ +package util + +import ( + "context" + "errors" + "fmt" + "net" + "net/http" + "net/netip" + "net/url" + "strings" + "syscall" + "time" +) + +var ( + cgnat = netip.MustParsePrefix("100.64.0.0/10") + nat64 = netip.MustParsePrefix("64:ff9b::/96") + // Azure's wire server is link-local in spirit but not in range. + azureWire = netip.MustParseAddr("168.63.129.16") +) + +func isInternal(ip netip.Addr) bool { + ip = ip.Unmap() + if nat64.Contains(ip) { + // IPv6-only clusters reach public IPv4 hosts through NAT64: judge the embedded address. + b := ip.As16() + ip = netip.AddrFrom4([4]byte{b[12], b[13], b[14], b[15]}) + } + return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || + ip.IsLinkLocalMulticast() || ip.IsMulticast() || ip.IsUnspecified() || + cgnat.Contains(ip) || ip == azureWire +} + +// CheckPublicHost returns an error if host (optionally host:port) is, or +// resolves to, an internal address. It is a pre-flight check; use PublicOnly +// and PublicDialContext to enforce the same policy at connect time. +func CheckPublicHost(ctx context.Context, host string) error { + if err := ValidHost(host); err != nil { + return err + } + h := host + if hh, _, err := net.SplitHostPort(host); err == nil { + h = hh + } + h = strings.Trim(h, "[]") + if h == "" { + return errors.New("empty host") + } + if ip, err := netip.ParseAddr(h); err == nil { + if isInternal(ip) { + return fmt.Errorf("host %q is an internal address", host) + } + return nil + } + lh := strings.ToLower(h) + if lh == "localhost" || strings.HasSuffix(lh, ".localhost") || strings.HasSuffix(lh, ".local") || + strings.HasSuffix(lh, ".internal") || strings.HasSuffix(lh, ".svc") || strings.Contains(lh, ".svc.") { + return fmt.Errorf("host %q is an internal name", host) + } + addrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip", h) + if err != nil { + return fmt.Errorf("resolving host %q: %w", host, err) + } + for _, a := range addrs { + if isInternal(a) { + return fmt.Errorf("host %q resolves to internal address %s", host, a) + } + } + return nil +} + +// controlPublicOnly is a net.Dialer.Control hook: it sees the address after +// DNS resolution, so it also covers redirects and DNS rebinding. +func controlPublicOnly(_, address string, _ syscall.RawConn) error { + h, _, err := net.SplitHostPort(address) + if err != nil { + return err + } + ip, err := netip.ParseAddr(h) + if err != nil { + return err + } + if isInternal(ip) { + return fmt.Errorf("refusing to connect to internal address %s", ip) + } + return nil +} + +type denyKey struct{} + +// DialContext dials like a default net.Dialer but, for requests marked by +// PublicOnly, refuses internal addresses after DNS resolution. That covers +// redirects and DNS rebinding, which the pre-flight CheckPublicHost cannot. +// Behind an egress proxy the proxy does the dialing, so only PublicOnly's host +// check applies. +func DialContext(ctx context.Context, network, addr string) (net.Conn, error) { + d := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second} + if ctx.Value(denyKey{}) != nil { + probe := &http.Request{URL: &url.URL{Scheme: "https", Host: "registry.invalid"}} + if p, _ := http.ProxyFromEnvironment(probe); p == nil { + d.Control = controlPublicOnly + } + } + return d.DialContext(ctx, network, addr) +} + +type publicOnly struct{ http.RoundTripper } + +// PublicOnly wraps rt so every request, redirects included, passes +// CheckPublicHost and is dialed through DialContext's address check (rt's +// transport must use DialContext), and response bodies are capped at +// MaxResponseBytes. +func PublicOnly(rt http.RoundTripper) http.RoundTripper { return publicOnly{rt} } + +func (p publicOnly) RoundTrip(req *http.Request) (*http.Response, error) { + if err := CheckPublicHost(req.Context(), req.URL.Host); err != nil { + return nil, err + } + resp, err := p.RoundTripper.RoundTrip(req.WithContext(context.WithValue(req.Context(), denyKey{}, true))) + if err == nil { + resp.Body = http.MaxBytesReader(nil, resp.Body, MaxResponseBytes) + } + return resp, err +} diff --git a/pkg/client/util/hostcheck_test.go b/pkg/client/util/hostcheck_test.go new file mode 100644 index 00000000..4b6f4d4e --- /dev/null +++ b/pkg/client/util/hostcheck_test.go @@ -0,0 +1,50 @@ +package util + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +func TestCheckPublicHost(t *testing.T) { + for _, h := range []string{ + "127.0.0.1", "10.0.0.1:5000", "169.254.169.254", "[::1]", "localhost", "x.svc.cluster.local", + "100.64.0.1", "168.63.129.16", "[64:ff9b::a00:1]", "[::ffff:127.0.0.1]", + // URL metacharacters must not smuggle an internal host past the check. + "x@169.254.169.254", "127.0.0.1?x", "127.0.0.1#x", "kubernetes.default.svc?", + } { + if CheckPublicHost(context.Background(), h) == nil { + t.Errorf("%q should be rejected", h) + } + } + // A public IPv4 reached through NAT64 is allowed. + for _, h := range []string{"8.8.8.8", "[64:ff9b::808:808]"} { + if err := CheckPublicHost(context.Background(), h); err != nil { + t.Errorf("%q: %v", h, err) + } + } +} + +// The httptest server listens on loopback, so a guarded client must refuse it +// both before dialing (PublicOnly) and at connect time (DialContext). +func TestPublicOnlyRefusesLoopback(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) + defer srv.Close() + + tr := &http.Transport{DialContext: DialContext} + if _, err := (&http.Client{Transport: PublicOnly(tr)}).Get(srv.URL); err == nil { + t.Error("PublicOnly should refuse a loopback host") + } + + addr := srv.Listener.Addr().String() + if c, err := DialContext(context.Background(), "tcp", addr); err != nil { + t.Errorf("unmarked dial should work: %v", err) + } else { + _ = c.Close() + } + if c, err := DialContext(context.WithValue(context.Background(), denyKey{}, true), "tcp", addr); err == nil { + _ = c.Close() + t.Error("marked dial should refuse a loopback address") + } +}