Skip to content

Commit 1a86b0f

Browse files
ilaigoldclaude
andcommitted
fix(rp1): keep RP1 for option values that may split and for 2&>log
The operand scan still accepted two kinds of words that do not prove which word is the image: - An option value that the shell can turn into several arguments or none. With DOCKER_ENV='MODE=dev ubuntu', `docker run --rm -e $DOCKER_ENV alpine:3.20` runs ubuntu, and alpine:3.20 becomes the container command. Every option, option value and redirection target read before the image now has to stay one argument: no unquoted expansion ($ENV, -e$ENV, --env=$ENV, $(pwd)), brace expansion ({A,B}=1, X{1..2}), or glob, and no "$@" or "${arr[@]}". A command substitution in double quotes counts only when it is plain words, because a quote inside it ("$(echo " x ")") ends the double-quoted string early here, and a case pattern can hide its closing ). Otherwise the image is unresolved and the command is still reported. Redirection targets get the same check because the scan can end them early too (2>${LOG:-a b}). - A number before &> or &>>. Those operators take no file descriptor, so in `docker run --rm 2&>log alpine:3.20` the 2 is an argument and the image. The descriptor prefix now applies only to the other operators; `&>log alpine:3.20` still reads alpine:3.20 as the image. Signed-off-by: Ilai Goldschmidt <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b25acf8 commit 1a86b0f

2 files changed

Lines changed: 138 additions & 10 deletions

File tree

