Cursor security: secrets, hooks and the app you ship
Keep keys out of Cursor's reach (.cursorignore, .env, hooks, MCP tokens) and check the app it built for leaked keys and open data. Free scan.
Cursor gives you real code in your own repository, which means you are responsible for the parts a hosted builder would hide. First, how to lock down Cursor itself. Then the five places apps built with it most often go wrong. Start with the Cursor app secrets checklist if keys are your main worry.
Cursor secrets: where keys leak
- A committed
.envfile. The file may be gone from your folder but still sit in git history. - A
NEXT_PUBLIC_orVITE_name on a secret. Those values are sent to every visitor's browser. - A key pasted into
.cursor/mcp.json, a chat message or a prompt. Use environment references instead. - The agent reading
.env..cursorignoreblocks the agent, Tab and@mentions, but Cursor says terminal and MCP tools are not covered, so keep live keys out of local files where you can.
0. Secure Cursor itself
- Turn on Privacy Mode. With it enabled, Cursor says your code is never used for training by Cursor or its model providers.
- Add a
.cursorignorefor.envand other secrets. It stops the agent, Tab and@mentions from reading those files. Cursor notes that protection is not guaranteed, and that terminal and MCP tools are not covered by it. - Keep tokens out of
.cursor/mcp.json. Use${env:NAME}references instead of pasting keys into the file, and only add MCP servers from developers you trust: Cursor warns they can access external services and run code on your behalf. - Read terminal commands before they run. By default the agent asks for approval; Cursor describes its run modes as best-effort guardrails, not a hard security boundary, so be careful with auto-run.
- Keep production keys off your laptop where you can. Use test or development keys locally and put live keys only in your host's environment settings.
Full walkthrough: Secrets in a Cursor app: .env, .cursorignore and git history.
Secrets management for a Cursor app
- Keep a
.envfile for test or development keys only, listed in.gitignoreand in.cursorignore. - Put live keys in your host's environment settings (Vercel, Netlify, Fly and so on), never in the repository.
- Keep secrets in server-only code. Anything the browser needs must be a public key by design.
- If a key was ever in git, a screenshot or a prompt, make a new one in that service. The old one is already out.
Cursor hooks security
Hooks are scripts that Cursor runs around what the agent does. They live in .cursor/hooks.json in your project or ~/.cursor/hooks.json for you, and they exchange JSON with Cursor. Per Cursor's docs, you can use them as a guard:
beforeReadFilecan answerdenyso a sensitive file is not sent to the model.beforeShellExecutionandbeforeMCPExecutioncan answerallow,denyoraskbefore a command or MCP tool runs.- A hook that crashes or times out lets the action through by default. Set
failClosed: trueon security-critical hooks so a failure blocks instead. - Project hooks are committed to git and load in trusted workspaces. Treat a change to
.cursor/hooks.jsonin a pull request like a change to executable code.
Hooks are a guardrail, not a promise. Read what Cursor documents for your version at cursor.com/docs before you rely on one.
1. Keep .env out of git
Check that .env and .env.local are in .gitignore, and that they were never committed:
git log --all --oneline -- .env .env.local
If that prints anything, the secrets are in your history even if the file is gone. Rotate those keys. Check your history with the command above before you launch.
2. Watch the public prefixes
In Next.js, variables starting with NEXT_PUBLIC_ are sent to the browser. In Vite, it is VITE_. Never put a secret key behind those names. Our post on API keys in your JavaScript shows how to find them in the built site.
Search the project for environment variables starting with NEXT_PUBLIC_ or VITE_ and tell me which ones contain secrets (secret API keys, service role keys, private tokens). Rename them without the prefix and move the code that uses them to server-only code.
3. Protect every API route
AI-generated routes often fetch data for whoever asks. Each route that returns or changes user data must check the session on the server.
Review every file in app/api (or pages/api). For each route that returns or changes user data, add a server-side check that the caller is signed in and owns the data, returning 401 or 403 otherwise. Do not trust user ids sent in the request body.
4. Lock down CORS and headers
Do not set Access-Control-Allow-Origin: * on routes that use cookies or tokens. Add security headers in next.config (HSTS, X-Frame-Options, a Content-Security-Policy).
5. If you use Supabase
Turn on Row Level Security for each table and never use the service_role key in client code. See our Supabase RLS post.
Before you deploy
Run the free scan on your preview or production URL to see your score and problem count. The Full Scan then gives each finding a prompt written for Cursor, ready to paste into the chat.
The Friday leak report
One short email a week: the newest ways AI-built apps leak data, and the one-line fix for each. No spam, unsubscribe any time.