Introduction
As Large Language Models (LLMs) increasingly take on the role of automated coders, they introduce significant security risks. These models can be manipulated into generating malware, biased algorithms, or vulnerable code. While "red teaming"—the practice of stress-testing models to find flaws—has advanced rapidly, "blue teaming"—the development of automated defenses to catch these flaws—has lagged behind.
Existing defensive measures often struggle with abstract security concepts and suffer from high false-positive rates, where safe code is incorrectly flagged as dangerous. This "over-conservatism" erodes developer trust. To bridge this gap, we present BlueCodeAgent, an end-to-end defense framework that leverages automated red teaming to build a robust knowledge base of risks, guiding an LLM agent to evaluate code safety with high precision.
The BlueCodeAgent Framework
BlueCodeAgent operates through a two-phase process designed to accumulate risk knowledge and apply it dynamically to new inputs.
1. Diverse Red-Teaming Pipeline
The first phase involves building a comprehensive database of unsafe prompts and code, known as BlueCodeKnow. This pipeline uses three primary methods:
- Policy-based generation: Creating biased instructions based on specific demographic or socio-economic criteria.
- Adversarial jailbreaks: Using optimized prompts to trick models into generating malicious code (e.g., computer viruses).
- Knowledge-driven generation: Producing vulnerable code snippets based on Common Weakness Enumerations (CWEs) to train the defender on specific security flaws.
2. Multi-level Defense Strategy
When a user provides a prompt or the model generates code, BlueCodeAgent employs a layered defense:
- Principled-Level Defense (Constitutions): The agent retrieves similar red-teamed examples from its knowledge base. An LLM then synthesizes these into a "constitution"—a set of actionable, context-specific safety rules. This allows the model to reject biased or malicious requests more effectively than generic safety reminders.
- Nuanced-Level Analysis (Dynamic Testing): To combat over-conservatism in vulnerability detection, the agent goes beyond static analysis. If a vulnerability is suspected, the agent generates executable test cases and runs the code in an isolated Docker sandbox. The runtime results are used to verify the presence of a flaw, significantly reducing false positives.
Experimental Evaluation
The framework was evaluated using BlueCodeEval, a dataset covering bias, malicious instructions, and code vulnerabilities. To test the agent's ability to generalize, the evaluation focused on "unseen risks"—categories not present in the initial knowledge base.
Key Results
- Performance Gains: BlueCodeAgent achieved an average 12.7% F1 score improvement across all tasks compared to baseline models.
- Bias and Malice Detection: The agent outperformed state-of-the-art defenses like PurpCode, with F1 scores up to 29% higher for bias detection.
- Vulnerability Precision: By combining constitutions with dynamic testing, the agent achieved an F1 score of 0.77 using Claude-3.7-Sonnet.
- Reducing Over-conservatism: Dynamic testing proved crucial in lowering false positives. For instance, it reduced false alarms in GPT-4o from 116 to 97, demonstrating that grounding reasoning in execution results prevents "crying wolf."
Conclusion
BlueCodeAgent demonstrates that red teaming data can directly empower blue teaming defenses. By dynamically generating context-aware "constitutions" and verifying suspicions through sandbox execution, the framework provides a scalable and precise defense against the evolving risks of AI-driven code generation. Future work will focus on scaling this analysis to repository-level code and extending the framework to other generative modalities like image and video.
Summary updated