Repository navigation
Expand file tree
/
Copy pathsecurity.py
More file actions
346 lines (300 loc) · 10.7 KB
/
Copy pathsecurity.py
File metadata and controls
346 lines (300 loc) · 10.7 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
security.py — Hardened validation for user / tool inputs
Centralises the checks that keep DevMind safe:
- Path traversal / symlink escape protection
- Command sanitization for the bash tool
- Generic input validators (length, type, encoding)
Every public helper returns `(ok: bool, message: str, value)` so callers
can surface the result to the user without raising inside the tool path.
"""
from __future__ import annotations
import os
import re
import shlex
from pathlib import Path
from config import config
from logger import get_logger
log = get_logger("security")
# ============================================================
# Path validation
# ============================================================
def validate_path(
filepath: str,
*,
base_dir: Path | None = None,
must_exist: bool = False,
allow_symlinks: bool = False,
) -> tuple[bool, str, Path | None]:
"""
Validate a filesystem path.
Rules:
- Input must be a non-empty string
- Null bytes are rejected
- Resolved path must be inside `base_dir` (defaults to cwd)
- Symlinks that escape the base_dir are rejected
Returns:
(is_valid, error_message, resolved_path)
"""
if not isinstance(filepath, str) or not filepath.strip():
return False, "Path must be a non-empty string.", None
if "\x00" in filepath:
log.warning("Null byte in path blocked")
return False, "Invalid path (null byte).", None
try:
base = (base_dir or Path(os.getcwd())).resolve()
# First, resolve without strict=True so missing files are allowed
candidate = Path(filepath)
if not candidate.is_absolute():
candidate = base / candidate
resolved = candidate.resolve()
# Block traversal using relative_to
try:
resolved.relative_to(base)
except ValueError:
log.warning(f"Path traversal attempt blocked: {filepath!r}")
return (
False,
(
"Security: Only files within the current working directory "
"can be accessed."
),
None,
)
if must_exist and not resolved.exists():
return False, f"File not found: {filepath}", None
# Detect symlink escapes even when relative_to passes
if not allow_symlinks and resolved.exists() and resolved.is_symlink():
real = resolved.resolve(strict=True)
try:
real.relative_to(base)
except ValueError:
log.warning(f"Symlink escape blocked: {filepath!r} → {real}")
return False, "Security: symlink points outside the workspace.", None
return True, "", resolved
except Exception as e:
return False, f"Invalid path: {e}", None
# ============================================================
# Command validation
# ============================================================
_SUSPICIOUS_CHAR_RUNS = re.compile(r"`.+`|\$\([^)]+\)")
_SHELL_OPERATORS = re.compile(r"&&|\|\||[;|\n]")
_ENV_ASSIGNMENT = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=")
_WRAPPERS = {
"builtin",
"command",
"env",
"exec",
"nice",
"nohup",
"setsid",
"stdbuf",
"time",
"timeout",
"xargs",
}
_WRAPPER_VALUE_OPTIONS = {
"env": {"-u", "--unset", "-C", "--chdir"},
"exec": {"-a"},
"nice": {"-n", "--adjustment"},
"stdbuf": {"-i", "-o", "-e", "--input", "--output", "--error"},
"time": {"-f", "--format", "-o", "--output"},
"timeout": {"-s", "--signal", "-k", "--kill-after"},
"xargs": {
"-a",
"--arg-file",
"-d",
"--delimiter",
"-E",
"--eof",
"-I",
"--replace",
"-L",
"--max-lines",
"-n",
"--max-args",
"-P",
"--max-procs",
"-s",
"--max-chars",
},
}
_PRIVILEGE_TOOLS = {"sudo", "su", "doas", "pkexec"}
_SHELLS = {"bash", "sh", "zsh", "ksh", "fish", "dash", "powershell", "pwsh", "cmd"}
_INLINE_CODE = {
"py": {"-c"},
"python": {"-c"},
"python3": {"-c"},
"node": {"-e", "--eval"},
"perl": {"-e"},
"ruby": {"-e"},
"php": {"-r"},
}
def _segments(command: str) -> list[str]:
return [
segment.strip()
for segment in _SHELL_OPERATORS.split(command)
if segment.strip()
]
def _split_env_string(value: str) -> list[str]:
try:
return shlex.split(value, posix=True)
except ValueError:
return value.split()
def _executable(tokens: list[str]) -> tuple[str, list[str]]:
while tokens:
if _ENV_ASSIGNMENT.match(tokens[0]):
tokens = tokens[1:]
continue
head = Path(tokens[0]).name.lower()
if head not in _WRAPPERS:
break
tokens = tokens[1:]
while tokens and tokens[0].startswith("-"):
option = tokens[0]
tokens = tokens[1:]
if option == "--":
break
if head == "env" and (
option in {"-S", "--split-string"}
or option.startswith("--split-string=")
or (option.startswith("-S") and len(option) > 2)
):
if option.startswith("--split-string="):
value = option.split("=", 1)[1]
elif option.startswith("-S") and len(option) > 2:
value = option[2:]
elif tokens:
value = tokens[0]
tokens = tokens[1:]
else:
break
tokens = _split_env_string(value) + tokens
continue
if option in _WRAPPER_VALUE_OPTIONS.get(head, set()) and tokens:
tokens = tokens[1:]
if head == "timeout" and tokens and not tokens[0].startswith("-"):
tokens = tokens[1:]
if not tokens:
return "", []
return Path(tokens[0]).name.lower(), tokens[1:]
def _shell_has_command_flag(arguments: list[str]) -> bool:
for argument in arguments:
lowered = argument.lower()
if lowered in {"-c", "/c", "/k"}:
return True
if (
lowered.startswith("-")
and not lowered.startswith("--")
and "c" in lowered[1:]
):
return True
return False
def _has_inline_flag(arguments: list[str], flags: set[str]) -> bool:
short_flags = {
flag[1:] for flag in flags if flag.startswith("-") and not flag.startswith("--")
}
for argument in arguments:
lowered = argument.lower()
if not lowered.startswith("-"):
break
if lowered in flags:
return True
if lowered.startswith("-") and not lowered.startswith("--"):
if any(flag in lowered[1:] for flag in short_flags):
return True
return False
def validate_command(command: str) -> tuple[bool, str]:
"""
Best-effort shell guardrail for the bash tool.
This is a denylist over parsed shell segments, not an OS sandbox: a
determined caller can still run arbitrary code through an allowed
interpreter. The agent-level approval flow is the real boundary.
Rules:
- Must be a non-empty string
- Null bytes rejected
- Command substitution (backticks, `$()`) rejected
- Blocked substrings from config.tool.blocked_commands rejected
- Privilege escalation (sudo/su/doas/pkexec) rejected anywhere,
including via env wrappers and absolute paths
- Nested shell `-c` and inline interpreter code (`python -c`, `node -e`)
rejected
- base64 decode piped into a shell rejected
- Length capped (10 KB)
Returns:
(is_valid, error_message)
"""
if not isinstance(command, str):
return False, "Command must be a string."
stripped = command.strip()
if not stripped:
return False, "No command provided."
if "\x00" in command:
return False, "Invalid command (null byte)."
if len(command) > 10_000:
return False, "Command too long (max 10 KB)."
if _SUSPICIOUS_CHAR_RUNS.search(stripped):
log.warning(f"Command substitution blocked: {stripped[:80]}")
return False, "Command substitution (backticks or $()) is blocked."
lower = stripped.lower()
for blocked in config.tool.blocked_commands:
if blocked.strip() and blocked.lower() in lower:
log.warning(f"Blocked command pattern '{blocked}': {stripped[:80]}")
return (
False,
(
"This command has been blocked for security reasons "
f"({blocked.strip()})."
),
)
if re.search(r"\bbase64\b[^|]*(?:-d|--decode)[^|]*\|\s*(?:ba|z|k)?sh\b", lower):
return False, "Decoding base64 into a shell is blocked."
for segment in _segments(stripped):
try:
tokens = shlex.split(segment, posix=True)
except ValueError:
tokens = segment.split()
executable, arguments = _executable(tokens)
if not executable:
continue
if executable in _PRIVILEGE_TOOLS:
log.warning(f"Privilege escalation blocked: {stripped[:80]}")
return False, "Privilege escalation commands (sudo/su/doas) are blocked."
if executable == "eval":
return False, "Shell eval is blocked."
if executable in _SHELLS and _shell_has_command_flag(arguments):
return False, "Nested shell execution (-c) is blocked."
inline_flags = _INLINE_CODE.get(executable, set())
if inline_flags and _has_inline_flag(arguments, inline_flags):
return False, (
f"Inline code execution via {executable} is blocked; "
"write a script file instead."
)
return True, ""
# ============================================================
# Generic string validator
# ============================================================
def validate_string(
value,
*,
name: str = "value",
max_length: int = 100_000,
allow_empty: bool = False,
) -> tuple[bool, str]:
"""
Basic string guardrails shared across tools.
"""
if not isinstance(value, str):
return False, f"{name} must be a string."
if not allow_empty and not value.strip():
return False, f"{name} must not be empty."
if len(value) > max_length:
return False, f"{name} too long (max {max_length} characters)."
if "\x00" in value:
return False, f"{name} contains a null byte."
return True, ""
if __name__ == "__main__":
print(validate_command("echo hi"))
print(validate_command("sudo rm -rf /"))
print(validate_command("curl https://evil"))
print(validate_path("README.md"))
print(validate_path("../../etc/passwd"))