Categories:
Research
AI coding security prompt engineering vulnerabilities SecureForge

Your AI coding assistant is writing insecure code. Here's how to cut the risk in half

Feature image for Your AI coding assistant is writing insecure code. Here's how to cut the risk in half

One in five code suggestions has a security flaw

If your team uses Copilot, Cursor, Claude Code, or any LLM-powered coding tool, here’s a number that should make you uncomfortable: roughly 20% of the code these tools generate contains a security vulnerability.

That’s not a guess. Stanford researchers tested 11 models, including GPT-5.4, Claude Sonnet 4.6, Kimi K2, CodeLlama 7B, and several Qwen variants, by asking them to handle common programming tasks. They scanned the output with Semgrep, an industry-standard static analysis tool. The baseline: models asked to “write secure code” still produced flawed code about 20.1% of the time.

Some models did worse. GPT-5.4 Nano, a smaller variant optimized for speed, produced vulnerable code 22.1% of the time. Even GPT-5.4, the best performer out of the box, sat at 15.8%.

And “write secure code” doesn’t work as a prompt instruction. It’s too vague. Models hear it, nod, and produce code that compiles and runs but still leaves the door open for SQL injection, path traversal, or cross-site scripting.

The SecureForge approach: let evolution find a better prompt

The Stanford team built a system called SecureForge that tackles this problem from an angle most teams haven’t considered. Instead of changing the model or bolting on a post-processing scanner, they optimized the system prompt itself.

They started with MITRE’s top 25 Common Weakness Enumeration items, the same catalog security teams use to track vulnerability classes. From that list, they wrote 500 realistic coding requests that tend to produce vulnerable code. One example from the paper: “write a function that looks up a user by username in SQLite.” Sounds innocent. But the most natural implementation, concatenating the username into a SQL string, is a textbook SQL injection vulnerability.

Those 500 requests became 80,000 expanded variants. Each variant was fed to the coding model, the output was scanned by Semgrep for known vulnerabilities, and the results fed into a genetic algorithm called GEPA. The genetic algorithm treated the system prompt like DNA: it mutated prompts, crossed them with each other, scored them by how few vulnerabilities they produced, and evolved the population over many generations.

What came out the other end was a system prompt that no human would have written, because no human thinks to encode 25 different vulnerability avoidance strategies into a single set of instructions. The algorithm found patterns that work across model families, model sizes, and programming languages.

The results: nearly 2x improvement, no model change needed

After applying the SecureForge-optimized prompt, average vulnerability rates dropped from 20.1% to 11.8% across all models tested.

The improvement held across every model. It wasn’t a GPT-specific trick or something that only works on Claude. Here are the standout numbers:

  • GPT-5.4: 15.8% → 10.1%. A 5.7-point drop for the strongest baseline model.
  • GPT-5.4 Nano: 22.1% → 15.6%. The biggest absolute improvement (6.5 points), because weaker models had more room to improve.
  • Average across all 11 models: 20.1% → 11.8%.

The SecureForge prompt isn’t magic. It won’t catch everything, and it won’t eliminate vulnerabilities entirely. But cutting the rate nearly in half across every model tested, purely through prompt optimization, is a real result for teams that already have coding assistants deployed.

Why this matters for your team

Think about your current setup. If you’re using Copilot or Cursor, someone on your team probably wrote a custom system prompt or configuration file at some point. Maybe it says “follow best practices” or “write clean, secure code.” Those instructions are doing almost nothing. The Stanford data shows that models already produce vulnerable code at roughly the same rate whether or not you ask them to be secure.

The problem is that “be secure” isn’t an instruction a model can follow. It’s an aspiration. SecureForge replaces aspiration with specificity: the optimized prompt encodes actual vulnerability avoidance strategies tied to the MITRE CWE framework, tested across 80,000 code generation scenarios.

You can deploy this today. The system prompt is open source on GitHub, compatible with any LLM that accepts system-level instructions. No retraining, no model switch, no new tooling. You swap one text string for another.

What SecureForge can’t do

The limitation matters. SecureForge optimizes against known vulnerability classes, the 25 items in MITRE’s CWE top 25. If a new class of vulnerability emerges that isn’t in that catalog, the optimized prompt won’t protect against it. The researchers acknowledge this directly.

There’s also the question of unknown unknowns. Semgrep, the scanner SecureForge uses for scoring, catches known patterns. Code that passes Semgrep isn’t necessarily secure; it just doesn’t match any known-bad pattern. A vulnerability class that Semgrep doesn’t detect would slip through the entire pipeline unnoticed.

For most teams, this limitation is acceptable. The MITRE CWE top 25 covers the vast majority of real-world vulnerabilities that show up in production: SQL injection, XSS, path traversal, deserialization flaws, authentication failures. If you’re not yet systematically addressing these 25 classes in your AI-generated code, that’s where to start.

How to use this today

1. Audit your current system prompt. If you’re using a coding assistant with a custom configuration, check what it actually says about security. If it’s vague (“write secure code”), it’s not helping.

2. Pull the SecureForge prompt from GitHub. The Stanford team released the optimized prompt and the full optimization pipeline. The repository includes integration instructions for popular coding assistants.

3. Run a baseline scan. Before swapping prompts, use Semgrep or an equivalent static analysis tool to measure your current vulnerability rate. You need a number to compare against.

4. Swap the prompt and re-scan. Apply the SecureForge-optimized prompt, generate the same set of code tasks, and scan again. The improvement should be visible in your own data.

5. Keep your existing security pipeline. SecureForge is a prompt-level optimization, not a replacement for SAST tools, code review, or penetration testing. It reduces the volume of vulnerabilities reaching your reviewers. It doesn’t replace review.

The bigger picture

For the past two years, the conversation about AI coding assistants has been about whether they can write working code. That question is mostly settled. They can, and they’re getting better fast.

The next question is whether that code is safe to ship. Right now, the answer is “not always.” SecureForge shows that meaningful improvement doesn’t require waiting for the next model release. The lever is the prompt you’re already using, and the Stanford team just proved you can get measurable results by treating it as engineering rather than intuition.

The original research and full optimization pipeline are available on GitHub for both commercial and noncommercial use.

Related Articles