From bc8e93456837e3d87e079aa98e36dcb9699aa8d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 01:12:53 +0100 Subject: [PATCH] refactor: move private address guard into netguard Checks need the same protection as identity provider requests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/netguard/netguard.go | 58 ++++++++++++++++++++++++++++++ internal/netguard/netguard_test.go | 50 ++++++++++++++++++++++++++ internal/sso/httpclient.go | 29 +++------------ internal/sso/httpclient_test.go | 24 ------------- 4 files changed, 113 insertions(+), 48 deletions(-) create mode 100644 internal/netguard/netguard.go create mode 100644 internal/netguard/netguard_test.go diff --git a/internal/netguard/netguard.go b/internal/netguard/netguard.go new file mode 100644 index 0000000..4f55ea0 --- /dev/null +++ b/internal/netguard/netguard.go @@ -0,0 +1,58 @@ +// Package netguard keeps customer-supplied addresses from reaching our own +// infrastructure. Identity providers and monitored targets are configured by +// customers; in the cloud edition they must not be able to probe private +// networks through us. +package netguard + +import ( + "context" + "errors" + "net" + "net/netip" + "syscall" + "time" +) + +// ErrForbiddenAddress is returned when a destination resolves to an address +// the caller may not contact. +var ErrForbiddenAddress = errors.New("netguard: destination resolves to a non-public address") + +var cgnat = netip.MustParsePrefix("100.64.0.0/10") + +// IsPublic reports whether a is a globally routable unicast address. +func IsPublic(a netip.Addr) bool { + a = a.Unmap() + return a.IsGlobalUnicast() && !a.IsPrivate() && !cgnat.Contains(a) +} + +// Dialer returns a dialer that, unless allowPrivate is set, refuses to +// connect to non-public addresses. The check runs on the resolved address at +// connect time, so DNS rebinding and redirects cannot bypass it. +func Dialer(allowPrivate bool, timeout time.Duration) *net.Dialer { + d := &net.Dialer{Timeout: timeout} + if !allowPrivate { + d.Control = func(_, address string, _ syscall.RawConn) error { + ap, err := netip.ParseAddrPort(address) + if err != nil || !IsPublic(ap.Addr()) { + return ErrForbiddenAddress + } + return nil + } + } + return d +} + +// CheckHost resolves host and fails if any of its addresses is non-public. +// Use it where no dialer is involved, such as ICMP. +func CheckHost(ctx context.Context, host string) ([]netip.Addr, error) { + addrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host) + if err != nil { + return nil, err + } + for _, a := range addrs { + if !IsPublic(a) { + return nil, ErrForbiddenAddress + } + } + return addrs, nil +} diff --git a/internal/netguard/netguard_test.go b/internal/netguard/netguard_test.go new file mode 100644 index 0000000..47967af --- /dev/null +++ b/internal/netguard/netguard_test.go @@ -0,0 +1,50 @@ +package netguard + +import ( + "context" + "errors" + "net" + "net/netip" + "testing" + "time" +) + +func TestIsPublic(t *testing.T) { + tests := map[string]bool{ + "8.8.8.8": true, + "2606:4700::1111": true, + "127.0.0.1": false, + "10.1.2.3": false, + "172.16.0.1": false, + "192.168.1.1": false, + "169.254.169.254": false, + "100.64.0.1": false, + "0.0.0.0": false, + "::1": false, + "fd00::1": false, + "fe80::1": false, + "::ffff:127.0.0.1": false, + } + for addr, want := range tests { + if got := IsPublic(netip.MustParseAddr(addr)); got != want { + t.Errorf("IsPublic(%s) = %t, want %t", addr, got, want) + } + } +} + +func TestDialerBlocksLoopback(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer func() { _ = ln.Close() }() + ctx := context.Background() + if _, err := Dialer(false, time.Second).DialContext(ctx, "tcp", ln.Addr().String()); !errors.Is(err, ErrForbiddenAddress) { + t.Errorf("Dialer(false).Dial(loopback) error = %v, want %v", err, ErrForbiddenAddress) + } + conn, err := Dialer(true, time.Second).DialContext(ctx, "tcp", ln.Addr().String()) + if err != nil { + t.Fatalf("Dialer(true).Dial(loopback) failed: %v", err) + } + _ = conn.Close() +} diff --git a/internal/sso/httpclient.go b/internal/sso/httpclient.go index c132918..069d24b 100644 --- a/internal/sso/httpclient.go +++ b/internal/sso/httpclient.go @@ -2,35 +2,21 @@ package sso import ( "context" - "errors" "net" "net/http" - "net/netip" - "syscall" "time" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/netguard" ) // ErrForbiddenAddress is returned when an IdP resolves to an address the // client may not contact. -var ErrForbiddenAddress = errors.New("sso: issuer resolves to a non-public address") - -var cgnat = netip.MustParsePrefix("100.64.0.0/10") +var ErrForbiddenAddress = netguard.ErrForbiddenAddress // SafeHTTPClient returns a client for talking to customer-configured IdPs. -// Unless allowPrivate is set it refuses to connect to loopback, private, -// link-local and similar ranges. The check runs on the resolved address at -// dial time, so DNS rebinding and redirects cannot bypass it. +// Unless allowPrivate is set it refuses to connect to non-public addresses. func SafeHTTPClient(allowPrivate bool) *http.Client { - d := &net.Dialer{Timeout: 5 * time.Second} - if !allowPrivate { - d.Control = func(_, address string, _ syscall.RawConn) error { - ap, err := netip.ParseAddrPort(address) - if err != nil || !isPublic(ap.Addr()) { - return ErrForbiddenAddress - } - return nil - } - } + d := netguard.Dialer(allowPrivate, 5*time.Second) tr := http.DefaultTransport.(*http.Transport).Clone() tr.Proxy = nil tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { @@ -38,8 +24,3 @@ func SafeHTTPClient(allowPrivate bool) *http.Client { } return &http.Client{Transport: tr, Timeout: 10 * time.Second} } - -func isPublic(a netip.Addr) bool { - a = a.Unmap() - return a.IsGlobalUnicast() && !a.IsPrivate() && !cgnat.Contains(a) -} diff --git a/internal/sso/httpclient_test.go b/internal/sso/httpclient_test.go index 226344d..11712e2 100644 --- a/internal/sso/httpclient_test.go +++ b/internal/sso/httpclient_test.go @@ -4,33 +4,9 @@ import ( "errors" "net/http" "net/http/httptest" - "net/netip" "testing" ) -func TestIsPublic(t *testing.T) { - tests := map[string]bool{ - "8.8.8.8": true, - "2606:4700::1111": true, - "127.0.0.1": false, - "10.1.2.3": false, - "172.16.0.1": false, - "192.168.1.1": false, - "169.254.169.254": false, - "100.64.0.1": false, - "0.0.0.0": false, - "::1": false, - "fd00::1": false, - "fe80::1": false, - "::ffff:127.0.0.1": false, - } - for addr, want := range tests { - if got := isPublic(netip.MustParseAddr(addr)); got != want { - t.Errorf("isPublic(%s) = %t, want %t", addr, got, want) - } - } -} - func TestSafeHTTPClientBlocksLoopback(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) defer srv.Close()