VibeSecCheck
Home/Security Guides/cursor-nextjs-security
High RiskAI Builders & StacksCursorNext.js 15App RouterServer Actions

Securing Cursor-Built Next.js 15 Apps

Cursor Composer excels at building Next.js App Router SaaS apps, but App Router introduces subtle security traps: unauthenticated Server Actions, accidentally importing server secrets into 'use client' trees, and trusting middleware alone.

Why AI Builders Introduce This

Cursor frequently writes Server Actions assuming 'only my dashboard button calls this', forgetting that Server Actions are public HTTP POST endpoints anyone can invoke directly with forged arguments.

Real-World Exploit Impact
  • Unauthenticated attackers invoke exported 'use server' mutations directly
  • Passing sensitive DB objects from Server Components into Client Components leaks hidden fields (passwordHash, stripeCustomerId) inside RSC flight payloads
  • Default next.config.ts exposes X-Powered-By: Next.js and omits CSP/HSTS headers

Vulnerable Pattern vs. Production Fix

Vulnerable Server Action (No Session Or Ownership Check)typescript
"use server";
// app/actions/billing.ts — VULNERABLE!
export async function updateWorkspaceName(workspaceId: string, name: string) {
  // Anyone can POST to this Server Action with ANY workspaceId!
  await db.workspace.update({
    where: { id: workspaceId },
    data: { name },
  });
}
Secured Server Action (Session + Ownership + Input Validation)
"use server";
import { requireSession } from "@/lib/auth";

export async function updateWorkspaceName(workspaceId: string, name: string) {
  const session = await requireSession(); // Throws 401 if unauthenticated
  const cleanName = name.trim().slice(0, 80);
  if (!cleanName) throw new Error("Workspace name required");

  // Enforce ownership directly in the query predicate
  const updated = await db.workspace.updateMany({
    where: { id: workspaceId, ownerId: session.user.id },
    data: { name: cleanName },
  });
  if (updated.count === 0) throw new Error("Forbidden");
  return { ok: true };
}

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit this Cursor-generated Next.js App Router codebase:
1. Check every "use server" Server Action and app/api/**/route.ts handler: verify each one authenticates the user and checks resource ownership before reading or mutating data.
2. Add "import 'server-only'" to database/secret helper modules so they can never be imported into "use client" components.
3. Audit Server Components passing props to Client Components to ensure sensitive fields (tokens, password hashes, internal flags) are stripped via explicit DTO mapping.
4. Set poweredByHeader: false and add security headers in next.config.ts.

How to Verify Your Fix Worked

  1. 1Search the codebase for `'use server'` and confirm every exported function calls your session guard.
  2. 2Verify `server-only` protects your Stripe, OpenAI, and Supabase admin modules.
  3. 3Run VibeSecCheck on your deployed Vercel/Cloudflare preview URL.

Frequently Asked Questions

Are Next.js Server Actions automatically protected by authentication?

No! Every exported function in a 'use server' file is a publicly reachable POST endpoint. You must check session auth and resource ownership inside every Server Action.

Is Next.js middleware enough to protect my API routes?

Never rely on middleware alone. Always enforce defense-in-depth authentication inside your Data Access Layer, Route Handlers, and Server Actions.

How do I stop Cursor from leaking server files into client components?

Install the `server-only` package and place `import 'server-only';` at the top of any module touching secrets or privileged database clients — Next.js will fail the build immediately if a client component imports it.

Related Security Playbooks