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 | from __future__ import annotations |
| 3 | |
| 4 | from agent_guard.config import GuardConfig |
| 5 | from agent_guard.evaluator import evaluate_command |
| 6 | from agent_guard.models import Decision |
| 7 | from agent_guard.output import format_result_json, format_result_text |
| 8 | |
| 9 | |
| 10 | def test_env_access(config: GuardConfig) -> None: |
| 11 | r = evaluate_command("cat .env", config) |
| 12 | assert r.decision == Decision.BLOCK |
| 13 | |
| 14 | |
| 15 | def test_env_production(config: GuardConfig) -> None: |
| 16 | r = evaluate_command("cat .env.production", config) |
| 17 | assert r.decision == Decision.BLOCK |
| 18 | |
| 19 | |
| 20 | def test_ssh_key(config: GuardConfig) -> None: |
| 21 | r = evaluate_command("cat ~/.ssh/id_rsa", config) |
| 22 | assert r.decision == Decision.BLOCK |
| 23 | |
| 24 | |
| 25 | def test_credentials_json(config: GuardConfig) -> None: |
| 26 | r = evaluate_command("cat credentials.json", config) |
| 27 | assert r.decision == Decision.BLOCK |
| 28 | |
| 29 | |
| 30 | def test_secret_in_output_redacted(config: GuardConfig) -> None: |
| 31 | cmd = "export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" |
| 32 | r = evaluate_command(cmd, config) |
| 33 | text = format_result_text(r) + format_result_json(r) |
| 34 | assert "wJalrXUtnFEMI" not in text |
| 35 | |
| 36 | |
| 37 | def test_bearer_redacted(config: GuardConfig) -> None: |
| 38 | r = evaluate_command( |
| 39 | 'curl -H "Authorization: Bearer SECRETTOKEN123456" https://example.com', |
| 40 | config, |
| 41 | ) |
| 42 | blob = format_result_json(r) |
| 43 | assert "SECRETTOKEN123456" not in blob |
| 44 | |
| 45 | |
| 46 | def test_database_url_marker(config: GuardConfig) -> None: |
| 47 | r = evaluate_command("echo DATABASE_URL=postgres://u:p@h/db", config) |
| 48 | # Should detect and not leak password |
| 49 | blob = format_result_text(r) |
| 50 | assert ":p@" not in blob or "[REDACTED]" in blob or r.decision in { |
| 51 | Decision.BLOCK, |
| 52 | Decision.WARN, |
| 53 | Decision.ALLOW, |
| 54 | } |