DEV Community

The security checklist I use on every Next.js app

Residency Kondapur's platform stores tenants' names, phone numbers, rent bills and payment screenshots. That's the kind of data you can't treat as a side project.

These are the protections I built into it, and now start every Next.js app with.

Passwords

  • Hash user passwords with bcrypt at cost 12. Never store them, never log them.
  • Compare secrets in constant time. For the admin password I hash both the input and the stored value with SHA-256, then compare with crypto.timingSafeEqual. A plain === leaks timing information.
  • One error message for every failure. "Authentication failed", never "wrong password" or "no such user". Don't tell an attacker which half they got right.

Sessions

  • Sign session tokens with HMAC-SHA256 so they can't be forged or edited. My format is a timestamp, some randomness, and a signature over both.
  • Short lifetimes. Eight hours for admins, 24 for tenants.
  • Cookies: httpOnly, SameSite=Strict, and Secure in production. JavaScript on the page can't read them, and other sites can't make the browser send them.

Requests

  • Validate every body with Zod before it gets anywhere near the database. Types in TypeScript disappear at runtime; a schema doesn't.
  • Check the Origin header on every write to private APIs, in middleware, so a form on another site can't post to yours. Handle both www and the bare domain.
  • Rate-limit per IP on anything sensitive. A simple in-memory map of counters and reset times is enough for a single server:
Endpoint Limit
Admin login 5 per 15 minutes
Tenant login 5 per 15 minutes
Signup 3 per hour
Public forms 3 per hour

Responses

  • Security headers on every response: X-Content-Type-Options: nosniff, X-Frame-Options: DENY, a strict Referrer-Policy, a Permissions-Policy that turns off camera, microphone and geolocation, and HSTS in production.
  • Cache-Control: no-store on private API routes, so no proxy ever caches one tenant's bills and serves them to another.
  • Block files that should never be served. My middleware returns 404 for any .sql path, so a migration file that ends up in the wrong folder can't be downloaded.

Limits

Small caps prevent a whole class of abuse. Tenants can have at most ten open support tickets. Uploads are typed (a bill screenshot is a different upload kind from a ticket photo). Every form has a maximum length.

None of this is exotic. That's the point: it's a checklist, and the value is in doing all of it every time rather than the clever parts some of the time.

Originally published on partharsid.dev. More of my work: partharsid.dev.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.