Repository navigation
Expand file tree
/
Copy pathshell_command.py
More file actions
479 lines (397 loc) · 15.8 KB
/
Copy pathshell_command.py
File metadata and controls
479 lines (397 loc) · 15.8 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
from __future__ import annotations
import asyncio
import os
import re
import shlex
import shutil
import sys
import time
from collections.abc import Callable
from pathlib import Path
from loguru import logger
from pydantic_ai import ApprovalRequired, RunContext, Tool
from .. import constants as cs
from .. import logs as ls
from .. import tool_errors as te
from ..config import settings
from ..decorators import async_timing_decorator
from ..schemas import ShellCommandResult
from . import tool_descriptions as td
PIPELINE_PATTERNS_COMPILED = tuple(
(re.compile(pattern, re.IGNORECASE), reason)
for pattern, reason in cs.SHELL_DANGEROUS_PATTERNS_PIPELINE
)
SEGMENT_PATTERNS_COMPILED = tuple(
(re.compile(pattern, re.IGNORECASE), reason)
for pattern, reason in cs.SHELL_DANGEROUS_PATTERNS_SEGMENT
)
def _is_outside_single_quotes(command: str, pos: int) -> bool:
in_single = False
i = 0
while i < pos:
char = command[i]
if char == "\\" and not in_single and i + 1 < len(command):
i += 2
continue
if char == "'":
in_single = not in_single
i += 1
return not in_single
def _has_subshell(command: str) -> str | None:
for pattern in cs.SHELL_SUBSHELL_PATTERNS:
start = 0
while True:
pos = command.find(pattern, start)
if pos == -1:
break
if _is_outside_single_quotes(command, pos):
return pattern
start = pos + 1
return None
class CommandGroup:
__slots__ = ("commands", "operator")
def __init__(self, commands: list[str], operator: str | None = None):
self.commands = commands
self.operator = operator
def _parse_command(command: str) -> list[CommandGroup]:
groups: list[CommandGroup] = []
current_pipeline: list[str] = []
current_segment: list[str] = []
in_single = False
in_double = False
pending_operator: str | None = None
i = 0
def finalize_segment() -> None:
seg = "".join(current_segment).strip()
if seg:
current_pipeline.append(seg)
current_segment.clear()
def finalize_group(new_operator: str) -> None:
nonlocal pending_operator
finalize_segment()
if current_pipeline:
groups.append(CommandGroup(list(current_pipeline), pending_operator))
current_pipeline.clear()
pending_operator = new_operator
while i < len(command):
char = command[i]
if char == "\\" and i + 1 < len(command):
current_segment.append(char)
current_segment.append(command[i + 1])
i += 2
continue
if char == "'" and not in_double:
in_single = not in_single
current_segment.append(char)
elif char == '"' and not in_single:
in_double = not in_double
current_segment.append(char)
elif char == "|" and not in_single and not in_double:
if i + 1 < len(command) and command[i + 1] == "|":
finalize_group("||")
i += 2
continue
finalize_segment()
elif char == "&" and not in_single and not in_double:
if i + 1 < len(command) and command[i + 1] == "&":
finalize_group("&&")
i += 2
continue
current_segment.append(char)
elif char == ";" and not in_single and not in_double:
finalize_group(";")
else:
current_segment.append(char)
i += 1
finalize_segment()
if current_pipeline:
groups.append(CommandGroup(list(current_pipeline), pending_operator))
return groups
def _is_blocked_command(cmd: str) -> bool:
return cmd in cs.SHELL_DANGEROUS_COMMANDS
def _is_dangerous_rm(cmd_parts: list[str]) -> bool:
if not cmd_parts or cmd_parts[0] != cs.SHELL_CMD_RM:
return False
flags = "".join(part for part in cmd_parts[1:] if part.startswith("-"))
return "r" in flags and "f" in flags
def _is_dangerous_rm_path(cmd_parts: list[str], project_root: Path) -> tuple[bool, str]:
if not cmd_parts or cmd_parts[0] != cs.SHELL_CMD_RM:
return False, ""
path_args = [p for p in cmd_parts[1:] if not p.startswith("-")]
for path_arg in path_args:
if path_arg in ("*", ".", ".."):
return True, f"rm targeting dangerous path: {path_arg}"
try:
if path_arg.startswith("/"):
resolved = Path(path_arg).resolve()
else:
resolved = (project_root / path_arg).resolve()
except (OSError, ValueError):
return True, f"rm with invalid path: {path_arg}"
resolved_str = str(resolved)
if resolved == resolved.parent:
return True, "rm targeting root directory"
try:
resolved.relative_to(project_root)
except ValueError:
parts = resolved.parts
if len(parts) >= 2 and parts[1] in cs.SHELL_SYSTEM_DIRECTORIES:
return True, f"rm targeting system directory: {resolved_str}"
return True, f"rm targeting path outside project: {resolved_str}"
return False, ""
def _check_pipeline_patterns(full_command: str) -> str | None:
for pattern, reason in PIPELINE_PATTERNS_COMPILED:
if pattern.search(full_command):
return reason
return None
def _check_segment_patterns(segment: str) -> str | None:
for pattern, reason in SEGMENT_PATTERNS_COMPILED:
if pattern.search(segment):
return reason
return None
def _is_dangerous_command(cmd_parts: list[str], full_segment: str) -> tuple[bool, str]:
if not cmd_parts:
return False, ""
base_cmd = cmd_parts[0]
if _is_blocked_command(base_cmd):
return True, f"blocked command: {base_cmd}"
if _is_dangerous_rm(cmd_parts):
return True, "rm with dangerous flags"
if reason := _check_segment_patterns(full_segment):
return True, reason
return False, ""
def _validate_segment(
segment: str, available_commands: str, bypass_allowlist: bool = False
) -> str | None:
try:
cmd_parts = shlex.split(segment)
except ValueError:
return te.COMMAND_INVALID_SYNTAX.format(segment=segment)
if not cmd_parts:
return None
base_cmd = cmd_parts[0]
if not bypass_allowlist and base_cmd not in settings.SHELL_COMMAND_ALLOWLIST:
suggestion = cs.GREP_SUGGESTION if base_cmd == cs.SHELL_CMD_GREP else ""
return te.COMMAND_NOT_ALLOWED.format(
cmd=base_cmd, suggestion=suggestion, available=available_commands
)
is_dangerous, reason = _is_dangerous_command(cmd_parts, segment)
if is_dangerous:
return te.COMMAND_DANGEROUS_BLOCKED.format(cmd=base_cmd, reason=reason)
return None
def _has_redirect_operators(parts: list[str]) -> bool:
return any(p in cs.SHELL_REDIRECT_OPERATORS for p in parts)
def _requires_approval(command: str) -> bool:
if not command.strip():
return True
try:
groups = _parse_command(command)
except (ValueError, IndexError):
return True
has_commands = False
for group in groups:
for segment in group.commands:
segment = segment.strip()
if not segment:
continue
try:
parts = shlex.split(segment)
except ValueError:
return True
if not parts:
continue
if _has_redirect_operators(parts):
return True
has_commands = True
base_cmd = parts[0]
if base_cmd in settings.SHELL_READ_ONLY_COMMANDS:
continue
if base_cmd == cs.SHELL_CMD_GIT and len(parts) > 1:
if parts[1] in settings.SHELL_SAFE_GIT_SUBCOMMANDS:
continue
return True
return not has_commands
class ShellCommander:
__slots__ = ("project_root", "timeout", "is_yolo")
def __init__(
self,
project_root: str = ".",
timeout: int = 30,
is_yolo: Callable[[], bool] | None = None,
):
self.project_root = Path(project_root).resolve()
self.timeout = timeout
self.is_yolo = is_yolo or (lambda: False)
logger.info(ls.SHELL_COMMANDER_INIT.format(root=self.project_root))
async def _execute_pipeline(self, segments: list[str]) -> tuple[int, bytes, bytes]:
start_time = time.monotonic()
input_data: bytes | None = None
all_stderr: list[bytes] = []
last_return_code = 0
env = os.environ.copy()
if sys.platform == "win32":
git_bin = r"C:\Program Files\Git\usr\bin"
if os.path.isdir(git_bin) and git_bin not in env["PATH"]:
env["PATH"] = f"{git_bin};{env['PATH']}"
for segment in segments:
elapsed = time.monotonic() - start_time
remaining_timeout = self.timeout - elapsed
if remaining_timeout <= 0:
raise TimeoutError
cmd_parts = shlex.split(segment)
executable = shutil.which(cmd_parts[0], path=env["PATH"])
if not executable:
executable = cmd_parts[0]
try:
proc = await asyncio.create_subprocess_exec(
executable,
*cmd_parts[1:],
stdin=asyncio.subprocess.PIPE if input_data is not None else None,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=self.project_root,
env=env,
)
except OSError as e:
# A bare str(OSError) hides WHICH segment failed to spawn, so
# an intermittent runner failure surfaces as an opaque -1
# (issue #902). Name the segment and the resolved executable.
raise RuntimeError(
te.COMMAND_SPAWN_FAILED.format(
segment=segment, executable=executable, error=e
)
) from e
try:
stdout, stderr = await asyncio.wait_for(
proc.communicate(input=input_data), timeout=remaining_timeout
)
except TimeoutError:
proc.kill()
await proc.wait()
raise
last_return_code = (
proc.returncode
if proc.returncode is not None
else cs.SHELL_RETURN_CODE_ERROR
)
input_data = stdout
if stderr:
all_stderr.append(stderr)
return last_return_code, input_data or b"", b"".join(all_stderr)
@async_timing_decorator
async def execute(self, command: str) -> ShellCommandResult:
logger.info(ls.TOOL_SHELL_EXEC.format(cmd=command))
try:
if subshell_pattern := _has_subshell(command):
err_msg = te.COMMAND_SUBSHELL_NOT_ALLOWED.format(
pattern=subshell_pattern
)
logger.error(err_msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR, stdout="", stderr=err_msg
)
if pattern_reason := _check_pipeline_patterns(command):
err_msg = te.COMMAND_DANGEROUS_PATTERN.format(reason=pattern_reason)
logger.error(err_msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR,
stdout="",
stderr=err_msg,
)
groups = _parse_command(command)
if not groups:
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR,
stdout="",
stderr=te.COMMAND_EMPTY,
)
available_commands = ", ".join(sorted(settings.SHELL_COMMAND_ALLOWLIST))
bypass_allowlist = self.is_yolo()
for group in groups:
for segment in group.commands:
if err_msg := _validate_segment(
segment, available_commands, bypass_allowlist=bypass_allowlist
):
logger.error(err_msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR,
stdout="",
stderr=err_msg,
)
try:
cmd_parts = shlex.split(segment)
except ValueError:
continue
is_dangerous, reason = _is_dangerous_rm_path(
cmd_parts, self.project_root
)
if is_dangerous:
err_msg = te.COMMAND_DANGEROUS_BLOCKED.format(
cmd=cmd_parts[0], reason=reason
)
logger.error(err_msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR,
stdout="",
stderr=err_msg,
)
all_stdout: list[str] = []
all_stderr: list[str] = []
last_return_code = 0
for group in groups:
should_run = True
if group.operator == "&&":
should_run = last_return_code == 0
elif group.operator == "||":
should_run = last_return_code != 0
if not should_run:
continue
return_code, stdout, stderr = await self._execute_pipeline(
group.commands
)
last_return_code = return_code
stdout_str = stdout.decode(cs.ENCODING_UTF8, errors="replace").strip()
stderr_str = stderr.decode(cs.ENCODING_UTF8, errors="replace").strip()
if stdout_str:
all_stdout.append(stdout_str)
if stderr_str:
all_stderr.append(stderr_str)
final_stdout = "\n".join(all_stdout)
final_stderr = "\n".join(all_stderr)
logger.info(ls.TOOL_SHELL_RETURN.format(code=last_return_code))
if final_stdout:
logger.info(ls.TOOL_SHELL_STDOUT.format(stdout=final_stdout))
if final_stderr:
logger.warning(ls.TOOL_SHELL_STDERR.format(stderr=final_stderr))
return ShellCommandResult(
return_code=last_return_code,
stdout=final_stdout,
stderr=final_stderr,
)
except TimeoutError:
msg = te.COMMAND_TIMEOUT.format(cmd=command, timeout=self.timeout)
logger.error(msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR, stdout="", stderr=msg
)
except Exception as e:
logger.error(ls.TOOL_SHELL_ERROR.format(error=e))
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR, stdout="", stderr=str(e)
)
def create_shell_command_tool(shell_commander: ShellCommander) -> Tool:
async def run_shell_command(
ctx: RunContext[None], command: str
) -> ShellCommandResult:
if (
not shell_commander.is_yolo()
and _requires_approval(command)
and not ctx.tool_call_approved
):
raise ApprovalRequired(metadata={"command": command})
return await shell_commander.execute(command)
return Tool(
function=run_shell_command,
name=td.AgenticToolName.EXECUTE_SHELL,
description=td.SHELL_COMMAND,
)