{"slug": "why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798", "title": "Why Cursor Keeps Hardcoding Secrets in AI-Generated Code (CWE-798)", "summary": "AI coding assistants like Cursor, Claude Code, Copilot, and Windsurf frequently generate code with hardcoded secrets (CWE-798), such as API keys and database credentials, because their training data includes illustrative examples that inline sensitive values. A developer found that Cursor produced a live Stripe secret key in a billing integration, nearly committing it to a public repo. The recommended fix is to use environment variables, pre-commit hooks with secret scanners like Gitleaks, and tools like SafeWeave to catch secrets before they reach git history.", "body_md": "I asked Cursor to wire up Stripe billing for a side project last week. It gave me working code in about ten seconds. It also gave me this:\n\n``` js\nconst stripe = require('stripe')('sk_live_51Abc...realkey...xyz');\nconst JWT_SECRET = 'super-secret-key-change-me';\n```\n\nThe code ran. Payments worked. And I almost committed a live Stripe key to a public GitHub repo without noticing, because everything looked fine.\n\nThis is not a Cursor problem specifically. Claude Code, Copilot, and Windsurf all do it. The pattern is everywhere in the training data, so the model reproduces it.\n\nHere is the kind of thing AI editors generate constantly:\n\n``` js\n// Hardcoded secret - CWE-798\nconst stripe = require('stripe')('sk_live_51Abc...xyz');\nconst JWT_SECRET = 'super-secret-key-change-me';\nconst DB_URL = 'postgres://admin:hunter2@db.prod.internal:5432/app';\n```\n\nThree secrets, three liabilities. Once this hits a repo, the secret is in your git history forever, even if you delete the line in a later commit. Public repos get scraped by bots within minutes. There are reports of freshly committed AWS keys getting used by attackers in under five minutes.\n\nLLMs learn from public code: tutorials, StackOverflow answers, sample apps. A huge amount of that code hardcodes secrets because it is meant to be illustrative, not production-ready. The model has no concept of \"this value is sensitive.\" To it, the key string is just an argument like any other.\n\nThe model also optimizes for code that runs immediately. Reading from process.env requires the developer to set up a .env file first, so the path of least resistance for a working snippet is to inline the value.\n\nMove every secret to an environment variable and load it at runtime:\n\n``` js\n// Secrets from environment - CWE-798 resolved\nconst stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);\nconst JWT_SECRET = process.env.JWT_SECRET;\nconst DB_URL = process.env.DATABASE_URL;\n```\n\nThen keep the file out of git and scan before committing:\n\n```\n# .env (never commit this)\necho \".env\" >> .gitignore\n\n# Catch secrets before they are committed\nnpx gitleaks detect --source . --verbose\n```\n\nIf a secret already made it into git history, changing the code is not enough. Rotate the key at the provider immediately, then scrub history with git filter-repo or BFG. Assume any committed secret is already burned.\n\nA pre-commit hook is the real win here. The goal is to never let a secret reach a commit in the first place, because cleanup after the fact is painful and rotation is the only safe assumption.\n\nI have been running [SafeWeave](https://safeweave.dev) for this. It hooks into Cursor and Claude Code as an MCP server and flags hardcoded secrets the moment the code is generated, before I move on to the next prompt. Its secrets scanner is built on Gitleaks and runs on the free tier with no signup. That said, even a plain pre-commit hook with gitleaks will catch most of what is in this post. The important thing is catching secrets early, whatever tool you use.", "url": "https://wpnews.pro/news/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798", "canonical_source": "https://dev.to/c_k_fb750e731394/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798-1kjk", "published_at": "2026-06-28 08:44:59+00:00", "updated_at": "2026-06-28 09:03:41.051210+00:00", "lang": "en", "topics": ["large-language-models", "ai-tools", "developer-tools", "ai-safety"], "entities": ["Cursor", "Claude Code", "Copilot", "Windsurf", "Stripe", "Gitleaks", "SafeWeave", "CWE-798"], "alternates": {"html": "https://wpnews.pro/news/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798", "markdown": "https://wpnews.pro/news/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798.md", "text": "https://wpnews.pro/news/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798.txt", "jsonld": "https://wpnews.pro/news/why-cursor-keeps-hardcoding-secrets-in-ai-generated-code-cwe-798.jsonld"}}