Repository navigation
Expand file tree
/
Copy pathshell_command.py
More file actions
768 lines (656 loc) · 27.7 KB
/
Copy pathshell_command.py
File metadata and controls
768 lines (656 loc) · 27.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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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 ..taint import ReadContentRecord
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 _flag_name(arg: str) -> str:
# `--type=bool` and `--type bool` should compare equal on the flag name.
return arg.split("=", 1)[0]
def _is_git_config_exec_key(key: str) -> bool:
key = key.lower()
if key in cs.SHELL_GIT_CONFIG_EXEC_KEYS:
return True
return any(
key.startswith(prefix)
and key.endswith(suffix)
and len(key) > len(prefix) + len(suffix)
for prefix, suffix in cs.SHELL_GIT_CONFIG_EXEC_KEY_PATTERNS
)
def _git_config_exec_key(cmd_parts: list[str]) -> str | None:
"""Return the shell-executing git config key this command would write.
`git config` writes to keys like `core.sshCommand` plant a value git later
hands to a shell, so the write itself is RCE on the next git operation
(GHSA-2rr7-8xrw-gmhr). Reading a key is fine, and clearing one (`--unset`)
is how a victim recovers, so those return None.
"""
if len(cmd_parts) < 3 or cmd_parts[0] != cs.SHELL_CMD_GIT:
return None
if cmd_parts[1] != cs.SHELL_GIT_SUBCMD_CONFIG:
return None
args = cmd_parts[2:]
flags = [_flag_name(arg) for arg in args if arg.startswith("-")]
if any(flag in cs.SHELL_GIT_CONFIG_READ_ACTIONS for flag in flags):
return None
if any(flag in cs.SHELL_GIT_CONFIG_UNSET_FLAGS for flag in flags):
return None
return next(
(
arg
for arg in args
if not arg.startswith("-") and _is_git_config_exec_key(arg)
),
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 key := _git_config_exec_key(cmd_parts):
return True, f"git config write to '{key}' plants a command git will run"
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 _find_requires_approval(parts: list[str]) -> bool:
return any(part in cs.SHELL_FIND_MUTATING_ACTIONS for part in parts[1:])
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 == "find" and _find_requires_approval(parts):
return True
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:
"""Run a command after the safety checks, capturing both streams."""
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, read_record: ReadContentRecord | None = None
) -> Tool:
"""Build the `execute_shell_command` tool, recording stdout and stderr in
`read_record` so they feed the egress taint gate (issue #1128)."""
async def run_shell_command(
ctx: RunContext[None], command: str
) -> ShellCommandResult:
"""Run a shell command, recording both output streams."""
if (
not shell_commander.is_yolo()
and _requires_approval(command)
and not ctx.tool_call_approved
):
raise ApprovalRequired(metadata={"command": command})
result = await shell_commander.execute(command)
if read_record is not None:
# Shell output is repository content too (`cat`, `grep`); feed
# the egress taint gate (issue #1128). Both streams reach the
# model, and stderr carries source just as readily (compiler
# diagnostics, tracebacks quoting the offending line).
read_record.record(result.stdout)
read_record.record(result.stderr)
return result
return Tool(
function=run_shell_command,
name=td.AgenticToolName.EXECUTE_SHELL,
description=td.SHELL_COMMAND,
)
_ESCAPING_PATH_ARG = re.compile(r"(?:^|=)[/~]")
def _long_option_matches(arg: str, canonical: str) -> bool:
# GNU tools accept any unambiguous abbreviation of a long option, so
# `--out`, `--outp`, ... all mean `--output` and `--files0` means
# `--files0-from`. Match the arg's option name (before any `=`) as a
# non-empty prefix of the canonical name, which covers the full spelling
# too (Greptile review on PR #1388). Abbreviation ambiguity only widens
# what GNU rejects, never what it accepts, so treating every prefix as
# the dangerous option is safe.
name = arg.split("=", 1)[0]
return len(name) > 2 and canonical.startswith(name)
def _noninteractive_write_form(parts: list[str]) -> bool:
# Write-capable invocations of otherwise read-only commands: `sort -o` /
# `--output[=]` writes a file, and uniq's SECOND positional operand is an
# output file -- counted through `--`, after which every argument is an
# operand (CodeRabbit and Greptile reviews on PR #1388).
if parts[0] == "sort":
for arg in parts[1:]:
if arg == "--":
break
if arg.startswith("--"):
if _long_option_matches(arg, "--output"):
return True
continue
if not arg.startswith("-") or len(arg) < 2:
continue
# Walk the short-option cluster: `-ro` hides the output option
# behind other flags, and `-rT` hides the temp-dir option
# (Greptile review on PR #1388). A value-taking option (-k, -t,
# -S) consumes the rest of the cluster as its value, so an `o`
# after one is data, not a flag.
for ch in arg[1:]:
if ch in "oT":
return True
if ch in "ktS":
break
return False
if parts[0] == "uniq":
operands = 0
operands_only = False
for arg in parts[1:]:
if not operands_only and arg == "--":
operands_only = True
continue
if operands_only or not arg.startswith("-"):
operands += 1
return operands > 1
return False
_FOLLOW_LONG_FLAGS = ("--follow", "--dereference")
_SHORT_CLUSTER = re.compile(r"-[A-Za-z]+")
def _follows_symlinks(parts: list[str]) -> bool:
# Symlink-following traversal (`find -L .`, `rg -L pat .`, `ls -RL`)
# reads through outward symlinks the per-operand containment check never
# sees, because no explicit operand names the escaped target (Greptile
# review on PR #1388).
cmd = parts[0]
if cmd not in ("find", "rg", "ls"):
return False
for arg in parts[1:]:
if arg == "--":
break
if cmd == "find":
if arg in ("-L", "-follow"):
return True
elif arg in _FOLLOW_LONG_FLAGS or (
_SHORT_CLUSTER.fullmatch(arg) and "L" in arg
):
return True
return False
def _option_carries_file_input(parts: list[str]) -> bool:
# `sort --files0-from=paths` makes sort read every file a repo-local
# list names, including /etc/passwd, and `--compress-program`/`--pre`
# execute a program; the operand containment loop never sees either, so
# the whole indirect-input mode is denied (Greptile review on PR #1388).
denied = cs.SHELL_NONINTERACTIVE_DENIED_OPTIONS.get(parts[0])
if not denied:
return False
for arg in parts[1:]:
if arg == "--":
break
name = arg.split("=", 1)[0]
for opt in denied:
if len(opt) == 2:
# A short option: exact, or the attached-value form `-Tdir`.
if name == opt or arg.startswith(opt):
return True
# A long option matches any unambiguous GNU abbreviation, so
# `--files0` reaches `--files0-from` (Greptile review on
# PR #1388).
elif _long_option_matches(arg, opt):
return True
return False
def _noninteractive_denial(command: str, project_root: Path) -> str | None:
# The denial reason for an operator-less run, or None when every segment
# is a confined read: a read-only command in a non-writing form, no
# redirection tokens, no find mutating actions, and no absolute,
# parent-traversal, or symlink-escaping path arguments (subprocess runs
# without a shell, so `~` never expands and a redirect token is inert,
# but both signal intent the harness must not honor; a repo-local
# symlink, however, WOULD be followed outside the root by the child
# process — CodeRabbit review on PR #1388).
try:
groups = _parse_command(command)
except (ValueError, IndexError):
return te.COMMAND_INVALID_SYNTAX.format(segment=command)
read_only = (
settings.SHELL_READ_ONLY_COMMANDS | settings.SHELL_NONINTERACTIVE_READ_COMMANDS
)
root = project_root.resolve()
for group in groups:
for segment in group.commands:
if not (segment := segment.strip()):
continue
try:
parts = shlex.split(segment)
except ValueError:
return te.COMMAND_INVALID_SYNTAX.format(segment=segment)
if not parts:
continue
if parts[0] not in read_only:
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_NOT_READ_ONLY
)
if _noninteractive_write_form(parts):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_WRITE_FORM
)
if _follows_symlinks(parts):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_FOLLOW_SYMLINKS
)
if _option_carries_file_input(parts):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_OPTION_CARRIED_INPUT
)
if _has_redirect_operators(parts):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_REDIRECT
)
if parts[0] == "find" and _find_requires_approval(parts):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_FIND_MUTATES
)
operands_only = False
for arg in parts[1:]:
if not operands_only and arg == "--":
# After `--` every argument is an operand, even one that
# starts with `-` (CodeRabbit review on PR #1388).
operands_only = True
continue
if _ESCAPING_PATH_ARG.search(arg) or ".." in arg.split("/"):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_PATH_ESCAPES
)
if not operands_only and arg.startswith("-"):
# An `=`-attached option value (`--file=linked_pats`) is a
# path the operand check below never sees, so it gets the
# same traversal and symlink containment (Greptile review
# on PR #1388).
value = arg.partition("=")[2]
if value and (
".." in value.split("/")
or (
os.path.lexists(candidate := root / value)
and not candidate.resolve().is_relative_to(root)
)
):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_PATH_ESCAPES
)
continue
candidate = root / arg
if os.path.lexists(
candidate
) and not candidate.resolve().is_relative_to(root):
return te.COMMAND_NONINTERACTIVE_DENIED.format(
command=segment, reason=te.NONINTERACTIVE_PATH_ESCAPES
)
return None
def create_noninteractive_shell_command_tool(shell_commander: ShellCommander) -> Tool:
# For operator-less runs (benchmarks, batch jobs): a command that would
# need interactive approval is DENIED instead of yolo-bypassed, and the
# allowlist stays enforced, so a model-selected command can never mutate
# the host or read outside the project root (Greptile security review on
# PR #1388). The error text tells the model why, so it can retry with a
# confined read-only command.
async def run_shell_command(
ctx: RunContext[None], command: str
) -> ShellCommandResult:
if err_msg := _noninteractive_denial(command, shell_commander.project_root):
logger.error(err_msg)
return ShellCommandResult(
return_code=cs.SHELL_RETURN_CODE_ERROR, stdout="", stderr=err_msg
)
return await shell_commander.execute(command)
return Tool(
function=run_shell_command,
name=td.AgenticToolName.EXECUTE_SHELL,
description=td.SHELL_COMMAND,
)