Challenge lifecycle
The challenge lifecycle is the gate between a request that policy marks for review and a request that Anubis sends to the protected application. It selects a challenge from the matched policy rule, binds that challenge to a short-lived browser cookie and stored record, checks the submitted proof and required browser signals, then grants a signed clearance cookie for later requests.
Lifecycle stages
An incoming request first passes through policy evaluation. A challenge rule causes Anubis to create a one-use challenge, show a page that contains the work to perform, and wait for a proof request. A valid proof changes the request from CHALLENGE to PASS; later requests use the signed clearance cookie until it expires or no longer matches the current policy.
The main stages are:
- Selection — request checks choose a bot rule or threshold with the
CHALLENGEaction. - Issuance — Anubis stores a random challenge record and sets a verification cookie.
- Page generation — the selected challenge method renders a page with the challenge data and a return target.
- Submission — the browser sends the challenge ID, proof fields, and return target to the pass endpoint.
- Validation — Anubis re-checks the redirect, verification cookie, policy identity, challenge method, difficulty, extensions, and proof.
- Clearance — Anubis marks the challenge spent, signs a JWT clearance cookie, and redirects to the original target.
- Forwarding — the next request is checked against the JWT and then sent to the protected application.
This diagram shows how a request moves from policy selection to protected application access.
flowchart TD
A[Incoming request] --> B[Evaluate policy]
B -->|ALLOW| Z[Protected application]
B -->|CHALLENGE| C[Create stored challenge]
C --> D[Render challenge page]
D --> E[Browser submits proof]
E --> F[Re-evaluate target and rule]
F --> G[Validate proof and extensions]
G -->|failure| H[Reject or retry]
G -->|success| I[Spend challenge and sign clearance]
I --> J[Redirect to original target]
J --> K[Verify clearance JWT]
K --> Z
References: lib/anubis.go• L328-427 lib/anubis.go• L447-487 lib/anubis.go• L863-947
Challenge selection
Challenge selection uses the same policy result for the initial request and the proof request. Bot rules are checked first. Matching CHALLENGE rules return a copy of the rule, while threshold rules can select a challenge after weight changes from earlier rules; this keeps the challenge tied to the request risk decision. [lib/anubis.go#L863-L947]
Each issued record stores the rule identity and the challenge settings that the browser must solve. The rule hash, method, difficulty, and extensions form the binding between the proof and the policy decision. If policy changes before proof submission, the old challenge does not become a pass for a different rule.
References: lib/anubis.go• L182-228 lib/anubis.go• L863-947 lib/…/policy.go• L235-289
Challenge issuance
Issuance creates a short-lived record with a unique ID, random work data, issue time, method, difficulty, policy rule hash, extensions, and request metadata. The record is stored for 30 minutes, and the same ID is placed in the verification cookie so the proof request must refer to the issued challenge.
The challenge page also carries the selected rule settings. The method-specific issuer builds the page and its submission target. Anubis wraps that page in a no-store response and adds the verification cookie with a 30-minute lifetime. API clients can use the same issuance path through the make-challenge endpoint, which returns the challenge data and ID instead of a full page.
References: lib/anubis.go• L147-228 lib/anubis.go• L525-605 lib/http.go• L285-395 lib/…/interface.go• L46-68
Challenge page
The challenge page exposes only the data needed by the selected method and includes the rule settings that control the client work. The page can also load configured extensions; an extension may add browser work that must succeed before clearance is issued. Unknown extensions fail page generation or policy loading rather than silently reducing the checks.
Challenge methods share one issue and validation contract. A method can present client-side work, a timed response, or a browser interaction, but the server remains the final authority because it validates the submitted fields against the stored challenge record.
References: lib/…/interface.go• L92-102 lib/http.go• L267-282 web/index.go• L20-27 lib/…/preact.go• L39-87 lib/…/metarefresh.go• L18-72
Proof validation
The pass endpoint validates the return target before it reads or uses the proof. It requires a local path or an allowed HTTP or HTTPS destination, rejects unsafe URL forms, and applies configured redirect-domain limits. This prevents a successful proof from becoming an open redirect.
After the target is accepted, validation checks the verification cookie, runs policy again for the target request, and loads the stored challenge. The challenge must be unspent and must still match the selected rule. Anubis then calls the registered method validator and each listed extension validator. A failure clears the clearance cookie and returns a failure response without granting access.
Validation errors have a public reason and a private cause. A failed proof or invalid format is reported as a client challenge failure, while an unexpected validator or store error is treated as an internal failure. The system does not grant clearance for any validator error.
References: lib/anubis.go• L615-745 lib/http.go• L64-108 lib/…/error.go• L9-38 lib/challenge_validation_security_test.go• L23-51
Token and cookie binding
The verification cookie binds the browser submission to the stored challenge ID. The clearance cookie is a signed JWT that binds later requests to the challenge ID, method, policy rule hash, and action. It may also bind to a configured request header and may include the difficulty when that option is enabled.
On each protected request, Anubis checks the JWT signature, required expiry, claim type, policy rule hash, and optional request-header restriction before forwarding the request. A missing, invalid, expired, or policy-mismatched clearance causes a new challenge instead of access. Cookie settings such as path, domain, secure mode, same-site mode, and an optional dynamic name suffix must remain consistent between write and read.
References: lib/anubis.go• L134-145 lib/anubis.go• L328-427 lib/anubis.go• L756-805 lib/http.go• L110-172
Redirects and retries
Successful proof validation marks the challenge spent before it redirects the browser to the original target. The redirect is not cacheable because it sets a clearance cookie. A one-use referer cookie can restore the original referer on the next forwarded request; Anubis clears that cookie after use.
If the browser omits the verification cookie, Anubis allows one fresh-challenge retry for the same client and live challenge. The retry key uses the client IP and user agent and lasts for the challenge lifetime. A second miss, an unknown challenge, or a spent challenge ends with a cookies-disabled error rather than an unbounded loop.
References: lib/anubis.go• L156-180 lib/anubis.go• L639-649 lib/anubis.go• L746-805 lib/anubis.go• L430-445 lib/challenge_cookie_retry_test.go• L46-177 lib/challenge_redirect_cache_test.go• L12-26
Security limits
Challenge endpoints parse request forms through a 64 KiB maximum body. Empty bodies remain valid for proxy auth subrequests, but malformed or oversized bodies are rejected. Multipart temporary files are removed after the endpoint finishes.
The challenge record is one-use. A per-challenge lock and the stored Spent state ensure that concurrent proof requests can mint at most one clearance. The challenge is spent only after the method and extension checks succeed, and a failure to save that spent state prevents clearance issuance.
The server also limits the time before a proof can pass for methods that require a wait. It compares the proof with the stored random data using constant-time comparison where the method supports it. Challenge pages and pass redirects use no-store behavior to avoid replay through shared caches.
References: lib/challenge_form.go• L10-41 lib/anubis.go• L663-679 lib/anubis.go• L729-751 lib/…/preact.go• L69-87 lib/…/metarefresh.go• L55-72
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the fields written into the stored challenge aligned with the fields checked in
PassChallenge; a new challenge setting must be both issued and validated. - Keep verification-cookie naming, path, and domain settings aligned across challenge page issuance and proof submission, especially when a base prefix or dynamic cookie suffix is enabled.
- Keep JWT claim creation aligned with request-time claim checks; changes to policy identity or header restrictions affect both clearance minting and forwarding.
- Keep challenge extensions registered, rendered, and validated as one contract; an extension that is shown but not checked weakens the challenge.
- Keep retry storage lifetime tied to challenge lifetime so missing-cookie recovery cannot create a loop or outlive the challenge it refers to.
- Keep spend locking and the stored
Spentupdate together when changing validation or store behavior; either change can affect one-clearance-only behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The policy rule, method, difficulty, or extension list changes before proof submission | The proof is rejected instead of transferring to a new rule | Stored policy hash and challenge settings are compared with the current rule |
| The verification cookie is missing | Anubis retries once, then shows the cookies-disabled error | Retry state is keyed by client identity and expires with the challenge |
| Two requests submit the same live challenge at once | Only one request can receive clearance | Per-challenge locking and the stored Spent flag prevent double spend |
| Proof or extension validation returns any error | No clearance cookie is minted | All validation errors take a failure path |
| The return target uses an unsafe scheme or disallowed host | The request is rejected without a redirect | Redirect parsing and allowed-domain checks run before validation |
| The challenge or clearance response is cached | Another client or stale browser state may reuse challenge results | Challenge pages and pass redirects use no-store |
| A challenge form exceeds 64 KiB or is malformed | The endpoint returns a request error | Form parsing uses a bounded body reader and rejects parse errors |
Key files
| File | Why to read it |
|---|---|
| lib/anubis.go | Defines challenge creation, policy re-check, proof validation, retry handling, spend control, JWT claims, and the final redirect. |
| lib/http.go | Defines challenge-page response behavior, cookie settings, redirect validation, and forwarding checks for the clearance cookie. |
| lib/…/challenge.go | Defines the stored challenge state that links issuance to validation. |
| lib/…/interface.go | Defines the shared issue and validate contract for challenge methods. |
| lib/challenge_form.go | Defines the request-body limit and cleanup shared by challenge endpoints. |
| lib/…/policy.go | Shows how configured bot and threshold rules receive challenge settings during policy load. |
| web/index.go | Shows the data passed from the server into the challenge page shell. |
| lib/…/preact.go | Illustrates a client-work challenge from page issue through server proof validation. |
| lib/…/cssload.go | Illustrates an extension that adds a browser signal to the validation contract. |
| lib/challenge_cookie_retry_test.go | Captures the missing-cookie retry and loop limit behavior. |
| lib/challenge_spend_test.go | Captures the one-clearance-only guarantee under concurrent submission and store failure. |