Hardening sujeethm.com: CSP, abuse controls and an honest audit
How this site is secured in production: a per-request nonce CSP, abuse controls on its two public APIs, and three real flaws an audit found and fixed.
- Role
- Sole developer and owner
- Stack
- Next.js 15, TypeScript, Vercel, Resend, Cloudflare Turnstile
- Outcome
- A+ on Mozilla Observatory (12 of 12 tests); strict CSP and abuse controls in production; three API flaws fixed; framework advisories closed
Summary
This site is small, but it has the same exposure as any public web app: it renders third-party content, runs two public API routes (a contact form that sends email and an AI assistant that spends money per request), and is a target simply for being online. This case study covers how it is secured, and what a structured audit in October 2026 found wrong with that.
Context and constraints
- Stack: Next.js (App Router) on Vercel, Tailwind, MDX content.
- Public endpoints:
/api/contact(sends through Resend) and/api/chat(streams responses from a hosted LLM). - Constraints: no database, no paid WAF, serverless functions with no shared memory, and a framework that injects its own inline scripts.
Threat model
| Asset | Threat | Control |
|---|---|---|
| Visitors | Injected script (XSS) via content or dependencies | Nonce-based CSP with strict-dynamic |
| Visitors | Clickjacking | frame-ancestors 'none' and X-Frame-Options: DENY |
| My inbox and domain reputation | Contact form used to spam or relay | Honeypot, rate limit, validation, optional Turnstile, fixed autoreply |
| API budget | Token burn or prompt injection through the chat | Rate limit, server-held key, bounded history, role filtering |
| Trust | Telling a visitor a message was sent when it wasn't | Check the email provider's result before reporting success |
Key decisions
A per-request nonce instead of 'unsafe-inline'. Next.js injects inline
scripts, so a static CSP either allows all inline script or breaks the site.
Middleware generates a fresh nonce for every document request and passes it to
Next.js, which applies it to its own scripts. With strict-dynamic, only
nonced scripts and what they load can run:
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
const csp = [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
"style-src 'self' 'unsafe-inline'",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
"object-src 'none'",
].join('; ');
The trade-off: every page becomes dynamically rendered, because the nonce has to differ per request. For a site this size, that cost is acceptable.
Prefetches don't get a nonce. The middleware skips router prefetch requests, so a prefetch can't consume a nonce meant for the real navigation.
'unsafe-inline' for styles only. The animation library writes inline
style attributes, which a nonce can't cover. Style injection is far less
dangerous than script injection, so this is a deliberate, documented exception.
A separate, stricter policy for static pages. The Pigeon Express page is
plain HTML served from public/, outside the app. It gets its own fixed
policy (script-src 'self', no inline anything), because it doesn't need the
nonce machinery at all.
Abuse controls on the APIs
- Contact: a hidden honeypot field (bots that fill it get a fake success), 3 submissions per 10 minutes per IP, type and length checks on every field, an email-format check that rejects header-injection characters, HTML escaping of everything placed into the email, and optional Cloudflare Turnstile.
- Chat: 15 messages per minute per IP, the API key held only on the server, and a capped response length.
The audit: what was wrong
In October 2026 I audited the site against its own claims. Four real problems came out of it, each confirmed before it was fixed:
- False success on the contact form. The email SDK reports failures in a
returned
errorfield rather than throwing, and the route never checked it. Running the production build with a deliberately invalid API key,POST /api/contactreturned200 {"success":true}, so a visitor would have been told their message was sent when it went nowhere. Fix: check the result and return502with a "please email me directly" message. - Autoreply as a relay. The confirmation email went to whatever address was typed into the form and quoted the visitor's message back. Anyone could have used it to send their own text, from my domain, to a third party. Fix: the autoreply no longer contains any visitor-supplied text.
- Prompt injection by role. The chat route forwarded the client's message
array verbatim, including a
systemrole, so a visitor could override the assistant's instructions and send unbounded history. Fix: onlyuserandassistantturns are accepted, the history is capped at 12 turns of 1,000 characters, and the last turn must come from the user. - An outdated framework.
npm auditlisted 25 advisories against the Next.js 14.2.35 the site ran on. One was directly relevant: cross-site scripting in App Router applications that use CSP nonces, which is exactly this design. Others included denial of service in Server Components and remote code execution in the image optimiser. Fix: upgraded to Next.js 15.5.28 (the patched 15.x release) and React 19, which meant movingheaders()and route parameters to Next 15's asynchronous APIs, then re-testing every route for CSP violations.
Testing and validation
Each fix was verified against a local production build, never against real recipients:
| Request | Before | After |
|---|---|---|
| Contact, invalid email-provider key | 200 success | 502 with fallback message |
| Contact, honeypot filled | 200 success (silently dropped) | unchanged |
| Contact, malformed email | 400 | unchanged |
| Chat, only a system message | accepted | 400 |
| Chat, history ending with assistant | accepted | 400 |
| Every route after the framework upgrade (desktop and mobile) | — | 200, no CSP violations, no console errors |
Response headers were checked with curl -I in production: HSTS (two years),
the nonce CSP, nosniff, X-Frame-Options, Referrer-Policy and
Permissions-Policy are all present, and the framework's x-powered-by header
is now removed.
An independent check agrees: on 20 November 2026,
Mozilla Observatory
graded the site A+ (score 110), passing all 12 of its tests. The site also
publishes a security.txt (RFC 9116), so anyone
who finds a problem knows where to report it.
What's still open
- Rate limiting is per serverless instance, not global. It blunts casual abuse but isn't a hard limit; a shared store such as Redis would make it one.
- A pinned build-time dependency. Next.js 15 bundles an older PostCSS
with known advisories. Since November 2026 an npm override makes it use the
patched release, and
npm auditreports no vulnerabilities; the override can go once the site moves to Next.js 16.
Lessons
- Read what the SDK actually returns. "No exception" is not "success".
- Anything that sends mail to an address a visitor types is an outbound channel and should carry no visitor-controlled content.
- Treat the client as untrusted even when you wrote the client. The chat widget only ever sends user and assistant turns, but the server must not assume that.