Securing a public contact form

Engineering note, , by Kansi Digital Library Foundation

A contact form is the one place on a mostly static website where strangers can send data to a server. That makes it the part most worth getting right. This note describes how the form on this site is built and why.

What can go wrong

A form that emails its contents to someone has a short, well-known list of risks:

  • Spam. Automated senders find forms within days of a site going live.
  • Header injection. If a submitted value such as the subject is placed into an email header unchecked, a line break in that value can add recipients of the attacker’s choosing.
  • Leaked secrets. Mail credentials placed in browser code are public.
  • Abuse of the mail account. Without limits, the form becomes a way to send thousands of messages through someone else’s account.
  • Cross-site scripting. If a message is later displayed as HTML, a script inside it runs.

The design

The browser never sends email. It posts JSON to a small API, and the API decides what happens next.

  1. The server validates everything. Validation in the browser exists to help people fill the form in; it protects nothing. The API checks every field again: type, length, format, and a fixed list of enquiry categories. Unknown fields are rejected.
  2. Single-line fields stay single-line. Name and subject are refused if they contain a line break or control character, which removes header injection at the door. The message is built with Python’s email.message.EmailMessage, which also refuses to write a header containing a line break.
  3. Bad input raises an error that is caught. A validator that finds a problem raises ValueError. The framework converts that into a 422 response naming the field. The process does not stop, and the response does not repeat what was submitted.
  4. Secrets live only in the API’s environment. The destination address and the mail credentials are environment variables on the server. Neither appears in the site’s source or in any response.
  5. Abuse is limited. Each address may send a small number of enquiries per hour. A hidden field that people never see, and a check on how quickly the form was completed, catch most automated senders. Those are answered as if they succeeded, so the sender learns nothing.
  6. Email is plain text. The notification is sent as text/plain, so nothing a sender types is interpreted as markup by the mail client.
  7. Only this site may call the API from a browser. CORS allows the site’s own origin, and requests carrying another Origin header are refused.

What it does not do

The enquiry is not stored. Once the email is handed to the mail server, the API keeps nothing but a log line recording that an enquiry of a given category arrived. That is a deliberate choice: data that is not kept cannot leak, and a single inbox is enough for the volume this site expects.

The rate limiter keeps its counts in memory. That is correct for one API instance and would need a shared store if the API were ever run on several.

Testing

Each behaviour above has an automated test: valid enquiries are delivered, each kind of invalid input is rejected with the right field named, header-injection attempts never reach the mail function, the rate limit holds, a spoofed forwarding header does not evade it, and an unexpected failure returns a generic error without internal detail.

Questions about this article?

Describe what you need. Kansi reads every enquiry and replies by email.

Contact Kansi

Ask about KDLF

I answer from what is published on this site: services, expertise, projects and research. For anything else, use the contact page.

An AI assistant. It can be wrong. Contact Kansi for a definite answer.