RESCUE: Retrieval Augmented Secure Code Generation

Jiahao Shi, Tianyi Zhang

TL;DR

RESCUE is a novel Retrieval-Augmented Generation (RAG) framework designed to help Large Language Models (LLMs) generate more secure code without sacrificing functionality. By distilling raw security data into high-level guidelines and "slicing" code examples down to their security-critical components, RESCUE achieves a new state-of-the-art in secure code generation across multiple benchmarks and models.

What Problem Does This Paper Address?

While LLMs are remarkably proficient at coding, they frequently produce "vulnerable" code—snippets that contain security flaws like SQL injection or buffer overflows. Existing solutions often fall into two camps: expensive fine-tuning on security datasets or using static analysis tools to provide feedback. Fine-tuning is data-intensive, while static analysis tools are often too rigid to catch new types of vulnerabilities.

Standard RAG (Retrieval-Augmented Generation) seems like a natural fit, but it faces two major hurdles in the security domain:

  1. Noise: Raw security documents and code examples often contain "distractor" logic (like database connection setups) that isn't relevant to the specific security fix, confusing the LLM.
  2. Semantic Gap: Standard retrieval methods look for text similarity but often miss the security intent hidden in a task description. A prompt might not mention "security," but the underlying task (e.g., handling user input) has massive security implications.

Methods

The authors introduce RESCUE, which features two primary innovations to overcome these hurdles.

Overview of the RESCUE framework

1. Hybrid Knowledge Base Construction

Instead of just indexing raw documents, RESCUE builds a refined, two-tier knowledge base:

  • Cluster-then-Summarize Distillation: The system groups raw vulnerability data by CWE (Common Weakness Enumeration) types. It then uses an LLM to recursively summarize these clusters into high-level "Security Guidelines" and "Vulnerability Causes."
  • Security-Focused Static Program Slicing: To provide better code examples, RESCUE uses Program Dependence Graphs (PDGs). It identifies the exact lines of code changed in a security patch and "slices" the program to keep only the statements that directly influence or are influenced by that fix. This removes 81.5% of irrelevant code, leaving concise, laser-focused examples.

2. Hierarchical Multi-Faceted Retrieval

RESCUE doesn't just do a single search. It follows a top-down approach:

  • CWE-Level Retrieval: It first identifies the most likely vulnerability types (CWEs) relevant to the task.
  • Code-Level Retrieval: It then narrows the search to specific sliced code examples.
  • Proactive Analysis: To bridge the semantic gap, RESCUE performs "proactive analysis" before retrieving. It asks an LLM to explain the potential vulnerability in the task and generates a "draft" code snippet to see which APIs are being used. These facets (Vulnerability Cause, API Patterns, and Code Similarity) are then fused to find the best matches.

Datasets

  • Knowledge Base: Built using a training dataset from SafeCoder, containing 704 vulnerability-fix instances from CVE and GitHub across Python, C, and C++.
  • Evaluation Benchmarks:
  • CodeGuard+: A security-centric benchmark with 94 scenarios.
  • HumanEval+, BigCodeBench, and LiveCodeBench: Standard benchmarks used to ensure that improving security doesn't break the model's ability to write functionally correct code.
  • CWEval: A dynamic testing benchmark used to validate that the results aren't just artifacts of static analysis tools.

Key Insights and Ideas

  • Less is More (Slicing): One of the most significant insights is that providing an LLM with a full, working code example is actually worse than providing a "sliced" version. The extra logic in a full example distracts the model from the security pattern it needs to learn.
  • Functionality vs. Security: The authors argue that measuring security alone is a trap. A code snippet that does nothing is "secure" but useless. They introduce SecurePass@k, a joint metric that requires code to pass both functional unit tests and security checks.
  • Proactive Drafting: By generating a "quick and dirty" draft first, the system can extract API calls to use as search queries. This is much more effective than searching based on a vague natural language description.

Key Results

  • State-of-the-Art Performance: RESCUE improved the SecurePass@1 metric by an average of 4.8 points across six different LLMs (including GPT-4o-mini, Llama 3.1, and Qwen 2.5).
Impact of hierarchical retrieval on SecurePass@1 across different LLMs
  • Minimal Functional Loss: While many security-focused methods "break" the model's general coding ability, RESCUE retained 98.7% of the original models' functional correctness.
  • Cross-Language Generalization: Even though the knowledge base was built on Python and C/C++, RESCUE successfully improved security for Go and JavaScript tasks, proving that distilled security principles are often language-agnostic.

Limitations and Future Work

  • Static Analysis Reliance: Most of the evaluation relies on CodeQL. While the authors also tested on the dynamic CWEval, static tools can still have false positives/negatives.
  • Inference Overhead: RESCUE requires multiple LLM calls (for drafting and analysis) before the final generation. While the authors show this adds a few seconds of latency, it might be a hurdle for real-time IDE integration unless optimized or parallelized.
  • Scope: Currently, the framework focuses on function-level generation. Future work could expand this to repository-level generation where vulnerabilities might span multiple files.

Summary updated

BibTeX
@inproceedings{shi2026rescue,
  author = {Jiahao Shi and Tianyi Zhang},
  title = {{RESCUE: Retrieval Augmented Secure Code Generation}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=gbxhesw4UH},
}
Figure