Skip to content

Commit 901acae

Browse files
committed
fix(rp1): respect YAML runner comments and quoted args keys
Signed-off-by: Whj9283 <[email protected]>
1 parent 9be213a commit 901acae

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ def analyzer_exhausted(self) -> bool:
129129
)
130130
_RP1_CONFIG_RUNNER = re.compile(
131131
r"^(?P<indent>[ \t]*)(?P<item>-[ \t]+)?(?:command|cmd)[ \t]*:[ \t]*"
132-
r"(?P<quote>[\"']?)(?P<runner>(?:[^\s\"'#]*/)?p?npx)(?P=quote)[ \t]*(?:#.*)?$",
132+
r"(?P<quote>[\"'])?(?P<runner>(?:[^\s\"'#]*/)?p?npx)"
133+
r"(?(quote)(?P=quote)|(?=[ \t]|$))[ \t]*(?:#.*)?$",
133134
re.IGNORECASE,
134135
)
135136
_RP1_CONFIG_ARGS = re.compile(
136-
r"^(?P<indent>[ \t]*)(?P<item>-[ \t]+)?args[ \t]*:[ \t]*(?P<value>.*)$",
137+
r"^(?P<indent>[ \t]*)(?P<item>-[ \t]+)?(?:args|\"args\"|'args')[ \t]*:[ \t]*(?P<value>.*)$",
137138
re.IGNORECASE,
138139
)
139140
_RP1_CONFIG_KEY = re.compile(

‎tests/test_mcp_rug_pull.py‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,102 @@ def test_rp1_yaml_mapping_indicator_requires_separator(line, is_key):
318318
assert bool(_RP1_CONFIG_KEY.match(line)) is is_key
319319

320320

321+
@pytest.mark.parametrize("runner", ["npx", "pnpx", "/usr/bin/npx"])
322+
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
323+
@pytest.mark.parametrize(
324+
("value", "expected"),
325+
[
326+
("RUNNER#suffix", False),
327+
("RUNNER#", False),
328+
('"RUNNER#suffix"', False),
329+
("'RUNNER#suffix'", False),
330+
("RUNNER # comment", True),
331+
('"RUNNER"# comment', True),
332+
("'RUNNER'# comment", True),
333+
('"RUNNER" # comment', True),
334+
("RUNNER", True),
335+
],
336+
)
337+
def test_rp1_yaml_runner_hash_boundaries(runner, newline, value, expected):
338+
value = value.replace("RUNNER", runner)
339+
content = f'mcpServers:\n fs:\n command: {value}\n args: ["@scope/server"]\n'
340+
content = content.replace("\n", newline)
341+
parsed = yaml.safe_load(content)["mcpServers"]["fs"]
342+
assert (parsed["command"] == runner) is expected
343+
matches = _iter_config_npx_commands(content, _RugPullBudget({}), "mcp.yaml")
344+
assert len(matches) == int(expected)
345+
rp1 = [
346+
f for f in node(_state(file_cache={"mcp.yaml": content}))["findings"] if f.rule_id == "RP1"
347+
]
348+
assert len(rp1) == int(expected)
349+
350+
351+
@pytest.mark.parametrize("runner", ["npx", "pnpx", "/usr/bin/npx"])
352+
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
353+
def test_rp1_yaml_runner_tab_comment_retains_existing_scanner_behavior(runner, newline):
354+
# PyYAML rejects raw tab separators; this is a scanner compatibility control,
355+
# separate from the parsed-YAML oracle above.
356+
content = f'command: {runner}\t# comment\nargs: ["@scope/server"]\n'.replace("\n", newline)
357+
matches = _iter_config_npx_commands(content, _RugPullBudget({}), "mcp.yaml")
358+
assert len(matches) == 1
359+
assert matches[0][2] == "@scope/server"
360+
361+
362+
@pytest.mark.parametrize("key", ["args", '"args"', "'args'"])
363+
@pytest.mark.parametrize("layout", ["mapping", "list"])
364+
@pytest.mark.parametrize("args_first", [False, True])
365+
@pytest.mark.parametrize("flow", [False, True])
366+
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
367+
@pytest.mark.parametrize("pinned", [False, True])
368+
def test_rp1_yaml_quoted_args_keys(key, layout, args_first, flow, newline, pinned):
369+
package = "@scope/[email protected]" if pinned else "@scope/server"
370+
header, indent = (
371+
("mcpServers:\n fs:\n", " ") if layout == "mapping" else ("servers:\n", " ")
372+
)
373+
command = "command: npx\n"
374+
args = (
375+
f'{key}: ["-y", "{package}"]\n'
376+
if flow
377+
else f'{key}:\n{indent}- -y\n{indent}- "{package}"\n'
378+
)
379+
first, second = (args, command) if args_first else (command, args)
380+
marker = "- " if layout == "list" else indent
381+
content = (header + marker + first + indent + second).replace("\n", newline)
382+
parsed = yaml.safe_load(content)
383+
server = parsed["mcpServers"]["fs"] if layout == "mapping" else parsed["servers"][0]
384+
assert server == {"command": "npx", "args": ["-y", package]}
385+
matches = _iter_config_npx_commands(content, _RugPullBudget({}), "mcp.yaml")
386+
assert len(matches) == 1
387+
assert matches[0][2] == package
388+
rp1 = [
389+
f for f in node(_state(file_cache={"mcp.yaml": content}))["findings"] if f.rule_id == "RP1"
390+
]
391+
assert len(rp1) == (0 if pinned else 1)
392+
if rp1:
393+
assert rp1[0].start_line == next(
394+
i for i, line in enumerate(content.splitlines(), 1) if "command:" in line
395+
)
396+
397+
398+
@pytest.mark.parametrize("key", ['"args"', "'args'"])
399+
@pytest.mark.parametrize("layout", ["mapping", "list"])
400+
@pytest.mark.parametrize("args_first", [False, True])
401+
def test_rp1_yaml_quoted_args_do_not_cross_server_boundaries(key, layout, args_first):
402+
command = "command: npx\n"
403+
args = f'{key}: ["@scope/server"]\n'
404+
first, second = (args, command) if args_first else (command, args)
405+
content = (
406+
f"mcpServers:\n first:\n {first} second:\n {second}"
407+
if layout == "mapping"
408+
else f"servers:\n - {first} - {second}"
409+
)
410+
assert yaml.safe_load(content) is not None
411+
assert not _iter_config_npx_commands(content, _RugPullBudget({}), "mcp.yaml")
412+
assert not [
413+
f for f in node(_state(file_cache={"mcp.yaml": content}))["findings"] if f.rule_id == "RP1"
414+
]
415+
416+
321417
@pytest.mark.parametrize("direction", ["before", "after"])
322418
@pytest.mark.parametrize("at_limit", [True, False])
323419
def test_rp1_yaml_physical_search_hard_limit(direction, at_limit):

0 commit comments

Comments
 (0)