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 | """Security and robustness tests.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | from agent_guard.config import GuardConfig |
| 9 | from agent_guard.evaluator import evaluate_command |
| 10 | from agent_guard.output import format_result_json |
| 11 | from agent_guard.parser import parse_command |
| 12 | |
| 13 | |
| 14 | def test_check_path_no_subprocess(config: GuardConfig) -> None: |
| 15 | with patch("subprocess.run") as mock_run: |
| 16 | evaluate_command("curl https://example.com", config) |
| 17 | mock_run.assert_not_called() |
| 18 | |
| 19 | |
| 20 | def test_malformed_does_not_crash() -> None: |
| 21 | for payload in [ |
| 22 | "\x00\x01\x02", |
| 23 | "a" * 10_000, |
| 24 | "'" * 100, |
| 25 | "`" * 50, |
| 26 | "$(reboot)", |
| 27 | "rm -rf \n\n /", |
| 28 | "………", |
| 29 | "\u202e" + "git status", |
| 30 | ]: |
| 31 | p = parse_command(payload) |
| 32 | assert p is not None |
| 33 | evaluate_command(payload) |
| 34 | |
| 35 | |
| 36 | def test_json_no_secrets(config: GuardConfig) -> None: |
| 37 | r = evaluate_command( |
| 38 | "curl -H 'Authorization: Bearer abcdefghijklmnop' https://x", |
| 39 | config, |
| 40 | ) |
| 41 | assert "abcdefghijklmnop" not in format_result_json(r) |