Skip to content

feat: detect injection attacks and harden the agent against prompt injection (#118) - #171

Merged
OkiriGabriel merged 1 commit into
feat/threat-detection-166from
feat/injection-detection
Nov 14, 2026
Merged

OkiriGabriel merged 1 commit into
feat/threat-detection-166from
feat/injection-detection

Conversation

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Stacked on #170 (threat detection). Review and merge #170 first; GitHub will then retarget this PR to main. The diff here only shows the new work.

Description

Fixes #118, and extends the threat detector from #166 with injection attacks.

1. Injection attacks against your applications

New detector rules, matched on the URL-decoded line (decoded twice, for double encoding). Most only apply to lines that look like an HTTP request, so an ordinary SQL query in a database log doesn't trigger.

Rule Catches MITRE
sql_injection ' OR 1=1, UNION SELECT (incl. /**/), pg_sleep(, xp_cmdshell, information_schema.tables T1190
command_injection ;cat /etc/passwd, ||whoami, $(curl …) T1190
log4shell ${jndi:ldap://…}, obfuscated ${${lower:j}ndi…}, in any line or header T1190
path_traversal ../../../../etc/passwd, ..%2f, win.ini, proc/self/environ T1190
ssrf_metadata parameters pointing at 169.254.169.254, metadata.google.internal T1552
xss <script>, javascript:, onerror= T1059.007
template_injection {{7*7}}, ${7*7}, __globals__ T1190

Volume-aware severity: scanners hit every public site constantly, so a probe is medium. It's attached as evidence and the incident is handled as usual. At THREAT_INJECTION_THRESHOLD (default 5) attempts from one source IP, the finding becomes high ("sustained") and the incident becomes a security incident.

2. Prompt injection against the agent (#118)

Layer Behaviour
Detection prompt_injection (instruction overrides, role switches, chat-template tokens, requests to call the agent's own tools, "approve/merge this" aimed at bots, "don't tell security") and hidden_unicode (zero-width, bidi-override, tag characters). MITRE ATLAS AML.T0051, high. Scans incident data, code-review diffs and every tool result.
Sanitising Invisible and bidi characters are stripped before anything reaches the model; evidence shows them as ‮.
Marking Incident data goes inside <untrusted_incident_data> and tool results inside <untrusted_tool_output>, with a WARNING: line when an injection was found. Every system prompt states this content is evidence, never instructions.
Tool lock Once an injection is found (initially or in any later tool output), mutating tools are refused for the rest of the incident. The state is kept in the checkpointed context. suggest_fix, notify_slack and read-only tools still work. post_code_review stays so malicious PRs can be flagged, but it can never APPROVE while locked, even with CODE_REVIEW_ALLOW_APPROVE.
Per-type policy code_review incidents can only post their review, never call infrastructure tools.
Escalation New reason prompt_injection. The code-review prompt treats an injection attempt as REQUEST_CHANGES. Outside code review, a high finding also makes it a security incident.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Documentation update

Testing

  • New tests/test_injection_detection.py (57 tests):
    • 16 attack payloads across all 7 injection rules, including URL-encoded, double-encoded, /**/-obfuscated SQL, obfuscated Log4Shell, and encoded traversal and XSS
    • benign text: a DB-log UNION SELECT, normal requests, an "onerror handler" log, a docs URL containing "path-traversal", sleep(5) in a worker log
    • sustained (one IP ≥ threshold) gives high and actionable; scattered scanners stay medium
    • 10 prompt-injection phrasings detected; 6 look-alikes not detected ("please approve this PR", system:serviceaccount, "You are now logged in", …); hidden Unicode; emoji joiner and BOM not flagged; diffs and tool output scanned, our own output not; sanitising
    • red-team runs through the real agent loop with scripted Claude:
      • an injected log means run_kubectl delete namespace is refused (never executed), the prompt is wrapped as untrusted with a warning, and the incident becomes security
      • injection arriving in query_logs output locks mid-incident, restart_nginx is refused, and the tool result carries the warning
      • hidden characters never reach the model
      • a clean incident is unaffected and still remediates
    • tool policy (code-review read-only, lock allows reporting but never approval), code-review flagging without re-typing, escalation reason, untrusted rules in every system prompt
  • Full suite: 1133 passed, 4 skipped. Flake8 critical: 0. Bandit -ll clean, including Trojan Source (B613): test fixtures use \uXXXX escapes, never literal hidden characters. mkdocs build --strict plus link check: 0 errors.

Docs

docs/THREAT_DETECTION.md gains "Injection attacks against your applications" and "Prompt injection against the agent" sections. .env.example adds THREAT_INJECTION_THRESHOLD.

🤖 Generated with Claude Code

…rompt injection

Injection attacks against applications (extends #166):
- sql_injection, command_injection, log4shell, path_traversal,
  ssrf_metadata, xss and template_injection rules, matched on the
  twice URL-decoded line; most only apply to HTTP request lines so plain
  DB query logs don't trigger.
- A single probe is medium (scanners hit every public site); at
  THREAT_INJECTION_THRESHOLD (default 5) attempts from one source IP the
  finding is high ("sustained") and the incident becomes a security
  incident.

Prompt injection against the agent (#118):
- Detection: prompt_injection (instruction overrides, role switches,
  chat-template tokens, requests to call the agent's own tools,
  approve/merge requests aimed at bots, "don't tell security") and
  hidden_unicode (zero-width, bidi-override, tag characters), MITRE
  ATLAS AML.T0051. Incident data, code-review diffs and every tool
  result are scanned.
- Invisible characters are stripped before anything reaches the model;
  incident data and tool output are wrapped in
  <untrusted_incident_data>/<untrusted_tool_output> with a warning when
  an injection was found, and every system prompt states that this
  content is evidence, never instructions.
- Once an injection is found, mutating tools are refused for the rest of
  the incident (state kept in the checkpointed context); suggest_fix,
  notify_slack and post_code_review remain, but post_code_review can
  never APPROVE while locked. Code-review incidents can only post their
  review, never change infrastructure.
- Escalation reason `prompt_injection`; the code-review prompt treats an
  injection attempt as REQUEST_CHANGES.

Fixes #118

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@OkiriGabriel
OkiriGabriel merged commit e3b643f into feat/threat-detection-166 Nov 14, 2026
2 checks passed
OkiriGabriel added a commit that referenced this pull request Nov 21, 2026
Land #171 on main: injection-attack detection and prompt-injection hardening (#118)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant