Expression Environment
CEL expressions in Anubis have access to request properties and specialized functions:lib/policy/expressions/environment.go:19-33
Available Variables
Request Properties
remoteAddress(string): Client IP address fromX-Real-IpheadercontentLength(int): Request body size in byteshost(string): Host header valuemethod(string): HTTP method (GET, POST, etc.)userAgent(string): User-Agent headerpath(string): URL path componentquery(map[string]string): Query parametersheaders(map[string]string): All HTTP headers
lib/policy/celchecker.go:59-88
System Load
load_1m(double): System load average over 1 minuteload_5m(double): System load average over 5 minutesload_15m(double): System load average over 15 minutes
lib/policy/expressions/loadavg.go:53-69
Built-in Functions
String Manipulation
regexSafe(string) string
Escapes a string for safe insertion into regular expressions:
lib/policy/expressions/environment.go:152-171
segments(string) list[string]
Splits a path into segments:
lib/policy/expressions/environment.go:173-194
DNS Functions
reverseDNS(string) list[string]
Performs reverse DNS lookup on an IP address:
lib/policy/expressions/environment.go:61-78
lookupHost(string) list[string]
Resolves a hostname to IP addresses:
lib/policy/expressions/environment.go:80-97
verifyFCrDNS(string) bool
verifyFCrDNS(string, string) bool
Verifies Forward-Confirmed reverse DNS (FCrDNS). Optionally accepts a regex pattern:
lib/policy/expressions/environment.go:99-127
arpaReverseIP(string) string
Transforms an IP address into ARPA reverse notation:
lib/policy/expressions/environment.go:132-149
Header Functions
missingHeader(map, string) bool
Checks if a specific header is missing:
lib/policy/expressions/environment.go:35-59
Random Functions
randInt(int) int
Generates a random integer from 0 to n-1:
lib/policy/expressions/environment.go:215-228
String Extensions
Anubis includes the CEL strings extension:lib/policy/expressions/environment.go:206-209
Type Wrappers
Anubis provides CEL type wrappers for HTTP headers and query parameters:HTTPHeaders
lib/policy/expressions/http_headers.go:14-67
URLValues
lib/policy/expressions/url_values.go:16-56
Example Expressions
Block Specific User Agents
Rate Limiting by Load
Geographic Restrictions
Missing Headers Detection
Path Segment Matching
Query Parameter Validation
Compilation and Execution
Expressions are compiled at startup for performance:lib/policy/expressions/environment.go:237-255
Request Activation
CEL variables are resolved from HTTP requests:lib/policy/celchecker.go:61-88
Best Practices
- Validate at startup: CEL expressions are compiled during config parsing to catch errors early
- Use standard library: Leverage CEL’s built-in string, list, and map functions
- Cache DNS results: DNS functions use a TTL-based cache to avoid repeated lookups
- Combine conditions: Use logical operators (
&&,||,!) to build complex rules - Test expressions: Invalid CEL syntax causes Anubis to refuse to start
- Mind performance: DNS lookups and regex matching add latency; use judiciously
Threshold Expressions
Thresholds use a simplified CEL environment with only theweight variable:
lib/policy/expressions/environment.go:198-202