Skip to main content

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.Impl
Checker implementation that determines if this rule matches a request.Can be a single checker or a checker.List combining multiple conditions.
*config.ChallengeRules
Challenge configuration for CHALLENGE actions
*config.Weight
Weight adjustment for WEIGH actions
string
Unique identifier for this rule (e.g., “googlebot”, “known-bad-bot”)
config.Rule
Action to take when this rule matchesValues:
  • ALLOW: Permit request immediately
  • DENY: Block request immediately
  • CHALLENGE: Issue a challenge
  • WEIGH: Adjust weight and continue evaluation
  • DEBUG_BENCHMARK: Show benchmark page
Example
See lib/policy/bot.go:11-17

Bot.Hash

Computes a deterministic hash of the bot rule configuration.
string
Hex-encoded hash of the rule name and checker configuration
Used to detect when policy rules change, invalidating existing JWT cookies. See lib/policy/bot.go:19-21

CheckResult

The result of evaluating policy rules against a request.
string
Identifier of the matched rulePrefixes:
  • bot/: Direct bot rule match
  • threshold/: Threshold rule match
  • default/: Fell through to default action
config.Rule
Action determined by policy evaluation
int
Cumulative weight from all matched WEIGH rules
Example
See lib/policy/checkresult.go:9-13

ParsedConfig

Fully parsed and validated policy configuration.
store.Interface
Storage backend instance
*config.Impressum
Legal/contact information
config.OpenGraph
OpenGraph tag caching configuration
[]Bot
Parsed bot detection rules (evaluated in order)
[]*Threshold
Weight-based threshold rules
config.StatusCodes
HTTP status codes for CHALLENGE and DENY actions
int
Default proof-of-work difficulty (0-64)
bool
Enable DroneBL blocklist checking
*slog.Logger
Structured logger instance
See lib/policy/policy.go:36-49

Functions

ParseConfig

Parses a policy configuration from YAML.
context.Context
required
Context (may contain Thoth client for ASN/GeoIP features)
io.Reader
required
Reader containing YAML policy configuration
string
required
Filename for error messages
int
required
Default challenge difficulty (0-64)
string
required
Log level: “debug”, “info”, “warn”, or “error”
*ParsedConfig
Parsed and validated configuration
error
Validation or parse errors
Example
See 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.
[]string
required
List of CIDR ranges (e.g., [“192.168.1.0/24”, “10.0.0.0/8”])
checker.Impl
IP address matcher using efficient prefix tree
error
Error if CIDR parsing fails
Example
See lib/policy/checker.go:25-41

NewUserAgentChecker

Creates a checker that matches the User-Agent header against a regex.
string
required
Regular expression pattern
checker.Impl
User-Agent matcher
error
Error if regex compilation fails
Example
See lib/policy/checker.go:72-74

NewPathChecker

Creates a checker that matches the request path against a regex.
string
required
Regular expression pattern for path matching
checker.Impl
Path matcher
error
Error if regex compilation fails
Example
See lib/policy/checker.go:101-107

NewHeadersChecker

Creates a checker that matches multiple HTTP headers.
map[string]string
required
Map of header names to regex patternsSpecial value: Use ".*" to check for header existence without pattern matching
checker.Impl
Multi-header matcher (all headers must match)
error
Error if any regex compilation fails
Example
See lib/policy/checker.go:148-172

NewCELChecker

Creates a checker using Common Expression Language (CEL).
*config.ExpressionOrList
required
CEL expression or list of expressions
*dns.Dns
required
DNS resolver for reverse DNS lookups in expressions
checker.Impl
CEL expression evaluator
error
Error if expression compilation fails
Available CEL variables:
  • request.method: HTTP method
  • request.path: Request path
  • request.headers: Header map
  • request.query: Query parameters
  • request.remote_addr: Client IP
  • env: Environment variables
  • dns.reverse(ip): Reverse DNS lookup
Example
See lib/policy/celchecker.go

Checker Interface

Check

Evaluates if a request matches this checker’s conditions.
*http.Request
required
HTTP request to evaluate
bool
True if the request matches this checker
error
Error if check evaluation fails

Hash

Returns a deterministic hash of the checker configuration.
string
Hex-encoded hash string

Checker.List

Combines multiple checkers with AND semantics.
Behavior:
  • Returns true only if ALL checkers return true
  • Short-circuits on first false
  • Returns error if any checker errors
Example
See lib/policy/checker/checker.go:25-55

YAML Configuration

Example policy.yaml: