-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_safety.py
More file actions
116 lines (94 loc) · 3.28 KB
/
Copy pathcommand_safety.py
File metadata and controls
116 lines (94 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from __future__ import annotations
import os
import re
from dataclasses import dataclass, field
from typing import List, Sequence, Literal
RiskLevel = Literal["safe", "caution", "block"]
# Commands that routinely perform irreversible mutations.
HIGH_RISK_COMMANDS = {
"rm",
"rmdir",
"del",
"erase",
"mv",
"move",
"cp",
"copy",
"dd",
"chmod",
"chown",
"kill",
"pkill",
"killall",
"shutdown",
"reboot",
"mkfs",
"diskpart",
"format",
"docker",
"kubectl",
"systemctl",
}
# Shell metacharacters that indicate control flow or redirection.
BLOCKING_PATTERNS = [
r";",
r"\|\|",
r"&&",
r"\|",
r"`",
r"\$\(",
]
CAUTION_PATTERNS = [
r">",
r"<",
]
FORCE_FLAGS = {"-f", "--force", "-rf", "-fr", "/f"}
RECURSIVE_FLAGS = {"-r", "-R", "--recursive", "/s"}
ROOT_PATHS = {"/", "C:\\", "C:/"}
SHELL_COMMANDS = {"sh", "bash", "zsh", "fish", "cmd", "cmd.exe", "powershell", "pwsh"}
SHELL_EXECUTE_FLAGS = {"-c", "/c"}
@dataclass
class CommandSafetyResult:
risk: RiskLevel = "safe"
reasons: List[str] = field(default_factory=list)
def downgrade_to_block(self, reason: str) -> None:
self.risk = "block"
self.reasons.append(reason)
def elevate_to_caution(self, reason: str) -> None:
if self.risk != "block":
self.risk = "caution"
self.reasons.append(reason)
def analyze_command(cmd: str, args: Sequence[str]) -> CommandSafetyResult:
result = CommandSafetyResult()
command_name = os.path.basename(cmd).lower()
full_text = " ".join([cmd] + list(args))
for pattern in BLOCKING_PATTERNS:
if re.search(pattern, full_text):
result.downgrade_to_block("Shell control operators are not permitted in agent-managed commands.")
break
if result.risk != "block":
for pattern in CAUTION_PATTERNS:
if re.search(pattern, full_text):
result.elevate_to_caution("Shell redirection was detected; review carefully before execution.")
break
if command_name in HIGH_RISK_COMMANDS:
result.elevate_to_caution(f"Command '{command_name}' is considered high risk.")
lowered_args = {arg.lower() for arg in args}
if FORCE_FLAGS.intersection(lowered_args):
result.elevate_to_caution("Force flag detected; this can bypass safety prompts.")
if RECURSIVE_FLAGS.intersection(lowered_args):
result.elevate_to_caution("Recursive flag detected; review target paths carefully.")
if command_name in SHELL_COMMANDS and SHELL_EXECUTE_FLAGS.intersection(lowered_args):
result.downgrade_to_block("Shell execution with '-c' is disabled for safety.")
for arg in args:
normalized = arg.strip('"').strip("'")
if normalized in ROOT_PATHS:
result.elevate_to_caution("Command targets the filesystem root.")
if normalized.startswith(("/", "~/", "C:/", "C:\\")) and command_name in {"rm", "del", "erase", "rmdir"}:
result.elevate_to_caution("Deleting from an absolute path is high risk.")
return result
def dry_run_required(result: CommandSafetyResult) -> bool:
env = os.getenv("AGENT_HIGH_RISK_DRY_RUN", "").strip().lower()
if env in {"1", "true", "yes"} and result.risk != "safe":
return True
return False