Skip to content

Back to the catalog

SecReason: Vulnerability Detection and In-Stream Self-Repair for LLM Code Generation
arxiv.orgpaper

SecReason: Vulnerability Detection and In-Stream Self-Repair for LLM Code Generation

SecReason is an in-stream defense for reasoning LLMs that monitors code drafted inside the streamed chain of thought. Dual-track incremental scanning applies security rules per statement and traces dataflow across accumulated code. When a rule fires, generation is interrupted, a warning is inserted and decoding resumes, so the model repairs its draft without regenerating the full response. On CWEval and SecCodeBench it raises the share of secure and functionally correct runs from below 15% to 72.8%–98.1%, uses fewer tokens than post-hoc repair, and limits functional regressions from false warnings.

TL;DR Paper: SecReason scans code inside a reasoning LLM's chain of thought, interrupts on rule hits and injects warnings so the model self-repairs, lifting secure+correct output from under 15% to 72.8–98.1%.
Also seen from another angle🧠 AI →
Document

What it is

SecReason (Han, Sun, Ma, Niu, Ma, Lo; arXiv 2610.14453v1, cs.CR) is an in-stream monitor that sits between the LLM inference engine and downstream clients. It works at the serving layer and does not modify model weights. It targets reasoning models that write code drafts inside fenced blocks in their chain of thought, before that code is delivered.

Why the premise holds (the paper's empirical study)

The study ran six open-weight reasoning models (Qwen3.6-35B-A3B, Gemma-4-26B-A4B, DeepSeek-V4-Flash, MiniMax-M3, GLM-5.3-Flash, Kimi-K3) on 173 tasks: 97 from CWEval and 76 from SecCodeBench, in Python, JavaScript, Java, C and C++. Closed APIs were excluded because they return only thought summaries.

  • Insecure delivery: Of functionally valid generations, 13.2–35.6% failed security tests on CWEval and 42.6–61.3% on SecCodeBench.
  • Security awareness doesn't help: Security-term mentions in the reasoning were equal or higher among secure outputs in 11 of 12 model-benchmark cells. Of traces that did mention security, 5.4–61.1% still failed security tests.
  • Code is drafted early: 52.4–99.8% of delivered lines appear verbatim in a fenced block in the trace. The draft closes at 45.2–89.1% of the trace. That leaves 203 to 6,028 mean post-draft tokens (DeepSeek-V4-Flash is the high end), which is the window for intervention.

The worked example is an SSRF (CWE-918) in a URL-building task. The model noted # and ? as invalid characters, then wrote validation that checked only / and empty strings. Its self-review approved the draft, and evil.com# bypasses the check.

How it works

Static analyzers such as Semgrep and Bandit need complete, valid code, so the paper names three problems and a design response for each:

  1. When to scan. Scanning at arbitrary token boundaries gives unparseable fragments, and waiting for the fence to close is too late. SecReason parses incrementally and scans when a statement completes inside the open fence.
  2. What to rescan. Rescanning everything on each statement adds latency, while scanning each statement alone misses taint flows such as command or SQL injection. SecReason runs two tracks: a statement-local track and a context-dependent track for dataflows across accumulated code.
  3. Feeding findings back. Structured findings (rule IDs, line coordinates) are converted into guidance the model can act on. Generation is interrupted, the warning is inserted, and decoding resumes from the existing token prefix. This avoids starting a new turn, which would discard the reasoning trace and force full regeneration.

The task prompt is left unchanged.

Results

The evaluation used six models for streaming detection and two locally deployed models for self-repair. On tasks where unassisted generation introduces vulnerabilities, the share of secure and functionally correct runs rose from below 15% to 72.8–98.1%. SecReason uses fewer tokens than post-hoc repair and causes smaller functional regressions when rules misfire on secure code. The fetched text does not give per-model, per-language or rule-coverage breakdowns, or latency figures.

Preconditions / Affected

  • You must control the inference or streaming layer. The paper evaluated self-repair only on locally deployed models.
  • The model must expose its full reasoning stream. Closed APIs that return only thought summaries cannot be monitored this way.
  • The model must draft code in fenced blocks during reasoning. Kimi-K3 had the weakest fit, with 52.4% verbatim recall on CWEval.

Caveats

  • The text I had cuts off at the start of the Method section. Ruleset contents, warning wording, and the false-positive and latency details are not covered here.
  • Detection is only as good as the security rules. A rule miss means no warning, and a misfire can still cause a regression, although the paper says SecReason limits that.
  • The authors' own benchmarks and models are the only evidence; this is a v1 preprint.

Organisations named here

  • SecReason

Technologies named here

  • LLM-generated code
  • LLM
  • CWEval
  • SecCodeBench

Techniques named here

  • Dual-track incremental scanning
  • In-stream interruption and warning insertion
  • Related defenses compared