← Lab /Security & privacy

CodeSentinel detects indirect prompt injection in code with 0.80 node-level F1

CodeSentinel achieves 0.80 average node-level F1 across six attack families, outperforming existing defenses

Security & privacy arXiv 17 Jun 2026 Defense Simulation evidence

Published as “CodeSentinel: A Three-Layer Defense Against Indirect Prompt Injection in Code Contexts”

Why it matters

The technique can be integrated into existing code-LLM pipelines to substantially lower the risk of hidden malicious instructions while keeping most functional utility intact.

Summary

CodeSentinel detects indirect prompt injection in code contexts with an average node-level F1 of 0.80, beating the next best baseline (CodeGarrison) by 0.10. It reduces attack success rates on commercial coding agents, e.g., Claude-3.5-Haiku from 27.11% to 7.72%. The defense works at inference time, preserves compilation on standard benchmarks, but incurs modest drops in functional metrics such as MBPP Pass@1 (-6.23 points). The paper evaluates six recent attack families and reports consistent performance across multiple surrogate models and black-box victim settings.

What they found 2

  1. CodeSentinel achieves the best average node-level F1 of 0.80, outperforming CodeGarrison (0.70), DePA (0.51), and KillBadCode (0.30).

    “CodeSentinel achieves the best average F1 score of 0.80, outperforming CG (0.70), DePA (0.51), and KBC (0.30).”

  2. Sanitization reduces attack success rate on Claude-3.5-Haiku from 27.11% to 7.72%, a drop of 19.39 percentage points.

    “Claude-3.5-Haiku 27.11 7.72 -19.39”

Caveats 4

  • MBPP Pass@1 drops from 96.11 to 89.88, a decrease of 6.23 points after sanitization.
  • RepoBench Exact Match falls from 100.00 to 67.33, a 32.67-point reduction, indicating surface-level code similarity loss.
  • CodeSentinel currently focuses on single-context sanitization and does not fully address repositoryscale multi-file reasoning, long-horizon interactive agents, or triggers distributed across many files.
  • Identifier sanitization also requires careful scope-aware renaming, and overly aggressive cleaning may change surface-level code similarity even when compilation is preserved.

What’s new here 4

  • Treats untrusted code as a concrete syntax tree (CST) and sanitizes at the node level rather than flat text.
  • Combines three complementary layers: syntax-guided pre-filtering, CST-guided Dynamic Min-K% scoring, and node perturbation analysis.
  • Operates entirely at inference time and can be used with black-box downstream code LLMs.
  • Demonstrates cross-surrogate generalization, showing detection does not depend on a specific logits provider.