Skip to content

Captcha

In short

The threat

A bot farm with many addresses stays under every rate limit, and a captcha token solved elsewhere, or a provider that is down, lets it through anyway.

The rule

Check the captcha before anything else on the doors bots target, check where and for what the token was solved, and treat an outage as a failure.

In the package
http/turnstile.ts, passed to createAuth as captcha.

Rate limits count requests per IP, per account and per address. A bot farm with many addresses can stay under all of them: credential stuffing at login, mass sign-ups, floods of reset mail. A captcha makes each request cost a solved challenge, but the token it produces is just a string the client sends, and the checks around it decide whether it costs anything:

  • A token solved elsewhere still verifies. One solved on the attacker’s own page that embeds your public sitekey, or on a cheap form of your own app, passes at the provider.
  • An outage opens the gate. A server that lets requests through whenever the provider does not answer has no captcha during an outage, which an attacker can wait for or try to cause.
The captcha is the first thing a request meets, and it fails closed: a token solved elsewhere and a provider that does not answer are both refused before any rate limit, store call or password hash.

On login, sign-up and the password reset request, three requests reach the captcha check, which comes before the door limit, any store call and any password hash. A token solved on your own form for this door passes and goes on through the door limit, the store calls and the password hash. A token solved on another hostname, or for another door, is rejected with captcha_failed. A request the provider does not answer within 5000 ms is refused with captcha_unavailable, never let through. Neither refused request reaches the later steps.

Text version

The captcha is a gate in front of the expensive work, and anything short of a clear pass keeps it shut.

When captcha is configured, login, sign-up and the password reset request check it before anything else: before the door’s rate limit, before any store call and before any password hash. Plugins can call the same check through their context.

Every verdict is counted in the auth_captcha metric by door and verdict, and a refused login is audited as a login.failure.

A verifier answers pass, reject or unavailable. Both refusals stop the request; they differ only in the code the client sees:

Code Means
captcha_failed The challenge was not solved.
captcha_unavailable The provider could not answer, so the app can tell the user to try again later instead of telling them they failed.

turnstile(options) sends the token, and the client IP when there is one, to Cloudflare’s siteverify endpoint.

  • expectedHostnames: a token solved on any other hostname is rejected.
  • checkAction: true: the widget’s action must equal the door name (login, register or password-reset), so a token solved on the sign-up form cannot be spent at login.

These all count as unavailable:

  • a network error;
  • no answer within 5000 ms (the default timeoutMs);
  • an answer whose status is not a success;
  • a body without a boolean success;
  • Cloudflare’s own internal-error.

Any other failure Cloudflare reports, such as a token that expired or was already used, is reject. A missing token, or one longer than 2048 characters, is rejected without calling the provider.

turnstile() returns a verifier with a second method, check(token, context), that verifies exactly like verify and returns the verdict with its cause. The optional onResult option gets the same outcome for every call: a pass, a refusal and an outage. Use it to log or count, so an outage shows apart from a misconfiguration such as a wrong secret.

An outcome holds the verdict, the door, the errorCodes siteverify answered with (empty when it was not called) and a reason:

Verdict reason When
unavailable timeout No answer within timeoutMs, while connecting or reading the body
unavailable network-error The request or the body read failed (DNS, TLS, a reset connection)
unavailable http-status An answer whose status is not a success; status holds the code
unavailable unparsable-body A success status with a body that is not JSON or has no boolean success
unavailable provider-error Cloudflare reported internal-error
reject missing-token No token; the provider was not called
reject token-too-long Longer than the limit above; the provider was not called
reject provider-refused Any other failure siteverify reported: see errorCodes (timeout-or-duplicate for an expired or reused token, invalid-input-secret for a wrong secret)
reject hostname-mismatch Solved on a hostname outside expectedHostnames
reject action-mismatch Solved for another door, with checkAction

An outcome never carries the token or the secret. The hook is not awaited and cannot change the verdict.

Its synchronous part runs on the request path, so keep it cheap and hand slow work (a network call to a metrics service) to a promise or a queue: the outcome it gets is frozen, and a hook that throws or rejects is reported to the verifier’s logger option (silent by default) with the error message only.

The unavailable reasons are a shared type, UnavailableReason, meant for every provider check the package makes.

Why the captcha fails closed when the breach check fails open

Section titled “Why the captcha fails closed when the breach check fails open”

The breach check judges a password the user chose; skipping it during an outage accepts one password that might have leaked, and the outage is logged.

The captcha is a gate against automated traffic; skipping it during an outage opens the gate to exactly that traffic, at a moment an attacker can choose.

A captcha raises the price of a request; it does not stop people or paid solving services from solving challenges, so the rate limits and the lockout still do the counting. The verifier does not track tokens itself: it relies on siteverify to refuse a token that expired or was already used.

A Turnstile verifier with both checks on and every verdict counted by its cause.

captcha.ts
import { turnstile, type TurnstileOutcome } from '@noctcore/auth';
const secret = process.env.TURNSTILE_SECRET;
if (!secret) throw new Error('Set TURNSTILE_SECRET');
/** Count every verdict by its cause, so an outage shows apart from a wrong secret. */
function record(outcome: TurnstileOutcome): void {
const cause = outcome.verdict === 'pass' ? 'pass' : outcome.reason;
// Cheap and synchronous: the hook runs on the request path.
process.stdout.write(`captcha ${outcome.door} ${outcome.verdict} ${cause}\n`);
}
// Pass it to createAuth as `captcha`. Both checks are off unless set.
export const captcha = turnstile({
secret,
// A token solved on any other hostname is rejected.
expectedHostnames: ['app.example'],
// The widget's action must equal the door: login, register, password-reset.
checkAction: true,
onResult: record,
});

The guide has no page on captchas. Its passwords page warns against strict IP-based rate limits, which block people on shared networks and are bypassed through proxy networks. The package agrees: its per-IP door limits are generous (see Brute force and lockout), and a captcha is the optional per-request cost for the doors where proxy networks are the problem.

Test Attack Stopped by
A077 Passing a captcha with a token solved elsewhere, or while the provider is down http/turnstile.ts, core/auth-core.ts