Next.js Security Best Practices: A Practical Checklist

A practical Next.js security checklist covering server actions, API routes, environment variables, middleware auth, headers and dependency risks.

Yrzo AI28 November 2026 3 min read

Why Next.js needs its own checklist

Next.js blurs the line between front end and back end. The same project contains React components that run in the browser, server components, API routes and server actions that run on the server. Most Next.js security bugs come from forgetting which side of that line a piece of code is on.

1. Treat server actions and API routes as public endpoints

Every server action and route handler can be called directly, not just from your UI. For each one:

  • check the user is signed in
  • check the user is allowed to act on this specific record — see IDOR
  • validate input with a schema library rather than trusting types

2. Do not rely on middleware alone for auth

Middleware is a convenient place to redirect signed-out users, but it should not be your only authorisation layer. Re-check permissions where data is read and written. A bypass in routing should never mean a bypass in data access.

3. Keep secrets on the server

Only variables prefixed NEXT_PUBLIC_ are sent to the browser — so never prefix a secret that way. Review your built JavaScript for keys; see API keys exposed in JavaScript. Keep .env files out of version control and out of public folders — see exposed .env files.

4. Watch what you pass to client components

Anything passed as props from a server component to a client component ends up in the page. Do not pass whole user objects with emails, roles or tokens when the component only needs a name.

Is your site exposed to this?

Run a free, non-intrusive check of what attackers can see from outside. No account needed.

5. Set security headers

Use headers() in next.config.js or middleware to set Content-Security-Policy, Strict-Transport-Security, X-Frame-Options and Referrer-Policy. See how to fix security headers and HSTS explained.

6. Be careful with dangerouslySetInnerHTML

React escapes output by default, which prevents most XSS. dangerouslySetInnerHTML switches that off. Sanitise any HTML first.

7. Lock down images and redirects

Restrict images.remotePatterns to domains you trust, and validate any redirect target to avoid open redirects.

8. Rate limit sensitive routes

Login, sign-up, password reset and contact forms need rate limiting — see API rate limiting and password reset vulnerabilities.

9. Keep dependencies updated

Next.js and its ecosystem release security fixes regularly. Update promptly and run npm audit in CI.

Test the running app

A checklist only goes so far. Yrzo AI's Professional tier tests custom applications like Next.js apps for access control, injection, auth and exposure, with a retest included — see plans or start free.

Related reading: React App Security Checklist: Front-End Risks That Matter · API Keys Exposed in JavaScript: How to Find and Fix Them · Vibe Coding Security: Risks in AI-Built Apps and How to Fix Them

Next.js security best practicesNext.js security checklistsecure Next.js appNext.js API route security
Share

Check your own website in 60 seconds

Free check, no account needed. Want the full picture? Full reports start at £149, with up to 110 checks with a 14-day money-back guarantee.

Keep reading