SecureForge: Finding and Preventing Vulnerabilities in LLM-Generated Code via Prompt Optimization

Houjun Liu, Lisa Einstein, John Yang, Joachim Baumann, Duncan Eddy, Christopher D. Manning, Mykel Kochenderfer, Diyi YangarXiv 2026

TL;DR

Even when explicitly instructed to write secure code, frontier Large Language Models (LLMs) generate security vulnerabilities up to 23% of the time. SecureForge is an automated, inference-time pipeline that addresses this by finding benign prompts that trigger vulnerabilities, amplifying them via Markov Chain Monte Carlo (MCMC) sampling, and automatically optimizing system prompts to reduce code vulnerabilities by up to 48% without degrading functional performance.

What Problem Does This Paper Address?

As LLM coding agents are deployed at an unprecedented scale, they frequently introduce hidden cybersecurity vulnerabilities into production codebases. This failure mode is uniquely dangerous because the generated code often passes functional unit tests and looks correct to human developers, yet contains security flaws (like SQL injections or cross-site scripting) that static analysis tools can detect and attackers can exploit.

Existing methods to harden code generation typically rely on adversarial red-teaming or white-box fine-tuning. However, adversarial prompts don't match how real users interact with coding assistants. Furthermore, fine-tuning requires access to proprietary model weights, is computationally expensive, and risks degrading the model's general coding capabilities due to distribution shift. SecureForge introduces a lightweight, black-box, inference-time alternative to proactively secure LLM coding agents.

Methods

SecureForge operates via an automated three-step pipeline that requires only API access to the target LLM and a static analysis tool:

  1. Single-Shot Falsification: The pipeline bootstraps realistic, non-malicious coding prompts from the MITRE Common Weakness Enumeration (CWE) database. It filters out explicit security keywords or attack mentions to ensure the prompts mimic normal user behavior, then uses Semgrep (a static analysis tool) to identify "seed prompts" that cause the LLM to generate vulnerable code.
  2. Failure Distribution Discovery (MCMC Amplification): To prevent overfitting to a small set of examples, SecureForge uses a Metropolis-Hastings MCMC sampling algorithm. It treats an LLM rephrasing prompt as an exploration kernel to continuously mutate the seed prompts into a massive, linguistically diverse synthetic corpus (80,000 prompts) while maintaining high vulnerability rates.
  3. Hardening via Prompt Optimization: Using the amplified corpus, SecureForge applies a zeroth-order genetic algorithm called Genetic-Pareto (GEPA) Prompt Optimization. GEPA uses static analysis failures as a reward signal to iteratively evolve and refine a secure system prompt via self-reflection, achieving a Pareto improvement in both code security and functional correctness.
Figure 1: SecureForge is an automated three-step pipeline that reduces output code vulnerabilities for any LLM: (i) discover benign prompts that elicit vulnerabilities, (ii) augment them into diverse failing prompts, and (iii) optimize for a secured system prompt that reduces output vulnerabilities.

Datasets

  • MITRE CWE Top-25: Used to generate the initial seed prompts and evaluate in-distribution code security across 25 common vulnerability classes.
  • SWE-chat: A dataset containing real-world, multi-turn human-agent coding sessions. The authors collapsed these multi-turn interactions into 203 realistic, single-turn Python task descriptions to evaluate out-of-distribution, "in-the-wild" performance.

Key Insights and Ideas

  • The "Secure Prompting" Illusion: Simply telling a frontier model to "write secure code" or warning it about specific CWEs in context is surprisingly ineffective. Models still output double-digit vulnerability rates (>13%) even when the code successfully passes functional unit tests.
  • Benign Prompts are More Vulnerable: Safety fine-tuning has successfully taught frontier models to reject explicitly malicious adversarial attacks, but it leaves them blind to unintentional harm triggered by normal, benign user requests.
  • MCMC over Simple Rephrasing: Sequential exploration via MCMC discovers unique vulnerable prompts up to 2 times more successfully than simple, static rephrasing of seed prompts, providing a much richer training ground for prompt optimization.

Key Results

  • Significant Vulnerability Reduction: SecureForge achieved up to a 48% reduction in CWE rates across a wide suite of frontier and open-source models (including the GPT-5 series, Claude Sonnet 4.6, and Qwen 2.5 Coder).
  • Pareto Improvements: For GPT-5.4, the vulnerability rate dropped from 18.3% (baseline) to 7.9% (SecureForge) while maintaining or slightly improving unit test passage rates.
  • Zero-Shot Transfer: The optimized system prompts transferred successfully to completely unseen, real-world user prompts from the SWE-chat dataset, improving the joint rate of test passage and security by up to 13.5% without any prior exposure to real user prompt distributions.

Limitations and Future Work

  • Model Specificity: A system prompt optimized for one specific LLM architecture may not transfer effectively to another model, meaning developers must run the pipeline individually for each model deployment.
  • One-Time Compute Cost: The pipeline requires an upfront optimization cost (averaging around $150 or 50 million tokens per model) to search for the securitized system prompt.
  • Scope Restrictions: The current evaluation is limited to single-turn code generation and focuses exclusively on Python. Real-world agent deployments span multiple turns and languages, where structural differences (like memory management in C/C++) introduce entirely different classes of vulnerabilities.

Summary updated

BibTeX
@misc{liu2026secureforge,
  author = {Houjun Liu and Lisa Einstein and John Yang and Joachim Baumann and Duncan Eddy and Christopher D. Manning and Mykel Kochenderfer and Diyi Yang},
  title = {{SecureForge: Finding and Preventing Vulnerabilities in LLM-Generated Code via Prompt Optimization}},
  year = {2026},
  eprint = {2605.08382},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.08382},
}
Figure