vibesupport
Security Tutorial

How to Configure .cursorrules and System Prompts to Stop AI From Writing Security Holes

When you build software using Cursor, you have likely noticed a frustrating pattern: Cursor writes clean, functional UI code, but repeatedly leaves out authentication checks, exposes environment variables, or writes database queries without tenant isolation.

You shouldn't have to remind the AI to write secure code in every single prompt.

By configuring a root \.cursorrules\ file in your repository, you inject persistent instructions directly into Cursor's system context. Every time the AI generates a new component, server action, or database migration, it follows your strict security constraints.

Here is how to set up an enterprise-grade \.cursorrules\ file that stops AI security bugs before they happen.


Why Cursor Needs Security Rules

By default, LLMs optimize for the most probable code completion based on their vast training datasets. Much of that training data consists of quick tutorials, hackathon projects, and introductory documentation where security checks were omitted for brevity.

Without explicit guidance, Cursor defaults to:

  • Leaving server actions unauthenticated.
  • Querying database tables by primary key without checking user ownership.
  • Suggesting \NEXT_PUBLIC_\ prefixes when an environment variable isn't resolving on the client.
  • Passing full database objects with password hashes to client components.

Rule 1: Server Action Security Mandate

Instruct Cursor that all Server Actions must be treated as public HTTP POST endpoints:

\\\`markdown

Server Actions & API Endpoints

  • ALWAYS treat files marked with "use server" or route handlers in app/api/ as public, unauthenticated network boundaries.
  • ALWAYS verify the user session using our auth client before reading or mutating any data.
  • NEVER rely on frontend UI hiding (e.g. conditional rendering based on isAdmin) as a security check.
  • ALWAYS validate incoming request payloads with Zod schemas at runtime.

\\\`


Rule 2: Database Tenant Isolation Mandate

Force Cursor to always scope queries to the authenticated user's ID:

\\\`markdown

Database Queries & Multi-Tenancy

  • When querying, updating, or deleting rows from user-owned tables, ALWAYS include "eq(table.userId, session.user.id)" in the WHERE clause.
  • NEVER query an entity by its primary key ID alone if that entity belongs to a user or organization.
  • NEVER use "SELECT *" or unprojected queries when sending data to client components. Always explicitly select only the necessary non-sensitive columns.

\\\`


The Complete Production-Ready .cursorrules Template

Create a file named \.cursorrules\ in the root of your project and paste the following configuration:

\\\`markdown

# vibesupport Security & Architectural Guardrails

You are a senior full-stack software engineer writing production-ready, highly secure TypeScript and Next.js software.

Core Security Directives

  • SECRETS & ENVIRONMENT VARIABLES:
  • NEVER suggest or use "NEXT_PUBLIC_" prefixes for API keys, secret tokens, or database connection strings.
  • All external API calls that require authorization tokens (OpenAI, Stripe, Resend) MUST run inside server-only route handlers or server actions.
  • Mark server modules with 'import "server-only"' at the top of the file.
  • AUTHENTICATION & AUTHORIZATION:
  • In every Server Action and API Route, verify session validity first:

const session = await requireAuth();

  • Enforce tenant isolation on every database mutation:

where(and(eq(table.id, id), eq(table.userId, session.user.id)))

  • Return 404 (Not Found) instead of 403 when a resource does not belong to the user, preventing resource enumeration attacks.
  • DATA PROJECTION & PRIVACY:
  • NEVER pass raw database rows directly into Client Components.
  • Always map database records to minimal Data Transfer Objects (DTOs) with explicit column selection.
  • Never expose password hashes, internal tokens, or user emails to other users.
  • RATE LIMITING & ABUSE PREVENTION:
  • Any endpoint performing AI inference or sending emails MUST include Upstash Redis rate limiting.
  • Never store raw visitor IP addresses; always hash IPs using HMAC with a secret salt.
  • ERROR HANDLING:
  • Catch internal exceptions and log details ONLY on the server.
  • Return clean, calm, sanitized error messages to the client. Never leak database table names, SQL constraints, or stack traces.

\\\`


Summary

Adding a \.cursorrules\ file takes less than 2 minutes and permanently upgrades the security posture of everything Cursor writes for your project.

BUILT WITH AI? A SECOND LOOK BEFORE YOU LAUNCH

Don't let a hidden bug derail your launch.

vibesupport scans your AI-built codebase or live website for exposed secrets, missing database RLS, and broken access controls—then pairs you with senior engineers to fix them directly in GitHub.

Related Tutorials & Guides