|
| 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 | + |
| 7 | +"""Review policy — the Filter decision logic (issue #92, requirement 7 & 8). |
| 8 | +
|
| 9 | +Shared by two enforcement sites (plan decision #5): the framework ``ReviewGuardFilter`` on the agent |
| 10 | +path, and a direct gate in the sandbox runner on the deterministic path. Both call ``evaluate`` and |
| 11 | +must refuse to execute anything that comes back ``deny`` or ``needs_human_review``. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import os |
| 16 | +import re |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Iterable, Literal |
| 19 | + |
| 20 | +Decision = Literal["allow", "deny", "needs_human_review"] |
| 21 | + |
| 22 | +# High-risk command patterns → hard deny. |
| 23 | +_DANGEROUS = [ |
| 24 | + (re.compile(r"\brm\s+-[rfRF]"), "recursive/force delete"), |
| 25 | + (re.compile(r"\b(mkfs|shred)\b|\bdd\s+if="), "disk-destructive command"), |
| 26 | + (re.compile(r":\s*\(\s*\)\s*\{.*\}\s*;"), "fork bomb"), |
| 27 | + (re.compile(r"(curl|wget)\b[^\n|]*\|\s*(sh|bash)"), "pipe-to-shell"), |
| 28 | + (re.compile(r"\bchmod\s+-?R?\s*777\b"), "world-writable chmod"), |
| 29 | + (re.compile(r"\bsudo\b"), "privilege escalation"), |
| 30 | + (re.compile(r">\s*/dev/sd|/etc/passwd|/etc/shadow"), "write to sensitive target"), |
| 31 | +] |
| 32 | + |
| 33 | +# Sensitive roots a review must never touch (temp dirs under /var/folders are intentionally allowed). |
| 34 | +_FORBIDDEN_PATHS = ("/etc", "/root", "/boot", os.path.expanduser("~/.ssh")) |
| 35 | + |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class PolicyDecision: |
| 39 | + decision: Decision |
| 40 | + reason: str = "" |
| 41 | + category: str = "ok" # script | path | network | budget | ok |
| 42 | + |
| 43 | + @property |
| 44 | + def allowed(self) -> bool: |
| 45 | + return self.decision == "allow" |
| 46 | + |
| 47 | + def as_block(self, *, script: str = "") -> dict: |
| 48 | + return {"script": script, "decision": self.decision, "reason": self.reason, "category": self.category} |
| 49 | + |
| 50 | + |
| 51 | +class ReviewPolicy: |
| 52 | + """Decides whether a sandbox action may run. Deny/needs_human_review must NOT reach the sandbox.""" |
| 53 | + |
| 54 | + def __init__(self, network_allowlist: Iterable[str] | None = None, max_budget_sec: float = 120.0) -> None: |
| 55 | + self.network_allowlist = set(network_allowlist or ()) |
| 56 | + self.max_budget_sec = max_budget_sec |
| 57 | + |
| 58 | + def evaluate( |
| 59 | + self, |
| 60 | + *, |
| 61 | + command: str = "", |
| 62 | + touched_paths: Iterable[str] = (), |
| 63 | + network_hosts: Iterable[str] = (), |
| 64 | + budget_sec: float = 0.0, |
| 65 | + ) -> PolicyDecision: |
| 66 | + for pat, why in _DANGEROUS: |
| 67 | + if pat.search(command): |
| 68 | + return PolicyDecision("deny", f"high-risk command: {why}", "script") |
| 69 | + |
| 70 | + for p in touched_paths: |
| 71 | + ap = os.path.abspath(p) |
| 72 | + if any(ap == fp or ap.startswith(fp + os.sep) for fp in _FORBIDDEN_PATHS): |
| 73 | + return PolicyDecision("deny", f"forbidden path: {p}", "path") |
| 74 | + |
| 75 | + unlisted = [h for h in network_hosts if h not in self.network_allowlist] |
| 76 | + if unlisted: |
| 77 | + return PolicyDecision("needs_human_review", f"non-whitelisted network: {unlisted}", "network") |
| 78 | + |
| 79 | + if budget_sec and budget_sec > self.max_budget_sec: |
| 80 | + return PolicyDecision("needs_human_review", f"over budget: {budget_sec}s > {self.max_budget_sec}s", |
| 81 | + "budget") |
| 82 | + |
| 83 | + return PolicyDecision("allow") |
0 commit comments