|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Governance policy: script allowlist, forbidden paths, network policy, |
| 7 | +risk heuristics and execution budget.""" |
| 8 | +import posixpath |
| 9 | +from dataclasses import dataclass, field |
| 10 | + |
| 11 | +DEFAULT_ALLOWED_SCRIPTS = ( |
| 12 | + "parse_diff.py", |
| 13 | + "check_security.py", |
| 14 | + "check_async_leak.py", |
| 15 | + "check_db_lifecycle.py", |
| 16 | + "check_tests_missing.py", |
| 17 | + "check_secrets.py", |
| 18 | +) |
| 19 | + |
| 20 | +NETWORK_TOOLS = frozenset({"curl", "wget", "pip", "pip3", "ssh", "scp", "nc", "git", "apt", "apt-get"}) |
| 21 | +RISK_TOKENS = frozenset({"sudo", "docker", "chmod", "chown", "mount", "rm", "mkfs"}) |
| 22 | +PYTHON_EXECUTABLES = frozenset({"python", "python3"}) |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class GovernanceDecision: |
| 27 | + """Outcome of one governance check.""" |
| 28 | + |
| 29 | + target: str |
| 30 | + decision: str # allow | deny | needs_human_review |
| 31 | + rule: str = "" |
| 32 | + reason: str = "" |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class GovernanceEngine: |
| 37 | + """Stateful policy engine; deny/needs_human_review must never reach the sandbox.""" |
| 38 | + |
| 39 | + allowed_scripts: tuple = DEFAULT_ALLOWED_SCRIPTS |
| 40 | + max_runs: int = 20 |
| 41 | + max_sandbox_seconds: float = 300.0 |
| 42 | + _runs: int = field(default=0, init=False) |
| 43 | + _elapsed: float = field(default=0.0, init=False) |
| 44 | + |
| 45 | + def record_run(self, duration_s: float) -> None: |
| 46 | + self._runs += 1 |
| 47 | + self._elapsed += max(0.0, duration_s) |
| 48 | + |
| 49 | + def _check_budget(self, target: str): |
| 50 | + if self._runs >= self.max_runs or self._elapsed >= self.max_sandbox_seconds: |
| 51 | + return GovernanceDecision(target, "deny", "budget_exceeded", |
| 52 | + f"budget: {self._runs} runs / {self._elapsed:.1f}s used") |
| 53 | + return None |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def _check_paths(target: str, argv): |
| 57 | + for arg in argv: |
| 58 | + if arg.startswith("/") or arg.startswith("~") or ".." in arg: |
| 59 | + return GovernanceDecision(target, "deny", "forbidden_path", |
| 60 | + f"path escapes workspace: {arg!r}") |
| 61 | + return None |
| 62 | + |
| 63 | + def check_script(self, script: str, argv) -> GovernanceDecision: |
| 64 | + target = f"{script} {' '.join(argv)}".strip() |
| 65 | + blocked = self._check_budget(target) or self._check_paths(target, argv) |
| 66 | + if blocked: |
| 67 | + return blocked |
| 68 | + if posixpath.basename(script) not in self.allowed_scripts: |
| 69 | + return GovernanceDecision(target, "deny", "script_allowlist", |
| 70 | + f"script {script!r} is not allowlisted") |
| 71 | + return GovernanceDecision(target, "allow") |
| 72 | + |
| 73 | + def check_command(self, command: str) -> GovernanceDecision: |
| 74 | + tokens = command.split() |
| 75 | + if not tokens: |
| 76 | + return GovernanceDecision(command, "deny", "empty_command", "empty command") |
| 77 | + head = posixpath.basename(tokens[0]) |
| 78 | + if head in NETWORK_TOOLS or any(posixpath.basename(t) in NETWORK_TOOLS for t in tokens): |
| 79 | + return GovernanceDecision(command, "deny", "network_policy", |
| 80 | + "non-whitelisted network access") |
| 81 | + if head in RISK_TOKENS or any(posixpath.basename(t) in RISK_TOKENS for t in tokens): |
| 82 | + return GovernanceDecision(command, "needs_human_review", "risk_command", |
| 83 | + "high-risk command requires human review") |
| 84 | + if head in PYTHON_EXECUTABLES and len(tokens) >= 2: |
| 85 | + return self.check_script(tokens[1], tokens[2:]) |
| 86 | + return GovernanceDecision(command, "needs_human_review", "unknown_command", |
| 87 | + f"executable {head!r} is not recognized") |
0 commit comments