AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | """Secret file and secret-content detection (never prints values).""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from agent_guard.config import GuardConfig |
| 9 | from agent_guard.filesystem import is_sensitive_filename, matches_protected_path |
| 10 | from agent_guard.models import Decision, Finding, ParsedCommand, RiskLevel |
| 11 | from agent_guard.redactor import find_secret_markers |
| 12 | from agent_guard.risk import SCORE_SECRET_ACCESS |
| 13 | from agent_guard.rules import RuleDefinition, register_rule |
| 14 | |
| 15 | SECRET_FILE_NAMES = { |
| 16 | ".env", |
| 17 | "credentials.json", |
| 18 | "service-account.json", |
| 19 | "id_rsa", |
| 20 | "id_ed25519", |
| 21 | } |
| 22 | |
| 23 | |
| 24 | @register_rule( |
| 25 | RuleDefinition( |
| 26 | id="secrets.file_access", |
| 27 | category="secret_access", |
| 28 | risk=RiskLevel.HIGH, |
| 29 | default_decision=Decision.BLOCK, |
| 30 | description="Detects access to common secret files (.env, PEM keys, cloud credentials).", |
| 31 | ) |
| 32 | ) |
| 33 | def rule_secret_files(parsed: ParsedCommand, config: GuardConfig) -> list[Finding]: |
| 34 | findings: list[Finding] = [] |
| 35 | for target in parsed.positional: |
| 36 | sensitive = is_sensitive_filename(target) |
| 37 | matched = matches_protected_path(target, config.protected_paths, config.project_root) |
| 38 | if not sensitive and not matched: |
| 39 | # Check ~/.ssh/id_rsa style |
| 40 | low = target.replace("\\", "/").lower() |
| 41 | if any( |
| 42 | x in low |
| 43 | for x in ( |
| 44 | ".ssh/", |
| 45 | ".aws/credentials", |
| 46 | "id_rsa", |
| 47 | "id_ed25519", |
| 48 | "credentials.json", |
| 49 | "service-account.json", |
| 50 | ) |
| 51 | ): |
| 52 | sensitive = True |
| 53 | if sensitive or matched: |
| 54 | findings.append( |
| 55 | Finding( |
| 56 | rule_id="secrets.file_access", |
| 57 | category="secret_access", |
| 58 | risk=RiskLevel.HIGH, |
| 59 | decision=Decision.BLOCK, |
| 60 | reason="Sensitive file access detected.", |
| 61 | target=target, |
| 62 | score_delta=SCORE_SECRET_ACCESS, |
| 63 | detected=(matched or Path(target).name,), |
| 64 | suggestions=("Avoid reading secrets in agent sessions; use placeholders.",), |
| 65 | ) |
| 66 | ) |
| 67 | return findings |
| 68 | |
| 69 | |
| 70 | @register_rule( |
| 71 | RuleDefinition( |
| 72 | id="secrets.content_markers", |
| 73 | category="secret_access", |
| 74 | risk=RiskLevel.HIGH, |
| 75 | default_decision=Decision.BLOCK, |
| 76 | description="Detects secret-like environment variable names in commands (values redacted).", |
| 77 | ) |
| 78 | ) |
| 79 | def rule_secret_content(parsed: ParsedCommand, config: GuardConfig) -> list[Finding]: |
| 80 | markers = find_secret_markers(parsed.raw) |
| 81 | if not markers: |
| 82 | return [] |
| 83 | # Only alert when it looks like assignment or export of secrets |
| 84 | raw = parsed.raw |
| 85 | if not any(m in raw.upper() for m in markers): |
| 86 | return [] |
| 87 | # Heuristic: KEY=value or export KEY |
| 88 | if "=" not in raw and "export " not in raw.lower() and "bearer " not in raw.lower(): |
| 89 | # Mention-only may be noisy; still warn for explicit secret file dumps |
| 90 | if not any(x in raw for x in (".env", "credentials", "SECRET", "PASSWORD")): |
| 91 | return [] |
| 92 | |
| 93 | return [ |
| 94 | Finding( |
| 95 | rule_id="secrets.content_markers", |
| 96 | category="secret_access", |
| 97 | risk=RiskLevel.HIGH, |
| 98 | decision=Decision.BLOCK, |
| 99 | reason="Sensitive content detected.", |
| 100 | score_delta=SCORE_SECRET_ACCESS, |
| 101 | detected=tuple(markers[:10]), |
| 102 | suggestions=("Never paste live credentials into agent prompts or commands.",), |
| 103 | ) |
| 104 | ] |