← Back to Guides

Security Headers, HttpOnly Sessions, and Rate Limiting Without a Framework Maze

A practical walkthrough of the defensive HTTP boundary every web app needs—and why you can implement it cleanly in under a hundred lines.

Security middleware does not need a dozen third-party npm packages to be effective. In fact, adding opaque middleware wrappers often hides what your application actually enforces on every request.

Understanding the HTTP boundary makes it trivial to implement—and verify—in any stack, whether Rust (axum), Go (chi), or TypeScript (Hono).

1. Keep sessions opaque and invisible to JavaScript

Storing bearer tokens in localStorage exposes user sessions to any XSS payload or compromised third-party script. Server-side opaque session IDs stored in cookies close that entire class of risk:

Set-Cookie: sid=<random-256bit-id>; Path=/; HttpOnly; SameSite=Lax; Secure
  • HttpOnly prevents document.cookie from reading the session token.
  • SameSite=Lax blocks cross-site POST requests from automatically carrying session cookies while preserving normal link navigation.
  • Secure ensures browsers never transmit the cookie over plaintext HTTP in production.

2. Rate-limit by route sensitivity, not uniformly

Global rate limits alone are either too strict for normal browsing or too loose to stop brute-force attacks. Separate your buckets:

  • Strict bucket for /login, /register, /forgot-password, and token verification endpoints.
  • Standard bucket for authenticated write endpoints and file uploads.
  • Honest proxy awareness: if your app sits behind Cloudflare or Caddy, explicitly configure trusted proxy handling (TRUST_PROXY)—otherwise every user shares a single rate-limit bucket under the proxy’s IP.

3. Baseline security headers on every response

Set defensive defaults in one middleware layer so individual handlers never forget:

X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()

Pair those with a strict Content-Security-Policy tuned to your asset strategy. If your frontend avoids inline script injection and third-party trackers, your CSP stays short and auditable.

Why clarity beats magic

Whether you inspect rate-limit.ts in dulak or the Axum/Chi middleware in fondasi, every security boundary is plain code you can read in five minutes. When something goes wrong, you never have to guess which magic dependency swallowed the header.