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 | """Safe command execution after policy evaluation.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import subprocess |
| 7 | import sys |
| 8 | from collections.abc import Sequence |
| 9 | from dataclasses import dataclass |
| 10 | |
| 11 | from agent_guard.config import GuardConfig |
| 12 | from agent_guard.evaluator import evaluate_argv |
| 13 | from agent_guard.models import Decision, GuardResult, OperationType, RiskLevel |
| 14 | from agent_guard.output import confirm_warn |
| 15 | from agent_guard.redactor import redact |
| 16 | |
| 17 | |
| 18 | @dataclass |
| 19 | class RunOutcome: |
| 20 | result: GuardResult |
| 21 | executed: bool |
| 22 | exit_code: int |
| 23 | message: str | None = None |
| 24 | |
| 25 | |
| 26 | def run_command( |
| 27 | argv: Sequence[str], |
| 28 | config: GuardConfig, |
| 29 | *, |
| 30 | allow_risk: bool = False, |
| 31 | yes: bool = False, |
| 32 | json_mode: bool = False, |
| 33 | ) -> RunOutcome: |
| 34 | """ |
| 35 | Evaluate then optionally execute *argv* with ``shell=False``. |
| 36 | |
| 37 | BLOCK is never bypassed by ``allow_risk``. |
| 38 | WARN requires interactive confirmation unless ``allow_risk`` or ``yes``. |
| 39 | """ |
| 40 | args = list(argv) |
| 41 | if not args: |
| 42 | return RunOutcome( |
| 43 | result=GuardResult( |
| 44 | decision=Decision.BLOCK, |
| 45 | risk=RiskLevel.LOW, |
| 46 | operation=OperationType.UNKNOWN, |
| 47 | rule_id=None, |
| 48 | reason="No command provided.", |
| 49 | ), |
| 50 | executed=False, |
| 51 | exit_code=30, |
| 52 | message="No command provided.", |
| 53 | ) |
| 54 | |
| 55 | result = evaluate_argv(args, config) |
| 56 | |
| 57 | if result.decision == Decision.BLOCK: |
| 58 | return RunOutcome( |
| 59 | result=result, |
| 60 | executed=False, |
| 61 | exit_code=20, |
| 62 | message="Blocked by agent-guard policy.", |
| 63 | ) |
| 64 | |
| 65 | if result.decision == Decision.WARN: |
| 66 | if allow_risk or yes: |
| 67 | pass # explicit override for WARN only |
| 68 | elif not sys.stdin.isatty(): |
| 69 | return RunOutcome( |
| 70 | result=result, |
| 71 | executed=False, |
| 72 | exit_code=10, |
| 73 | message="Non-interactive execution denied. Use --allow-risk explicitly if intentional.", |
| 74 | ) |
| 75 | else: |
| 76 | if json_mode: |
| 77 | # Still require confirmation in interactive JSON mode |
| 78 | pass |
| 79 | approved = confirm_warn(result) |
| 80 | if not approved: |
| 81 | return RunOutcome( |
| 82 | result=result, |
| 83 | executed=False, |
| 84 | exit_code=10, |
| 85 | message="Aborted by user.", |
| 86 | ) |
| 87 | |
| 88 | # ALLOW or approved WARN |
| 89 | try: |
| 90 | completed = subprocess.run(args, shell=False, check=False) |
| 91 | except FileNotFoundError: |
| 92 | return RunOutcome( |
| 93 | result=result, |
| 94 | executed=False, |
| 95 | exit_code=30, |
| 96 | message=redact(f"Executable not found: {args[0]}"), |
| 97 | ) |
| 98 | except OSError as exc: |
| 99 | return RunOutcome( |
| 100 | result=result, |
| 101 | executed=False, |
| 102 | exit_code=30, |
| 103 | message=redact(str(exc)), |
| 104 | ) |
| 105 | |
| 106 | return RunOutcome( |
| 107 | result=result, |
| 108 | executed=True, |
| 109 | exit_code=int(completed.returncode), |
| 110 | message=None, |
| 111 | ) |