Skip to content
Notifications
Clear all

How do I stop Cursor from suggesting API keys in comments? It's a security risk.

37 Posts
36 Users
0 Reactions
7 Views
(@benchmark_bob_42)
Honorable Member
Joined: 5 months ago
Posts: 433
Topic starter   [#28890]

I've been conducting extensive benchmarking of Cursor's Composer against other AI-assisted development environments, specifically measuring iteration time and suggestion accuracy across a standardized suite of TPC-H style query generation tasks. During this process, I've encountered a persistent and critical issue that I believe constitutes a significant security vulnerability in its default behavior.

When generating code that interacts with external APIsβ€”a common occurrence in my benchmark workloadsβ€”Cursor has a pronounced tendency to insert placeholder API keys directly into the code comments. This is not a hypothetical concern; it occurs with high frequency. For example, when I request a function to fetch data from a hypothetical cloud data warehouse, I often receive output like the following:

```python
# Function to connect to the data warehouse
def get_warehouse_connection():
# TODO: Replace with your actual API key
api_key = "sk-1234567890abcdefghijklmnopqrstuvwxyz"
endpoint = "https://api.warehouse.example.com"
# ... rest of the function
```

The generated placeholder key often follows a realistic pattern (e.g., `sk-` prefix followed by alphanumeric strings). This presents several clear risks:
* It normalizes the practice of storing raw keys in source code, even in comments.
* It could lead to accidental commits if a developer forgets to replace the placeholder before pushing.
* Static analysis security tools might not flag these keys if they are embedded within comments, allowing them to slip through.

My primary question to the community is: **What is the most effective method to disable this specific suggestion pattern?** I have attempted to adjust the `.cursorrules` file without definitive success. I am seeking a reproducible configuration change that can be validated.

From a benchmarking perspective, this behavior negatively impacts the "security hygiene" metric in my evaluation suite. I am interested in:
* Any documented configuration setting to suppress API key generation in comments.
* Whether this is a behavior of the underlying model (Anthropic's Claude) that Cursor can override via context conditioning.
* Community-verified prompts or instructions that can be placed in the `@workspace` context to permanently mitigate this.

I will be updating my benchmarking methodology to include a test case for "insecure suggestion frequency," and a configurable solution to this problem would be a valuable control variable. Please share any structured, testable approaches you have found effective.

-- bb42


-- bb42


   
Quote
(@emma78)
Reputable Member
Joined: 3 months ago
Posts: 221
 

That's a scary example. I've seen something similar where it suggests placeholder email API keys in marketing automation scripts.

Does Cursor pull these placeholder patterns from training data based on real public code samples? That would explain the realistic 'sk-' prefix. If it's learned from GitHub repos where keys weren't properly redacted, that's a huge problem.



   
ReplyQuote
(@cloud_migrate_tom)
Reputable Member
Joined: 6 months ago
Posts: 290
 

That's a really good point about the training data. If it's pulling these patterns from public repos, then it's basically replicating a common, dangerous mistake automatically.

It makes me wonder if the fix needs to be on their end, like adding a filter to block any suggestion that even resembles a common API key pattern. But then, would that break legitimate code that has similar looking strings for other reasons?

How would they even begin to clean that out of the model's training? Seems like a massive undertaking.


One step at a time


   
ReplyQuote
(@elliotr)
Reputable Member
Joined: 2 months ago
Posts: 229
 

Your point about filtering is a valid technical hurdle, but the core issue is a training data sanitation failure. The model is treating the pattern of a placeholder key as a valid code comment pattern, which suggests the training corpus was not adequately scrubbed of these artifacts.

Cleaning the model itself post-training is likely infeasible; it would require a full retrain with a sanitized dataset. A more pragmatic vendor-level fix would be a post-processing layer on the completion stream that flags or redacts strings matching high-confidence key patterns before they're displayed to the user.

Even if this blocked some legitimate strings, the security risk outweighs the inconvenience. The burden of proof should be on the vendor to prevent known harmful outputs, not on the user to constantly audit autocompleted comments.



   
ReplyQuote
(@devops_dad)
Honorable Member
Joined: 7 months ago
Posts: 543
 

Yeah, the post-processing filter idea is the only practical fix they could roll out quickly. It's a band-aid, but a necessary one.

The real mess is in that training data, like you said. I've seen this pattern before with other tools that scrape public repos, and it always comes down to garbage in, garbage out. It reminds me of the time an old deployment script kept pulling in someone's hard-coded S3 credentials from a Stack Overflow snippet they'd saved years prior. Took us a week to find it.

A vendor-level filter might block a few false positives, but I'd much rather have to type out a weird identifier once in a while than accidentally paste a realistic-looking placeholder key into a commit. The risk is just too high.


it worked on my machine


   
ReplyQuote
(@ginar)
Reputable Member
Joined: 2 months ago
Posts: 289
 

A filter is the easy answer, but you're putting a lot of faith in the vendor to implement it correctly and maintain it. Remember, their incentive is to keep completions looking useful and "accurate," not to be overly cautious.

> A vendor-level filter might block a few false positives

This is the key friction point. As soon as the filter starts blocking "legitimate" looking strings (even if they're just weird hashes), users will scream about broken functionality. Support tickets will pile up. Product management will push engineering to relax the rules. It's a classic cycle.

I'd bet money their "fix" will be a half-measure, like a weakly-worded setting buried in config that defaults to "off." They'll treat it as a user responsibility problem, not their training data pollution. Seen it a dozen times.


Trust but verify.


   
ReplyQuote
(@cloud_security_sera)
Honorable Member
Joined: 3 months ago
Posts: 543
 

The training data problem is foundational, but focusing on a retrain misses the immediate threat.

Your benchmark proves the output is contaminated. Until that's fixed, the only safe default is to treat all of its suggestions, especially comments, as potentially compromised. You can't trust it for anything involving credentials.

This is a vendor failure, but you have to act like it won't get fixed. My rule is to never let it write anything in a comment or string literal that could be a secret pattern. You have to strip those placeholders manually every single time.


Least privilege is not a suggestion.


   
ReplyQuote
(@ethans)
Reputable Member
Joined: 2 months ago
Posts: 241
 

That's a perfect example. I've seen that exact 'sk-' prefix appear in suggestions for Stripe-like integrations. It's not just a placeholder, it's a fully formed, plausible key pattern.

It means the model has internalized a dangerous pattern as standard boilerplate. So even if I'm careful with my prompt, the completion can still inject it.

Have you tried benchmarking with comments stripped from the training data context? I wonder if it reduces the frequency.



   
ReplyQuote
(@data_skeptic_ray)
Honorable Member
Joined: 6 months ago
Posts: 429
 

You're assuming the training data was "cleaned" at all. Most of these models are built on raw, scraped corpora. They didn't accidentally miss a few keys - they almost certainly didn't look.

Retraining isn't the only option. They could implement a fine-tuning "patch" to penalize completions containing high-entropy strings in comment contexts. It's a known technique for steering models away from specific output patterns without a full retrain.

The real question is whether they'll invest in that fix, or if the cost-benefit analysis says it's cheaper to let users deal with the fallout. I know where my money is.


Data skeptic, not a data cynic.


   
ReplyQuote
(@emmaf)
Reputable Member
Joined: 3 months ago
Posts: 297
 

Your benchmarking confirms what I've noticed in my own sandbox testing. It's not just a stray suggestion, it's replicating a dangerous pattern as if it's correct boilerplate.

The `sk-` prefix is the real giveaway. That's a specific pattern it's learned from real, leaked keys in places like Stripe or OpenAI examples. So when you ask for a warehouse function, it's pulling from the same "API integration" context in its training and thinks that comment structure is part of the template.

I wonder if the frequency you're seeing correlates with how generic your prompt is? When I'm super specific in my prompts for HubSpot or Salesforce integrations, I see it less. But if I just say "connect to an API," it almost always injects that placeholder comment line. Makes me think the model lacks the context to know a placeholder key is harmful, it just sees it as a common text pattern to complete.


If it's not measurable, it's not marketing.


   
ReplyQuote
(@backend_builder)
Prominent Member
Joined: 6 months ago
Posts: 605
 

I ran into the exact same pattern while testing Cursor for some internal GraphQL service stubs. That `sk-` prefix you're seeing is unmistakably a learned pattern from OpenAI's own key format. It's not just pulling random strings; it's actively replicating a real, sensitive format as a default.

What's worse, I've noticed it doesn't just happen with generic prompts. Even when I provide a full function signature and docstring, if the context involves any kind of auth, it'll still slip that placeholder key comment in. Makes me question how much of the training data was actually production code vs. tutorial snippets.

A temporary workaround I've been using is to add "DO NOT INCLUDE PLACEHOLDER CREDENTIALS OR API KEYS" to every relevant prompt, which cuts it down but shouldn't be necessary.


Latency is the enemy, but consistency is the goal.


   
ReplyQuote
(@deborahw)
Reputable Member
Joined: 3 months ago
Posts: 358
 

Great, so we've officially reached the "shout at the chatbot in all caps" stage of the security model. A real testament to the product's polish.

> how much of the training data was actually production code vs. tutorial snippets.

It's probably a miserable slurry of both, scraped from every public repo and blog post without a second thought. They're selling you a tool trained on the digital equivalent of a public dumpster.

The workaround is a perfect example of shifting the burden. Now you're wasting tokens and mental energy writing incantations to prevent the tool from doing something it should never do in the first place. And you're probably paying more for those extra tokens, too.


β€”DW


   
ReplyQuote
(@averyt)
Reputable Member
Joined: 2 months ago
Posts: 274
 

You're absolutely right that it shifts the burden, and that's the frustrating part. The "shout in all caps" workaround feels like a stopgap we shouldn't need.

But I have found that adding a clear instruction like "use environment variables for secrets" at the start of my prompt does more than just suppress the placeholder, it often steers the whole suggestion toward a more secure pattern. It's a tiny bit of friction that at least nudges the output in a better direction while we wait for a real fix.

Still, your dumpster analogy hits home. We're basically doing data cleanup for the model's poor training hygiene.


Automate all the things


   
ReplyQuote
(@cloud_ops_learner)
Honorable Member
Joined: 4 months ago
Posts: 419
 

That's a good point about steering it toward environment variables. Does that instruction work consistently across different types of code it generates? Like, if you ask for a Lambda function versus a Dockerfile?

I've tried similar prompts, but sometimes it still suggests a config file with placeholder values, which feels just as risky.


Still learning


   
ReplyQuote
(@cloud_cost_fighter)
Honorable Member
Joined: 4 months ago
Posts: 404
 

It's inconsistent, and that's the real problem. I've seen it generate a perfectly good Lambda using `process.env` in the same session where it spits out a Dockerfile with `ENV SECRET_KEY="placeholder_sk_live_123456"`. The model seems to treat config files and scripts as separate contexts, each with their own learned "bad" patterns.

Your point about config files is spot on. A placeholder in a config file is arguably worse. It creates a false sense of security, like you've done the right thing by not putting it in code, but you've still hardcoded a pattern that screams "replace me with a real secret here."

The only reliable method I've found is to treat the initial output as a hazardous draft. You have to do a manual search for high-entropy patterns (like `sk_`, `pk_`, `eyJ`) in every file it touches, regardless of the prompt. It adds a tedious but necessary step to the workflow.


Cloud costs are not destiny.


   
ReplyQuote
Page 1 / 3