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.

prajwalfullstack prajwalfullstack Hi Im a full stack developer, a vibe coded MVP to Market ready product, I'm here to help PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too Milan Surelia Milan Surelia Milan Surelia is a Mobile App Developer with 5+ years of experience crafting scalable, cross-platform apps at 7Span and Meticha. At 7Span, he engineers feature-rich Flutter apps with smooth performance and modern UI. As the Co-Founder of Meticha, he builds open-source tools and developer-focused products that solve real-world problems. Expertise: 💡 Developing cross-platform apps using Flutter, Dart, and Jetpack Compose for Android, iOS, and Web. 🖋️ Sharing insights through technical writing, blogging, and open-source contributions. 🤝 Collaborating closely with designers, PMs, and developers to build seamless mobile experiences. Notable Achievements: 🎯 Revamped the Vepaar app into Vepaar Store & CRM with a 2x performance boost and smoother UX. 🚀 Launched Compose101 — a Jetpack Compose starter kit to speed up Android development. 🌟 Open source contributions on Github & StackOverflow for Flutter & Dart 🎖️ Worked on improving app performance and user experience with smart solutions. Milan is always happy to connect, work on new ideas, and explore the latest in technology. Bastien Labelle Bastien Labelle Full stack dev w/ 20+ years of experience AUXLE AUXLE I am a Full Stack Developer experienced in building Websites, Web apps and Cross Platform Mobile Apps for Startups and Companies. Matt Butler Matt Butler Software Engineer @ AWS 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. Basel Issmail Basel Issmail ’m a Senior Full-Stack Developer and Tech Lead with experience designing and building scalable web platforms. I work across the full development lifecycle, from translating business requirements into technical architecture to delivering reliable production systems. My work focuses on modern web technologies, including TypeScript, Angular, Node.js, and cloud-based architectures. I enjoy solving complex technical problems and helping teams turn product ideas and prototypes into working platforms that can grow and scale. In addition to development, I often collaborate closely with product managers, business analysts, designers, and QA teams to ensure that solutions align with both technical and business goals. I enjoy working with startups and product teams where I can contribute both as a hands-on engineer and as a technical partner in designing and delivering impactful software. Jen Jacobsen Jen Jacobsen I’m a Full-Stack Developer with over 10 years of experience building modern web and mobile applications. I enjoy working across the full product lifecycle — turning ideas into real, well-built products that are intuitive for users and scalable for businesses. I particularly enjoy building mobile apps, modern web platforms, and solving complex technical problems in a way that keeps systems clean, reliable, and easy to maintain.

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