Skip to content
Darin Larimore
All posts

Cloudflare Turnstile on a contact form, the parts the docs skip

3 min read
On this page
  1. Tokens are single-use
  2. Verify server-side, pass the IP, fail closed
  3. Turnstile is one signal, not a bouncer
  4. Load the script once in an SPA
  5. Testing without Cloudflare
  6. Reply-to the visitor

The contact form on this site uses Cloudflare Turnstile. I picked it over reCAPTCHA because there are no puzzles, nothing from Google on the page, it is free, and it does not profile the visitor. The widget itself takes ten minutes. Everything below is what I had to add around it.

Tokens are single-use

The widget hands you a token. Your server sends it to siteverify once, and after that it is dead. So if a submit fails validation for any other reason, the name field say, the second attempt posts the same spent token and Turnstile rejects it with a message that blames the visitor. After every failed submit you must reset the widget:

const turnstile = useTurnstile(props.turnstileSiteKey, widget, {
    onToken: (token) => { form['cf-turnstile-response'] = token; },
    onExpired: () => { form['cf-turnstile-response'] = ''; },
});

form.post(store.url(), {
    onSuccess: () => { form.reset(); turnstile.reset(); },
    onError: () => turnstile.reset(),
});

Verify server-side, pass the IP, fail closed

The widget only proves a browser solved the challenge. The server call is what stops a bot from posting the token's name. Send the remote IP along with it. And decide what happens when the secret is missing. Mine refuses the submission instead of letting everything through, and the validation rule tells the visitor the truth:

if (! $this->turnstile->isConfigured()) {
    $fail('Verification is unavailable right now. Please email me instead.');
    return;
}

The log line has the operator's detail. The visitor gets a way out that still reaches me.

Turnstile is one signal, not a bouncer

Two more layers cost nothing. A honeypot field called website, hidden from people and validated with prohibited, catches the bots that fill every input. And the POST route sits behind throttle:5,10, so one IP gets five attempts per ten minutes. Each layer catches a different kind of junk and none of them bothers a person.

Load the script once in an SPA

Inertia keeps the document alive between visits. If the contact page appends the Turnstile script on every mount you get a second script tag and a second widget after back navigation. Load api.js?render=explicit once, reuse the tag if it exists, render the widget into a ref on mount, and call turnstile.remove(widgetId) on unmount. The visitor can also navigate away while the script is still downloading, so check that the component is still mounted before rendering.

Testing without Cloudflare

The suite fakes the HTTP client and asserts what was sent:

Http::assertSent(fn ($request) => $request['secret'] === 'test-secret-key'
    && $request['response'] === 'valid-token');

Dummy keys live in phpunit.xml, so CI never needs a real secret. For clicking through locally, Cloudflare publishes always-pass and always-fail site keys, which makes a dev environment deterministic.

Reply-to the visitor

The mail goes to me, but replyTo is set to the visitor's name and address, so hitting reply in my mail client starts a normal thread. That one line is the difference between a contact form and a notification.