HumboldtJoker commited on
Commit
b5eecdb
·
verified ·
1 Parent(s): ab666c8

Clean up nested duplicate: v2/v2/tools/guard.py

Browse files
Files changed (1) hide show
  1. v2/v2/tools/guard.py +0 -93
v2/v2/tools/guard.py DELETED
@@ -1,93 +0,0 @@
1
- """SecurityMonitor for the tool harness (Kintsugi §5c).
2
-
3
- Every subprocess the tool layer spawns goes through run_checked().
4
- The check is code, not model judgment — it cannot be reasoned away.
5
- Commands are argv lists (never shell=True), checked against blocked
6
- patterns, allowlisted binaries, and a path jail before execution.
7
- """
8
-
9
- import re
10
- import subprocess
11
- from dataclasses import dataclass
12
- from pathlib import Path
13
-
14
- # Kintsugi SecurityMonitor patterns + Rivet additions.
15
- SUSPICIOUS_PATTERNS = [
16
- r"base64\s+--decode",
17
- r"curl.*\|.*sh",
18
- r"\benv\b", r"\bprintenv\b",
19
- r">\s*/dev/tcp",
20
- r"nc\s+-e",
21
- r"chmod\s+777",
22
- r"\.ssh/authorized_keys",
23
- r"rm\s+-rf\s+/",
24
- r"\bsudo\b",
25
- r"DROP\s+DATABASE",
26
- ]
27
-
28
- # Binaries the tool harness is allowed to spawn. Nothing else runs.
29
- ALLOWED_BINARIES = {
30
- "git", "npm", "npx", "tsc", "node", "pg_dump", "psql",
31
- }
32
-
33
-
34
- class ToolSecurityError(Exception):
35
- pass
36
-
37
-
38
- @dataclass
39
- class ToolResult:
40
- ok: bool
41
- stdout: str = ""
42
- stderr: str = ""
43
- returncode: int = -1
44
- blocked_reason: str = ""
45
-
46
-
47
- def check_command(argv: list) -> str:
48
- """Return a block reason, or '' if the command is clean."""
49
- if not argv:
50
- return "empty command"
51
- binary = Path(argv[0]).name
52
- if binary not in ALLOWED_BINARIES:
53
- return f"binary '{binary}' not in tool allowlist"
54
- joined = " ".join(str(a) for a in argv)
55
- for pattern in SUSPICIOUS_PATTERNS:
56
- if re.search(pattern, joined, re.IGNORECASE):
57
- return f"matches blocked pattern: {pattern}"
58
- return ""
59
-
60
-
61
- def check_path(path: Path, roots: list) -> str:
62
- """Return a block reason unless path resolves inside an allowed root."""
63
- resolved = Path(path).resolve()
64
- for root in roots:
65
- try:
66
- resolved.relative_to(Path(root).resolve())
67
- return ""
68
- except ValueError:
69
- continue
70
- return f"path {resolved} escapes allowed roots {roots}"
71
-
72
-
73
- def run_checked(argv: list, cwd: str | None = None,
74
- timeout: int = 60) -> ToolResult:
75
- reason = check_command(argv)
76
- if reason:
77
- return ToolResult(ok=False, blocked_reason=reason)
78
- try:
79
- proc = subprocess.run(
80
- argv, cwd=cwd, capture_output=True, text=True, timeout=timeout,
81
- )
82
- except subprocess.TimeoutExpired:
83
- return ToolResult(ok=False, stderr=f"timed out after {timeout}s")
84
- except FileNotFoundError as exc:
85
- return ToolResult(ok=False, stderr=str(exc))
86
- except OSError as exc:
87
- return ToolResult(ok=False, stderr=str(exc))
88
- return ToolResult(
89
- ok=proc.returncode == 0,
90
- stdout=proc.stdout,
91
- stderr=proc.stderr,
92
- returncode=proc.returncode,
93
- )