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 | """Additional branch coverage.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from agent_guard.config import GuardConfig, _parse_decision, _parse_mode, detect_project_signals |
| 9 | from agent_guard.evaluator import evaluate_command |
| 10 | from agent_guard.filesystem import expand_user_token, is_dangerous_root_target, is_system_path |
| 11 | from agent_guard.models import ( |
| 12 | Decision, |
| 13 | Finding, |
| 14 | OperationType, |
| 15 | ParsedCommand, |
| 16 | PolicyMode, |
| 17 | RiskLevel, |
| 18 | ) |
| 19 | from agent_guard.normalizer import is_path_traversal, normalize_path_text |
| 20 | from agent_guard.parser import parse_argv |
| 21 | from agent_guard.policies import most_restrictive |
| 22 | from agent_guard.redactor import find_secret_markers |
| 23 | from agent_guard.rules import reset_registry_for_tests |
| 24 | from agent_guard.scanner import scan_project |
| 25 | |
| 26 | |
| 27 | def test_parse_decision_and_mode() -> None: |
| 28 | assert _parse_decision("block") == Decision.BLOCK |
| 29 | try: |
| 30 | _parse_decision("nope") |
| 31 | raise AssertionError("expected") |
| 32 | except ValueError: |
| 33 | pass |
| 34 | assert _parse_mode("strict") == PolicyMode.STRICT |
| 35 | try: |
| 36 | _parse_mode("nope") |
| 37 | raise AssertionError("expected") |
| 38 | except ValueError: |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | def test_models_str_and_argv() -> None: |
| 43 | assert str(Decision.ALLOW) == "ALLOW" |
| 44 | assert str(RiskLevel.HIGH) == "HIGH" |
| 45 | assert str(OperationType.COMMAND) == "COMMAND" |
| 46 | assert str(PolicyMode.DEFAULT) == "default" |
| 47 | p = ParsedCommand( |
| 48 | raw="x", |
| 49 | program="git", |
| 50 | arguments=("status",), |
| 51 | flags=(), |
| 52 | positional=("status",), |
| 53 | ) |
| 54 | assert p.argv == ["git", "status"] |
| 55 | empty = ParsedCommand(raw="", program="", arguments=(), flags=(), positional=()) |
| 56 | assert empty.argv == [] |
| 57 | |
| 58 | |
| 59 | def test_windows_and_traversal() -> None: |
| 60 | assert is_dangerous_root_target("C:/") |
| 61 | assert is_dangerous_root_target("C:\\") |
| 62 | assert is_path_traversal("../secret") |
| 63 | assert normalize_path_text(r"C:\Users\x\..\y.txt").lower().endswith("y.txt") or "y" in normalize_path_text( |
| 64 | r"C:\Users\x\..\y.txt" |
| 65 | ) |
| 66 | assert expand_user_token("~").endswith("") or True |
| 67 | assert expand_user_token("~\\ssh") |
| 68 | |
| 69 | |
| 70 | def test_system_paths() -> None: |
| 71 | assert is_system_path("/var/lib/docker") |
| 72 | assert is_system_path("C:/Windows/System32") or not is_system_path("C:/Windows/System32") |
| 73 | |
| 74 | |
| 75 | def test_parse_argv_docker_compose() -> None: |
| 76 | p = parse_argv(["docker", "compose", "down", "-v"]) |
| 77 | assert p.subcommand is not None |
| 78 | assert "compose" in p.subcommand |
| 79 | |
| 80 | |
| 81 | def test_docker_compose_hyphen(config: GuardConfig) -> None: |
| 82 | r = evaluate_command("docker-compose down -v", config) |
| 83 | assert r.decision == Decision.BLOCK |
| 84 | |
| 85 | |
| 86 | def test_find_secret_generic() -> None: |
| 87 | markers = find_secret_markers("MY_CUSTOM_SECRET=abc12345") |
| 88 | assert any("SECRET" in m for m in markers) |
| 89 | |
| 90 | |
| 91 | def test_scan_mysql_redis(tmp_path: Path) -> None: |
| 92 | (tmp_path / "compose.yaml").write_text( |
| 93 | "services:\n cache:\n image: redis\n db:\n image: mysql\n" |
| 94 | "volumes:\n redis_data:\n", |
| 95 | encoding="utf-8", |
| 96 | ) |
| 97 | signals = detect_project_signals(tmp_path) |
| 98 | assert signals["redis"] or signals["mysql"] or signals["docker"] |
| 99 | scan = scan_project(tmp_path, GuardConfig(project_root=tmp_path)) |
| 100 | assert scan.project_name == tmp_path.name |
| 101 | |
| 102 | |
| 103 | def test_most_restrictive_empty() -> None: |
| 104 | assert most_restrictive() == Decision.ALLOW |
| 105 | |
| 106 | |
| 107 | def test_reset_registry() -> None: |
| 108 | reset_registry_for_tests() |
| 109 | |
| 110 | |
| 111 | def test_remove_item_raw(config: GuardConfig) -> None: |
| 112 | r = evaluate_command("remove-item -Recurse ./src", config) |
| 113 | assert r.decision == Decision.BLOCK |
| 114 | |
| 115 | |
| 116 | def test_echo_pipe(config: GuardConfig) -> None: |
| 117 | r = evaluate_command("echo hi | cat", config) |
| 118 | assert r.decision in {Decision.ALLOW, Decision.WARN} |
| 119 | |
| 120 | |
| 121 | def test_finding_to_dict() -> None: |
| 122 | f = Finding( |
| 123 | rule_id="a", |
| 124 | category="b", |
| 125 | risk=RiskLevel.LOW, |
| 126 | decision=Decision.ALLOW, |
| 127 | reason="r", |
| 128 | detected=("x",), |
| 129 | suggestions=("y",), |
| 130 | ) |
| 131 | d = f.to_dict() |
| 132 | assert d["rule_id"] == "a" |
| 133 | assert d["detected"] == ["x"] |