Skip to main content
Anubis uses Google’s Common Expression Language (CEL) for flexible policy expressions. CEL allows you to write complex bot detection rules using a familiar, safe syntax.

Expression Environment

CEL expressions in Anubis have access to request properties and specialized functions:
Source: lib/policy/expressions/environment.go:19-33

Available Variables

Request Properties

  • remoteAddress (string): Client IP address from X-Real-Ip header
  • contentLength (int): Request body size in bytes
  • host (string): Host header value
  • method (string): HTTP method (GET, POST, etc.)
  • userAgent (string): User-Agent header
  • path (string): URL path component
  • query (map[string]string): Query parameters
  • headers (map[string]string): All HTTP headers
Source: lib/policy/celchecker.go:59-88

System Load

  • load_1m (double): System load average over 1 minute
  • load_5m (double): System load average over 5 minutes
  • load_15m (double): System load average over 15 minutes
Load values are updated every 15 seconds in a background thread. Source: lib/policy/expressions/loadavg.go:53-69

Built-in Functions

String Manipulation

regexSafe(string) string

Escapes a string for safe insertion into regular expressions:
Source: lib/policy/expressions/environment.go:152-171

segments(string) list[string]

Splits a path into segments:
Source: lib/policy/expressions/environment.go:173-194

DNS Functions

reverseDNS(string) list[string]

Performs reverse DNS lookup on an IP address:
Source: lib/policy/expressions/environment.go:61-78

lookupHost(string) list[string]

Resolves a hostname to IP addresses:
Source: 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:
Source: lib/policy/expressions/environment.go:99-127

arpaReverseIP(string) string

Transforms an IP address into ARPA reverse notation:
Source: lib/policy/expressions/environment.go:132-149

Header Functions

missingHeader(map, string) bool

Checks if a specific header is missing:
Source: lib/policy/expressions/environment.go:35-59

Random Functions

randInt(int) int

Generates a random integer from 0 to n-1:
Source: lib/policy/expressions/environment.go:215-228

String Extensions

Anubis includes the CEL strings extension:
Source: lib/policy/expressions/environment.go:206-209

Type Wrappers

Anubis provides CEL type wrappers for HTTP headers and query parameters:

HTTPHeaders

Source: lib/policy/expressions/http_headers.go:14-67

URLValues

Source: 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:
Source: lib/policy/expressions/environment.go:237-255

Request Activation

CEL variables are resolved from HTTP requests:
Source: lib/policy/celchecker.go:61-88

Best Practices

  1. Validate at startup: CEL expressions are compiled during config parsing to catch errors early
  2. Use standard library: Leverage CEL’s built-in string, list, and map functions
  3. Cache DNS results: DNS functions use a TTL-based cache to avoid repeated lookups
  4. Combine conditions: Use logical operators (&&, ||, !) to build complex rules
  5. Test expressions: Invalid CEL syntax causes Anubis to refuse to start
  6. Mind performance: DNS lookups and regex matching add latency; use judiciously

Threshold Expressions

Thresholds use a simplified CEL environment with only the weight variable:
Example threshold configuration:
Source: lib/policy/expressions/environment.go:198-202