httpsec

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: GPL-3.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func IsIPBlocked(ip net.IP) bool

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 SafeHTTPClient

func SafeHTTPClient(timeout time.Duration) *http.Client

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

func ValidateHost(ctx context.Context, host string) error

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

type ValidationResult struct {
	URL         *url.URL
	ResolvedIPs []net.IP
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL