mlsanityData to 14 Oct 2026

Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge Injection

arXiv 2504.16429 ↗First posted 23 Apr 2025cs.CR cs.SEcited by 16

On the map: Retrieval-augmented generation → Privacy and security of RAG

Abstract (arXiv)

Retrieval-Augmented Code Generation (RACG) leverages external knowledge to enhance Large Language Models (LLMs) in code synthesis, improving the functional correctness of the generated code. However, existing RACG systems largely overlook security, leading to substantial risks. Especially, the poisoning of malicious code into knowledge bases can mislead LLMs, resulting in the generation of insecure outputs, which poses a critical threat in modern software development. To address this, we propose a security-hardening framework for RACG systems, CodeGuarder, that shifts the paradigm from retrieving only functional code examples to incorporating both functional code and security knowledge. Our framework constructs a security knowledge base from real-world vulnerability databases, including secure code samples and root cause annotations. For each code generation query, a retriever decomposes the query into fine-grained sub-tasks and fetches relevant security knowledge. To prioritize critical security guidance, we introduce a re-ranking and filtering mechanism by leveraging the LLMs' susceptibility to different vulnerability types. This filtered security knowledge is seamlessly integrated into the generation prompt. Our evaluation shows CodeGuarder significantly improves code security rates across various LLMs, achieving average improvements of 20.12% in standard RACG, and 31.53% and 21.91% under two distinct poisoning scenarios without compromising functional correctness. Furthermore, CodeGuarder demonstrates strong generalization, enhancing security even when the targeted language's security knowledge is lacking. This work presents CodeGuarder as a pivotal advancement towards building secure and trustworthy RACG systems.

Key facts. Cited by 16 papers in the map (data through 14 Oct 2026). Placed in Privacy and security of RAG, one of 15 directions in Retrieval-augmented generation. Builds on 29 of its 54 references that are in the map. First posted on arXiv 23 Apr 2025.

Builds on

29 of 54 references.

Closest ancestors · 1

Same direction, newest first

  1. 2025Exploring the Security Threats of Knowledge Base Poisoning in Retrieval-Augmented Code Generation5 Feb

Other references · 28

  1. 2020Language Models are Few-Shot Learnerscited by 32,419
  2. 2021Evaluating Large Language Models Trained on Codecited by 8,320
  3. 2020Scaling Laws for Neural Language Modelscited by 6,586
  4. 2021Prefix-Tuning: Optimizing Continuous Prompts for Generationcited by 3,915
  5. 2021Program Synthesis with Large Language Modelscited by 3,637
  6. 2023Lost in the Middle: How Language Models Use Long Contextscited by 3,455
  7. 2022Scaling Instruction-Finetuned Language Modelscited by 3,024
  8. 2023Efficient Streaming Language Models with Attention Sinkscited by 2,186
  9. 2023Code Llama: Open Foundation Models for Codecited by 2,139
  10. 2019CodeSearchNet Challenge: Evaluating the State of Semantic Code Searchcited by 654
  11. 2020CodeBLEU: a Method for Automatic Evaluation of Code Synthesiscited by 479
  12. 2021Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributionscited by 431
16 more
  1. 2023Language Models can Solve Computer Taskscited by 372
  2. 2023From Sparse to Soft Mixtures of Expertscited by 248
  3. 2023Large Language Models for Code: Security Hardening and Adversarial Testingcited by 155
  4. 2024jina-embeddings-v3: Multilingual Embeddings With Task LoRAcited by 141
  5. 2023Structured Chain-of-Thought Prompting for Code Generationcited by 138
  6. 2023An Empirical Study of the Non-determinism of ChatGPT in Code Generationcited by 137
  7. 2023Jina Embeddings 2: 8192-Token General-Purpose Text Embeddings for Long Documentscited by 104
  8. 2024ReposVul: A Repository-Level High-Quality Vulnerability Datasetcited by 36
  9. 2024Prompting Techniques for Secure Code Generation: A Systematic Investigationcited by 34
  10. 2024Using AI Assistants in Software Development: A Qualitative Study on Security Practices and Concernscited by 28
  11. 2024How secure is AI-generated Code: A Large-Scale Comparison of Large Language Modelscited by 24
  12. 2024EVOR: Evolving Retrieval for Code Generationcited by 22
  13. 2024LLM Security Guard for Codecited by 15
  14. 2024Preference-Guided Refactored Tuning for Retrieval Augmented Code Generationcited by 10
  15. 2025SOSecure: Safer Code Generation with RAG and StackOverflow Discussionscited by 5
  16. 2024Rethinking CyberSecEval: An LLM-Aided Approach to Evaluation Critiquecited by 3

Built on by

Cited by year (2026 to 14 Oct)

Citing papers per year: 2025: 4; 2026: 12.

16 papers.

Sort:

By area:

  1. 2025AI Agentic Programming: A Survey of Techniques, Challenges, and Opportunitiescited by 28
  2. 2025Retrieval-Augmented Code Generation: A Survey with Focus on Repository-Level Approachescited by 23
  3. 2025RESCUE: Retrieval Augmented Secure Code Generationcited by 7
  4. 2026Confundo: Learning to Generate Robust Poison for Practical RAG Systemscited by 5
  5. 2026Enhancing Reliability in LLM-Based Secure Code Generationcited by 2
  6. 2026Agent-Assisted Side-Channel Attacks on Non-Prefix KV Cache in RAGcited by 2
  7. 2025Exploring the Security Threats of Retriever Backdoors in Retrieval-Augmented Code Generationcited by 1
  8. 2026Knowdit: Agentic Smart Contract Vulnerability Detection with Auditing Knowledge Summarizationcited by 1
  9. 2026Correct Code, Vulnerable Dependencies: A Large Scale Measurement Study of LLM-Specified Library Versionscited by 1
  10. 2026CodePoisonRAG: Knowledge Poisoning Attacks on Retrieval-Augmented Code Generationcited by 1
  11. 2026A Quasi-Experimental Developer Study of Security Training in LLM-Assisted Web Application Developmentcited by 0
  12. 2026Activating Latent Security Knowledge through LLM-Guided Risk Analysis for Secure Code Generationcited by 0
4 more
  1. 2026From Adoption to Deployment: A Qualitative Study on AI Integration in Software Development Practicecited by 0
  2. 2026MACGen: Toward Functionally Correct and Secure Code Generation via Multi-Agent Collaborationcited by 0
  3. 2026Interpreting and Steering for Safe and Correct Code Generationcited by 0
  4. 2026SEEK: Agentic Knowledge Graph Retrieval for Secure Code Generationcited by 0

Similar, not cited

Close in wording, no citation either way

  1. 2025ImportSnare: Directed "Code Manual" Hijacking in Retrieval-Augmented Code Generation
  2. 2026Inference-Time Safety For Code LLMs Via Retrieval-Augmented Revision
  3. 2026Improving LLM-Assisted Secure Code Generation through Retrieval-Augmented-Generation and Multi-Tool Feedback
  4. 2025RefleXGen:The unexamined code is not worth using
  5. 2025RAG-Pull: Turning Retrieval into a Code-Injection Channel via Invisible Unicode Perturbations
  6. 2025Rethinking the Evaluation of Secure Code Generation
  7. 2025Fortifying LLM-Based Code Generation with Graph-Based Reasoning on Secure Coding Practices
  8. 2026Securing Retrieval-Augmented Generation: A Taxonomy of Attacks, Defenses, and Future Directions