Cybersecurity

Securing a public web form: a checklist

A contact form looks harmless, but it is an open door to your server, your inbox and sometimes your database. Most of the protection takes very little code. This checklist covers what to put in place before a public form goes live.

By BuildQore · · 3 min read

Validate on the server

Browser validation is there to help honest visitors. Anyone can bypass it by sending a request directly, so the server has to check everything again. Define the shape you accept, including maximum lengths, and reject the rest. An allowlist of what is valid is much stronger than a blocklist of what looks dangerous.

enquiries.ts
import express from 'express';
import rateLimit from 'express-rate-limit';
import { z } from 'zod';
import { saveEnquiry } from './store';

const Enquiry = z.object({
  name: z.string().trim().min(1).max(120),
  email: z.string().trim().email().max(254),
  message: z.string().trim().min(10).max(5000),
  website: z.string().optional(), // honeypot: hidden from real visitors
});

const app = express();
app.use(express.json({ limit: '32kb' }));

const limiter = rateLimit({ windowMs: 60_000, limit: 5 });

app.post('/api/enquiries', limiter, async (req, res) => {
  const parsed = Enquiry.safeParse(req.body);
  if (!parsed.success) {
    res.status(422).json({ error: 'invalid_input' });
    return;
  }
  // A filled honeypot means a bot. Accept quietly, store nothing.
  if (!parsed.data.website) await saveEnquiry(parsed.data);
  res.status(202).json({ status: 'received' });
});

Slow down abuse

  • Rate limit by IP address, and add a limit per email address if the form sends anything out.
  • Add a honeypot: a field hidden from people that simple bots fill in. It costs visitors nothing.
  • Add a CAPTCHA only if spam continues. It puts work on real people and can create accessibility problems.
  • If the form acts on a signed-in session, protect it against cross-site request forgery with SameSite cookies and CSRF tokens.

Treat submissions as hostile data

The risk often appears later, when a staff member opens a submission in an admin screen. A message containing a script tag is harmless in a database and dangerous if it is rendered as HTML. Escape output by default, and use parameterised queries for every database write.

Email needs the same care. Never place visitor input in headers such as From or Subject without removing line breaks, or an attacker can inject extra headers. Send from your own address, and put the visitor's address in Reply-To after validating it.

If the form accepts files

Uploads carry the most risk on the page, so be strict.

  • Allow specific file types only, and check the file content as well as the extension and the declared type.
  • Enforce a size limit on the server, not only in the browser.
  • Generate a random file name, and never use the uploaded name in a storage path.
  • Store files outside the web root, or in private object storage, and serve them through signed links.
  • Scan uploads for malware before anyone on your team opens them.

Respect the data you collect

  • Ask only for what you will use. Every extra field is more data to protect.
  • Serve the form over HTTPS, and link to a privacy notice beside the submit button.
  • Decide how long you will keep submissions, then delete them on schedule.
  • Limit who can read submissions, and keep them out of application logs.

Test the unhappy paths

Before launch, submit an empty form, a very long message, a script tag and the same request twenty times. Then confirm that the visitor sees a useful error when the server is down. A form that fails silently loses enquiries without anyone noticing, so add an alert for delivery failures too.

Keep reading

All articles

Get new articles by email.

Occasional notes on building and testing software. No filler.

Used only for these updates. See the privacy policy.

Want this done on your project?

Tell us what you are building or fixing, and we will talk through how we could help.