Overview
The policy package defines bot detection rules, threshold-based actions, and policy configuration parsing. Policies determine when to allow, deny, or challenge requests based on pattern matching and weighted scoring.Types
Bot
A bot detection rule with matching conditions and an action.Checker implementation that determines if this rule matches a request.Can be a single checker or a
checker.List combining multiple conditions.Challenge configuration for CHALLENGE actions
Weight adjustment for WEIGH actions
Unique identifier for this rule (e.g., “googlebot”, “known-bad-bot”)
Action to take when this rule matchesValues:
ALLOW: Permit request immediatelyDENY: Block request immediatelyCHALLENGE: Issue a challengeWEIGH: Adjust weight and continue evaluationDEBUG_BENCHMARK: Show benchmark page
lib/policy/bot.go:11-17
Bot.Hash
Computes a deterministic hash of the bot rule configuration.Hex-encoded hash of the rule name and checker configuration
lib/policy/bot.go:19-21
CheckResult
The result of evaluating policy rules against a request.Identifier of the matched rulePrefixes:
bot/: Direct bot rule matchthreshold/: Threshold rule matchdefault/: Fell through to default action
Action determined by policy evaluation
Cumulative weight from all matched WEIGH rules
lib/policy/checkresult.go:9-13
ParsedConfig
Fully parsed and validated policy configuration.Storage backend instance
Legal/contact information
OpenGraph tag caching configuration
Parsed bot detection rules (evaluated in order)
Weight-based threshold rules
HTTP status codes for CHALLENGE and DENY actions
Default proof-of-work difficulty (0-64)
Enable DroneBL blocklist checking
Structured logger instance
lib/policy/policy.go:36-49
Functions
ParseConfig
Parses a policy configuration from YAML.Context (may contain Thoth client for ASN/GeoIP features)
Reader containing YAML policy configuration
Filename for error messages
Default challenge difficulty (0-64)
Log level: “debug”, “info”, “warn”, or “error”
Parsed and validated configuration
Validation or parse errors
lib/policy/policy.go:59-248
Checker Implementations
Policy rules use checker implementations to match requests.NewRemoteAddrChecker
Creates a checker that matches IP addresses against CIDR ranges.List of CIDR ranges (e.g., [“192.168.1.0/24”, “10.0.0.0/8”])
IP address matcher using efficient prefix tree
Error if CIDR parsing fails
lib/policy/checker.go:25-41
NewUserAgentChecker
Creates a checker that matches the User-Agent header against a regex.Regular expression pattern
User-Agent matcher
Error if regex compilation fails
lib/policy/checker.go:72-74
NewPathChecker
Creates a checker that matches the request path against a regex.Regular expression pattern for path matching
Path matcher
Error if regex compilation fails
lib/policy/checker.go:101-107
NewHeadersChecker
Creates a checker that matches multiple HTTP headers.Map of header names to regex patternsSpecial value: Use
".*" to check for header existence without pattern matchingMulti-header matcher (all headers must match)
Error if any regex compilation fails
lib/policy/checker.go:148-172
NewCELChecker
Creates a checker using Common Expression Language (CEL).CEL expression or list of expressions
DNS resolver for reverse DNS lookups in expressions
CEL expression evaluator
Error if expression compilation fails
request.method: HTTP methodrequest.path: Request pathrequest.headers: Header maprequest.query: Query parametersrequest.remote_addr: Client IPenv: Environment variablesdns.reverse(ip): Reverse DNS lookup
lib/policy/celchecker.go
Checker Interface
Check
Evaluates if a request matches this checker’s conditions.HTTP request to evaluate
True if the request matches this checker
Error if check evaluation fails
Hash
Returns a deterministic hash of the checker configuration.Hex-encoded hash string
Checker.List
Combines multiple checkers with AND semantics.- Returns
trueonly if ALL checkers returntrue - Short-circuits on first
false - Returns error if any checker errors
lib/policy/checker/checker.go:25-55
YAML Configuration
Example policy.yaml:Related Types
- Anubis Server - Policy enforcement
- Challenge - Challenge implementation
- Store - Policy data persistence