
How We Moved Our Contact Form to Turnstile and Broke It
So we have a contact form on this site, and there was a hidden field in it which is supposed to catch the bots, but a lot of bots now are using a normal browser so they don't fill it. We moved to Turnstile from Cloudflare. It is free and most visitors are not doing anything, no pictures with traffic lights. Before you do the same, check which key is which, because this is where we lost half an hour.
<form action="/contact" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="message">Message</label>
<textarea id="message" name="message" required></textarea>
<div class="cf-turnstile" data-sitekey="YOUR_SITE_KEY"></div>
<button>Send</button>
</form>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js"
async defer></script>
On the page it is one div and one script, and with them Turnstile adds a hidden input to your form with a long token in it, it is called cf-turnstile-response. Some people stop here and think they are protected, but a bot does not need your page at all, it can post to the same address with any text in that field, so the server has to send the token to Cloudflare and ask. Ours is a Worker and it looks like this.
async function isHuman(token, secret, ip) {
if (!token || !secret) return false;
const body = new FormData();
body.append('secret', secret);
body.append('response', token);
if (ip) body.append('remoteip', ip);
try {
const response = await fetch(
'https://challenges.cloudflare.com/turnstile/v0/siteverify',
{ method: 'POST', body },
);
const result = await response.json();
return result.success === true;
} catch (error) {
console.error('siteverify failed', error);
return false;
}
}
If Cloudflare is down, or we forgot to set the secret, it is also false and the answer is 403 and no email. We think it is better this way, you can always open it later, and the person at least sees a page which says wait a few seconds and send again.
Now the keys. One is for the page and one is for the server, and they look almost the same, so the first one went into the Worker, and the Worker was sending the public key to Cloudflare and Cloudflare was answering invalid-input-secret, and the form answered 403 to everyone. It was already on production. We put the old form back from git, it took like 5 minutes, then set the key again and again it was the wrong one. If your secret is 24 characters long it is not the secret, ours is 35.
What helped is wrangler dev with the --remote flag. It takes the code from your laptop and the secrets from the server, so you can check the key and nobody is affected. You send some garbage instead of the token. If the answer is invalid-input-response then you are good, Cloudflare accepted the secret and only did not like the garbage. If it still says invalid-input-secret do not deploy it.
Also after deploy don't trust the first try in the browser, for us it went to the previous version and looked like all works. Post to it with curl without any token, should be 403.
curl -s -o /dev/null -w "%{http_code}\n" \
-d "[email protected]&message=hi" \
https://example.com/contact
And one small thing about the button, because the token lives only five minutes and somebody can write a message for ten, so the button is disabled until we have a token and disabled again if it expired, and Turnstile by itself goes and gets the new one.
<div class="cf-turnstile"
data-sitekey="YOUR_SITE_KEY"
data-callback="onVerified"
data-expired-callback="onExpired"></div>
const button = document.querySelector('form button');
button.disabled = true;
window.onVerified = () => { button.disabled = false; };
window.onExpired = () => { button.disabled = true; };
See also: Rate Limiting Scrapers on Cloudflare Without Blocking Google.