Documentation
¶
Overview ¶
Package ssrf provides shared SSRF guards for outbound transports.
Package ssrf provides shared Server-Side Request Forgery (SSRF) guards for outbound WebDAV / backup transports.
Both the admin settings handler (handler/admin) and the backup scheduler previously carried byte-identical copies of this logic under different names (rejectUnsafeWebdavDialHost / rejectUnsafeBackupWebdavDialHost, etc.). Centralizing it here keeps the two call sites from drifting: a fix to the unsafe-range predicate or the TOCTOU-aware dial check lands in one place.
Each call site passes its own allowPrivate flag so operator/test escape hatches (e.g. pointing a WebDAV backup at a localhost test server) remain independent per feature without coupling the two packages.
Index ¶
- func IsAllowedWebdavTargetHost(host string, allowPrivate bool) bool
- func IsForbiddenSiteAddr(addr netip.Addr) bool
- func IsForbiddenSiteHostname(host string) bool
- func IsPrivateOrLoopbackLiteral(host string) bool
- func IsUnsafeAddr(addr netip.Addr) bool
- func RejectUnsafeWebdavDialHost(ctx context.Context, host string, allowPrivate bool) error
- type DialContextFunc
- type NetIPResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAllowedWebdavTargetHost ¶
IsAllowedWebdavTargetHost applies hostname-level allow/deny rules before DNS resolution. A bare hostname is allowed (its IPs are checked at dial time by RejectUnsafeWebdavDialHost). Literal IP literals are allowed only when they are not in an unsafe range. "localhost" and *.localhost are always denied. allowPrivate=true permits all hosts (operator escape hatch).
func IsForbiddenSiteAddr ¶ added in v0.16.10
IsForbiddenSiteAddr applies the product's site-upstream policy at dial time. It rejects link-local, multicast, unspecified/reserved, and explicit metadata addresses while preserving loopback, RFC1918, and IPv6 ULA connectivity.
func IsForbiddenSiteHostname ¶ added in v0.16.10
IsForbiddenSiteHostname reports hostnames that are metadata service aliases independent of DNS. Literal IPs are classified by IsForbiddenSiteAddr. Loopback and RFC1918/ULA addresses deliberately remain allowed because Metapi supports operator-hosted upstreams on local networks.
func IsPrivateOrLoopbackLiteral ¶
IsPrivateOrLoopbackLiteral reports whether host is a literal IP address in a private, loopback, link-local, multicast, or unspecified range. This covers:
- Loopback: 127.0.0.0/8, ::1
- Link-local: 169.254.0.0/16 (incl. cloud metadata 169.254.169.254), fe80::/10
- Private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
- Unspecified: 0.0.0.0/8, ::
Hostnames that are not literal IPs return false: DNS resolution for hostnames is deferred to the dial-time guard (RejectUnsafeWebdavDialHost) to avoid TOCTOU races (a hostname that resolves to a safe IP at validation time could resolve to a private IP by dial time, and vice versa).
func IsUnsafeAddr ¶
IsUnsafeAddr reports whether an IP address falls in an unsafe range for outbound SSRF-sensitive traffic: unspecified, loopback, private, link-local unicast, link-local multicast, or multicast.
The whole 0.0.0.0/8 block is treated as unsafe, not just the exact unspecified address: RFC 5735 defines 0.0.0.0/8 as "this network", and several kernels/containers route it to the local stack. netip's IsUnspecified() only matches the all-zero address, so the first octet is checked explicitly to match the documented intent.
func RejectUnsafeWebdavDialHost ¶
RejectUnsafeWebdavDialHost validates a host immediately before a network dial. It applies hostname-level allow/deny rules and then resolves the host to verify that none of the resolved IPs fall in an unsafe range, preventing the TOCTOU race where a hostname resolves to a safe IP at validation time but a private IP by dial time (and vice versa).
allowPrivate=true disables the guard entirely (operator escape hatch / test isolation for httptest servers on 127.0.0.1).
Types ¶
type DialContextFunc ¶ added in v0.16.10
DialContextFunc matches net.Dialer's DialContext method.
func NewSiteDialContext ¶ added in v0.16.10
func NewSiteDialContext(resolver NetIPResolver, dial DialContextFunc) DialContextFunc
NewSiteDialContext returns a DNS-pinning DialContext for operator-configured site upstreams. Hostnames are resolved exactly once, every answer is checked, and the connection is made to the already-checked IP rather than asking the underlying dialer to resolve the hostname again. This closes the usual DNS rebinding / validation-to-dial window.
type NetIPResolver ¶ added in v0.16.10
type NetIPResolver interface {
LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
}
NetIPResolver is the subset of net.Resolver used by the site dial guard. It is intentionally injectable so tests can exercise DNS alias behavior without relying on public DNS.