Documentation
¶
Overview ¶
Package security provides request validation, SSRF protection, domain whitelisting, and certificate pinning for the httpc library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CertificatePinner ¶ added in v1.3.8
type CertificatePinner interface {
// Pin returns a string representation of the pin for logging/debugging
Pin() string
// VerifyPeerCertificate verifies the peer certificate chain against the pin.
// rawCerts contains the ASN.1 DER-encoded certificates.
// verifiedChains contains the verified certificate chains (may be nil if verification was skipped).
VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
}
CertificatePinner defines the interface for certificate pinning implementations. Certificate pinning protects against MITM attacks by verifying that the server's certificate matches a known, expected certificate or public key.
type Config ¶
type Config struct {
ValidateURL bool
ValidateHeaders bool
MaxResponseBodySize int64
MaxRequestBodySize int64
AllowPrivateIPs bool
ExemptNets []*net.IPNet
}
Config defines security validation settings.
type DomainWhitelist ¶ added in v1.3.8
type DomainWhitelist struct {
// contains filtered or unexported fields
}
DomainWhitelist manages a list of allowed domains for redirect validation. It supports exact matches and wildcard patterns (e.g., *.example.com).
func NewDomainWhitelist ¶ added in v1.3.8
func NewDomainWhitelist(domains ...string) *DomainWhitelist
NewDomainWhitelist creates a new DomainWhitelist from a list of domains. Domains can be:
- Exact matches: "example.com", "api.example.com"
- Wildcard patterns: "*.example.com" matches any subdomain of example.com
Example:
whitelist := security.NewDomainWhitelist("example.com", "*.trusted.org")
func (*DomainWhitelist) Add ¶ added in v1.3.8
func (w *DomainWhitelist) Add(domain string)
Add adds a domain to the whitelist. Thread-safe: can be called while the whitelist is in use.
func (*DomainWhitelist) Domains ¶ added in v1.3.8
func (w *DomainWhitelist) Domains() (exact []string, wildcards []string)
Domains returns a copy of all domains in the whitelist. Returns two slices: exact matches and wildcard patterns.
func (*DomainWhitelist) IsAllowed ¶ added in v1.3.8
func (w *DomainWhitelist) IsAllowed(hostname string) bool
IsAllowed checks if a hostname is in the whitelist. The hostname is checked against exact matches first, then wildcards. Returns true if the hostname is allowed, false otherwise.
func (*DomainWhitelist) Remove ¶ added in v1.3.8
func (w *DomainWhitelist) Remove(domain string)
Remove removes a domain from the whitelist. Thread-safe: can be called while the whitelist is in use.
type Request ¶
type Request struct {
Method string
URL string
Headers map[string]string
QueryParams map[string]any
Body any
}
Request represents a security validation request with method, URL, headers, and body.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator validates HTTP requests for URL, header, and SSRF security.
func NewValidator ¶
func NewValidator() *Validator
NewValidator creates a new Validator with default security settings.
func NewValidatorWithConfig ¶
NewValidatorWithConfig creates a new Validator with the given security configuration.
func (*Validator) ValidateRequest ¶
ValidateRequest validates an HTTP request against the configured security rules.