Documentation
¶
Index ¶
- Variables
- func ExtractCandidatePaths(rawPath, pathStr, requestURI string) []string
- func ExtractClientIP(req *http.Request) string
- func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)
- type CaptchaConfig
- type Config
- type IPFilter
- type ResponseConfig
- type ResponseHandler
- type RouteWarden
Constants ¶
This section is empty.
Variables ¶
var DefaultAllowPatterns = []string{
`(?i)^/robots\.txt$`,
`(?i)^/sitemap.*\.xml$`,
`(?i)^/ads\.txt$`,
`(?i)^/security\.txt$`,
`(?i)^/\.well-known(/.*)?$`,
}
DefaultAllowPatterns contains typical legitimate endpoints that might otherwise match broad patterns.
var DefaultBlockPatterns = []string{
`(?i)(^|/)(\.env.*|.*\.(txt|log|bak|backup|sql|conf|config|ini|yaml|yml))$`,
`(?i)(^|/)\.(git|svn|hg|bzr|cvs)(/.*|$)`,
`(?i)(^|/)\.(aws|ssh|kube|docker)(/.*|$)`,
`(?i).*\.(tar|tar\.gz|tgz|zip|rar|7z|gz|bz2|iso|dump|sqlite|sqlite3|db)$`,
`(?i)(^|/)(phpinfo\.php|info\.php|server-status|server-info|actuator(/.*)?|metrics|heapdump|trace|env)$`,
`(?i)(^|/)(composer\.(json|lock)|package-lock\.json|yarn\.lock|pnpm-lock\.yaml|Pipfile|Pipfile\.lock|requirements\.txt)$`,
`(?i).*\.(pem|key|crt|pfx|p12|jks|kdb)$`,
`(?i)(^|/)(dockerfile.*|docker-compose.*\.ya?ml)$`,
`(?i)(^|/)\.ds_store$`,
`(?i)(^|/)(wp-config\.php.*|configuration\.php.*|settings\.py|local_settings\.py)$`,
}
DefaultBlockPatterns contains well-known sensitive endpoints and file extensions.
Functions ¶
func ExtractCandidatePaths ¶
ExtractCandidatePaths normalizes and extracts all representations of a request URI path, neutralizing common evasion techniques like double encoding, backslash substitution, matrix parameters, and null bytes.
func ExtractClientIP ¶
ExtractClientIP extracts the client IP address from proxy headers or RemoteAddr socket.
Types ¶
type CaptchaConfig ¶
type CaptchaConfig struct {
Provider string `json:"provider,omitempty"` // "turnstile", "hcaptcha", "recaptcha", or "custom"
SiteKey string `json:"siteKey,omitempty"` // Public site key
Title string `json:"title,omitempty"` // Challenge page title
Template string `json:"template,omitempty"` // Custom HTML template
}
CaptchaConfig holds captcha configuration options.
type Config ¶
type Config struct {
Enabled bool `json:"enabled,omitempty"`
EnableDefaultPatterns bool `json:"enableDefaultPatterns,omitempty"`
EnableDefaultAllowPatterns bool `json:"enableDefaultAllowPatterns,omitempty"` // Controls built-in whitelist (robots.txt, sitemap.xml, .well-known)
PathPatterns []string `json:"pathPatterns,omitempty"` // Synonym for blockPatterns
BlockPatterns []string `json:"blockPatterns,omitempty"`
AllowPatterns []string `json:"allowPatterns,omitempty"`
AllowedIPs []string `json:"allowedIps,omitempty"` // Whitelist of IPs or CIDR subnets exempt from blocking
Methods []string `json:"methods,omitempty"` // HTTP verbs to inspect (defaults to ["GET"])
StatusCode int `json:"statusCode,omitempty"`
CustomResponseText string `json:"customResponseText,omitempty"`
Action string `json:"action,omitempty"` // Convenience alias for response mode (e.g. "silentDrop", "fakeSuccess", "json")
Mode string `json:"mode,omitempty"` // Convenience alias for response mode
CheckQuery bool `json:"checkQuery,omitempty"`
CheckHeaders []string `json:"checkHeaders,omitempty"` // Optional headers to inspect (e.g. X-Forwarded-Uri, X-Rewrite-URL)
Debug bool `json:"debug,omitempty"` // Enable verbose debug logging to stdout/stderr
SecurityLog bool `json:"securityLog,omitempty"` // Emit structured JSON security audit events (CrowdSec/SIEM compatible) on block
Response *ResponseConfig `json:"response,omitempty"`
}
Config holds the plugin configuration.
func CreateConfig ¶
func CreateConfig() *Config
CreateConfig creates the default plugin configuration.
type IPFilter ¶
type IPFilter struct {
// contains filtered or unexported fields
}
IPFilter evaluates incoming requests against an IP or CIDR subnet whitelist.
func NewIPFilter ¶
NewIPFilter parses and creates an IPFilter from a list of IP strings and CIDR notation subnets.
type ResponseConfig ¶
type ResponseConfig struct {
Mode string `json:"mode,omitempty"` // "text", "json", "html", "captcha", "redirect"
StatusCode int `json:"statusCode,omitempty"` // HTTP status code (e.g. 403, 404, 429)
ContentType string `json:"contentType,omitempty"` // Custom Content-Type header override
Body string `json:"body,omitempty"` // Response payload (JSON string, HTML, or text)
Headers map[string]string `json:"headers,omitempty"` // Custom response headers (e.g. Retry-After, X-Blocked-By)
RedirectURL string `json:"redirectUrl,omitempty"` // Target URL when Mode is "redirect"
ProxyURL string `json:"proxyUrl,omitempty"` // Target backend honeypot URL when Mode is "proxy"
Captcha *CaptchaConfig `json:"captcha,omitempty"` // Captcha settings when Mode is "captcha"
GzipBombMB int `json:"gzipBombMB,omitempty"` // Uncompressed size in Megabytes for gzipBomb mode (default: 10)
RetryAfterSeconds int `json:"retryAfterSeconds,omitempty"` // Seconds for Retry-After header when Mode is "rateLimitChallenge" (default: 300)
TarpitDelayMs int `json:"tarpitDelayMs,omitempty"` // Milliseconds between bytes for tarpit mode (default: 1000)
TarpitMaxDurationSeconds int `json:"tarpitMaxDurationSeconds,omitempty"` // Max seconds before terminating tarpit connection (default: 60)
StreamSizeMB int `json:"streamSizeMB,omitempty"` // Size in Megabytes for infiniteStream/garbageStream mode (default: 100)
}
ResponseConfig defines how blocked requests should be answered.
type ResponseHandler ¶
type ResponseHandler struct {
// contains filtered or unexported fields
}
ResponseHandler manages custom response execution (JSON, HTML, Captcha, Redirect, Text).
func NewResponseHandler ¶
func NewResponseHandler(respCfg *ResponseConfig, topStatusCode int, topCustomText string, silentDrop bool) (*ResponseHandler, error)
NewResponseHandler initializes a ResponseHandler with compiled templates and proxy handlers.
func (*ResponseHandler) ServeBlockedRequest ¶
func (h *ResponseHandler) ServeBlockedRequest(w http.ResponseWriter, req *http.Request)
ServeBlockedRequest handles writing the configured response to the client.
func (*ResponseHandler) SetCaptchaTemplateForTest ¶ added in v0.3.3
func (h *ResponseHandler) SetCaptchaTemplateForTest(tmpl *template.Template)
SetCaptchaTemplateForTest allows unit tests to inject custom/faulty captcha templates.
func (*ResponseHandler) SetProxyHandlerForTest ¶
func (h *ResponseHandler) SetProxyHandlerForTest(p http.Handler)
SetProxyHandlerForTest allows unit tests to inject a mock reverse proxy handler without listening on network sockets.
type RouteWarden ¶
type RouteWarden struct {
// contains filtered or unexported fields
}
RouteWarden is the Traefik middleware plugin handler.
func (*RouteWarden) ServeHTTP ¶
func (rw *RouteWarden) ServeHTTP(w http.ResponseWriter, req *http.Request)