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.

MFox MFox Full-stack professional senior engineer (15+years). Extensive experience in software development, qa, and IP networking. Costea Adrian Costea Adrian Embedded Engineer specilizing in perception systems. Latest project was a adas camera calibration system. 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. 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. legrab legrab I'll fill this later Caio Rodrigues Caio Rodrigues I'm a full-stack developer focused on building practical and scalable web applications. My main experience is with **React, TypeScript, and modern frontend architectures**, where I prioritize clean code, component reusability, and maintainable project structures. I have strong experience working with **dynamic forms, state management (Redux / React Hook Form), and complex data-driven interfaces**. I enjoy solving real-world problems by turning ideas into reliable software that companies can actually use in their daily operations. Beyond coding, I care about **software quality and architecture**, following best practices for componentization, code organization, and performance optimization. I'm also comfortable working across the stack when needed, integrating APIs, handling business logic, and helping transform prototypes into production-ready systems. My goal is always to deliver solutions that are **simple, efficient, and genuinely useful for the people using them.** Franck Plazanet Franck Plazanet I am a Strategic Engineering Leader with over 8 years of experience building high-availability enterprise systems and scaling high-performing technical teams. My focus is on bridging the gap between complex technology and business growth. Core Expertise: 🚀 Leadership: Managing and coaching teams of 15+ engineers, fostering a culture of accountability and continuous improvement. 🏗️ Architecture: Enterprise Core Systems, Multi-system Integration (ERP/API/ETL), and Core Database Structure. ☁️ Cloud & Scale: AWS Expert; architected systems handling 10B+ monthly requests and managing 100k+ SKUs. 📈 Business Impact: Aligning tech strategy with P&L goals to drive $70k+ in monthly recurring revenue. I thrive on "out-of-the-box" thinking to solve complex technical bottlenecks and am always looking for ways to use automation to improve business productivity. 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 Yovel Cohen Yovel Cohen I got a lot of experience in building Long-horizon AI Agents in production, Backend apps that scale to millions of users and frontend knowledge as well. Matt Butler Matt Butler Software Engineer @ AWS

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