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:
1 parent
b52698ffbc
commit
bc8e934568
4 files changed
+113
-48
No files matched your search
@@ -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
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -2,35 +2,21 @@ package sso
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/netguard"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrForbiddenAddress is returned when an IdP resolves to an address the
|
// ErrForbiddenAddress is returned when an IdP resolves to an address the
|
||||||
// client may not contact.
|
// client may not contact.
|
||||||
var ErrForbiddenAddress = errors.New("sso: issuer resolves to a non-public address")
|
var ErrForbiddenAddress = netguard.ErrForbiddenAddress
|
||||||
|
|
||||||
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
|
|
||||||
|
|
||||||
// SafeHTTPClient returns a client for talking to customer-configured IdPs.
|
// SafeHTTPClient returns a client for talking to customer-configured IdPs.
|
||||||
// Unless allowPrivate is set it refuses to connect to loopback, private,
|
// Unless allowPrivate is set it refuses to connect to non-public addresses.
|
||||||
// link-local and similar ranges. The check runs on the resolved address at
|
|
||||||
// dial time, so DNS rebinding and redirects cannot bypass it.
|
|
||||||
func SafeHTTPClient(allowPrivate bool) *http.Client {
|
func SafeHTTPClient(allowPrivate bool) *http.Client {
|
||||||
d := &net.Dialer{Timeout: 5 * time.Second}
|
d := netguard.Dialer(allowPrivate, 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tr := http.DefaultTransport.(*http.Transport).Clone()
|
tr := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
tr.Proxy = nil
|
tr.Proxy = nil
|
||||||
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
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}
|
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)
|
|
||||||
}
|
|
||||||
@@ -4,33 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/netip"
|
|
||||||
"testing"
|
"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) {
|
func TestSafeHTTPClientBlocksLoopback(t *testing.T) {
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|||||||
Reference in new issue
Block a user