cd /news/large-language-models/why-cursor-keeps-hardcoding-secrets-… · home topics large-language-models article
[ARTICLE · art-42377] src=dev.to ↗ pub= topic=large-language-models verified=true sentiment=↓ negative

Why Cursor Keeps Hardcoding Secrets in AI-Generated Code (CWE-798)

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.

read2 min views1 publishedJun 28, 2026

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:

const stripe = require('stripe')('sk_live_51Abc...realkey...xyz');
const JWT_SECRET = 'super-secret-key-change-me';

The code ran. Payments worked. And I almost committed a live Stripe key to a public GitHub repo without noticing, because everything looked fine.

This 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.

Here is the kind of thing AI editors generate constantly:

// Hardcoded secret - CWE-798
const stripe = require('stripe')('sk_live_51Abc...xyz');
const JWT_SECRET = 'super-secret-key-change-me';
const DB_URL = 'postgres://admin:hunter2@db.prod.internal:5432/app';

Three 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.

LLMs 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.

The 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.

Move every secret to an environment variable and load it at runtime:

// Secrets from environment - CWE-798 resolved
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const JWT_SECRET = process.env.JWT_SECRET;
const DB_URL = process.env.DATABASE_URL;

Then keep the file out of git and scan before committing:

echo ".env" >> .gitignore

npx gitleaks detect --source . --verbose

If 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.

A 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.

I have been running SafeWeave 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.

── more in #large-language-models 4 stories · sorted by recency
── more on @cursor 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/why-cursor-keeps-har…] indexed:0 read:2min 2026-06-28 ·