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 | """Network command risk detection.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import re |
| 7 | from urllib.parse import urlparse |
| 8 | |
| 9 | from agent_guard.config import GuardConfig |
| 10 | from agent_guard.models import Decision, Finding, ParsedCommand, RiskLevel |
| 11 | from agent_guard.risk import SCORE_PRODUCTION_NETWORK |
| 12 | from agent_guard.rules import RuleDefinition, register_rule |
| 13 | |
| 14 | NETWORK_PROGRAMS = { |
| 15 | "curl", |
| 16 | "wget", |
| 17 | "invoke-webrequest", |
| 18 | "iwr", |
| 19 | "nc", |
| 20 | "netcat", |
| 21 | "ncat", |
| 22 | "ssh", |
| 23 | "scp", |
| 24 | "rsync", |
| 25 | "ftp", |
| 26 | "sftp", |
| 27 | } |
| 28 | |
| 29 | _URL_RE = re.compile(r"https?://[^\s\"']+", re.IGNORECASE) |
| 30 | _PROD_HINT = re.compile(r"(?i)\b(prod|production|live)\b") |
| 31 | |
| 32 | |
| 33 | def _prog(parsed: ParsedCommand) -> str: |
| 34 | prog = parsed.program.lower().replace("\\", "/").split("/")[-1] |
| 35 | if prog.endswith(".exe"): |
| 36 | prog = prog[:-4] |
| 37 | return prog |
| 38 | |
| 39 | |
| 40 | def _extract_hosts(text: str) -> list[str]: |
| 41 | hosts: list[str] = [] |
| 42 | for match in _URL_RE.finditer(text): |
| 43 | try: |
| 44 | host = urlparse(match.group(0)).hostname |
| 45 | if host: |
| 46 | hosts.append(host.lower()) |
| 47 | except ValueError: |
| 48 | continue |
| 49 | # ssh user@host |
| 50 | for match in re.finditer(r"(?i)(?:^|[\s@])([A-Za-z0-9._-]+\.[A-Za-z]{2,})(?:\s|$)", text): |
| 51 | hosts.append(match.group(1).lower()) |
| 52 | return hosts |
| 53 | |
| 54 | |
| 55 | @register_rule( |
| 56 | RuleDefinition( |
| 57 | id="network.production", |
| 58 | category="production_network", |
| 59 | risk=RiskLevel.HIGH, |
| 60 | default_decision=Decision.BLOCK, |
| 61 | description="Flags high-risk network calls to protected/production hosts.", |
| 62 | ) |
| 63 | ) |
| 64 | def rule_network(parsed: ParsedCommand, config: GuardConfig) -> list[Finding]: |
| 65 | prog = _prog(parsed) |
| 66 | if prog not in NETWORK_PROGRAMS: |
| 67 | return [] |
| 68 | |
| 69 | findings: list[Finding] = [] |
| 70 | flags = {f.lower() for f in parsed.flags} |
| 71 | args_lower = [a.lower() for a in parsed.arguments] |
| 72 | hosts = _extract_hosts(parsed.raw) |
| 73 | protected = {h.lower() for h in config.protected_hosts} |
| 74 | |
| 75 | # Destructive HTTP methods |
| 76 | method: str | None = None |
| 77 | if "-x" in [f.lower() for f in parsed.flags] or "--request" in flags: |
| 78 | for i, arg in enumerate(parsed.arguments): |
| 79 | if arg.lower() in {"-x", "--request"} and i + 1 < len(parsed.arguments): |
| 80 | method = parsed.arguments[i + 1].upper() |
| 81 | break |
| 82 | for a in args_lower: |
| 83 | if a in {"delete", "put", "patch"}: |
| 84 | method = a.upper() |
| 85 | |
| 86 | risky_method = method in {"DELETE", "PUT", "PATCH"} |
| 87 | |
| 88 | for host in hosts: |
| 89 | is_protected = host in protected or any( |
| 90 | host.endswith("." + p) or host == p for p in protected |
| 91 | ) |
| 92 | is_prod = bool(_PROD_HINT.search(host)) or "production." in host or host.startswith("prod.") |
| 93 | |
| 94 | if is_protected or (is_prod and risky_method): |
| 95 | findings.append( |
| 96 | Finding( |
| 97 | rule_id="network.production_host", |
| 98 | category="production_network", |
| 99 | risk=RiskLevel.CRITICAL if risky_method else RiskLevel.HIGH, |
| 100 | decision=Decision.BLOCK, |
| 101 | reason="High-risk network request targeting a protected or production host.", |
| 102 | target=host, |
| 103 | score_delta=SCORE_PRODUCTION_NETWORK + (20 if risky_method else 0), |
| 104 | detected=(method or prog, host), |
| 105 | suggestions=("Use staging endpoints or add an explicit allow policy.",), |
| 106 | ) |
| 107 | ) |
| 108 | elif risky_method and is_prod: |
| 109 | findings.append( |
| 110 | Finding( |
| 111 | rule_id="network.destructive_method", |
| 112 | category="production_network", |
| 113 | risk=RiskLevel.HIGH, |
| 114 | decision=Decision.BLOCK, |
| 115 | reason=f"HTTP {method} against a production-like host.", |
| 116 | target=host, |
| 117 | score_delta=SCORE_PRODUCTION_NETWORK, |
| 118 | ) |
| 119 | ) |
| 120 | |
| 121 | if risky_method and not findings: |
| 122 | findings.append( |
| 123 | Finding( |
| 124 | rule_id="network.destructive_method", |
| 125 | category="production_network", |
| 126 | risk=RiskLevel.HIGH, |
| 127 | decision=Decision.WARN, |
| 128 | reason=f"HTTP {method} can mutate or delete remote resources.", |
| 129 | score_delta=45, |
| 130 | ) |
| 131 | ) |
| 132 | |
| 133 | if prog in {"nc", "netcat", "ncat"}: |
| 134 | findings.append( |
| 135 | Finding( |
| 136 | rule_id="network.netcat", |
| 137 | category="production_network", |
| 138 | risk=RiskLevel.MEDIUM, |
| 139 | decision=Decision.WARN, |
| 140 | reason="netcat can open arbitrary network connections or reverse shells.", |
| 141 | score_delta=40, |
| 142 | ) |
| 143 | ) |
| 144 | |
| 145 | if prog == "ssh" and any( |
| 146 | _PROD_HINT.search(h) or h in protected for h in hosts |
| 147 | ) and not any(f.rule_id == "network.production_host" for f in findings): |
| 148 | findings.append( |
| 149 | Finding( |
| 150 | rule_id="network.ssh_production", |
| 151 | category="production_network", |
| 152 | risk=RiskLevel.HIGH, |
| 153 | decision=Decision.WARN, |
| 154 | reason="SSH to a production-like host.", |
| 155 | target=hosts[0] if hosts else None, |
| 156 | score_delta=50, |
| 157 | ) |
| 158 | ) |
| 159 | |
| 160 | # Benign curl to example.com etc. → no extra findings (ALLOW by default) |
| 161 | return findings |