Guides · Security

Secure contact forms

A contact form receives external data and often triggers server-side processing. It is therefore a sensitive area that deserves simple, coherent safeguards kept in good shape.

Why a form is an exposure surface

Every field can become an entry point for spam, injection attempts, or abusive frequency. Even a simple form deserves a defensive approach because it often combines user input, server-side delivery, and feedback messages.

The goal is not to make the tool unusable, but to reduce the most likely risks: abusive submissions, malformed data, email header injection, or technical messages that reveal too much.

Validate on the client, and above all on the server

Front-end validation improves the experience, but it is never enough. Every important rule needs to be checked again on the server: required fields, email format, minimum length, and overall consistency of the received data.

  • The front-end can guide, but the server makes the final decision.
  • Error messages should stay clear without exposing technical details.
  • JSON or HTML responses should remain clean even when something fails.

Combine honeypot and rate limiting

A discreet honeypot can block some basic bots. Rate limiting by IP or session then reduces the risk of repeated spam and abuse on the submission endpoint.

  • The honeypot should not disturb real users.
  • A fake success can be preferable to an explicit error for suspicious submissions.
  • Rate limiting should stay light, clear, and compatible with normal form usage.

Clean fields and protect email headers

Fields used in headers need special attention. Line breaks, invisible characters, or badly formatted values can create serious issues if nothing is filtered.

  • Strip line breaks from fields used in From, Reply-To, or the subject.
  • Validate the email before using it in a header.
  • Avoid placing the user’s email directly as the main sender.

A secure form still needs to be understandable. GDPR consent, confirmation messages, and errors should remain readable, with no leakage of server paths, stack traces, or internal information.

A solid practice is to log details on the server, then return only a useful and restrained message to the visitor.

Good and bad examples

Defensive approach

Validation

Front-end rules for UX, server-side checks for real security.

Email

Domain address in From, cleaned user email in Reply-To.

Spam

Discreet honeypot, light rate limiting, and restrained feedback.

Avoid

Validation

Relying only on the browser or client-side JavaScript.

Email

Injecting the user’s name, subject, or email directly into headers.

Feedback

Showing raw errors or telling bots too precisely what was detected.

Mini checklist before publishing

  • Fields are validated on both the client and the server.
  • The form includes a non-intrusive honeypot.
  • A simple rate limit reduces repeated abuse.
  • Email header fields are cleaned from line breaks.
  • The user’s email is not used directly in From.
  • User-facing errors stay clean and non-technical.
  • GDPR consent is explicit when needed.

Conclusion

Securing a contact form mostly comes down to combining simple protections that stay readable and well placed. Together, they significantly reduce risk without weighing down the user experience.