security

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 9 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateRedirectWhitelist added in v1.3.8

func ValidateRedirectWhitelist(targetURL *url.URL, whitelist *DomainWhitelist) error

ValidateRedirectWhitelist validates that a redirect target URL is allowed based on the provided whitelist. Returns an error if the redirect target hostname is not in the whitelist.

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 CertificatePinnerChain added in v1.3.8

type CertificatePinnerChain struct {
	// contains filtered or unexported fields
}

CertificatePinnerChain combines multiple pinners. A certificate is considered valid if ANY of the pinners accepts it.

func NewCertificatePinnerChain added in v1.3.8

func NewCertificatePinnerChain(pinners ...CertificatePinner) *CertificatePinnerChain

NewCertificatePinnerChain creates a new chain of pinners. A certificate is valid if ANY pinner accepts it.

func (*CertificatePinnerChain) Pin added in v1.3.8

func (c *CertificatePinnerChain) Pin() string

Pin returns a description of all pinners in the chain.

func (*CertificatePinnerChain) VerifyPeerCertificate added in v1.3.8

func (c *CertificatePinnerChain) VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error

VerifyPeerCertificate verifies the certificate against all pinners. Returns nil if ANY pinner accepts the certificate.

type Config

type Config struct {
	ValidateURL         bool
	ValidateHeaders     bool
	MaxResponseBodySize int64
	AllowPrivateIPs     bool
}

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 NoOpPinner added in v1.3.8

type NoOpPinner struct{}

NoOpPinner is a pinner that accepts all certificates. Useful for disabling pinning while keeping the interface.

func (*NoOpPinner) Pin added in v1.3.8

func (n *NoOpPinner) Pin() string

Pin returns "no-op" to indicate no pinning.

func (*NoOpPinner) VerifyPeerCertificate added in v1.3.8

func (n *NoOpPinner) VerifyPeerCertificate(_ [][]byte, _ [][]*x509.Certificate) error

VerifyPeerCertificate always returns nil, accepting all certificates.

type PublicKeyPinner added in v1.3.8

type PublicKeyPinner struct {
	// contains filtered or unexported fields
}

PublicKeyPinner pins one or more public keys. The peer certificate's public key must match one of the pinned keys.

func NewPublicKeyPinner added in v1.3.8

func NewPublicKeyPinner(publicKeys ...[]byte) *PublicKeyPinner

NewPublicKeyPinner creates a new pinner from raw public keys. Each public key should be in DER-encoded PKIX format (as used in x509 certificates).

func NewPublicKeyPinnerFromBase64 added in v1.3.8

func NewPublicKeyPinnerFromBase64(hashes ...string) *PublicKeyPinner

NewPublicKeyPinnerFromBase64 creates a new pinner from base64-encoded public key hashes. Each hash should be the SHA-256 hash of the DER-encoded public key, base64-encoded.

func (*PublicKeyPinner) Pin added in v1.3.8

func (p *PublicKeyPinner) Pin() string

Pin returns a description of the pinned public keys.

func (*PublicKeyPinner) VerifyPeerCertificate added in v1.3.8

func (p *PublicKeyPinner) VerifyPeerCertificate(rawCerts [][]byte, _ [][]*x509.Certificate) error

VerifyPeerCertificate verifies that one of the peer certificates matches a pinned public key.

type Request

type Request struct {
	Method      string
	URL         string
	Headers     map[string]string
	QueryParams map[string]any
	Body        any
}

type RequestValidator added in v1.3.8

type RequestValidator interface {
	ValidateRequest(req *Request) error
}

RequestValidator defines the interface for request validation.

type SPKIHashPinner added in v1.3.8

type SPKIHashPinner struct {
	// contains filtered or unexported fields
}

SPKIHashPinner pins certificates by their Subject Public Key Info (SPKI) hash. This is the most common form of certificate pinning used in HTTP Public Key Pinning (HPKP) and similar security mechanisms.

func NewSPKIHashPinner added in v1.3.8

func NewSPKIHashPinner(hashes ...string) (*SPKIHashPinner, error)

NewSPKIHashPinner creates a new SPKI pinner from base64-encoded SHA-256 hashes. Each hash should be the base64-encoded SHA-256 hash of the DER-encoded SPKI.

Example hashes:

  • Google: "7HIpactkIAq2Y49orFOOQKurWxmmSVSJcOooMBf4tTM="
  • Let's Encrypt: "YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2fuihg="

You can generate these hashes using:

openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64

func (*SPKIHashPinner) Pin added in v1.3.8

func (p *SPKIHashPinner) Pin() string

Pin returns a description of the pinned SPKI hashes.

func (*SPKIHashPinner) VerifyPeerCertificate added in v1.3.8

func (p *SPKIHashPinner) VerifyPeerCertificate(rawCerts [][]byte, _ [][]*x509.Certificate) error

VerifyPeerCertificate verifies that one of the peer certificates matches a pinned SPKI hash.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func NewValidator

func NewValidator() *Validator

func NewValidatorWithConfig

func NewValidatorWithConfig(config *Config) *Validator

func (*Validator) ValidateRequest

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

Jump to

Keyboard shortcuts

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