Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
160 lines
4.9 KiB
Go
160 lines
4.9 KiB
Go
// Package safedial makes HTTP clients that refuse to reach addresses a
|
|
// fetch on someone else's behalf has no business reaching: a cloud's
|
|
// instance metadata, this machine, the network behind it.
|
|
//
|
|
// The check is made on the address actually dialled, after resolution and on
|
|
// every redirect, so a name that resolves somewhere public and then somewhere
|
|
// private is caught too. IPv6 addresses that carry an IPv4 one — NAT64,
|
|
// 6to4, IPv4-mapped and -compatible — are judged by the IPv4 address inside.
|
|
package safedial
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Policy is what may be reached besides the internet.
|
|
type Policy struct {
|
|
// Private allows RFC 1918, unique-local and carrier-grade NAT addresses:
|
|
// a control plane's own mirror lives on one.
|
|
Private bool
|
|
// Loopback allows this machine.
|
|
Loopback bool
|
|
// LinkLocal allows link-local addresses, instance metadata's included:
|
|
// only for a mirror told to with --allow-private-sources.
|
|
LinkLocal bool
|
|
}
|
|
|
|
// ErrBlocked is a fetch refused for where it would go.
|
|
var ErrBlocked = errors.New("refusing to reach a loopback, private or link-local address")
|
|
|
|
var (
|
|
cgnat = netip.MustParsePrefix("100.64.0.0/10")
|
|
nat64 = netip.MustParsePrefix("64:ff9b::/96")
|
|
nat64l = netip.MustParsePrefix("64:ff9b:1::/48")
|
|
sixTo4 = netip.MustParsePrefix("2002::/16")
|
|
compat = netip.MustParsePrefix("::/96")
|
|
)
|
|
|
|
// Blocked reports whether p refuses an address.
|
|
func Blocked(a netip.Addr, p Policy) bool {
|
|
a = a.Unmap()
|
|
if v4, ok := embedded(a); ok {
|
|
a = v4
|
|
}
|
|
switch {
|
|
case a.IsLinkLocalUnicast(), a.IsLinkLocalMulticast():
|
|
// Instance metadata lives at 169.254.169.254, and nothing a model is
|
|
// published from does.
|
|
return !p.LinkLocal
|
|
case a.IsMulticast(), a.IsUnspecified(), a.IsInterfaceLocalMulticast():
|
|
return !p.LinkLocal
|
|
case a.IsLoopback():
|
|
return !p.Loopback
|
|
case a.IsPrivate(), cgnat.Contains(a):
|
|
return !p.Private
|
|
}
|
|
return false
|
|
}
|
|
|
|
// embedded is the IPv4 address an IPv6 one carries, if it carries one.
|
|
func embedded(a netip.Addr) (netip.Addr, bool) {
|
|
if !a.Is6() {
|
|
return netip.Addr{}, false
|
|
}
|
|
b := a.As16()
|
|
switch {
|
|
case nat64.Contains(a):
|
|
return netip.AddrFrom4([4]byte(b[12:16])), true
|
|
case nat64l.Contains(a):
|
|
// RFC 8215's local-use prefix: the address in the last 32 bits too.
|
|
return netip.AddrFrom4([4]byte(b[12:16])), true
|
|
case sixTo4.Contains(a):
|
|
return netip.AddrFrom4([4]byte(b[2:6])), true
|
|
case compat.Contains(a) && !a.IsLoopback() && !a.IsUnspecified():
|
|
return netip.AddrFrom4([4]byte(b[12:16])), true
|
|
}
|
|
return netip.Addr{}, false
|
|
}
|
|
|
|
// Client is an HTTP client under a policy. timeout bounds a whole request;
|
|
// zero leaves it to the context, for downloads tens of gigabytes long.
|
|
//
|
|
// A proxy from the environment is used, since sites reach the internet
|
|
// through one; the dial check then sees only the proxy, so the target's own
|
|
// addresses are checked before the request is handed to it.
|
|
func Client(p Policy, timeout time.Duration) *http.Client {
|
|
d := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
|
|
d.Control = func(_, address string, _ syscall.RawConn) error {
|
|
host, _, err := net.SplitHostPort(address)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a, err := netip.ParseAddr(host)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if Blocked(a, p) {
|
|
return fmt.Errorf("%w (%s)", ErrBlocked, a)
|
|
}
|
|
return nil
|
|
}
|
|
return &http.Client{
|
|
Timeout: timeout,
|
|
Transport: &http.Transport{
|
|
Proxy: proxy(p),
|
|
DialContext: d.DialContext,
|
|
TLSHandshakeTimeout: 30 * time.Second,
|
|
ResponseHeaderTimeout: 2 * time.Minute,
|
|
ForceAttemptHTTP2: true,
|
|
},
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
if len(via) >= 10 {
|
|
return errors.New("too many redirects")
|
|
}
|
|
return CheckScheme(req.URL)
|
|
},
|
|
}
|
|
}
|
|
|
|
// proxy is the environment's proxy, for targets the policy allows.
|
|
func proxy(p Policy) func(*http.Request) (*url.URL, error) {
|
|
return func(req *http.Request) (*url.URL, error) {
|
|
u, err := http.ProxyFromEnvironment(req)
|
|
if err != nil || u == nil {
|
|
return u, err
|
|
}
|
|
addrs, err := net.DefaultResolver.LookupNetIP(req.Context(), "ip", req.URL.Hostname())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, a := range addrs {
|
|
if Blocked(a, p) {
|
|
return nil, fmt.Errorf("%w (%s)", ErrBlocked, a)
|
|
}
|
|
}
|
|
return u, nil
|
|
}
|
|
}
|
|
|
|
// CheckScheme allows the two schemes a model is published over. file:// and
|
|
// the rest would read this machine's own disk.
|
|
func CheckScheme(u *url.URL) error {
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
|
return fmt.Errorf("%q is not an http or https source", u.Redacted())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ControlPlane is the policy for fetches a control plane makes on an
|
|
// operator's word — checking that a model's source is there, reading a
|
|
// GGUF's header: the internet, and the cluster's own network, where its
|
|
// mirror is, but not instance metadata or anything link-local.
|
|
var ControlPlane = Policy{Private: true, Loopback: true}
|