/cursor-tutorials

How to stop Cursor from exposing secrets

Learn how to stop Cursor from exposing secrets with practical steps to secure your code, protect sensitive data, and prevent accidental leaks.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to stop Cursor from exposing secrets

The short version: Cursor exposes secrets only if you let them into the AI context (what you highlight, what files you include in a prompt, or what you paste). To stop Cursor from exposing secrets, you must keep secrets out of the AI’s input. That means: never commit secrets, put them in .env files, exclude those files from Cursor prompts, and use Cursor’s built‑in “exclude from AI” settings. The editor never sends code unless you explicitly include it, so the control is entirely in your hands.

 

What Actually Works to Stop Cursor from Exposing Secrets

 

Cursor is basically VS Code with an AI layer. It only sends the files or text you include in a prompt. So prevention comes from making sure secrets never enter the prompt window or the model context. These are the correct, real methods:

  • Use a .env file for secrets and keep real keys out of your source code.
  • Add .env to .gitignore (never commit secrets anywhere).
  • Add .env and any secret files to Cursor’s “exclude from AI” setting.
  • Never paste secrets into AI chat (the most common accidental leak).
  • Use environment variables in your code, not hardcoded strings.
  • Use placeholder values when you need to show an example to the AI.

 

1. Put every secret into a .env file

 

Secrets include API keys, database passwords, JWT secrets, OAuth tokens, and anything that can give access to a service. Those should always live in a plain-text file called .env, which your code reads with something like dotenv (Node) or python-dotenv (Python).

// Install dotenv in Node projects
npm install dotenv
// Load .env safely
import dotenv from "dotenv";
dotenv.config();

const dbPassword = process.env.DB_PASSWORD; // safe, not hardcoded

Cursor will not read your .env unless you explicitly send it. If you never include it in prompts, it stays local.

 

2. Add .env to .gitignore

 

This is absolutely required. If you commit secrets, AI safety becomes irrelevant — the secret is already public.

# Never commit secrets
.env
.env.local
.env.production

 

3. Use Cursor’s “Exclude from AI” setting

 

Cursor has a real setting called “Files excluded from AI”. This prevents those files from being sent to the model, even by accident.

To use it:

  • Open Cursor settings (bottom left gear icon).
  • Go to AI section.
  • Find the option “Exclude files from AI context”.
  • Add .env, .env.\*, and any other secret-containing files.

This is the single strongest protection aside from simply not including the file in prompts.

 

4. Never highlight a file with secrets when talking to Cursor

 

Cursor only sends what you highlight OR the files it thinks are relevant (unless they’re excluded). If you highlight a file containing secrets, it will be sent. So always keep secrets in files that you never open for AI use.

  • Keep .env closed.
  • Keep all credentials in excluded files.
  • Use placeholders for examples like "API_KEY_HERE".

 

5. If you need to show an API key pattern, use a fake one

 

Cursor doesn’t need real keys to help you write code. Give it dummy values:

STRIPE_SECRET_KEY=sk_test_123456789

This lets you safely ask questions like “How do I load this in Node?” without exposing the real secret.

 

6. Rotate any key that has already been exposed

 

If Cursor ever saw a real key (or you pasted one into chat), assume it is compromised. Go to the provider (Stripe, Firebase, AWS, etc.) and hit “Revoke” or “Regenerate”. Then update your .env.

 

The Bottom Line

 

Cursor does not magically read your files. It only knows what you feed it. To stop it from exposing secrets, you must keep secrets out of AI context. That means using .env files, excluding them from AI access, never pasting keys, and rotating any key that was ever shown to the AI. Follow these steps and you will never leak a secret through Cursor.

Still stuck?
Copy this prompt into ChatGPT and get a clear, personalized explanation.

This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.

AI AI Prompt

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022