CS-Guard: the first LLM guardrail evaluation benchmark for code generation safetyCS-Guard: Benchmarking LLM Guardrails for Code Generation Security
Quick read
Evaluation and benchmarksBased on the paper PDF (generated by Gemini); refer to the original
The researchers evaluated code security guardrails and found that base models achieve an ASR of 85.9%–99.5% under FSA attacks, and existing guardrails struggle to defend effectively.
- Large models are used for malware generation, and existing safety benchmarks mostly focus on natural language with malware as only a minor subcategory, lacking systematic evaluation of guardrails for code generation safety.
- Constructs the CS-Guard benchmark with text-to-code and code-to-code tasks, proposes the fictional scenario attack FSA, and establishes a three-layer guardrail taxonomy to evaluate 9 guardrails.
- Base models achieve ASR of 85.9%–99.5% under FSA, and guardrails reduce ASR only to a limited extent; models are more vulnerable in code infilling and translation than in code completion.
- Only English was evaluated with no multilingual analysis; the multi-turn jailbreak FSA-MT contains only 20 validation samples; white-box guardrails requiring additional training or neuron access were not evaluated; all models use quantized versions.
- Evaluated models
- CodeLlama-13B-InstructDeepSeekCoder-V2-Lite-16BGPT-OSS-20BDevstral-small-v2-24BQwen3-30BQwen3-Coder-30BGPT5-miniQwen3Guard-GenLlamaGuard4WildGuardNemoGuardMD-Judge
- Benchmarks
- CyberSecEvalRMCBenchRedcode-GenFSA-fullFSA-smallFSA-MT
- Metrics
- ASRASR reductionF1
CS-Guard is the first benchmark to systematically evaluate code generation safety guardrails, covering both text-to-code and code-to-code scenarios. The text-to-code part uses 1,000 high-quality malicious code generation prompts, 7 jailbreak attacks, and the newly proposed Fictional Scenario Attack (FSA), which embeds malicious intent in seemingly legitimate software development contexts; the code-to-code part contains 331 prompts covering code infilling, code completion, and code translation. The authors evaluated 9 guardrails on 7 LLMs and found that existing guardrails perform poorly on malicious code generation requests: for text-to-code, the average attack success rate (ASR) after jailbreaking is about 50% for many guardrails; for code-to-code, the average ASR on base models is close to 100%, and after applying guardrails it remains between 14.4% and close to 100%; FSA achieves an ASR close to 100% for many guardrails. The multi-turn FSA achieves an ASR of 65.0% to 92.3% on the selected guardrails. The authors organize guardrails by a three-layer taxonomy and release the benchmark and data under the CC BY-NC-SA 4.0 license.
Why it was selectedCS-Guard uses 1,000 malicious code generation prompts and 331 code-to-code samples to compare 9 categories of guardrails, and provides the failure ranges of various guardrail categories in code scenarios.
Related events and news
1 events · 1 news mentions
News that mentions this paper
Adversa AI 发布 10 月 GenAI 安全资源汇总
- Adversa AIcites the paper's id or linkAdversa AI 的 GenAI 安全资源精选(2026 年 10 月)
CS-Guard: benchmarking LLM guardrails for code generation security
Paper analysis
What problem does this paper attempt to solve?
The paper evaluates the defensive performance of LLM code generation safety guardrails in text and code scenarios, pointing out that current guardrails struggle to resist covert malicious code requests.
The paper attempts to address the lack of targeted safety guardrail evaluation benchmarks in LLM code generation scenarios, and the problem that existing guardrails struggle to defend against concealed malicious-intent code generation.
- Scenario and real-world risk: LLMs have been widely deployed as code agents in software engineering, but underground markets have already seen cases of using models to reduce malware production costs, making it urgent to protect code agents from malware generation abuse.
- Limitations of existing evaluations and guardrails: Existing safety benchmarks mostly focus on broad safety risk categories, with malware usually only as a secondary subclass, and evaluations concentrate on natural language instructions; code safety evaluations often use prompts with explicit malicious keywords, making detection too simple and inconsistent with real development scenarios.
- Core observation: Malware typically shares the same functional requirements as benign software (e.g., boot-stage injection, remote communication, and log cleanup), with the difference mainly in usage intent; attackers can evade safety detection by embedding malicious functionality into plausible fictional development scenarios.
- Threat model:
- Attacker goal: manipulate the victim system to generate executable malware or malicious code fragments.
- Attacker capability: interact with the model through natural language prompts, code fragments (completion, fill-in-the-blank, translation), or fictional legitimate contexts, supporting single-turn and multi-turn interactions.
- Attacker knowledge: black-box or no-box interaction, requiring no internal weights or gradient information of the victim system.
- Victim system: code agents in software development environments and their deployed policy-based, classifier-based, and internal safety guardrails.
- Proposed solution: The paper proposes the CS-Guard evaluation benchmark, covering text-to-code and code-to-code scenarios, proposes the fictional scenario attack (FSA), and establishes a three-layer guardrail taxonomy to systematically evaluate 9 guardrails.
What related research exists?
The paper reviews three lines of research—jailbreak attacks, guardrail defenses, and code safety evaluation—and notes the previous lack of systematic evaluation of safety guardrails for code generation.
The paper organizes three types of related work around code generation safety: jailbreak attacks, guardrail defenses, and code security evaluation.
Jailbreak Attacks
- Obfuscation-based attacks: CipherChat (Yuan et al., 2024) uses cipher encoding, FlipAttack (Liu et al., 2025) swaps word order, and EmojiAttack (Wei et al., 2025) evades detection by inserting emojis.
- Optimization-based attacks: AutoDAN (Liu et al., 2024a), Tree of attacks (Mehrotra et al., 2024), and Zou et al. (2023) use victim model feedback for adversarial iterative optimization.
- Template-based attacks: Albert (2023) collects human-written templates from the JailbreakChat community, including UCAR, EvilConfident, AIM, and DevmodeRanti.
Guardrails
- Pre-deployment methods: modify the model's internal mechanisms, including safety alignment (Ji et al., 2023; Zhao et al., 2025b), representation engineering (Zou et al., 2024), internal state classification (Hu et al., 2024; Liu et al., 2024b), and prompt fine-tuning (Mo et al., 2024; Zhou et al., 2024).
- Post-deployment methods: intervene at inference time, including LLM detectors (Zhao et al., 2025a; Meta, 2025a; Nvidia, 2025; Han et al., 2024), controlled sampling (Li et al., 2024b), safety prompt templates (Xie et al., 2023), and model self-checking (Zhang et al., 2024).
Evaluation on Code Generation Security
- Vulnerable code and robustness evaluation: SALLM (Siddiq et al., 2024) and Pearce et al. (2025) evaluate code vulnerabilities; CodeAttack (Jha and Reddy, 2023) evaluates the adversarial robustness of pretrained programming models.
- Malicious code generation evaluation: RMCBench (Chen et al., 2024) evaluates malicious code infilling and translation, RedCode (Guo et al., 2024) focuses on dangerous code execution and completion, Mocha (Wahed et al., 2025) studies multi-turn prompt decomposition, and CyberSecEval (Bhatt et al., 2023) provides expert-designed TTP prompts.
Benchmark datasets and comparison baselines: the paper integrates 1,000 TTP prompts from CyberSecEval, 171 code infilling and translation samples from RMCBench, and 160 code completion samples from Redcode-Gen; the evaluation baselines cover 5 policy-based guardrails (SmoothLLM, Parden, RPO, PAT, SelfReminder) and 5 classifier-based guardrails (Qwen3Guard-Gen, LlamaGuard4, WildGuard, NemoGuard, MDJudge).
The authors note that existing research mostly focuses on general risks or evaluates only the model itself, lacking systematic evaluation of guardrails for code generation safety, and CS-Guard fills this gap.
How does the paper address this problem?
CS-Guard constructs two types of code generation tasks and FSA attacks, proposes a three-layer guardrail classification system, and adopts an automated evaluation protocol with multiple sampling and majority voting.
The paper proposes the CS-Guard evaluation benchmark, which systematically evaluates different guardrails' defense capabilities against malicious code generation by constructing text-to-code and code-to-code task scenarios, proposing the fictional scenario attack (FSA), and designing a three-layer guardrail taxonomy.
Evaluation Task Design and Data Construction
- Text-to-code: Divided into two difficulty levels. Level 1 uses 1000 expert-designed TTP prompts from Meta's CyberSecEval, covering 10 categories of the ATT&CK framework (100 per category); Level 2 introduces 7 existing jailbreak methods (3 obfuscation attacks and 4 template attacks).
- Fictional scenario attack (FSA) construction: Leveraging the functional overlap between malware and benign software, constructs single-turn and multi-turn attacks. Single-turn FSA calls GPT5-mini to extract functionality from TTP prompts and generate benign application scenarios, then Venice-uncensored-1.1 generates 1000 fictional scenario attack prompts (FSA-full, with a 300-prompt subset FSA-small also provided). Appendix A.6 provides the complete prompt templates.
- Code-to-code: Collects 331 prompts containing malicious source code, without jailbreak augmentation. Includes 80 code fill-in-the-blank and 91 code translation prompts from RMCBench (scanned from GitHub repositories and filtered for single-file standalone samples), and 160 code completion prompts from Redcode-Gen synthesized by GPT-4 and human quality-checked, covering 11 malware types and 9 programming languages.
Multi-Turn Fictional Scenario Search Algorithm (FSA-MT)
- State search and candidate evaluation: Decomposes single-turn FSA prompts into 5 to 10 rounds of interaction, expanded based on the ToT-DFS algorithm. Each step samples 5 candidate sub-instructions, runs 5 dialogue simulations with a simulator classified as Possible or Impossible, and selects the branch with the highest completion probability.
- Response filtering and termination conditions: In round t, victim model M generates 5 responses to the selected instruction, filtering out refusal responses to retain the valid response set: Rt ← {rt ∈ Rt ∣ C(rt) = False} This formula represents filtering the subset of candidate responses not refused by the victim model through the refusal checker C. If all are refused, terminate; if not all are refused, randomly select one valid response to update the dialogue history, and terminate when the minimum number of rounds is reached and the judge determines success.
Three-Layer Guardrail Taxonomy
- Category layer: Divided into policy-based (relying on real-time generation by the base LLM, e.g., SmoothLLM, PAT), classifier-based (independently trained discriminative models, e.g., Qwen3Guard, LlamaGuard4), and internal (pre-deployment safety alignment).
- Operation layer: Divided into classification (identifying malicious prompts) and generation (controlling base LLM output to produce safe responses).
- Position layer: Divided into input-side (detecting prompts before LLM processing), output-side (evaluating both prompt and response), and flexible-side (generation-control guardrails).
Evaluation Protocol and Quality Validation
- Generation and majority vote adjudication: Deployed with vLLM, maximum generation length set to 14500 tokens. Each malicious prompt is sampled 5 times for responses, labeled 5 times by an LLM review model and adjudicated by majority vote (good, bad, unclear); any candidate response labeled bad means the model is considered jailbroken.
- Human agreement checks: Expert manual review of 88 FSA prompt samples yields 98.4% accuracy; expert consistency checks of LLM review labels for text-to-code Level 1 (97 samples), Level 2 (98 samples), and code-to-code (97 samples) yield accuracies of 96.8%, 93.8%, and 93.7%, respectively.
What experiments did the paper conduct?
The experiments evaluated 7 models and 9 guardrails, and the results show that existing guardrails are weak against FSA attacks and code infilling and translation defenses.
Experimental Setup
- Models under test: The evaluation covers 7 LLMs, including 2 classic models (CodeLlama-13B-Instruct, DeepSeekCoder-V2-Lite-16B), 4 open-source SOTA models (GPT-OSS-20B, Devstral-small-v2-24B, Qwen3-30B, Qwen3-Coder-30B), and 1 black-box closed-source model (GPT5-mini); all open-source models use quantized versions.
- Datasets and scale: Text-to-code includes 1000 TTP prompts from CyberSecEval, 7 jailbreak augmentations, and single-turn FSA (FSA-full 1000 prompts, FSA-small 300 prompts), as well as multi-turn FSA-MT (20 samples); code-to-code includes 331 malicious code prompts (RMCBench 80 fill-in-the-blank, 91 translation, Redcode-Gen 160 completion).
- Guardrail baselines under test: Policy-based guardrails include SmoothLLM, Parden, RPO, PAT, SelfReminder; classifier-based guardrails include Qwen3Guard-Gen, LlamaGuard4, WildGuard, NemoGuard, MDJudge; internal guardrails are the safety alignment inherent to each base model.
- Evaluation metrics: Input-side guardrails and internal guardrails use attack success rate (ASR); policy-based generation guardrails use ASR reduction after applying the guardrail (SmoothLLM is the proportion of detected malicious prompts among successful attacks on the base model); output-side guardrails use F1 score on a balanced test set.
- Review and adjudication protocol: Deployed with vLLM, maximum generation length 14500 tokens; each prompt is sampled 5 times for responses, labeled 5 times independently by an LLM review model and adjudicated by majority vote (good/bad/unclear); the paper does not specify the name of the review model used; a jailbreak is deemed successful if at least 1 response is labeled bad.
- Hardware and testing costs: Open-source model experiments ran on 3 RTX 5090 GPUs; black-box experiments cost approximately $630.3 in OpenAI API usage and $160 for Venice API to generate FSA prompts.
Main Results
The table below shows the ASR performance of the 7 base models on the main text-to-code and code-to-code tasks (data from Figure 3):
Swipe horizontally to read the table
Model L1 Baseline ASR L2-FSA ASR L2 Average ASR C2C Fill-in-the-blank ASR C2C Average ASR CodeLlama-13B 98.2% 96.9% 83.5% 98.7% 97.3% DeepSeekCoder-V2-Lite-16B 94.5% 99.4% 77.6% 100.0% 98.9% Devstral-small-v2-24B 61.4% 99.5% 91.5% 98.7% 70.8% Qwen3-Coder-30B 55.1% 98.5% 39.1% 100.0% 70.0% GPT-OSS-20B 25.3% 90.1% 31.4% 100.0% 65.6% Qwen3-30B 20.2% 92.3% 40.6% 100.0% 65.6% GPT5-mini 15.1% 85.9% 28.9% 95.0% 52.5% - Internal alignment has limited effect on defending basic malicious requests: The authors note that classic models have an ASR of 94.5%–98.2% at Level 1, while open-source SOTA and closed-source models also reach an ASR of 15.1%–61.4% (Figure 3).
- FSA attacks achieve high success rates on all base models: The authors note that FSA bypasses the model's intent recognition, with ASR rising to 85.9%–99.5% across models (Figure 3).
- Code fill-in-the-blank and translation are more easily exploited than code completion: The authors note that models are more resistant on code completion (ASR 0.0%–96.8%), but easily compromised on code fill-in-the-blank (ASR 95.0%–100.0%) and code translation (ASR 62.6%–100.0%), arguing that malicious code fragments fall outside the alignment training distribution (Figure 3).
Policy-Based Guardrail Defense Evaluation
- Policy-based guardrails show large variation in defense performance on text-to-code: Testing 4 generation-control guardrails for ASR reduction across models, PAT achieves a baseline ASR reduction of 4.7% on GPT-OSS-20B, but -1.2% on CodeLlama-13B (i.e., ASR increases), and an average ASR reduction of 3.8% on Devstral-small-v2-24B (Figure 4); the authors state that PAT has negative effects on some models, while SelfReminder and SmoothLLM are relatively stable, with average ASR reductions of 7.9%–28.3% and 4.1%–62.1%, respectively (Figure 4).
- Policy-based guardrails perform better on code-to-code tasks: Testing ASR reduction on code-to-code, SelfReminder achieves an average ASR reduction of 31.7%–55.0%, SmoothLLM 2.2%–60.5%, while RPO reductions range from -9.0% to 6.8% (Figure 4); the authors state that SelfReminder performs best, while RPO provides weak defense across models.
- Policy-based guardrails have limited effect in weakening FSA attacks: Testing defense reductions under FSA, PAT achieves the highest ASR reduction of 27.7% on Qwen3-30B, but only 1.3%–21.7% on the remaining models (Figure 4); the authors note that even with the highest reduction, Qwen3-30B's remaining ASR is still 64.7%, leaving significant security concerns.
Classifier-Based Guardrail Performance Comparison
- Input classifiers perform reasonably on baseline requests but decline under obfuscation and FSA: Testing 4 input classifiers for ASR, Level 1 baseline ASR is 6.9%–27.5%, but under CipherChat, LlamaGuard4 and NemoGuard ASR rises to 100.0% and 99.7%, respectively, and under FSA all four classifiers reach an ASR of 73.3%–98.5% (Figure 5); the authors note that Qwen3Guard is the most stable, while LlamaGuard4 is the weakest defense.
- Output classifiers are generally strong in text scenarios but fail notably under FSA: Testing 6 output classifiers for F1 score, Level 1 F1 ranges from 20.0%–85.6%, Level 2 average F1 from 40.0%–80.0%, but under FSA, except for WildGuard at 72.3%, the remaining 5 classifiers have F1 scores of only 6.9%–22.1% (Figure 6); the authors state that FSA can effectively evade most output classifiers.
- Classifiers perform poorly on fill-in-the-blank and translation in code-to-code tasks: Input classifiers have ASR of 22.5%–43.7% on code fill-in-the-blank and 20.8%–45.0% on translation, respectively (Figure 5); output classifiers achieve F1 of 88.5%–99.2% on code completion (except Parden at 20.2%), but F1 declines on code fill-in-the-blank (Figure 6); the authors argue that missing code in fill-in-the-blank tasks limits classifier inference ability.
Multi-Turn Fictional Scenario Attack (FSA-MT) Validation
- Multi-turn attacks maintain high success rates under typical combinations: Selecting GPT5-mini, PAT, and Qwen3Guard for a proof-of-concept with 20 samples, GPT5-mini with PAT defense achieves an ASR of 65.00%, while Qwen3Guard achieves ASR of 84.60% at the input side and 92.30% at the output side (Figure 7); the authors note that this result indicates existing guardrails lack robustness against multi-turn FSA attacks.
Other Ablations and Analyses
- Human and LLM review agreement (text Level 1): accuracy 96.8% on 97 samples, Fleiss' kappa 98.60% (Figure 2).
- Human and LLM review agreement (text Level 2): accuracy 93.8% on 98 samples, Fleiss' kappa 85.70% (Figure 2).
- Human and LLM review agreement (code-to-code): accuracy 93.7% on 97 samples, Fleiss' kappa 90.10% (Figure 2).
- FSA prompt construction validity review: accuracy 98.4% on 440 samples from 88 baselines, Fleiss' kappa 44.60% (Figure 2).
- Exceptional effect of prompt obfuscation on weak models: CipherChat reduces ASR to 28.5% on CodeLlama-13B, which the authors attribute to weak models' insufficient understanding of ciphertext leading to misjudgment or inability to execute (Figure 3, Appendix A.6.2).
What are some points that can be further explored?
The authors point out four limitations: English-only, small multi-turn evaluation sample size, no evaluation of white-box guardrails, and the use of quantized models.
Limitations and future directions noted by the authors
- Only English data evaluated and lack of multilingual evaluation: The paper notes that CS-Guard currently contains only English data and does not cover multilingual scenarios; existing research shows that LLMs face malicious prompt threats in multilingual settings, but no such research exists in the malware generation domain, left for future exploration due to budget and time constraints (source: Limitations section, item 1).
- Multi-turn jailbreak evaluation not comprehensive: The paper notes that this study focuses on first evaluating the effectiveness of guardrails in code generation safety and providing a standardized testbed; the multi-turn jailbreak FSA-MT used only 20 samples for a proof-of-concept study, and the authors plan to expand this in the future (source: Limitations section, item 2).
- White-box guardrails beyond alignment training not evaluated: The paper notes that due to budget and time constraints, this study did not evaluate white-box guardrails requiring additional training or access to neurons, but the community can use CS-Guard as a testbed for evaluation (source: Limitations section, item 3).
- Internal guardrail evaluation based on quantized models: The paper notes that evaluation of internal guardrails used quantized versions of LLMs due to budget constraints, and empirical results may not fully generalize to full-precision versions (source: Limitations section, item 4).
- Exploring honeypot defense to mitigate FSA attacks: The authors note in the discussion that honeypot defense (Wu et al., 2025) is a potential mitigation, where the model can pretend to be compromised and return harmless decoy suggestions to expose malicious intent; future research can explore its actual protective effect in depth (source: Section 4).
Experimental coverage
- Tested models include 2 classic open-source models (CodeLlama-13B-Instruct, DeepSeekCoder-V2-Lite-16B), 4 open-source SOTA models (GPT-OSS-20B, Devstral-small-v2-24B, Qwen3-30B, Qwen3-Coder-30B), and 1 closed-source black-box model (GPT5-mini); open-source models use quantized versions.
- Evaluated guardrails include 5 policy-based methods (SmoothLLM, Parden, RPO, PAT, SelfReminder) and 5 classifier-based methods (Qwen3Guard-Gen, LlamaGuard4, WildGuard, NemoGuard, MDJudge).
- Text-to-code generation tasks cover 1000 TTP prompts from 10 ATT&CK categories in CyberSecEval, 7 existing jailbreak methods, and 1000 single-turn FSA prompts; multi-turn validation covers 20 samples.
- Code generation tasks cover 331 prompts across 9 programming languages and 11 malware types (80 code infilling, 91 code translation, 160 code completion); this task did not apply jailbreak enhancement.
- The paper does not report the specific model names and versions used for the LLM judge models.
Summarize the main content of the paper
The paper proposes CS-Guard, a code security guardrail benchmark, and the evaluation finds that existing guardrails are weak against fictional scenario attacks and code-level malicious requests.
- Research positioning and core problem: Targeting the malware generation risk faced by LLM code agents, constructs the CS-Guard benchmark specifically for evaluating code generation safety guardrails, aiming to address the problem that existing evaluations favor natural language instructions and lack systematic examination of concealed malicious code intent.
- Benchmark construction and method design: The benchmark covers text-to-code (1000 TTP prompts, 7 existing jailbreaks, and the proposed fictional scenario attack FSA) and code-to-code (331 malicious code fill-in-the-blank, completion, and translation) scenarios, and establishes a three-layer guardrail taxonomy covering category, operation, and position, evaluating 9 guardrails of policy-based, classifier-based, and internal types.
- Core experimental results: Base model native alignment achieves ASR of 15.1%–98.2% on basic TTP requests (Figure 3); under concealed single-turn FSA attacks, model ASR rises to 85.9%–99.5%, and input classifiers also reach ASR as high as 73.3%–98.5% (Figure 3, Figure 5); in code-to-code tasks, base models achieve ASR of 95.0%–100.0% on code fill-in-the-blank (Figure 3); among policy-based guardrails, only SelfReminder and SmoothLLM provide relatively stable ASR reductions, while the remaining guardrails show limited improvement (Figure 4).
- Author conclusions and security implications: The authors note that currently deployed safety guardrails struggle to identify concealed malicious code generation requests that share benign functional features, and are fragile in handling code fragments, recommending future research explore new proactive detection mechanisms such as honeypot defenses, and establish a more comprehensive defense system for code generation safety.