SecReason: Vulnerability Detection and In-Stream Self-Repair for LLM Code Generation
cs.CR, cs.SE
Submitted: 2026-10-10
Updated: 2026-10-14
Code: https://github.com/PyCQA/bandit
License: http://creativecommons.org/licenses/by/4.0/
The gist: Large language models (LLMs) may introduce security vulnerabilities into generated code, which can persist into production when verification focuses on functional correctness.
Abstract
Large language models (LLMs) may introduce security vulnerabilities into generated code, which can persist into production when verification focuses on functional correctness. Existing defenses act before, during, or after generation, but none inspects the code during generation. Security prompts and retrieved security knowledge act before any code exists, constrained decoding limits token choices but cannot guide repair, and post-hoc repair must regenerate the full response. Reasoning LLMs draft code early in their streamed chain of thought, and in this pre-delivery window the code exists only inside the reasoning stream. We present SecReason, an in-stream defense that monitors this window. Dual-track incremental scanning applies security rules, checking each statement as it is emitted and tracing dataflows across the accumulated code. When a security rule fires, SecReason interrupts generation, inserts an in-stream warning, and resumes decoding, so the model repairs the draft within the same reasoning trace without regenerating the full response. On tasks where unassisted generation introduces vulnerabilities, raises the share of runs delivering secure and functionally correct code from below 15% to 72.8%--98.1% on and. consumes fewer tokens than post-hoc repair, and when security rules misfire on secure code, it limits the functional regressions from false warnings.
Sources
- Monitoring Reasoning Models for Misbehavior and the Risks of Promoting Obfuscation
- Purple Llama CyberSecEval: A Secure Coding Benchmark for Language Models
- SecCodeBench-V2 Technical Report
- Gemma 4 Technical Report
- (Don't) Trust, but (Don't) Verify: Developers' Attention to Security in AI-Generated Code
- OpenAI o1 System Card
- Efficient Guided Generation for Large Language Models
Related papers
- SoK: AI-Augmented Binary Reversing
- Relaxed Sender Anonymity for CBDC Interbank Settlement: A Zero-Knowledge Approach on Permissioned EVM
- Calibration-Family Overfit: Why Trusted Sabotage Monitors Don't Transfer Across Lineages
- Estimating the Social Cost of Corporate Data Breaches
- Efficient Fuzzy PSI under One-Sided Assumptions
- Sealing the Audit-Runtime Gap for LLM Skills