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

+5 -24
View File
@@ -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)
}
-24
View File
@@ -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()