Cursor security

Hardcoded API Keys and Secrets in Cursor-Generated Code

After using Cursor to scaffold or refactor your application, you discover API keys, database connection strings, JWT secrets, or third-party tokens hardcoded directly in source files. These secrets end up committed to your Git repository and potentially pushed to GitHub, making them publicly accessible.

This is one of the most dangerous issues with AI-generated code. Cursor's autocomplete and code generation may inline credentials from your context, environment, or prompt history directly into source files rather than referencing environment variables. Bots continuously scan GitHub for exposed keys and can exploit them within minutes of a push.

You might discover this when you receive a security alert from GitHub, an unexpected bill from a cloud provider, or when a dependency scanner flags your repository.

Error Messages You Might See

GitGuardian has detected a generic password in your commit GitHub push protection blocked: secret detected AWS Access Key exposed in public repository Error: API key is invalid or has been revoked Stripe: This API key has been revoked for security
GitGuardian has detected a generic password in your commitGitHub push protection blocked: secret detectedAWS Access Key exposed in public repositoryError: API key is invalid or has been revokedStripe: This API key has been revoked for security

Common Causes

  • AI inlined values from .env context — Cursor had access to your .env file or terminal output and copied actual values into source code instead of referencing process.env
  • Prompt included real credentials — You pasted a config snippet with real credentials into the chat, and Cursor reproduced them in the generated code
  • No .gitignore for secrets files — .env, .env.local, or config files containing secrets were never added to .gitignore
  • Autocomplete suggested full connection strings — Cursor's tab completion filled in a full database URL including username and password from your local context
  • Test files with production credentials — Test setup files were generated with real API keys instead of mock values or test environment variables

How to Fix It

  1. Scan your repository immediately — Run git log -p | grep -iE '(api_key|secret|password|token|connectionstring)' or use tools like truffleHog or gitleaks to find all exposed secrets
  2. Rotate every exposed credential — Assume any secret that was ever committed is compromised, even if you removed it later. Regenerate API keys, database passwords, and tokens in every affected service
  3. Move secrets to environment variables — Replace all hardcoded values with process.env.SECRET_NAME or your framework's equivalent, and add a .env.example file with placeholder values
  4. Add pre-commit hooks — Install detect-secrets or gitleaks as a pre-commit hook to prevent future secret commits
  5. Purge Git history if needed — Use git filter-branch or BFG Repo-Cleaner to remove secrets from your entire commit history, then force push
  6. Configure Cursor rules — Add a .cursorrules file instructing the AI to never inline secrets and always reference environment variables

Real developers can help you.

PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too Jared Hasson Jared Hasson Full time lead founding dev at a cyber security saas startup, with 10 yoe and a bachelor's in CS. Building & debugging software products is what I've spent my time on for forever Victor Denisov Victor Denisov Developer Jaime Orts-Caroff Jaime Orts-Caroff I'm a Senior Android developer, currently working at Aircall. I'm open to work in various fields! Mehdi Ben Haddou Mehdi Ben Haddou - Founder of Chessigma (1M+ users) & many small projects - ex Founding Engineer @Uplane (YC F25) - ex Software Engineer @Amazon and @Booking.com Matthew Jordan Matthew Jordan I've been working at a large software company named Kainos for 2 years, and mainly specialise in Platform Engineering. I regularly enjoy working on software products outside of work, and I'm a huge fan of game development using Unity. I personally enjoy Python & C# in my spare time, but I also specialise in multiple different platform-related technologies from my day job. Daniel Vázquez Daniel Vázquez Software Engineer with over 10 years of experience on Startups, Government, big tech industry & consulting. MFox MFox Full-stack professional senior engineer (15+years). Extensive experience in software development, qa, and IP networking. rayush33 rayush33 JavaScript (React.js, React Native, Node.js) Developer with demonstrated industry experience of 4+ years, actively looking for opportunities to hone my skills as well as help small-scale business owners with solutions to technical problems Alvin Voo Alvin Voo I’ve watched the tech landscape evolve over the last decade—from the structured days of Java Server Pages to the current "wild west" of Agentic-driven development. While AI can "vibe" a frontend into existence, I specialize in the architecture that keeps it from collapsing. My expertise lies in the critical backend infrastructure: the parts that must be fast, secure, and scalable. I thrive on high-pressure environments, such as when I had only three weeks to architect and launch an Ethereum redemption system with minimal prior crypto knowledge, turning it into a major revenue stream. What I bring to your project: Forensic Debugging: I don't just "patch" bugs; I use tools like Datadog and Explain Analyzers to map out bottlenecks and resolve root causes—like significantly reducing memory usage by optimizing complex DB joins. Full-Stack Context: Deep experience in Node.js and React, ensuring backends play perfectly with mobile and web teams. Sanity in the Age of AI: I bridge the gap between "best practices" and modern speed, ensuring your project isn't just built fast, but built to last.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

Can I undo a commit that contained secrets?

You can remove the secret from code and make a new commit, but the old commit still exists in Git history. Use BFG Repo-Cleaner or git filter-repo to purge it from history entirely. However, always assume the secret was compromised and rotate it regardless.

How do I prevent Cursor from hardcoding secrets?

Create a .cursorrules file in your project root with instructions like 'Never hardcode API keys or secrets. Always use environment variables.' Also avoid pasting real credentials into the Cursor chat.

Related Cursor Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help