-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_command_classification.py
More file actions
505 lines (434 loc) · 17 KB
/
Copy path_command_classification.py
File metadata and controls
505 lines (434 loc) · 17 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
"""Shared command classification primitives for guard scripts.
Supported interpreters: python3?, perl, ruby, node.
To add coverage for a new interpreter, update both _INTERPRETER_RE and
_INTERPRETER_LINE_RE.
"""
from __future__ import annotations
import os
import re
import shlex
from collections.abc import Sequence
from typing import Protocol
_INTERPRETER_RE = re.compile(
r"(?:^|&&|\|\||;)\s*(?:env\s+)?(?:python3?|perl|ruby|node)\s+"
r"(?:-[ce]\s|.*<<)"
)
_INTERPRETER_LINE_RE = re.compile(r"(?:python3?|perl|ruby|node)\s+(?:-[ce]\s|.*<<)")
_NESTED_SHELL_RE = re.compile(r"(?:^|&&|\|\||;)\s*(?:bash|sh|zsh|dash)\s+-c\s+")
_WRITE_APIS_RE = re.compile(
r"\.write_text\s*\(|\.write_bytes\s*\("
r"|open\s*\([^)]*['\"][wWaAxX]\+?[bB]?['\"]"
r"|shutil\.(?:copy|move|copyfile|copytree)\s*\("
)
_SUBPROCESS_APIS_RE = re.compile(
r"subprocess\.(?:run|call|Popen|check_call|check_output)\s*\("
r"|os\.(?:system|popen|exec[lv]p?e?)\s*\("
)
_LITERAL_OPEN_PATH_RE = re.compile(r"""open\s*\(\s*(['"])([^'"]+)\1\s*,\s*['"][wWaAxX]""")
_LITERAL_PATH_CONSTRUCTOR_RE = re.compile(
r"""Path\s*\(\s*(['"])([^'"]+)\1\s*\)\s*\.(?:write_text|write_bytes)\s*\("""
)
_WRITE_CALL_SITE_RE = re.compile(
r"open\s*\([^)]*['\"][wWaAxX]\+?[bB]?['\"]"
r"|Path\s*\([^)]*\)\s*\.(?:write_text|write_bytes)\s*\("
r"|shutil\.(?:copy|move|copyfile|copytree)\s*\("
)
_SHELL_OPS: frozenset[str] = frozenset({"&&", "||", ";", "!", "|", "("})
# Shell control words that mark the start of a new command in compound
# shell constructs (loops, conditionals, case statements). When a `gh` token
# is preceded by one of these, treat it as the verb of a fresh command — even
# though shlex does not treat them as operators. Keep this set narrow: only
# words that legitimately precede a command in real shell scripts.
_SHELL_CONTROL_WORDS: frozenset[str] = frozenset(
{
"do",
"done",
"then",
"else",
"elif",
"esac",
"fi",
"in",
}
)
_REDIRECT_TOKEN_RE = re.compile(r"^(\d*)>{1,2}(.+)$")
_REDIRECT_OP_ONLY_RE = re.compile(r"^(\d*)>{1,2}$")
_FD_REDIRECT_RE = re.compile(r"^\d*>{1,2}&")
_TRAILING_SHELL_CLOSERS = frozenset({")", "`", "}", "'", '"', ";", "&", "|"})
_SHELL_VAR_RE = re.compile(r"\$\{[A-Za-z_]|\$[A-Za-z_]")
_HEREDOC_BODY_RE = re.compile(
r"(<<-?\s*['\"]?(\w+)['\"]?[^\n]*)\n.*?\n\t*(\2)(?=[ \t]*(?:\n|$))",
re.DOTALL,
)
_PROTECTED_PATH_METADATA_GIT_SUBCOMMANDS: frozenset[str] = frozenset({"add", "diff", "status"})
# Command wrappers whose only effect is to invoke the next command with
# adjusted environment/priority. The verb is the token after the wrapper.
# 'xargs' is intentionally excluded: it dispatches a downstream reader and
# adding it would let `xargs cat src/.../foo.yaml` reach the reader check,
# weakening xargs-chain bypass detection (see D1 design decision).
_COMMAND_WRAPPERS: frozenset[str] = frozenset({"command", "nice", "time", "sudo", "nohup"})
# Wrappers that consume a mandatory DURATION as their first non-wrapper token.
_WRAPPERS_WITH_DURATION: frozenset[str] = frozenset({"timeout"})
# Wrappers that take a single short flag as their first non-wrapper token
# (e.g. 'stdbuf -o0', 'stdbuf -i0', 'stdbuf -e0').
_WRAPPERS_WITH_SHORT_FLAG: frozenset[str] = frozenset({"stdbuf"})
_GIT_ADD_CONTENT_FLAGS: frozenset[str] = frozenset(
{
"-p",
"--patch",
"-e",
"--edit",
"-i",
"--interactive",
"--pathspec-from-file",
# Content-staging flags: -A/--all stages all changes (incl. content);
# --force/--no-ignore-removal/--no-all are the no-restriction variants.
# Without these, `git add -A -- src/.../foo.yaml` is classified as
# metadata but actually stages content for indirect read via
# `git diff --staged`.
"-A",
"--all",
"--force",
"--no-ignore-removal",
"--no-all",
}
)
_GIT_STATUS_CONTENT_FLAGS: frozenset[str] = frozenset({"-v", "--verbose"})
_GIT_DIFF_CONTENT_FLAGS: frozenset[str] = frozenset(
{
"-p",
"--patch",
"--patch-with-stat",
"--patch-with-raw",
"--binary",
"--text",
"--word-diff",
"--color-words",
}
)
_GIT_DIFF_METADATA_FLAGS: frozenset[str] = frozenset(
{
"--name-only",
"--name-status",
"--stat",
"--shortstat",
"--numstat",
"--summary",
}
)
_SHELL_SUBSTITUTION_RE = re.compile(r"\$\(|`|[<>]\(")
_SHELL_STATE_VAR_RE = re.compile(r"\$(?:_|[A-Za-z][A-Za-z0-9_]*|\{[^}]+\})")
_PROTECTED_READ_SHELL_OPS: frozenset[str] = frozenset({"&&", "||", ";", "|", "&"})
_WC_FLAG_RE = re.compile(r"-l+|--lines$")
class SearchPattern(Protocol):
def search(self, string: str, /): ...
def strip_heredoc_bodies(command: str) -> str:
"""Strip heredoc body content, preserving the opening line and terminator.
The opening line (containing << and any real redirects) is kept intact.
Only the body lines between the opening and terminator are removed.
"""
return _HEREDOC_BODY_RE.sub(r"\1\n\3", command)
def resolve_write_target(path: str, cwd: str = "") -> str | None:
if not path:
return None
if path.startswith("&") or _FD_REDIRECT_RE.match(path):
return None
if _SHELL_VAR_RE.search(path):
path = os.path.expandvars(path)
if _SHELL_VAR_RE.search(path):
return None
if os.path.isabs(path):
return path
if cwd:
return os.path.join(cwd, path)
return None
def tokenize_command_segments(command: str) -> list[list[str]]:
"""Split a shell command into segments of (verb, args...) token lists.
Each segment is one logical command, separated by shell operators.
Returns [] on shlex parse error (unclosed quotes).
"""
try:
tokens = shlex.split(strip_heredoc_bodies(command))
except (ValueError, TypeError):
return []
segments: list[list[str]] = []
current: list[str] = []
for token in tokens:
if token in _SHELL_OPS:
if current:
segments.append(current)
current = []
else:
current.append(token)
if current:
segments.append(current)
return segments
def extract_redirect_targets(tokens: list[str], cwd: str = "") -> list[str]:
"""Extract redirect target paths from shlex-tokenized command tokens.
Returns resolved paths including pseudo-devices — caller filters.
Relative paths are resolved against cwd when provided.
Handles three redirect forms at depth 0 only:
- Separate: ['>', '/path'] or ['>>', '/path']
- Split: ['2>', '/path'] (operator-only token + next token)
- Merged: ['2>/path'] or ['2>>/path']
Tracks subshell nesting via '(' and ')' — both standalone and fused
with adjacent text (shlex merges '(' with following chars in POSIX mode).
"""
targets: list[str] = []
depth = 0
i = 0
while i < len(tokens):
tok = tokens[i]
if tok == "(" or (tok.startswith("(") and len(tok) > 1):
depth += 1
if tok.endswith(")") and len(tok) > 1:
depth -= 1
i += 1
continue
if tok == ")":
if depth > 0:
depth -= 1
i += 1
continue
if tok.endswith(")") and len(tok) > 1:
if depth > 0:
depth -= 1
i += 1
continue
if depth > 0:
i += 1
continue
if tok in (">", ">>"):
if i + 1 < len(tokens):
path = tokens[i + 1]
while path and path[-1] in _TRAILING_SHELL_CLOSERS:
path = path[:-1]
resolved = resolve_write_target(path, cwd)
if resolved is not None:
targets.append(resolved)
i += 2
continue
elif _REDIRECT_OP_ONLY_RE.match(tok):
if i + 1 < len(tokens):
path = tokens[i + 1]
while path and path[-1] in _TRAILING_SHELL_CLOSERS:
path = path[:-1]
resolved = resolve_write_target(path, cwd)
if resolved is not None:
targets.append(resolved)
i += 2
continue
else:
m = _REDIRECT_TOKEN_RE.match(tok)
if m:
path = m.group(2)
while path and path[-1] in _TRAILING_SHELL_CLOSERS:
path = path[:-1]
resolved = resolve_write_target(path, cwd)
if resolved is not None:
targets.append(resolved)
i += 1
return targets
def _command_start_index(segment: list[str]) -> int | None:
if not segment:
return None
start = 0
# Iteratively strip 'env', command wrappers, and any wrapper-required
# argument. This handles nested forms like
# 'env FOO=BAR command git diff' and 'command env FOO=BAR git diff'
# consistently — the real command verb is the first token that is not a
# known env/wrapper prefix. 'xargs' is intentionally not a wrapper
# (see _COMMAND_WRAPPERS docstring).
while start < len(segment):
token = segment[start]
if token == "env" and start + 1 < len(segment):
start += 1
while start < len(segment) and (
segment[start].startswith("-") or "=" in segment[start]
):
start += 1
continue
if token in _COMMAND_WRAPPERS:
start += 1
continue
if token in _WRAPPERS_WITH_DURATION and start + 1 < len(segment):
start += 2
continue
if (
token in _WRAPPERS_WITH_SHORT_FLAG
and start + 1 < len(segment)
and segment[start + 1].startswith("-")
):
start += 2
continue
break
return start if start < len(segment) else None
def command_verb(segment: list[str]) -> str:
"""Return the command verb from a segment, skipping 'env' prefix."""
start = _command_start_index(segment)
return segment[start] if start is not None else ""
def is_gh_command(segment: list[str]) -> bool:
"""Return True if the segment's command verb is 'gh'."""
return command_verb(segment) == "gh"
def is_git_command(segment: list[str]) -> bool:
"""Return True if the segment's command verb is 'git' or ends with '/git'."""
verb = command_verb(segment)
return verb == "git" or verb.endswith("/git")
_GIT_GLOBAL_FLAGS: frozenset[str] = frozenset(
{"-C", "--work-tree", "--git-dir", "--no-pager", "--bare", "-c"}
)
_GIT_GLOBAL_FLAGS_WITH_VALUE: frozenset[str] = frozenset({"-C", "--work-tree", "--git-dir", "-c"})
def extract_git_subcommand_and_flags(
segment: list[str],
) -> tuple[str, list[str]] | None:
"""Extract the git subcommand and its flags from a tokenized segment.
Skips global git flags (and their value tokens) to find the subcommand,
then returns (subcommand, remaining_tokens). Returns None if the segment
is not a git command or has no subcommand.
"""
start = _command_start_index(segment)
if start is None:
return None
verb = segment[start]
if verb != "git" and not verb.endswith("/git"):
return None
i = start + 1
while i < len(segment):
token = segment[i]
if token in _GIT_GLOBAL_FLAGS_WITH_VALUE:
i += 2 # skip flag and its value
continue
if token in _GIT_GLOBAL_FLAGS:
i += 1
continue
if token.startswith("-"):
i += 1
continue
# First non-flag token is the subcommand
subcommand = token
remaining = segment[i + 1 :]
return (subcommand, remaining)
return None
def _is_allowed_wc_flag(token: str) -> bool:
"""Return True when *token* is a wc flag that does not reveal file contents.
Allows ``-l``, repeated ``-l`` (e.g. ``-ll``), and the long form ``--lines``
only. Any value-bearing variant (``--lines=10``) or compound form
(``-lL``) is rejected because those are not used for metadata-only reads.
"""
return bool(_WC_FLAG_RE.fullmatch(token))
def is_allowed_protected_path_metadata_command(segment: list[str]) -> bool:
"""Return True for protected-path commands that inspect metadata or VCS state.
Protected recipe/skill/agent paths are normally deny-by-default because most
commands that mention them are content reads. These narrow exceptions support
legitimate pipeline work on files already in scope.
"""
verb = command_verb(segment)
if verb == "git" or verb.endswith("/git"):
git_parts = extract_git_subcommand_and_flags(segment)
if git_parts is None:
return False
subcommand, flags = git_parts
if subcommand not in _PROTECTED_PATH_METADATA_GIT_SUBCOMMANDS:
return False
if subcommand == "add":
return not any(
flag in _GIT_ADD_CONTENT_FLAGS or flag.startswith("--pathspec-from-file=")
for flag in flags
)
if subcommand == "status":
return not any(flag in _GIT_STATUS_CONTENT_FLAGS for flag in flags)
if subcommand == "diff":
if any(
flag in _GIT_DIFF_CONTENT_FLAGS
or flag.startswith("-U")
or flag.startswith("--unified")
or flag.startswith("--word-diff")
or flag.startswith("--color-words")
or flag.startswith("--patch-with-stat")
or flag.startswith("--patch-with-raw")
for flag in flags
):
return False
return any(
flag in _GIT_DIFF_METADATA_FLAGS or flag.startswith("--stat=") for flag in flags
)
return False
if verb == "wc" or verb.endswith("/wc"):
start = _command_start_index(segment)
if start is None:
return False
flags = [token for token in segment[start + 1 :] if token.startswith("-")]
return bool(flags) and all(_is_allowed_wc_flag(token) for token in flags)
return False
def _tokenize_protected_read_segments(command: str) -> list[list[str]]:
try:
lexer = shlex.shlex(command.replace("\n", " ; "), posix=True, punctuation_chars=";&|()")
lexer.whitespace_split = True
tokens = list(lexer)
except (ValueError, TypeError):
return []
segments: list[list[str]] = []
current: list[str] = []
for token in tokens:
if token in _PROTECTED_READ_SHELL_OPS:
if current:
segments.append(current)
current = []
else:
current.append(token)
if current:
segments.append(current)
return segments
def command_has_blocked_protected_path_read(
command: str, protected_path_patterns: Sequence[SearchPattern]
) -> bool:
"""Return True when a command reads a protected recipe/skill/agent path."""
if not any(pattern.search(command) for pattern in protected_path_patterns):
return False
if "<<" in command or _SHELL_SUBSTITUTION_RE.search(command):
return True
segments = _tokenize_protected_read_segments(command)
if not segments:
return True
if len(segments) > 1 and _SHELL_STATE_VAR_RE.search(command):
return True
for segment in segments:
segment_text = " ".join(segment)
if any(pattern.search(segment_text) for pattern in protected_path_patterns):
if not is_allowed_protected_path_metadata_command(segment):
return True
return False
def has_interpreter_write(command: str) -> bool:
if not _INTERPRETER_RE.search(command):
return False
return bool(_WRITE_APIS_RE.search(command))
def extract_interpreter_write_paths(command: str) -> list[str] | None:
"""Extract literal file paths from an interpreter write command.
Returns:
None — command is not an interpreter write (no prefix or no write API).
[] — interpreter write detected but not all paths are static literals
(dynamic variable, f-string, shutil two-arg, or mixed).
[paths] — all write target paths are static literals (may be relative).
"""
if not _INTERPRETER_RE.search(command):
return None
if not _WRITE_APIS_RE.search(command):
return None
call_site_count = len(_WRITE_CALL_SITE_RE.findall(command))
paths: list[str] = []
for m in _LITERAL_OPEN_PATH_RE.finditer(command):
paths.append(m.group(2))
for m in _LITERAL_PATH_CONSTRUCTOR_RE.finditer(command):
paths.append(m.group(2))
if len(paths) < call_site_count:
return []
return paths if paths else []
def has_interpreter_wrapped_command(command: str, *, target_commands: Sequence[str]) -> bool:
if not _INTERPRETER_RE.search(command):
return False
if not _SUBPROCESS_APIS_RE.search(command):
return False
cmd_lower = command.lower()
return any(tc.lower() in cmd_lower for tc in target_commands)
def has_nested_shell(command: str) -> bool:
return bool(_NESTED_SHELL_RE.search(command))