Documentation
¶
Overview ¶
Package validation provides input sanitization and validation utilities for credentials, headers, cookies, URLs, and network addresses.
Index ¶
- Constants
- func ContainsFold(s, substr string) bool
- func EqualFold(s, t string) bool
- func FilterAllowedIPs(ips []net.IP, exemptNets []*net.IPNet) []net.IP
- func HasSensitiveQueryParams(query string) bool
- func IsSensitiveQueryParam(name string) bool
- func IsValidHeaderString(s string) bool
- func SanitizeURL(urlStr string) string
- func ValidateAndParseURL(urlStr string) (*url.URL, error)
- func ValidateCookie(cookie *http.Cookie) error
- func ValidateCookieSecurity(cookie *http.Cookie, config *CookieSecurityConfig) error
- func ValidateCredential(cred string, maxLen int, checkColon bool, credType string) error
- func ValidateFieldName(name string, fieldType string) error
- func ValidateHeaderKeyValue(key, value string) error
- func ValidateIPWithExemptions(ip net.IP, exemptNets []*net.IPNet) error
- func ValidateProxyURL(rawURL string) (*url.URL, error)
- func ValidateQueryKey(key string) error
- func ValidateSSRFHost(host string, exemptNets []*net.IPNet, resolveDNS bool) error
- func ValidateToken(token string) error
- type CookieSecurityConfig
Constants ¶
const ( // MaxCredLen is the maximum allowed length for credentials (username/password). MaxCredLen = 255 // MaxTokenLen is the maximum allowed length for bearer tokens. MaxTokenLen = 2048 // MaxKeyLen is the maximum allowed length for query parameter keys. MaxKeyLen = 256 // MaxValueLen is the maximum allowed length for query parameter values. MaxValueLen = 8192 // MaxFilenameLen is the maximum allowed filename length for file uploads. MaxFilenameLen = 256 // MaxCookieNameLen is the maximum allowed length for cookie names. MaxCookieNameLen = 256 // MaxCookieValueLen is the maximum allowed length for cookie values. MaxCookieValueLen = 4096 // MaxCookieDomainLen is the maximum allowed length for cookie domains. MaxCookieDomainLen = 255 // MaxCookiePathLen is the maximum allowed length for cookie paths. MaxCookiePathLen = 1024 // MaxHeaderKeyLen is the maximum allowed length for HTTP header keys. MaxHeaderKeyLen = 256 // MaxHeaderValueLen is the maximum allowed length for HTTP header values. MaxHeaderValueLen = 8192 )
Validation length limits for credentials, query parameters, cookies, headers, and filenames used across the validation package.
Variables ¶
This section is empty.
Functions ¶
func ContainsFold ¶ added in v1.4.4
ContainsFold reports whether substr is contained within s, ASCII case-insensitively. Zero-allocation alternative to strings.Contains(strings.ToLower(s), substr).
func EqualFold ¶ added in v1.4.4
EqualFold checks if s equals t case-insensitively (ASCII only). Avoids allocation from strings.ToLower.
func FilterAllowedIPs ¶ added in v1.4.1
FilterAllowedIPs filters a list of IPs to only those allowed under SSRF rules. Private/reserved IPs are excluded unless they match an exempt CIDR range. Returns nil slice when no IPs are allowed. This supports Split-Horizon DNS environments where a domain may resolve to both public and private IPs — only public (or exempted) IPs are used for dialing.
func HasSensitiveQueryParams ¶ added in v1.5.0
HasSensitiveQueryParams checks if any key-value pair in the query string has a sensitive parameter name. Uses byte-level ASCII lowercase to avoid allocations from strings.ToLower.
func IsSensitiveQueryParam ¶ added in v1.4.0
IsSensitiveQueryParam reports whether the given query parameter name is considered sensitive and should be redacted from logs and cache keys.
func IsValidHeaderString ¶ added in v1.3.8
IsValidHeaderString checks if a string contains only valid characters for HTTP headers. It returns true if the string contains no control characters (except tab), DEL, or CR/LF. Optimized to use byte-level checks instead of rune iteration.
func SanitizeURL ¶ added in v1.3.9
SanitizeURL removes credentials and redacts sensitive query parameters from a URL for safe logging. URLs with credentials are transformed from user:pass@host to ***:***@host. Sensitive query parameters (token, api_key, password, etc.) have their values replaced with [REDACTED].
Returns the original string if the URL cannot be parsed.
This function is used to prevent credential leakage in:
- Log messages
- Error messages
- Audit events
- Debug output
Example:
SanitizeURL("https://user:pass@example.com/path?token=secret")
// Returns "https://***:***@example.com/path?token=[REDACTED]"
func ValidateAndParseURL ¶ added in v1.4.1
ValidateAndParseURL validates a URL and returns the parsed result. This avoids callers needing to parse the URL again after validation.
func ValidateCookie ¶ added in v1.3.7
ValidateCookie performs comprehensive validation of an HTTP cookie including name, value, domain, and path attributes.
func ValidateCookieSecurity ¶ added in v1.3.8
func ValidateCookieSecurity(cookie *http.Cookie, config *CookieSecurityConfig) error
ValidateCookieSecurity validates a cookie against the security configuration. Returns an error if the cookie does not meet the security requirements. This is the standard validation for general use.
func ValidateCredential ¶
ValidateCredential validates credentials for Basic Auth.
func ValidateFieldName ¶
ValidateFieldName validates form field names and filenames. It checks for dangerous characters and path traversal patterns to prevent directory traversal attacks and injection vulnerabilities.
func ValidateHeaderKeyValue ¶
ValidateHeaderKeyValue validates HTTP header keys and values.
func ValidateIPWithExemptions ¶ added in v1.4.0
ValidateIPWithExemptions checks if an IP is allowed, considering exempt CIDR ranges. IPs that are private/reserved but match an exempt CIDR are allowed.
func ValidateProxyURL ¶ added in v1.5.3
ValidateProxyURL parses and validates a proxy URL string, returning the parsed URL so callers do not need to re-parse.
Accepted schemes: http, https, socks5, socks5h. The socks5 schemes are supported natively by net/http.Transport when set via transport.Proxy = http.ProxyURL(u), so they must not be rejected here.
This is the single source of truth for proxy URL validation, shared by the public Config validator (ValidateConfig) and the internal connection pool (NewPoolManager). Centralizing it prevents the two layers from drifting — which is what previously let the public layer accept socks5 while the pool rejected it with a different error.
func ValidateQueryKey ¶
ValidateQueryKey validates query parameter keys.
func ValidateSSRFHost ¶ added in v1.4.1
ValidateSSRFHost checks whether a hostname (which may include a port) should be blocked under SSRF protection rules. It checks localhost, direct IP addresses, and optionally resolves DNS for domain names. Returns nil if the host is allowed, or an error describing why it was blocked.
func ValidateToken ¶
ValidateToken validates bearer tokens according to RFC 6750.
Types ¶
type CookieSecurityConfig ¶ added in v1.3.8
type CookieSecurityConfig struct {
// RequireSecure requires the Secure attribute to be set.
// Secure cookies are only sent over HTTPS connections.
// Recommended: true for production environments.
RequireSecure bool
// RequireHttpOnly requires the HttpOnly attribute to be set.
// HttpOnly cookies cannot be accessed via JavaScript, preventing XSS attacks.
// Recommended: true for session cookies.
RequireHttpOnly bool
// RequireSameSite requires a specific SameSite attribute.
// Valid values: "Strict", "Lax", "None", "" (empty means no requirement).
// - Strict: Cookie only sent in first-party context (most secure)
// - Lax: Cookie sent in first-party context and top-level navigations
// - None: Cookie sent in all contexts (requires Secure)
RequireSameSite string
// AllowSameSiteNone allows SameSite=None attribute.
// This is useful for cross-site cookies but reduces security.
// If false and RequireSameSite is empty, cookies with SameSite=None are rejected.
// Recommended: false for high-security applications.
AllowSameSiteNone bool
// RequireSecureForSameSiteNone requires the Secure attribute when
// SameSite=None. Modern browsers reject SameSite=None without Secure,
// so this catches misconfigured cookies early. Default: true.
RequireSecureForSameSiteNone bool
}
CookieSecurityConfig defines the security requirements for cookies. This is used to enforce security attributes like Secure, HttpOnly, and SameSite to protect against CSRF, XSS, and session hijacking attacks.
func DefaultCookieSecurityConfig ¶ added in v1.3.8
func DefaultCookieSecurityConfig() *CookieSecurityConfig
DefaultCookieSecurityConfig returns a recommended security configuration. This provides a good balance between security and compatibility.
func StrictCookieSecurityConfig ¶ added in v1.3.8
func StrictCookieSecurityConfig() *CookieSecurityConfig
StrictCookieSecurityConfig returns a strict security configuration. Use this for high-security applications (financial, medical, government).