CAT-RAG: Improving Secure Code Generation with Category-Aware Agentic Retrieval
Abstract
Despite rapid progress in LLM-based code generation, security vulnerabilities remain widespread, and retrieval-augmented code generation (RACG) can further amplify risk when retrieved knowledge is verbose or vulnerable to poisoning. Existing secure RACG approaches inject security knowledge at a fine-grained vulnerability level, causing prompt overhead and diluted model attention when a query implicates multiple related weaknesses. We propose CAT-RAG, an agentic security category-aware retrieval-augmented code generation framework. CAT-RAG performs adaptive category selection via an LLM as a categorizer agent and operates either as a standalone security layer or as a drop-in add-on to RACG pipelines, requiring no modification to the underlying model. We replace CWE-level retrieval with compact, language-specific security categories. These categories provide a higher-level abstraction over individual CWE weaknesses, organizing them by the security concern they share, such as Input Validation and Injection Attacks, rather than merely grouping related entries. Each category consolidates best practices and secure code examples into a single, prompt-efficient representation. We perform a comprehensive evaluation across CyberSecEval, CodeGuard+, and HumanEval. On CyberSecEval, CAT-RAG nearly doubles the secure code generation improvement over the state of the art (23.57% vs. 12.11%) while reducing token overhead by 41.71%.
Create a lesson
Metadata
- Categories
- cs.CR, cs.SE
Related papers
SAFE-Mem: Secure Agentic Memory with Fine-Grained Authority and Governance-Aware Retrieval
Shared title terminologycs.CRHaoming Li, Dingyi Kang, Xu Hu et al.
SPLASH: Contrastive Audit Pressure for Detecting and Localizing Security Hallucinations in Generated Code
Shared title terminologycs.CRMirza Masfiqur Rahman, MD Ajwad Akil, Imtiaz Karim et al.
Secure Agentic Coding through Counterexample-Grounded Feedback
Shared title terminologycs.CRTue Le, Anh Nguyen Hoang, Gabriel Sasseville et al.
How Effectively Do Security Skills Guide Coding Agents in Vulnerability Detection in Linux Kernel?
Shared title terminologycs.CRHaoran Xue, Nikta Akbarpour, Gias Uddin et al.
ParseGap: Testing What Coding Agents Authorize and What Shells Execute
Shared title terminologycs.CRJieyu Li
Who Is Prompt-Injecting Coding Agents? An Empirical Study of Hidden AI-Directed Text on GitHub
Shared title terminologycs.CRGabi Nakibly, Zvika Babo