How to secure a contact form against spam and abuse
November 25, 2026
A contact form is a public door into your mail system and your inbox, and it needs five things: server-side validation of every field, a check that the request came from your own page, a bot test whose token is verified on the server, a rate limit, and a privacy notice. The sixth, which most forms skip, is to make sure nothing a visitor types can become a mail header.
Validate on the server, by allowlist
"Input validation checks whether data meets an application's requirements before the application uses it", and the method is to "Define what the application accepts and reject values outside those rules. Do not try to recognize every malicious string" (OWASP Input Validation Cheat Sheet). For a contact form that means a maximum length on every field, a name that is text, a message that is text, and an email address checked with "a maintained email validation library compatible with the addresses your mail system supports", with the caveat that "Format validation does not prove mailbox access". Set "request size limits before buffering or parsing input" so a 50 MB message is refused before it is read.
Browser-side validation is a convenience for the visitor and nothing else; a bot posts straight to the endpoint.
Keep the mail headers clean
Forms that put the visitor's address or subject into the outgoing mail's headers are open to header injection: the input carries a line break followed by a header of the attacker's choosing, such as a list of recipients, and your server becomes a spam relay. The weakness is catalogued as CWE-93: "The product uses CRLF (carriage return line feeds) as a special element, e.g. to separate lines or records, but it does not neutralize or incorrectly neutralizes CRLF sequences from inputs" (MITRE, CWE-93).
The fix is two rules. Reject any field that will reach a header if it contains a carriage return or line feed. And send the mail to your own fixed address with a fixed subject, putting the visitor's address in the body or in a Reply-To that your library sets as a single validated value, never by string concatenation into the header block.
Check the request came from your page
A form that changes state, and sending mail is a change, should carry a token proving the request came from a page you served. "The synchronizer token pattern is one of the most popular and recommended methods to mitigate CSRF", and "CSRF tokens should be generated on the server-side and they should be generated only once per user session or each request" (OWASP CSRF Prevention Cheat Sheet). Most frameworks ship this, and OWASP says to use it: "Before building a custom token or Fetch-Metadata implementation, check whether your framework or platform already provides CSRF protection you can use." Two further rules from the same page: "Do not use GET requests for state changing operations", and the cookie attribute is a backstop, since "SameSite is useful as a defense-in-depth control but it does not replace a proper CSRF defense in most deployments."
A bot test, verified on the server
The widgets that tell a browser from a script all work the same way: the page gets a token, the server checks it. Cloudflare's documentation for Turnstile is blunt about the half of the job people skip: "The client-side widget alone does not protect your forms", because "Tokens can be forged. An attacker can submit any string to your form endpoint without completing a challenge" (Cloudflare, Turnstile server-side validation). A verified token is short-lived and single-use: "Each token is valid for 300 seconds (5 minutes) after generation" and "A replayed token will be rejected with the timeout-or-duplicate error code." hCaptcha states the same requirement: "Send the submitted token and your secret to https://api.hcaptcha.com/siteverify as a URL-encoded POST. Only allow the protected action when success is true. A frontend callback alone does not authorize the request" (hCaptcha documentation).
What the widget collects goes in your privacy notice. Turnstile "processes a variety of client-side signals ('Signals') such as client IP address, TLS Fingerprint, User-Agent Header and Sitekey and associated origin" (Cloudflare Turnstile Privacy Addendum), and if you run it invisibly, "you must reference Cloudflare's Turnstile Privacy Addendum" in your own policy (Cloudflare, widget configurations). hCaptcha's policy names "Communications metadata (e.g. information on originating IP address or other identifier, date and time of interaction)" (hCaptcha privacy policy).
A honeypot field, a hidden input that a human never sees and a crude bot fills in, is a common first line that costs the visitor nothing; it stops the simplest scripts and none of the rest. OWASP's AppSensor catalogues the idea as a "Honey Trap" detection point, "Fake (not otherwise needed by the application) data sent to the user and returned", that "is modified" (OWASP AppSensor detection points). The hidden-field version comes from practice; OWASP describes the principle without prescribing the field.
Rate limit the endpoint
A form that sends mail can be used to flood a mailbox, and OWASP uses that exact example: "The classic example is a webform that will send out an email after posting the request. A captcha could then prevent the mailbox from getting flooded by a malicious attacker or spambot" (OWASP Denial of Service Cheat Sheet). Limit submissions per address and per session, and answer excess with the status code made for it: "The 429 status code indicates that the user has sent too many requests in a given amount of time", and the response "MAY include a Retry-After header indicating how long to wait" (RFC 6585). How to rate limit a login page has the mechanics; the limits for a contact form can be far tighter, since nobody sends ten messages a minute by hand.
The privacy side
A contact form collects personal data, so it needs a lawful basis and a notice. The basis is usually that processing "is necessary in order to take steps at the request of the data subject prior to entering into a contract" or the legitimate interest of answering the message (GDPR Article 6). The notice is due "at the time when personal data are obtained" and must say "the period for which the personal data will be stored" (GDPR Article 13), which is why the form links to the privacy policy and the policy names how long messages are kept; How long to keep server and access logs covers the same question for the log the form writes to.
If the form has a newsletter checkbox, it starts unticked: "The use of pre-ticked opt-in boxes is invalid under the GDPR", and "If consent is bundled up as a non-negotiable part of terms and conditions it is presumed not to have been freely given" (EDPB Guidelines 05/2020 on consent).
The checklist
Server-side validation with length limits on every field, a validated email address and a request size limit.
No visitor input in a mail header; carriage returns and line feeds rejected; a fixed recipient and subject.
A CSRF token from your framework; POST only.
A bot test whose token is verified on the server and named in the privacy notice; a honeypot if you like.
A rate limit with a 429 response.
A privacy notice at the form and a retention period for the messages, with any marketing checkbox unticked.