‎src/skillspector/nodes/analyzers/mcp_rug_pull.py‎

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,15 @@ def analyzer_exhausted(self) -> bool:
191191
)
192192
_SHELL_WORD_GAP_RE = re.compile(r"(?:[ \t]+|\\\r?\n)+")
193193
_SHELL_QUOTING_RE = re.compile(r"""\\(.)|["']""", re.DOTALL)
194-
# A redirection operator with its optional file descriptor (2>, &>>, 2>&, <<<, {fd}>).
195-
# The shell removes it and its target word wherever it appears in the command.
194+
# A redirection operator with its optional file descriptor (2>, >>, 2>&, <<<, {fd}>).
195+
# `&>` and `&>>` take none: in `2&>log` the shell passes `2` as an argument. The
196+
# shell removes a redirection and its target word wherever they appear in the command.
196197
_SHELL_REDIRECTION_RE = re.compile(
197-
r"(?:[0-9]+|\{[A-Za-z_][A-Za-z0-9_]*\})?(?:&>>?|<<<|<<-?|<>|<&|>&|>>|>\||[<>])"
198+
r"&>>?|(?:[0-9]+|\{[A-Za-z_][A-Za-z0-9_]*\})?(?:<<<|<<-?|<>|<&|>&|>>|>\||[<>])"
198199
)
200+
# A command substitution of plain words. Unless it runs `case`, its closing `)` or
201+
# backquote is the one matched here; quotes, escapes, nesting or a comment could move it.
202+
_SHELL_PLAIN_SUBSTITUTION_RE = re.compile(r"\$\(([\w \t.,:=+%/-]*)\)|`([\w \t.,:=+%/-]*)`")
199203
# A parameter name or a special parameter after `$` ($HOME, $1, $?). `$@` is not
200204
# one of them here: it expands to several words even in double quotes.
201205
_SHELL_PARAMETER_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*|[0-9*#?$!-]")
@@ -363,16 +367,67 @@ def _docker_image_known_text(word: str) -> str | None:
363367
return known
364368

365369

370+
def _shell_word_is_one_argument(word: str) -> bool:
371+
"""Return whether the shell word *word* is certain to stay one argument.
372+
373+
Outside quotes, an expansion (``$X``, ``$(pwd)``), brace expansion
374+
(``{a,b}``, ``{1..3}``) or a pattern (``*``, ``?``, ``[``) can produce
375+
several arguments or none, and so can ``"$@"`` or ``"${arr[@]}"`` in double
376+
quotes (see ``_expansion_end``). A command substitution in double quotes
377+
counts only when it is plain words: a quote inside it ends the double-quoted
378+
string early here, so the shell's word may be longer than *word*.
379+
"""
380+
in_double_quotes = False
381+
brace_depth = 0
382+
index = 0
383+
while index < len(word):
384+
char = word[index]
385+
if char == "\\":
386+
index += 2
387+
continue
388+
if char == "'" and not in_double_quotes:
389+
close = word.find("'", index + 1)
390+
if close < 0:
391+
return False
392+
index = close + 1
393+
continue
394+
if char in "$`":
395+
if not in_double_quotes:
396+
return False
397+
plain = _SHELL_PLAIN_SUBSTITUTION_RE.match(word, index)
398+
if plain is not None and "case" not in plain[plain.lastindex].split():
399+
index = plain.end()
400+
continue
401+
end = _expansion_end(word, index)
402+
if end is None:
403+
return False
404+
index = end
405+
continue
406+
if char == '"':
407+
in_double_quotes = not in_double_quotes
408+
elif not in_double_quotes and char in "*?[":
409+
return False
410+
elif not in_double_quotes and char == "{":
411+
brace_depth += 1
412+
elif not in_double_quotes and char == "}" and brace_depth:
413+
brace_depth -= 1
414+
elif not in_double_quotes and brace_depth and (char == "," or word.startswith("..", index)):
415+
return False
416+
index += 1
417+
return True
418+
419+
366420
def _docker_image_operand(subcommand: str, text: str, truncated: bool) -> tuple[str | None, int]:
367421
"""Return the image operand of a docker command and where reading stopped.
368422
369423
*text* starts at the first argument after ``docker run|create|pull``. Options
370424
and their values are skipped using docker's option table, and redirections
371425
(``2>log``, ``<<<x``) are skipped with their targets. The image is None when
372426
it cannot be identified (an unknown option, a missing value or redirection
373-
target, the end of the command, a word cut off by the read bound) or when its
374-
tag depends on an expansion, so the caller still reports the command. For an
375-
image whose value is partly known, only the known part is returned.
427+
target, a word before the image that may not be one argument, the end of the
428+
command, a word cut off by the read bound) or when its tag depends on an
429+
expansion, so the caller still reports the command. For an image whose value
430+
is partly known, only the known part is returned.
376431
"""
377432
value_options, flag_options = _DOCKER_OPTIONS[subcommand.lower()]
378433
pos = 0
@@ -383,19 +438,25 @@ def _docker_image_operand(subcommand: str, text: str, truncated: bool) -> tuple[
383438
redirection = _SHELL_REDIRECTION_RE.match(text, gap.end() if gap else pos)
384439
if redirection is not None:
385440
target = _next_shell_word(text, redirection.end(), truncated)
386-
if target is None:
387-
return None, redirection.end()
441+
# A target that may not be one argument can also end later than the
442+
# word read here (2>${LOG:-a b}).
443+
if target is None or not _shell_word_is_one_argument(target.group(0)):
444+
return None, target.end() if target else redirection.end()
388445
pos = target.end()
389446
continue
390447
word_match = _next_shell_word(text, pos, truncated)
391448
if word_match is None:
392449
return None, pos
393450
pos = word_match.end()
394451
word = _SHELL_QUOTING_RE.sub(r"\1", word_match.group(0))
452+
if not expect_value and (end_of_options or not word.startswith("-") or word == "-"):
453+
return _docker_image_known_text(word_match.group(0)), pos
454+
# An option or value that splits into several arguments, or none, moves the
455+
# image to another word (-e $ENV with ENV='A=1 ubuntu').
456+
if not _shell_word_is_one_argument(word_match.group(0)):
457+
return None, pos
395458
if expect_value:
396459
expect_value = False
397-
elif end_of_options or not word.startswith("-") or word == "-":
398-
return _docker_image_known_text(word_match.group(0)), pos
399460
elif word == "--":
400461
end_of_options = True
401462
elif word.startswith("--"):

‎tests/test_mcp_rug_pull.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ def test_rp1_docker_redirections_before_pinned_image_no_finding():
278278
"docker run -i <input.txt alpine:3.20 cat\n",
279279
"docker run -i <<<'hello' alpine:3.20 cat\n",
280280
"docker run --rm &>>run.log alpine:3.20\n",
281+
"docker run --rm &>run.log alpine:3.20\n",
282+
"docker run -e 2&>run.log alpine:3.20\n",
281283
"docker run -e 2>err.log MODE=fast alpine:3.20\n",
282284
"docker run --rm alpine:3.20>out.log\n",
285+
'docker run --rm 2>"$LOG" >"$(pwd)/out.log" alpine:3.20\n',
283286
):
284287
assert _rp1_matches(content) == [], content
285288

@@ -306,6 +309,63 @@ def test_rp1_docker_unclear_redirection_is_still_reported():
306309
("docker run --rm 2>\n", "docker run --rm 2>"),
307310
("docker run --rm 2>|\n", "docker run --rm 2>|"),
308311
("docker run --env-file <(env) alpine:3.20\n", "docker run --env-file <"),
312+
# With LOG=err.log the shell redirects to err.log and runs ubuntu.
313+
("docker run --rm 2>${LOG:-err x:1} ubuntu\n", "docker run --rm 2>${LOG:-err"),
314+
('docker run --rm 2>"$(echo " x:1 ")" ubuntu\n', 'docker run --rm 2>"$(echo "'),
315+
):
316+
assert _rp1_matches(content) == [expected], content
317+
318+
319+
def test_rp1_docker_number_before_ampersand_redirection_is_an_argument():
320+
"""`&>` takes no file descriptor, so in `2&>log` the `2` is the image."""
321+
for content, expected in (
322+
("docker run --rm 2&>log alpine:3.20\n", "docker run --rm 2"),
323+
("docker run --rm 2&>>log alpine:3.20\n", "docker run --rm 2"),
324+
("docker run --rm {fd}&>log alpine:3.20\n", "docker run --rm {fd}"),
325+
):
326+
assert _rp1_matches(content) == [expected], content
327+
328+
329+
def test_rp1_docker_option_value_that_is_one_argument_no_finding():
330+
"""Quoted expansions and substitutions stay one argument before the image."""
331+
for content in (
332+
'docker run --rm -e "$DOCKER_ENV" alpine:3.20\n',
333+
'docker run --rm -e"$DOCKER_ENV" --env="${MODE:-dev}" alpine:3.20\n',
334+
'docker run --rm -e "$*" alpine:3.20\n',
335+
"docker run --rm -e 'MODE=$X' -e MODE=\\$X alpine:3.20\n",
336+
"docker run --rm -e \"A={x,y}\" -e 'GLOB=*.py' -e A={} alpine:3.20\n",
337+
"docker run --rm -v ~/.cache:/root/.cache alpine:3.20\n",
338+
'docker run --rm -v "$(git rev-parse --show-toplevel)":/src alpine:3.20\n',
339+
'docker run --rm --user "$(id -u):$(id -g)" alpine:3.20\n',
340+
'docker run --rm -v "`pwd`":/app alpine:3.20\n',
341+
):
342+
assert _rp1_matches(content) == [], content
343+
344+
345+
def test_rp1_docker_option_value_that_can_split_is_reported():
346+
"""An option value that can become several arguments, or none, can move the image."""
347+
for content, expected in (
348+
# With DOCKER_ENV='MODE=dev ubuntu' the image is ubuntu.
349+
("docker run --rm -e $DOCKER_ENV alpine:3.20\n", "docker run --rm -e $DOCKER_ENV"),
350+
("docker run --rm -e$DOCKER_ENV alpine:3.20\n", "docker run --rm -e$DOCKER_ENV"),
351+
("docker run --rm --env=$DOCKER_ENV alpine:3.20\n", "docker run --rm --env=$DOCKER_ENV"),
352+
("docker run --rm -e ${DOCKER_ENV} alpine:3.20\n", "docker run --rm -e ${DOCKER_ENV}"),
353+
("docker run --rm=$RM alpine:3.20\n", "docker run --rm=$RM"),
354+
('docker run --rm -e "${ARGS[@]}" alpine:3.20\n', 'docker run --rm -e "${ARGS[@]}"'),
355+
('docker run --rm --env="${ARGS[@]}" alpine:3.20\n', 'docker run --rm --env="${ARGS[@]}"'),
356+
('docker run --rm -e "$@" alpine:3.20\n', 'docker run --rm -e "$@"'),
357+
("docker run --rm -e $* alpine:3.20\n", "docker run --rm -e $*"),
358+
("docker run --rm -v $(pwd):/app node:20\n", "docker run --rm -v $(pwd):/app"),
359+
("docker run --rm -e {A,B}=1 alpine:3.20\n", "docker run --rm -e {A,B}=1"),
360+
("docker run --rm -e X{1..2}=a alpine:3.20\n", "docker run --rm -e X{1..2}=a"),
361+
("docker run --rm -v * alpine:3.20\n", "docker run --rm -v *"),
362+
# A quote inside the substitution ends the double-quoted string early here.
363+
('docker run --rm -e "$(echo " x:1 ")" ubuntu\n', 'docker run --rm -e "$(echo "'),
364+
('docker run --rm -e "`echo " x:1 "`" ubuntu\n', 'docker run --rm -e "`echo "'),
365+
(
366+
'docker run --rm --name "$(case a in a) echo " x:1 ";; esac)" ubuntu\n',
367+
'docker run --rm --name "$(case a in a) echo "',
368+
),
309369
):
310370
assert _rp1_matches(content) == [expected], content
311371

@@ -322,8 +382,15 @@ def test_rp1_docker_operand_scan_is_linear():
322382
'docker pull -q "' * 3_125,
323383
"docker run " + "2>x " * 10_000,
324384
"docker run " + "1" * 50_000 + ">x",
385+
"docker run " + "1" * 50_000 + "&>x",
386+
"docker run " + "2&>x " * 10_000,
325387
'docker run "' + "${a" * 10_000 + '"',
326388
'docker run "' + "$(" * 10_000 + '"',
389+
'docker run -e "' + "$(a" * 10_000 + '"',
390+
'docker run -e "$(' + "a " * 25_000 + ')" x',
391+
'docker run -e "x" ' * 5_000,
392+
"docker run -e " + "{a," * 10_000,
393+
"docker run -e " + "{a.." * 10_000,
327394
):
328395
started = time.perf_counter()
329396
_rp1_matches(content)

0 commit comments

Comments
 (0)