Repository navigation
feat: detect injection attacks and harden the agent against prompt injection (#118) - #171
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
sql_injection' OR 1=1,UNION SELECT(incl./**/),pg_sleep(,xp_cmdshell,information_schema.tablescommand_injection;cat /etc/passwd,||whoami,$(curl …)log4shell${jndi:ldap://…}, obfuscated${${lower:j}ndi…}, in any line or headerpath_traversal../../../../etc/passwd,..%2f,win.ini,proc/self/environssrf_metadata169.254.169.254,metadata.google.internalxss<script>,javascript:,onerror=template_injection{{7*7}},${7*7},__globals__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)
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") andhidden_unicode(zero-width, bidi-override, tag characters). MITRE ATLAS AML.T0051, high. Scans incident data, code-review diffs and every tool result..<untrusted_incident_data>and tool results inside<untrusted_tool_output>, with aWARNING:line when an injection was found. Every system prompt states this content is evidence, never instructions.suggest_fix,notify_slackand read-only tools still work.post_code_reviewstays so malicious PRs can be flagged, but it can never APPROVE while locked, even withCODE_REVIEW_ALLOW_APPROVE.code_reviewincidents can only post their review, never call infrastructure tools.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
Testing
tests/test_injection_detection.py(57 tests):/**/-obfuscated SQL, obfuscated Log4Shell, and encoded traversal and XSSUNION SELECT, normal requests, an "onerror handler" log, a docs URL containing "path-traversal",sleep(5)in a worker logsystem:serviceaccount, "You are now logged in", …); hidden Unicode; emoji joiner and BOM not flagged; diffs and tool output scanned, our own output not; sanitisingrun_kubectl delete namespaceis refused (never executed), the prompt is wrapped as untrusted with a warning, and the incident becomes securityquery_logsoutput locks mid-incident,restart_nginxis refused, and the tool result carries the warning-llclean, including Trojan Source (B613): test fixtures use\uXXXXescapes, never literal hidden characters.mkdocs build --strictplus link check: 0 errors.Docs
docs/THREAT_DETECTION.mdgains "Injection attacks against your applications" and "Prompt injection against the agent" sections..env.exampleaddsTHREAT_INJECTION_THRESHOLD.🤖 Generated with Claude Code