Skip to main content
Back to blog
May 10, 2026|8 min read|Antoine Duno|Developer Tools

How to Secure Your AI-Generated Next.js App (2026 Guide)

Built your Next.js app with Cursor or GitHub Copilot? AI assistants are great at writing features — but they consistently miss security configuration. Here's how to fix it in five steps.

Antoine Duno

379 words

AD

Antoine Duno

Founder of ZeriFlow · 10 years fullstack engineering · About the author

Key Takeaways

  • Built your Next.js app with Cursor or GitHub Copilot? AI assistants are great at writing features — but they consistently miss security configuration. Here's how to fix it in five steps.
  • Includes copy-paste code examples and step-by-step instructions.
  • Free automated scan available to verify your implementation.

What AI Tools Miss in Next.js

GitHub Copilot, Cursor, and other AI assistants generate excellent Next.js boilerplate. What they consistently skip: security configuration. A fresh AI-generated Next.js app ships without security headers, without a Content Security Policy, with weak NextAuth settings, and with unaudited dependencies.

Step 1 — Security Headers in next.config.js

Is your site actually secure?

Run a free check — 60 seconds

Scan free →
javascript
const nextConfig = {
  poweredByHeader: false,
  async headers() {
    return [{
      source: "/(.*)",
      headers: [
        { key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains; preload" },
        { key: "X-Frame-Options", value: "DENY" },
        { key: "X-Content-Type-Options", value: "nosniff" },
        { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
        { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
      ],
    }];
  },
};

Step 2 — Secure API Routes

Every protected route needs authentication:

typescript
import { getServerSession } from "next-auth";
export async function GET() {
  const session = await getServerSession(authOptions);
  if (!session) return new Response("Unauthorized", { status: 401 });
}

Add rate limiting on auth endpoints. Validate all input with Zod.

Step 3 — Configure NextAuth Securely

typescript
export const authOptions: AuthOptions = {
  secret: process.env.NEXTAUTH_SECRET, // openssl rand -base64 32
  useSecureCookies: process.env.NODE_ENV === "production",
  cookies: {
    sessionToken: {
      options: { httpOnly: true, sameSite: "lax", secure: true },
    },
  },
  callbacks: {
    async redirect({ url, baseUrl }) {
      if (url.startsWith(baseUrl)) return url;
      if (url.startsWith("/")) return `${baseUrl}${url}`;
      return baseUrl;
    },
  },
};

Generate NEXTAUTH_SECRET with openssl rand -base64 32. Set NEXTAUTH_URL to your production domain in Vercel environment variables.

Step 4 — Audit Dependencies

bash
npm audit
npm audit fix

Fix all high and critical vulnerabilities before launch.

Step 5 — Verify with ZeriFlow

Deploy your app, then scan it at zeriflow.com/free-scan. A secured Next.js app should score above 75/100. The most common issues in AI-generated Next.js apps: missing CSP (73%), missing Permissions-Policy (68%), cookies without Secure flag (54%).

Quick Reference

What to secureWhere to configureHow to verify
Security headersnext.config.js headers()ZeriFlow scan
Cookie flagsNextAuth optionsZeriFlow scan
API authenticationEach app/api/ routeCode review
Rate limitingMiddleware or per-routeLoad test
Dependenciesnpm auditSnyk/Dependabot
Secrets.env files onlygit grep

Verify your AI-generated app is production-ready.

80+ security checks in 60 seconds — free, no account needed.

Related articles

Keep reading