Documentation
¶
Overview ¶
Package httpsec — client IP extraction with trusted-proxy enforcement.
SECURITY (S-4): The previous implementations of getClientIP in middleware/ratelimit.go and handler/local_auth_handler.go honored X-Real-IP / X-Forwarded-For from any peer. That let attackers spoof IPs to defeat per-IP rate limits and to corrupt audit logs (login attempts, password resets recorded under fake IPs).
This package centralises the logic and only honors the proxy headers when the immediate TCP peer (r.RemoteAddr) sits inside a configured trusted CIDR. For requests originating outside that CIDR the headers are ignored and r.RemoteAddr wins.
Package httpsec provides SSRF-safe URL validation and HTTP client construction. Any code path that fetches a URL chosen at runtime by a tenant (webhook delivery, avatar import, OAuth callback, SCM discovery) MUST pipe the URL through ValidateURL before dialing, otherwise an attacker who controls that URL can point it at the cloud metadata service (169.254.169.254) or internal RFC1918 ranges and read back secrets via response echo.
The blocklist is deliberately conservative — prod deployments rarely need to POST to 10.x or 172.16.x from the API container, and when they do, the right fix is a per-tenant allowlist, not loosening this file.
This package is the canonical SSRF guard. internal/infra/fetchers/ has an older duplicate with identical ranges; follow-up work should consolidate that callsite onto this package.
Index ¶
- func AllowPrivate() bool
- func ClientIP(r *http.Request, trusted *TrustedProxySet) string
- func IsIPBlocked(ip net.IP) bool
- func ResolveSafeHost(ctx context.Context, host string) (net.IP, error)
- func SafeHTTPClient(timeout time.Duration) *http.Client
- func ValidateHost(ctx context.Context, host string) error
- type TrustedProxySet
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowPrivate ¶
func AllowPrivate() bool
AllowPrivate reports whether the RFC1918 / ULA ranges are currently treated as reachable. Exposed for log-at-startup observability; do not consult this to decide individual calls — that branches inside IsIPBlocked.
func ClientIP ¶ added in v0.3.0
func ClientIP(r *http.Request, trusted *TrustedProxySet) string
ClientIP returns the apparent client IP. If the immediate TCP peer (r.RemoteAddr) is inside the trusted-proxy set, it honors X-Real-IP and the leftmost X-Forwarded-For entry. Otherwise it returns the TCP peer.
Returns an empty string only if r.RemoteAddr is malformed.
func IsIPBlocked ¶
IsIPBlocked reports whether the given IP is not reachable under the current policy. Hard-blocked CIDRs always return true; the RFC1918 / ULA block is conditional on allowPrivate.
func ResolveSafeHost ¶ added in v0.4.0
ResolveSafeHost validates host (a bare hostname or host:port) exactly like ValidateHost — every resolved A/AAAA record must be out of the blocked ranges under the current policy — and additionally returns one safe resolved IP. Callers that dial a non-HTTP target (e.g. SMTP) should dial this pinned IP rather than re-resolve the hostname at dial time; re-resolving reopens a DNS-rebinding TOCTOU window where the second lookup returns an internal IP. Fail-closed on DNS resolution failure.
func SafeHTTPClient ¶
SafeHTTPClient returns an *http.Client whose dialer rejects any connection attempt to a blocked CIDR at the transport layer. This is the belt to ValidateURL's braces: even if a caller forgets to validate up front, the dial will fail closed. Use this as the default http.Client for tenant-facing outbound HTTP.
Callers should still ValidateURL themselves because the dialer-only check happens AFTER DNS resolution, so a request to a tenant-supplied URL will have burned a DNS lookup and possibly emitted it to a DNS server the attacker controls. ValidateURL rejects before the lookup leaves the host process.
func ValidateHost ¶ added in v0.3.0
ValidateHost resolves host (a bare hostname or host:port) and rejects it if any resolved A/AAAA record falls in a blocked CIDR, under the same policy as the HTTP SSRF guard. For non-HTTP outbound targets such as SMTP relays. Fail-closed on DNS resolution failure. Internal targets (RFC1918) are only permitted when the operator sets the allow-private flag (same as webhooks).
Types ¶
type TrustedProxySet ¶ added in v0.3.0
type TrustedProxySet struct {
// contains filtered or unexported fields
}
TrustedProxySet holds a parsed allowlist of CIDR ranges that the API trusts to populate forwarding headers. Construct once at startup.
func NewTrustedProxySet ¶ added in v0.3.0
func NewTrustedProxySet(entries []string) *TrustedProxySet
NewTrustedProxySet parses a list of CIDR strings (or bare IPs). Invalid entries are silently dropped; callers should validate up-front during config parsing if strict mode is desired.
func (*TrustedProxySet) Contains ¶ added in v0.3.0
func (s *TrustedProxySet) Contains(ip net.IP) bool
Contains reports whether ip is inside any trusted CIDR.
func (*TrustedProxySet) IsEmpty ¶ added in v0.3.0
func (s *TrustedProxySet) IsEmpty() bool
IsEmpty reports whether the allowlist contains zero CIDRs (i.e. no proxy is trusted, behave as if directly Internet-facing).
type ValidationResult ¶
ValidationResult carries the parsed URL + the DNS-pinned IP set so callers that want to prevent DNS rebinding can dial one of the resolved IPs rather than re-resolve at dial time.
func ValidateURL ¶
func ValidateURL(rawURL string) (*ValidationResult, error)
ValidateURL parses rawURL, confirms scheme is http/https, blocks common dangerous hostnames, resolves DNS and rejects if any A/AAAA hits a blocked CIDR. Returns the parsed URL + pinned IPs. On any failure, returns an error — callers MUST NOT proceed to dial.
Fail-closed on DNS lookup failure: if we cannot resolve, we cannot verify the target is safe, so the request is rejected.