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)
}