Stopping contact-form spam without a CAPTCHA
How the contact form on this website keeps spam out with no CAPTCHA and no outside service: a hidden field, a signed form timestamp, two plain rules for adverts and links, separate limits for spam, and a small puzzle the browser solves on its own.
- Topic
- Engineering
- Published
- Updated
- Reading time
- 7 min read
A contact form is an open door. Once bots find it, they fill it with adverts, link offers and nonsense. The usual answer is a CAPTCHA, which asks real visitors to pick out traffic lights and usually loads a script from another company.
The contact form on this website has no CAPTCHA and calls no outside service. Instead, the server runs a few checks on every message. Here is how each one works, with the real settings, so you can use the same ideas on your own form.
1. A hidden field people never fill in
The form has one extra field that is hidden from view and from screen readers. People never see it, so they leave it empty. Simple bots fill in every field they find. If that field arrives with anything in it, the server replies as if all went well and throws the message away.
2. Messages from the site's own address
Some spam pretends to come from the website's own domain. A real visitor writing to me does not use an address on my own domain, so any message whose sender address is on this domain, or on one of its subdomains, is treated as spam and not saved.
3. A signed timestamp: was the form opened first?
When the server builds the page, it adds a hidden value to the form: the time the form was shown, signed with a secret key that only the server knows. When a message comes back, the server checks that value.
- Missing or changed: the message was posted straight to the server without anyone opening the form.
- Older than 7 days: treated the same way, so a copied value cannot be reused forever.
- Less than 3 seconds old: nobody can read a form and write a message that fast.
Because the value is signed, a bot cannot write in an earlier time to get past the 3-second rule. In Python, the itsdangerous library does the signing and the age check in one step:
signer = URLSafeTimedSerializer(secret_key, salt="contact-form-started")
token = signer.dumps(int(time.time())) # put in the form as it is shown
shown = signer.loads(token, max_age=7 * 24 * 60 * 60) # fails if altered or too old
if time.time() - shown < 3:
... # faster than a person can typeMessages caught by checks 2 and 3 cannot come from a person using the form normally, so they are not saved at all.
4. Two rules for adverts and links
The next two rules catch messages that look like spam but could, once in a while, come from a real visitor:
- The message links to another website and uses advert wording, such as "buy now", "limited time", "guest post", "backlinks" or "SEO packages".
- The message links to three or more other websites.
Links to this website do not count. A message that breaks one of these rules is not deleted. It goes to a separate Spam folder in the admin, so a real enquiry that happens to include links is never lost. It is also never emailed to me.
5. Spam gets its own limit
Every message headed for the inbox passes through two rate limits: 5 messages per IP address in 15 minutes, and 100 for the whole site per hour. A person who goes over is asked to wait, and the form keeps what they typed. IP addresses are not stored as they are. Each one is turned into a keyed hash (an HMAC) first.
Suspected spam has a limit of its own: at most 20 saved per hour across the whole site. Keeping the two apart matters. If spam and real messages shared one limit, a burst of spam could use it up, and real visitors would be told to wait. Suspected spam older than 30 days is deleted automatically. That cleanup never touches the inbox.
6. A small puzzle the browser solves by itself
The last check makes every message cost the sender a little computing time. It uses the same scheme as ALTCHA, an open-source alternative to CAPTCHAs:
- When a visitor starts filling in the form, the browser asks the server for a puzzle. The server picks a random number from 0 to 50,000, adds it to a random salt that includes an expiry time, and sends back the SHA-256 hash of the two, together with a signature.
- The browser tries 0, 1, 2 and so on, hashing each one with the salt, until the result matches. This runs in the background while the visitor types, so they do nothing extra.
- The answer is sent with the message. The server checks the signature and the hash, rejects puzzles older than 20 minutes, and records each solved puzzle, so one answer cannot be used twice.
For one person sending one message, the work goes unnoticed. A bot sending thousands of messages has to solve a fresh puzzle for each one. The browser uses its built-in Web Crypto API, so no extra library is loaded.
async function sha256Hex(text) {
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
return Array.from(new Uint8Array(digest), b => b.toString(16).padStart(2, '0')).join('');
}
for (let number = 0; number <= puzzle.maxnumber; number++) {
if (await sha256Hex(puzzle.salt + number) === puzzle.challenge) {
// send number, salt, challenge and signature with the form
}
}Every sender gets the same reply
Whether a message lands in the inbox, in the Spam folder or nowhere, the form shows the same "thanks" reply. A bot testing the form cannot tell which rule caught it. Only two cases get a different answer, and both are aimed at real people: a sender who goes over the rate limit is asked to wait, and if the browser could not solve the puzzle, the visitor is asked to reload the page and try again.
On the server, the order is: the hidden field, basic checks on the name, email and message, the puzzle, then checks 2 to 4, and finally the rate limits.
What it does not stop
- People paid to send spam by hand. They open the form, take their time and solve the puzzle like anyone else. The link rules and the Spam folder are the backstop.
- Visitors without JavaScript. The puzzle needs it, so keep an email address on the page as a fallback.
- Every kind of advert. Each phrase you add to a word list could match a real message. That is why the advert rule only applies when the message also links to another website.
Using this on your own site
None of these checks needs a paid service. A sensible order is to start with the hidden field, the signed timestamp and separate rate limits, then add the puzzle if bots still get through.
If your form feeds a CRM or another system, the same care about duplicates applies further down the line: see idempotency keys for web APIs and building webhook pipelines that do not lose data. To check that real enquiries arrive once the form is live, read what to track first on a small business website, and how this site counts visits without cookies.
Before launch, work through the website launch checklist. Want spam protection like this on your own site? See the sites I have built, or send me a few lines.
Written by Lay Shah, an independent developer building websites and tools for small businesses. Discuss a brief More articles