# Security design (laravel-rust-captcha)

> This page explains how challenges are kept safe. The design is shared with the Go port of the same package.

- Package: laravel-rust-captcha (Laravel for Rust, alpha), crates: `laravel-rust-captcha`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-captcha), [crates.io](https://crates.io/crates/laravel-rust-captcha/0.1.0), [docs.rs](https://docs.rs/laravel-rust-captcha/0.1.0), [Changelog](https://oss.bowerbird.si/captcha/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/SECURITY.md)
- Based on: [mews/captcha](https://github.com/mewebstudio/captcha): The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its backgrounds and fonts ship with their own licences.
- PHP parity: full (54 covered, 33 diverged, 0 planned of 87 abilities; 44 fixtures; follows mews/captcha 3.5.1; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/captcha/docs/security/
- Source: https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/docs/security.md

This page explains how challenges are kept safe. The design is shared with the Go port of the same
package.

## What is stored

Each image is a challenge with a random 128-bit id. The store keeps a record with the id, its mode
(session or keyed), its profile, a deadline, the attempt limit, and a verifier. The verifier is an
HMAC-SHA256 of the id, profile and normalized answer, under a key derived from `APP_KEY`. The answer
itself is never stored, logged or sent. The configured answer and key fields are excluded from
flashed old input. Register every renamed or nested field in `CAPTCHA_ANSWER_FIELDS` or
`CAPTCHA_KEY_FIELD`.

## One pass per challenge

1. Issuing writes the attempt counter for a limited profile, then the record.
2. A check reserves a failure unit before any challenge read, including the current session pointer.
3. It reads the live record and atomically counts the attempt before comparing on a limited profile.
4. An exhausted challenge stops before comparison. A correct answer passes only if the check removes
   the live record and, for session mode, the challenge is still current.
5. The check returns its failure unit at most once on known outcomes other than a wrong answer.
   A store error, deadline or cancellation before the attempt increment returns the unit. Once the
   increment starts, an uncertain outcome keeps the unit. The same applies once comparison begins
   on an unlimited profile.

A store failure or timeout fails the check. If returning a unit fails after a correct comparison,
the challenge stays consumed and the check returns a store error or timeout. The caller has one
two-second deadline for the entire check.

## Sessions

The session only marks which challenge it last saw. The store keeps the current challenge per session
(by a hash of the session id) and profile, and both must agree. An old copy of a session cookie can't
bring back an earlier image.

## Limits

The challenge store admits at most N images per client per clock minute across profiles, and at most
N wrong answers per client and profile per clock minute. Both counters use atomic increments in
fixed windows. See [Deployment](https://oss.bowerbird.si/captcha/docs/deployment/) for the settings and operational limits.

The rule applies the 256-byte answer limit to the string it receives. Configure the answer and key
exemptions alongside other providers' exceptions in the application's final
[`with_middleware` callback](https://oss.bowerbird.si/captcha/docs/deployment/#input-middleware). The app checks the global replacements
at boot. On an exempt answer, the limit applies before whitespace normalization.
Laravel can trim unregistered fields or turn empty strings into null first. A missing or blank submission rejected by the rule spends no attempt. A value
that exceeds 256 bytes counts as a wrong answer if a live challenge is checked.

Math challenges offer low guessing resistance. There are only 29 possible sums, and boot requires
one to three attempts for a math profile.

## What a captcha can't do

An image captcha slows automated submissions down. It doesn't stop a determined attacker with OCR. Pair
it with rate limits on the form itself, and offer people who can't read the image another way in.
