Skip to main content

Overview

The challenge package defines the challenge system for bot detection. Challenges are cryptographic or computational puzzles that legitimate browsers can solve but bots typically cannot.

Types

Challenge

Metadata about a single challenge issuance.
time.Time
Timestamp when the challenge was created
map[string]string
Additional context stored with the challengeCommon keys:
  • User-Agent: Client user agent string
  • X-Real-Ip: Client IP address
string
Unique UUID (v7) identifier for this challenge
string
Challenge algorithm nameBuilt-in methods:
  • fast: Proof-of-work (SHA-256 based)
  • preact: Interactive JavaScript challenge
  • metarefresh: Meta refresh redirect challenge
string
Hexadecimal-encoded random bytes (64 bytes) that the client must process
string
Hash of the bot policy rule that triggered this challenge. Used to detect policy changes.
int
Computational difficulty for proof-of-work challenges (0-64). Higher values require more CPU time.Recommended values:
  • 15-18: Low security, fast solving (~100ms)
  • 19-22: Medium security (~500ms)
  • 23-25: High security (~2-5s)
  • 26+: Very high security (10s+)
bool
Whether this challenge has already been successfully solved. Prevents replay attacks.
Example
See lib/challenge/challenge.go:5-15

IssueInput

Input parameters for the challenge Issue method.
*config.Impressum
Legal/contact information to display on challenge page
*policy.Bot
The bot detection rule that triggered this challenge
*Challenge
The challenge instance being issued
map[string]string
OpenGraph metadata tags for the page being protected
store.Interface
Storage backend for persisting challenge state
See lib/challenge/interface.go:45-51

ValidateInput

Input parameters for the challenge Validate method.
*policy.Bot
The policy rule associated with this challenge
*Challenge
The challenge being validated
store.Interface
Storage backend for challenge data
See lib/challenge/interface.go:53-57

Interfaces

Impl

Interface for challenge algorithm implementations.

Setup

Registers any HTTP routes needed by the challenge implementation (e.g., for serving JavaScript bundles or API endpoints).
*http.ServeMux
HTTP router to register routes with
Example

Issue

Generates the challenge page component to display to the user.
http.ResponseWriter
HTTP response writer (for setting headers)
*http.Request
HTTP request being challenged
*slog.Logger
Structured logger with request context
*IssueInput
Challenge issuance parameters
templ.Component
Templ component to render as the challenge page
error
Error if challenge generation fails
Example
See lib/challenge/interface.go:64

Validate

Validates that the user correctly solved the challenge.
*http.Request
HTTP request containing the challenge solution
*slog.Logger
Structured logger with request context
*ValidateInput
Challenge validation parameters
error
Returns nil if validation succeeds, or an error describing why validation failed
Validation Errors: Return a *challenge.Error for user-facing validation failures:
See lib/challenge/interface.go:67

Functions

Register

Registers a challenge implementation with the global registry.
string
required
Unique name for the challenge algorithm (e.g., “fast”, “preact”)
Impl
required
Challenge implementation
Example
See lib/challenge/interface.go:20-24

Get

Retrieves a registered challenge implementation by name.
string
required
Challenge algorithm name
Impl
The challenge implementation, if found
bool
True if the challenge exists in the registry
Example
See lib/challenge/interface.go:27-32

Methods

Returns a sorted list of all registered challenge algorithm names.
[]string
Sorted slice of challenge algorithm names
Example
See lib/challenge/interface.go:34-43

Error Types

Error

Challenge validation error with public and private messages.
error
Internal error details (not shown to users)
string
Action that failed (e.g., “validate”, “decode”)
string
User-friendly error message displayed on error page
int
HTTP status code for the error response (default: 403)
See lib/challenge/error.go:24-29

NewError

Creates a new challenge error.
string
required
Action being performed when error occurred
string
required
User-facing error description
error
required
Internal error to log and wrap
*Error
Challenge error with status code 403
Example
See lib/challenge/error.go:15-21

Sentinel Errors

error
User submitted an incorrect solution
error
Required field missing from request
error
Field has incorrect format or encoding
See lib/challenge/error.go:9-13

Built-in Challenges

Anubis includes three challenge implementations:

fast (Proof of Work)

SHA-256 based proof-of-work challenge. Client must find a nonce that produces a hash with N leading zero bits. Difficulty mapping: Each difficulty level adds one zero bit requirement.

preact (Interactive)

React-based interactive challenge requiring user interaction. Tests JavaScript execution and user behavior.

metarefresh

Meta refresh redirect challenge. Tests basic HTML parsing and redirect following.