refactor: move private address guard into netguard

Checks need the same protection as identity provider requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 01:12:53 +01:00
1 parent b52698ffbc
commit bc8e934568
4 files changed
+113 -48

No files matched your search

+58
View File
@@ -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
}
+50
View File
@@ -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()
}