feat(ai_guard): scan agent instruction files for injection directives (#146)#155
Merged
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…orrules/.cursor/rules) (#146)
This was referenced Jun 16, 2026
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.
Closes #146.
What
Static-scan repo-local agent instruction files for high-signal exec / prompt-injection directives. Agentic coding assistants treat these files as trusted configuration, so an attacker who lands a malicious
CLAUDE.md/AGENTS.md/.cursorrulesin a repo can embed directives the agent will follow (taxonomy D2.1). Detection/posture only — advisory findings, never a hook block (avoids false-positive friction on legitimate instruction files).Scanned files → parser
CLAUDE.md(project +~/.claude/CLAUDE.md)AGENTS.md(project).cursorrules,.cursor/rules/*.github/copilot-instructions.mdis deferred (no Copilot parser yet) — tracked separately.How
AiGuardReason::InstructionFileDirective { directive_kind, path, snippet }(sigil-core), kindsFetchPipe/Destructive/Obfuscation/OverrideMarker. Rubric weights 3.0 / 3.0 / 2.5 / 2.0 (16 → 20 kinds). Posture-only.instruction_scanmodule (single source, called by per-tool parsers, mirrorsmcp_scan). Four line-oriented detectors:curl/wget … | (sudo) sh,bash <(curl …),sh -c "$(curl …)",… | python. Checked before generic destructive sorm -rf /; curl x | shclassifies as the higher-signal FetchPipe.rubric::first_destructive_pattern(rm -rf, dd, mkfs, …) for non-fetch lines.base64 -d/atob(/evalin execution context (bareeval/base64 prose excluded).read_text_optional(NotFound/empty/whitespace → None, 4 MiB scan cap, defensive: a read error on one instruction file logs + skips, never aborts the whole assess).discover_claude_reposunionsCLAUDE.md/AGENTS.mdmarkers; newdiscover_codex_repos(.codex/config.tomlORAGENTS.md); newdiscover_cursor_repos(.cursor/mcp.jsonOR.cursorrulesOR.cursor/rulesdir). Used at both boot and reload — so an instruction-file-only repo is discovered.WatchTargets for the instruction files, including a dedicated.cursor/rules/*glob target (a glob, not the bare directory, so the normalizer keeps child-file events;watched_paths()returns the dir for the dispatcher'sstarts_withrouting).Tests
~20 new tests: detector positives (fetch-pipe variants, override phrases, exec-context obfuscation), benign-file negative (no FP on a normal guide with a URL),
rm; curl|sh→ FetchPipe classification, one-per-category cap, continuation join, snippet cap, per-parser scanning, instruction-only-repo discovery, the.cursor/rules/*glob-matches-child / bare-dir-does-not matcher proof, rubric weight/bucket, serde round-trip. Full suite green;clippy -D warningsclean.Out of scope (follow-ups)
.github/copilot-instructions.md(no parser)..cursor/rules/<subdir>/*(flat-only in v1 — scanner and watch both flat; tracked separately).🤖 Generated with Claude Code