Documentation
¶
Index ¶
- Constants
- func EnforceCookieSecurity(cookie *http.Cookie, config *CookieSecurityConfig) *http.Cookie
- func IsLocalhost(hostname string) bool
- func IsPrivateOrReservedIP(ip net.IP) bool
- func IsValidHeaderChar(r rune) bool
- func IsValidHeaderString(s string) bool
- func ValidateCookie(cookie *http.Cookie) error
- func ValidateCookieName(name string) error
- func ValidateCookieSecurity(cookie *http.Cookie, config *CookieSecurityConfig) error
- func ValidateCookieStrict(cookie *http.Cookie, config *CookieSecurityConfig) error
- func ValidateCookieValue(value string) error
- func ValidateCredential(cred string, maxLen int, checkColon bool, credType string) error
- func ValidateCredentialStrict(cred string, maxLen int, checkColon bool, credType string) error
- func ValidateFieldName(name string, fieldType string) error
- func ValidateHeaderKeyValue(key, value string) error
- func ValidateIP(ip net.IP) error
- func ValidateInputString(input string, maxLen int, name string, additionalChecks func(rune) error) error
- func ValidateQueryKey(key string) error
- func ValidateToken(token string) error
- func ValidateTokenStrict(token string) error
- type CookieSecurityConfig
Constants ¶
const ( MaxCredLen = 255 // Maximum credential length (username/password) MaxTokenLen = 2048 // Maximum bearer token length MaxKeyLen = 256 // Maximum query parameter key length MaxValueLen = 8192 // Maximum query parameter value length MaxFilenameLen = 256 // Maximum filename length for uploads MaxCookieNameLen = 256 MaxCookieValueLen = 4096 MaxCookieDomainLen = 255 MaxCookiePathLen = 1024 MaxHeaderKeyLen = 256 MaxHeaderValueLen = 8192 MaxURLLen = 2048 // Maximum URL length )
Common validation constants
Variables ¶
This section is empty.
Functions ¶
func EnforceCookieSecurity ¶ added in v1.3.8
func EnforceCookieSecurity(cookie *http.Cookie, config *CookieSecurityConfig) *http.Cookie
EnforceCookieSecurity applies security attributes to a cookie based on the configuration. This modifies the cookie in place to meet the security requirements. Returns the modified cookie for chaining.
func IsLocalhost ¶ added in v1.3.8
IsLocalhost detects localhost variations including: - "localhost" (case-insensitive) - 127.0.0.1 - ::1 - 0.0.0.0 - :: - 127.x.x.x range - localhost.* subdomains (case-insensitive)
Optimized to avoid repeated string allocations from strings.ToLower.
func IsPrivateOrReservedIP ¶ added in v1.3.8
IsPrivateOrReservedIP checks if an IP address is private, reserved, or otherwise not suitable for public internet communication. This is used for SSRF (Server-Side Request Forgery) protection.
SECURITY: This function handles IPv4-mapped IPv6 addresses (::ffff:x.x.x.x) to prevent bypass attempts using mixed notation.
func IsValidHeaderChar ¶ added in v1.3.6
IsValidHeaderChar checks if a character is valid in HTTP header names. Optimized with a lookup table for O(1) character validation.
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 ValidateCookie ¶ added in v1.3.7
ValidateCookie performs comprehensive validation of an HTTP cookie including name, value, domain, and path attributes.
func ValidateCookieName ¶
ValidateCookieName validates HTTP cookie names.
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 ValidateCookieStrict ¶ added in v1.3.8
func ValidateCookieStrict(cookie *http.Cookie, config *CookieSecurityConfig) error
ValidateCookieStrict validates a cookie with strict security requirements. This is a convenience function that uses StrictCookieSecurityConfig. Use this for high-security applications.
func ValidateCookieValue ¶
ValidateCookieValue validates HTTP cookie values.
func ValidateCredential ¶
ValidateCredential validates credentials for Basic Auth.
func ValidateCredentialStrict ¶ added in v1.3.8
ValidateCredentialStrict validates credentials with additional security checks for high-security scenarios (financial, medical, government). In addition to standard validation, it blocks dangerous characters commonly used in injection attacks.
Parameters:
- cred: The credential string to validate
- maxLen: Maximum allowed length
- checkColon: If true, colons are not allowed (for usernames)
- credType: Description of the credential type for error messages
Returns an error if validation fails, nil otherwise.
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 ValidateIP ¶ added in v1.3.8
ValidateIP checks if an IP is allowed for outbound requests. Returns an error if the IP is blocked by security policy.
func ValidateInputString ¶
func ValidateInputString(input string, maxLen int, name string, additionalChecks func(rune) error) error
ValidateInputString performs common string validation to prevent injection attacks.
func ValidateQueryKey ¶
ValidateQueryKey validates query parameter keys.
func ValidateToken ¶
ValidateToken validates bearer tokens according to RFC 6750.
func ValidateTokenStrict ¶ added in v1.3.8
ValidateTokenStrict validates bearer tokens with additional security checks for high-security scenarios. It blocks dangerous characters commonly used in injection attacks in addition to standard token validation.
This is recommended for financial, medical, and government applications where defense-in-depth is required.
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
}
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).