security

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 12 Imported by: 0 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 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.

func NewCertificatePinnerChain added in v1.3.8

func NewCertificatePinnerChain(pinners ...CertificatePinner) CertificatePinner

NewCertificatePinnerChain combines multiple pinners; the certificate is accepted if ANY pinner accepts it. This enables key rotation by pinning both the current and next keys. Exported for use by the public httpc package.

func NewPublicKeyPinner added in v1.3.8

func NewPublicKeyPinner(publicKeys ...[]byte) (CertificatePinner, error)

NewPublicKeyPinner creates a CertificatePinner from one or more DER-encoded PKIX public keys (as returned by x509.MarshalPKIXPublicKey). Exported for use by the public httpc package.

PIN BASIS CAVEAT: verification hashes the certificate's raw SubjectPublicKeyInfo bytes (cert.RawSubjectPublicKeyInfo), and re-serializing a parsed key with x509.MarshalPKIXPublicKey can produce different DER (e.g. a non-canonical AlgorithmIdentifier), which would falsely reject the pin. Derive pins from the certificate's RawSubjectPublicKeyInfo bytes directly for maximum fidelity.

func NewSPKIHashPinner added in v1.3.8

func NewSPKIHashPinner(hashes ...string) (CertificatePinner, error)

NewSPKIHashPinner creates a CertificatePinner from one or more base64-encoded SHA-256 hashes of DER-encoded SubjectPublicKeyInfo (SPKI). This is the standard HPKP pin format. Exported for use by the public httpc package.

type Config

type Config struct {
	ValidateURL        bool
	ValidateHeaders    bool
	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 strict subdomain of example.com (e.g. api.example.com), but NOT the bare apex example.com. To allow both the apex and its subdomains, list both explicitly: "example.com", "*.example.com".

INVARIANT (deliberate, fail-closed): calling with NO domains returns a whitelist that DENIES every host — an empty whitelist means "nothing is allowed", not "everything is allowed". This differs from a nil *DomainWhitelist receiver, whose IsAllowed returns true for all hosts ("no whitelist configured = no restriction"). The engine only installs a whitelist when the config lists at least one domain (config_convert.go), preserving both semantics.

Example:

whitelist := security.NewDomainWhitelist("example.com", "*.trusted.org")

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.

type Request

type Request struct {
	URL     string
	Headers map[string]string
	Body    any
	// AllowPrivateIPs carries a per-request override of the client's SSRF policy.
	// A non-nil value overrides the Validator's configured AllowPrivateIPs for this
	// request only. nil means "use the client-level policy".
	AllowPrivateIPs *bool
}

Request represents a security validation request with 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 NewValidatorWithConfig

func NewValidatorWithConfig(config *Config) *Validator

NewValidatorWithConfig creates a new Validator with the given security configuration.

func (*Validator) ValidateRequest

func (v *Validator) ValidateRequest(req *Request) error

ValidateRequest validates an HTTP request against the configured security rules.

Jump to

Keyboard shortcuts

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