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.normalizer import ( |
| 5 | is_path_traversal, |
| 6 | normalize_command, |
| 7 | normalize_path_text, |
| 8 | normalize_whitespace, |
| 9 | strip_ansi, |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | def test_normalize_whitespace() -> None: |
| 14 | assert normalize_whitespace(" a b\tc ") == "a b c" |
| 15 | |
| 16 | |
| 17 | def test_strip_ansi() -> None: |
| 18 | assert strip_ansi("\x1b[31mred\x1b[0m") == "red" |
| 19 | |
| 20 | |
| 21 | def test_normalize_command_strips() -> None: |
| 22 | assert normalize_command(" git status ") == "git status" |
| 23 | assert "\x1b" not in normalize_command("\x1b[32mgit\x1b[0m status") |
| 24 | |
| 25 | |
| 26 | def test_normalize_empty() -> None: |
| 27 | assert normalize_command("") == "" |
| 28 | assert normalize_command(None) == "" # type: ignore[arg-type] |
| 29 | |
| 30 | |
| 31 | def test_path_dotdot() -> None: |
| 32 | assert normalize_path_text("./src/../.env") == ".env" |
| 33 | |
| 34 | |
| 35 | def test_path_traversal_detected() -> None: |
| 36 | assert is_path_traversal("../.env") or ".." in normalize_path_text("../.env") |
| 37 | |
| 38 | |
| 39 | def test_windows_path_normalize() -> None: |
| 40 | result = normalize_path_text(r"C:\Users\demo\file.txt") |
| 41 | assert "Users" in result or "users" in result.lower() or result.lower().startswith("c:") |
| 42 | |
| 43 | |
| 44 | def test_case_preserving() -> None: |
| 45 | assert "Status" in normalize_command("git Status") or normalize_command("git Status") == "git Status" |