Manuscript Offprint

arXiv:2609.09798

CS-Guard: Benchmarking LLM Guardrails for Code Generation Security

Jinyang Li, Mingyu Guo, Hung X. Nguyen

Abstract

Large language models (LLMs) have been exploited to generate malware, but the effectiveness of guardrails for code generation security remains unclear. We introduce CS-Guard, the first benchmark to systematically evaluate guardrails for code generation security. It covers 1) text-to-code generation with 1000 high-quality malware-generation prompts, 7 jailbreak attacks, and a novel fictional scenario attack (FSA) that embeds malicious intent in a legitimate fictional software-development scenario; and 2) code-to-code generation with 331 code prompts spanning code infilling, code completion, and code translation. We empirically evaluate 9 guardrails across seven LLMs. We find that current guardrails perform poorly against malicious code-generation requests: for text-to-code, the average attack success rate (ASR) after jailbreaks reaches about 50% for many guardrails; for code-to-code, average ASR approaches 100% on base LLMs and remains high across many guardrails (14.4% to nearly 100%). Our FSA also achieves ASR close to 100% across many guardrails, raising major reliability concerns for real-world software development. To support future research, CS-Guard uses a modular three-layer guardrail taxonomy that lets developers register guardrails for evaluation. We release the benchmark and data to enable further community evaluation.

1 Introduction

Large language models (LLMs) are increasingly deployed as code agents in software-development environments (OpenAI, 2025b; Microsoft, 2024; Rastogi et al., 2025; Cao et al., 2026), where their ability to understand human instructions and code enables tasks such as code editing (Fried et al., 2023; Jin et al., 2023), instruction-to-code generation (Zan et al., 2023), and code translation (Lu et al., 2021). However, they can also be

manipulated to generate malware (Ahi and Valizadeh, 2025; Hasanov et al., 2024); Anthropic reports a real-world case of hackers using LLMs to create malware for sale (ANTHROPIC, 2025), and underground-market evidence shows growing LLM-enabled malicious services, including lower-cost LLM-based malware production (Lin et al., 2024). Thus, protecting LLM code agents against malware generation is urgent.

Researchers and security vendors have proposed guardrails against LLM misuse (Das et al., 2025; Wang et al., 2025), spanning I/O filtering (Meta, 2025b; Liu et al., 2024b), weight adjustment (Xu et al., 2024a; Wang et al., 2024), and controlled sampling (Li et al., 2024b; Xie et al., 2023). Existing studies largely evaluate broad risk taxonomies where malware is a minor subcategory (Kang et al., 2025; Wang et al., 2026; Shen et al., 2025), yielding coarse evaluations with many focusing on natural-language instructions, whereas LLM code agents operate on both instructions and code in software-engineering settings. Although some work evaluates LLM security in coding contexts (Guo et al., 2024; Chen et al., 2024; Sheng et al., 2025; Bhatt et al., 2023; Li et al., 2025), fine-grained analysis of guardrails for malware-related prompts remains missing, limiting the assessment of their effectiveness in code generation security, which is crucial for protecting LLM code agents.

We present CS-Guard, the first benchmark for evaluating guardrails in code generation security. CS-Guard is designed around two principles: 1) realistic and comprehensive evaluation across diverse code-generation tasks, and 2) reproducibility and ease of use for future guardrail research. To satisfy the first principle, CS-Guard includes two representative settings: text-to-code generation, where LLMs generate malicious code from natural-language instructions and guardrails classify malicious prompts and responses; and code-to-code generation, where LLMs generate code from code snippets and guardrails classify prompts containing malicious code.

To construct our text-to-code samples, we identified two limitations in existing LLM security datasets: 1) prompts are often overly fine-grained and lack realistic functional requirements, and 2) many contain explicit malicious keywords that make detection trivial. To address the first issue, we adopt CyberSecEval (Bhatt et al., 2023), which provides 1000 high-quality prompts with realistic offensive objectives and rich technical details, and augment it with 7 representative jailbreak attacks. To address the second, we introduce fictional scenario attacks (FSA), where malicious intent is embedded within realistic legitimate software-development contexts. For code-to-code evaluation, we collect malware source code from existing benchmarks (Chen et al., 2024; Guo et al., 2024) and construct 331 samples spanning code infilling, code translation, and code completion. These tasks challenge guardrails with prompts containing malicious source code, reflecting a practical code generation security setting.

To meet the second principle, we define a 3-layer taxonomy of existing guardrails, making our benchmark reproducible and easy to use, and allowing developers to register guardrails accordingly. Since fine-grained studies on code-generation security remain scarce and guardrails lack a standardized testbed, we hope ours can support future evaluations as more datasets and attack methods emerge.

To summarize our benchmark, CS-Guard contributes the following:

  1. The first benchmark to systematically evaluate the effectiveness of guardrails for code generation security. Unlike prior studies, we evaluate guardrails on 1000 high-quality text-to-code prompts with realistic offensive objectives and rich technical details, rather than overly fine-grained prompts. Our code-to-code evaluation includes 331 prompts spanning code completion, code infilling, and code translation, enabling analysis of guardrail performance across representative code generation scenarios.

  2. We introduce the fictional scenario attack (FSA), a novel jailbreak that tests whether LLMs can detect malicious intent in code generation prompts without prior user knowledge. Unlike work relying on explicit malicious keywords, FSA exploits the fact that malware often shares functional requirements with legitimate software, framing malware generation as realistic benign development with detailed legitimate uses. We responsibly release FSA-full and FSA-small, with 1000 and 300 prompts, respectively.

  3. We defined a 3-level taxonomy for guardrails and provided a standardized testbed to support new guardrails and research on code generation security. Guardrail developers can easily adapt their guardrails to the testbed according to the taxonomy.

References (13)