mcpscan-cli 0.22.0__tar.gz → 0.23.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/PKG-INFO +1 -1
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/__init__.py +1 -1
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/path_traversal.py +71 -19
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan_cli.egg-info/PKG-INFO +1 -1
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/pyproject.toml +1 -1
- mcpscan_cli-0.23.0/tests/test_rule_mcp007.py +194 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_scanner.py +1 -1
- mcpscan_cli-0.22.0/tests/test_rule_mcp007.py +0 -107
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/LICENSE +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/README.md +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/__main__.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/cli.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/discover.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/drift.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/findings.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/fixer.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/loaders.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/mcp_server.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/owasp.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/report.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/__init__.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/auth_gaps.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/base.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/command_injection.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/debug_endpoint.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/dom_trust.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/hooks.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/insecure_deser.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/insecure_transport.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/origin_check.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/permissions.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/policy_override.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/sdk_versions.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/secrets.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/server_auth.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/ssrf.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/tool_annotations.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/tool_poisoning.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/webfetch_domain.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/rules/workflow_injection.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/runtime/__init__.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/runtime/guard.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/runtime/provenance.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/runtime/rugpull.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/runtime/sanitizer.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/scanner.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan/suppress.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan_cli.egg-info/SOURCES.txt +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan_cli.egg-info/dependency_links.txt +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan_cli.egg-info/entry_points.txt +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/mcpscan_cli.egg-info/top_level.txt +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/setup.cfg +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_debug_endpoint.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_discover.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_dom_trust.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_drift.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_mcp_server.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_origin_check.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_rule_mcp001.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_rule_mcp015.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_runtime_guard.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_runtime_provenance.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_runtime_rugpull.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_runtime_sanitizer.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_server_auth.py +0 -0
- {mcpscan_cli-0.22.0 → mcpscan_cli-0.23.0}/tests/test_workflow_injection.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mcpscan-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.23.0
|
|
4
4
|
Summary: Supply-chain security scanner for MCP servers and Claude Code projects — catch tool-poisoning, command injection and risky permissions before you install.
|
|
5
5
|
Author: Kiell Tampubolon
|
|
6
6
|
License: MIT
|
|
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|
|
9
9
|
|
|
10
10
|
import ast
|
|
11
11
|
import re
|
|
12
|
-
from typing import List
|
|
12
|
+
from typing import List, Tuple
|
|
13
13
|
|
|
14
14
|
from ..findings import Finding, Severity
|
|
15
15
|
from ..loaders import FileInfo, by_kind
|
|
@@ -30,21 +30,39 @@ TRAVERSAL = re.compile(r"\.\./|\.\.\\")
|
|
|
30
30
|
BOUNDARY_CALL = re.compile(r"\w*(?:safe_path|validate_path)\w*\s*\(")
|
|
31
31
|
RESOLVE = re.compile(r"\b(?:realpath|resolve)\s*\(")
|
|
32
32
|
PREFIX_CHECK = re.compile(r"\b(?:startswith|is_relative_to|commonpath)\s*\(")
|
|
33
|
+
# Parameters that never carry caller input (pytest fixtures included).
|
|
34
|
+
NOT_CALLER_INPUT = {"self", "cls", "tmp_path", "tmpdir"}
|
|
33
35
|
TWO_PATH_SINKS = {("shutil", "copy"), ("shutil", "copy2"), ("shutil", "copyfile"),
|
|
34
36
|
("shutil", "move"), ("os", "rename"), ("os", "replace")}
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
def _derived(value: ast.AST, tainted: set) -> bool:
|
|
38
|
-
"""`p`, `
|
|
40
|
+
"""`p`, `p[k]`, `p.get(k)`, `base / p`, or Path/abspath/str/os.path.join over p, where p is tainted."""
|
|
39
41
|
if isinstance(value, ast.Name):
|
|
40
42
|
return value.id in tainted
|
|
41
|
-
if isinstance(value, ast.
|
|
43
|
+
if isinstance(value, ast.Subscript):
|
|
44
|
+
return _derived(value.value, tainted)
|
|
45
|
+
if isinstance(value, ast.BinOp) and isinstance(value.op, ast.Div):
|
|
46
|
+
return _derived(value.left, tainted) or _derived(value.right, tainted)
|
|
47
|
+
if isinstance(value, ast.Call):
|
|
42
48
|
fn = value.func
|
|
49
|
+
if isinstance(fn, ast.Attribute) and fn.attr == "get":
|
|
50
|
+
return _derived(fn.value, tainted)
|
|
43
51
|
name = fn.id if isinstance(fn, ast.Name) else fn.attr if isinstance(fn, ast.Attribute) else ""
|
|
44
|
-
return name in ("Path", "abspath"
|
|
52
|
+
return name in ("Path", "abspath", "str", "join") \
|
|
53
|
+
and any(_derived(a, tainted) for a in value.args)
|
|
45
54
|
return False
|
|
46
55
|
|
|
47
56
|
|
|
57
|
+
def _is_join(value: ast.AST) -> bool:
|
|
58
|
+
"""`os.path.join(...)` or `base / name`: a path built onto a base directory."""
|
|
59
|
+
if isinstance(value, ast.BinOp):
|
|
60
|
+
return isinstance(value.op, ast.Div)
|
|
61
|
+
return isinstance(value, ast.Call) and isinstance(value.func, ast.Attribute) \
|
|
62
|
+
and value.func.attr == "join" and isinstance(value.func.value, ast.Attribute) \
|
|
63
|
+
and value.func.value.attr == "path"
|
|
64
|
+
|
|
65
|
+
|
|
48
66
|
def _write_targets(call: ast.Call) -> List[ast.AST]:
|
|
49
67
|
"""Path arguments of a filesystem write sink, or [] if `call` is not one."""
|
|
50
68
|
fn = call.func
|
|
@@ -64,13 +82,24 @@ def _write_targets(call: ast.Call) -> List[ast.AST]:
|
|
|
64
82
|
return []
|
|
65
83
|
|
|
66
84
|
|
|
67
|
-
def
|
|
68
|
-
"""
|
|
85
|
+
def _save_targets(call: ast.Call) -> List[ast.AST]:
|
|
86
|
+
"""openpyxl/PIL-style `wb.save(path)`. One positional arg skips torch/np.save(obj, path).
|
|
87
|
+
Only a path joined in the same function counts: a helper handed a finished path
|
|
88
|
+
(`wb.save(filepath)`) is checked where that path is built."""
|
|
89
|
+
fn = call.func
|
|
90
|
+
if isinstance(fn, ast.Attribute) and fn.attr == "save" and len(call.args) == 1:
|
|
91
|
+
return [call.args[0]]
|
|
92
|
+
return []
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _param_write_lines(text: str) -> List[Tuple[int, bool]]:
|
|
96
|
+
"""(line, is_builder) where a function writes to, or returns a path joined from, one of its
|
|
97
|
+
own parameters (or `arguments.get(...)`/`arguments[...]` of one) with no base-dir check."""
|
|
69
98
|
try:
|
|
70
99
|
tree = ast.parse(text)
|
|
71
100
|
except (SyntaxError, ValueError):
|
|
72
101
|
return []
|
|
73
|
-
hits =
|
|
102
|
+
hits = {}
|
|
74
103
|
for fn in ast.walk(tree):
|
|
75
104
|
if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
|
76
105
|
continue
|
|
@@ -78,18 +107,29 @@ def _param_write_lines(text: str) -> List[int]:
|
|
|
78
107
|
if BOUNDARY_CALL.search(src) or (RESOLVE.search(src) and PREFIX_CHECK.search(src)):
|
|
79
108
|
continue
|
|
80
109
|
a = fn.args
|
|
81
|
-
tainted = {p.arg for p in a.posonlyargs + a.args + a.kwonlyargs} -
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
110
|
+
tainted = {p.arg for p in a.posonlyargs + a.args + a.kwonlyargs} - NOT_CALLER_INPUT
|
|
111
|
+
joined = set() # tainted names built with os.path.join or `/`
|
|
112
|
+
steps = sorted((n for n in ast.walk(fn) if isinstance(n, (ast.Assign, ast.For))),
|
|
113
|
+
key=lambda n: n.lineno)
|
|
114
|
+
for n in steps:
|
|
115
|
+
if isinstance(n, ast.For): # `for item in tainted_list` taints item
|
|
116
|
+
target, value = n.target, n.iter
|
|
117
|
+
else:
|
|
118
|
+
target, value = (n.targets[0] if len(n.targets) == 1 else None), n.value
|
|
119
|
+
if isinstance(target, ast.Name) and _derived(value, tainted):
|
|
120
|
+
tainted.add(target.id)
|
|
121
|
+
if _is_join(value) or _derived(value, joined):
|
|
122
|
+
joined.add(target.id)
|
|
88
123
|
for n in ast.walk(fn):
|
|
89
|
-
if isinstance(n, ast.Call) and
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
124
|
+
if isinstance(n, ast.Call) and (
|
|
125
|
+
any(_derived(t, tainted) for t in _write_targets(n))
|
|
126
|
+
or any(_derived(t, joined) or (_is_join(t) and _derived(t, tainted))
|
|
127
|
+
for t in _save_targets(n))):
|
|
128
|
+
hits[n.lineno] = False
|
|
129
|
+
elif isinstance(n, ast.Return) and n.value is not None and _is_join(n.value) \
|
|
130
|
+
and _derived(n.value, tainted):
|
|
131
|
+
hits.setdefault(n.lineno, True)
|
|
132
|
+
return sorted(hits.items())
|
|
93
133
|
|
|
94
134
|
|
|
95
135
|
@register
|
|
@@ -121,9 +161,21 @@ class PathTraversal(Rule):
|
|
|
121
161
|
))
|
|
122
162
|
if is_py:
|
|
123
163
|
seen = {x.line for x in out if x.path == f.relpath}
|
|
124
|
-
for i in _param_write_lines(f.text):
|
|
164
|
+
for i, builder in _param_write_lines(f.text):
|
|
125
165
|
if i in seen:
|
|
126
166
|
continue
|
|
167
|
+
if builder:
|
|
168
|
+
out.append(self.finding(
|
|
169
|
+
f, i, f.lines[i - 1],
|
|
170
|
+
title="Path helper joins a caller-controlled name onto a base "
|
|
171
|
+
"directory with no containment check",
|
|
172
|
+
detail="os.path.join/`/` keep an absolute name and `../` walks out of "
|
|
173
|
+
"the base. Resolve the result and confirm it stays inside the "
|
|
174
|
+
"base (realpath + commonpath, or resolve() + is_relative_to) "
|
|
175
|
+
"before returning it (CVE-2026-40576 shape).",
|
|
176
|
+
severity=Severity.MEDIUM,
|
|
177
|
+
))
|
|
178
|
+
continue
|
|
127
179
|
out.append(self.finding(
|
|
128
180
|
f, i, f.lines[i - 1],
|
|
129
181
|
title="Write to a caller-controlled path with no base-directory check",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mcpscan-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.23.0
|
|
4
4
|
Summary: Supply-chain security scanner for MCP servers and Claude Code projects — catch tool-poisoning, command injection and risky permissions before you install.
|
|
5
5
|
Author: Kiell Tampubolon
|
|
6
6
|
License: MIT
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "mcpscan-cli"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.23.0"
|
|
8
8
|
description = "Supply-chain security scanner for MCP servers and Claude Code projects — catch tool-poisoning, command injection and risky permissions before you install."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""Tests for MCP007 (path traversal), mcpscan/rules/path_traversal.py.
|
|
2
|
+
|
|
3
|
+
Regression coverage for CVE-2026-27825 (sooperset/mcp-atlassian <= 0.16.0):
|
|
4
|
+
download_attachment(url, target_path) wrote to `target_path`, a bare tool
|
|
5
|
+
parameter, with no base-directory check. The line-based check missed it because
|
|
6
|
+
nothing on the `open(...)` line was interpolated. v0.17.0 fixed it with
|
|
7
|
+
validate_safe_path(). Also covers a false positive from the same repo: browser
|
|
8
|
+
`window.open(` inside a Python HTML string.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import unittest
|
|
12
|
+
|
|
13
|
+
from mcpscan.findings import Severity
|
|
14
|
+
from mcpscan.loaders import FileInfo
|
|
15
|
+
from mcpscan.rules.path_traversal import PathTraversal
|
|
16
|
+
|
|
17
|
+
VULNERABLE = '''
|
|
18
|
+
import os
|
|
19
|
+
|
|
20
|
+
def download_attachment(self, url, target_path):
|
|
21
|
+
if not os.path.isabs(target_path):
|
|
22
|
+
target_path = os.path.abspath(target_path)
|
|
23
|
+
with open(target_path, "wb") as f:
|
|
24
|
+
f.write(b"x")
|
|
25
|
+
'''
|
|
26
|
+
|
|
27
|
+
FIXED = '''
|
|
28
|
+
import os
|
|
29
|
+
from .io import validate_safe_path
|
|
30
|
+
|
|
31
|
+
def download_attachment(self, url, target_path):
|
|
32
|
+
if not os.path.isabs(target_path):
|
|
33
|
+
target_path = os.path.abspath(target_path)
|
|
34
|
+
validate_safe_path(target_path)
|
|
35
|
+
with open(target_path, "wb") as f:
|
|
36
|
+
f.write(b"x")
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _file(text, relpath="server.py"):
|
|
41
|
+
return FileInfo(
|
|
42
|
+
relpath=relpath, abspath=relpath, text=text, kind="source", in_dot_claude=False
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _check(text):
|
|
47
|
+
return PathTraversal().check([_file(text)])
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class TestParamWriteSink(unittest.TestCase):
|
|
51
|
+
def test_cve_2026_27825_shape_is_flagged(self):
|
|
52
|
+
findings = _check(VULNERABLE)
|
|
53
|
+
self.assertEqual([f.line for f in findings], [7])
|
|
54
|
+
self.assertEqual(findings[0].severity, Severity.HIGH)
|
|
55
|
+
|
|
56
|
+
def test_validate_safe_path_fix_is_clean(self):
|
|
57
|
+
self.assertEqual(_check(FIXED), [])
|
|
58
|
+
|
|
59
|
+
def test_resolve_plus_prefix_check_is_clean(self):
|
|
60
|
+
self.assertEqual(_check(
|
|
61
|
+
"from pathlib import Path\n"
|
|
62
|
+
"def save(p, data):\n"
|
|
63
|
+
" q = Path(p).resolve()\n"
|
|
64
|
+
" if not q.is_relative_to(BASE):\n"
|
|
65
|
+
" raise ValueError(p)\n"
|
|
66
|
+
" q.write_bytes(data)\n"
|
|
67
|
+
), [])
|
|
68
|
+
|
|
69
|
+
def test_path_derived_write_text_and_shutil_are_flagged(self):
|
|
70
|
+
findings = _check(
|
|
71
|
+
"import shutil\nfrom pathlib import Path\n"
|
|
72
|
+
"def save(dest, src):\n"
|
|
73
|
+
" out = Path(dest)\n"
|
|
74
|
+
" out.write_text('x')\n"
|
|
75
|
+
" shutil.copy(src, dest)\n"
|
|
76
|
+
)
|
|
77
|
+
self.assertEqual([f.line for f in findings], [5, 6])
|
|
78
|
+
|
|
79
|
+
def test_read_mode_and_mkdir_are_not_write_sinks(self):
|
|
80
|
+
self.assertEqual(_check(
|
|
81
|
+
"from pathlib import Path\n"
|
|
82
|
+
"def load(p):\n"
|
|
83
|
+
" Path(p).mkdir(parents=True)\n"
|
|
84
|
+
" return open(p, 'rb').read()\n"
|
|
85
|
+
), [])
|
|
86
|
+
|
|
87
|
+
def test_non_parameter_path_is_not_flagged(self):
|
|
88
|
+
self.assertEqual(_check(
|
|
89
|
+
"def save(data):\n"
|
|
90
|
+
" with open('out.json', 'w') as f:\n"
|
|
91
|
+
" f.write(data)\n"
|
|
92
|
+
), [])
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class TestJoinedAndArgumentPaths(unittest.TestCase):
|
|
96
|
+
"""CVE-2026-44336 (PraisonAI rules_create), CVE-2026-84201 (appium-mcp-server write_file /
|
|
97
|
+
write_files_batch), CVE-2026-40576 (excel-mcp-server get_excel_path)."""
|
|
98
|
+
|
|
99
|
+
def test_join_then_open_write_is_flagged(self):
|
|
100
|
+
findings = _check(
|
|
101
|
+
"import os\n"
|
|
102
|
+
"def rules_create(rule_name, content):\n"
|
|
103
|
+
" rule_path = os.path.join(RULES_DIR, rule_name)\n"
|
|
104
|
+
" with open(rule_path, 'w') as f:\n"
|
|
105
|
+
" f.write(content)\n"
|
|
106
|
+
)
|
|
107
|
+
self.assertEqual([f.line for f in findings], [4])
|
|
108
|
+
self.assertEqual(findings[0].severity, Severity.HIGH)
|
|
109
|
+
|
|
110
|
+
def test_join_with_commonpath_check_is_clean(self):
|
|
111
|
+
self.assertEqual(_check(
|
|
112
|
+
"import os\n"
|
|
113
|
+
"def rules_create(rule_name, content):\n"
|
|
114
|
+
" rule_path = os.path.realpath(os.path.join(RULES_DIR, rule_name))\n"
|
|
115
|
+
" if os.path.commonpath([RULES_DIR, rule_path]) != RULES_DIR:\n"
|
|
116
|
+
" raise ValueError(rule_name)\n"
|
|
117
|
+
" with open(rule_path, 'w') as f:\n"
|
|
118
|
+
" f.write(content)\n"
|
|
119
|
+
), [])
|
|
120
|
+
|
|
121
|
+
def test_arguments_get_and_batch_loop_are_flagged(self):
|
|
122
|
+
findings = _check(
|
|
123
|
+
"async def handle_call_tool(name, arguments):\n"
|
|
124
|
+
" path = arguments.get('path')\n"
|
|
125
|
+
" target = PROJECT_ROOT / path\n"
|
|
126
|
+
" with open(target, 'w') as f:\n"
|
|
127
|
+
" f.write(arguments['content'])\n"
|
|
128
|
+
"async def write_batch(arguments):\n"
|
|
129
|
+
" for file in arguments.get('files', []):\n"
|
|
130
|
+
" (PROJECT_ROOT / file['path']).write_text(file['content'])\n"
|
|
131
|
+
)
|
|
132
|
+
self.assertEqual([f.line for f in findings], [4, 8])
|
|
133
|
+
|
|
134
|
+
def test_arguments_get_with_resolve_check_is_clean(self):
|
|
135
|
+
self.assertEqual(_check(
|
|
136
|
+
"async def handle_call_tool(name, arguments):\n"
|
|
137
|
+
" target = (PROJECT_ROOT / arguments['path']).resolve()\n"
|
|
138
|
+
" if not target.is_relative_to(PROJECT_ROOT):\n"
|
|
139
|
+
" raise ValueError('outside project')\n"
|
|
140
|
+
" target.write_text(arguments['content'])\n"
|
|
141
|
+
), [])
|
|
142
|
+
|
|
143
|
+
def test_path_builder_helper_is_flagged(self):
|
|
144
|
+
findings = _check(
|
|
145
|
+
"import os\n"
|
|
146
|
+
"def get_excel_path(filename):\n"
|
|
147
|
+
" if os.path.isabs(filename):\n"
|
|
148
|
+
" return filename\n"
|
|
149
|
+
" return os.path.join(EXCEL_FILES_PATH, filename)\n"
|
|
150
|
+
)
|
|
151
|
+
self.assertEqual([f.line for f in findings], [5])
|
|
152
|
+
self.assertEqual(findings[0].severity, Severity.MEDIUM)
|
|
153
|
+
|
|
154
|
+
def test_path_builder_with_containment_check_is_clean(self):
|
|
155
|
+
self.assertEqual(_check(
|
|
156
|
+
"import os\n"
|
|
157
|
+
"def get_excel_path(filename):\n"
|
|
158
|
+
" base = os.path.realpath(EXCEL_FILES_PATH)\n"
|
|
159
|
+
" p = os.path.realpath(os.path.join(base, filename))\n"
|
|
160
|
+
" if os.path.commonpath([base, p]) != base:\n"
|
|
161
|
+
" raise ValueError(filename)\n"
|
|
162
|
+
" return p\n"
|
|
163
|
+
), [])
|
|
164
|
+
|
|
165
|
+
def test_save_of_joined_path_is_flagged_but_not_a_handed_in_path(self):
|
|
166
|
+
findings = _check(
|
|
167
|
+
"import os\n"
|
|
168
|
+
"def export(wb, name):\n"
|
|
169
|
+
" wb.save(os.path.join(OUT_DIR, name))\n"
|
|
170
|
+
"def write_data(filepath, wb):\n"
|
|
171
|
+
" wb.save(filepath)\n"
|
|
172
|
+
)
|
|
173
|
+
self.assertEqual([f.line for f in findings], [3])
|
|
174
|
+
|
|
175
|
+
def test_pytest_tmp_path_is_not_caller_input(self):
|
|
176
|
+
self.assertEqual(_check(
|
|
177
|
+
"def test_upload(tmp_path):\n"
|
|
178
|
+
" (tmp_path / 'a.txt').write_text('x')\n"
|
|
179
|
+
), [])
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class TestWindowOpenFalsePositive(unittest.TestCase):
|
|
183
|
+
def test_js_window_open_in_python_string_is_not_flagged(self):
|
|
184
|
+
self.assertEqual(_check(
|
|
185
|
+
'html = f"""<script>window.open(\'\', \'_self\').close(); {status}</script>"""\n'
|
|
186
|
+
), [])
|
|
187
|
+
|
|
188
|
+
def test_interpolated_open_is_still_flagged(self):
|
|
189
|
+
findings = _check('open("data/" + name).read()\n') # mcpscan: ignore[MCP007]
|
|
190
|
+
self.assertEqual(len(findings), 1)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
if __name__ == "__main__":
|
|
194
|
+
unittest.main()
|
|
@@ -392,7 +392,7 @@ class TestFixer(unittest.TestCase):
|
|
|
392
392
|
# readable/writable after this helper returns (see apply_fixes tests).
|
|
393
393
|
tmp = tempfile.mkdtemp()
|
|
394
394
|
abspath = os.path.join(tmp, relpath)
|
|
395
|
-
with open(abspath, "w", encoding="utf-8") as fh:
|
|
395
|
+
with open(abspath, "w", encoding="utf-8") as fh: # mcpscan: ignore[MCP007]
|
|
396
396
|
fh.write(text)
|
|
397
397
|
f = FileInfo(relpath=relpath, abspath=abspath, text=text,
|
|
398
398
|
kind="source", in_dot_claude=False)
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"""Tests for MCP007 (path traversal), mcpscan/rules/path_traversal.py.
|
|
2
|
-
|
|
3
|
-
Regression coverage for CVE-2026-27825 (sooperset/mcp-atlassian <= 0.16.0):
|
|
4
|
-
download_attachment(url, target_path) wrote to `target_path`, a bare tool
|
|
5
|
-
parameter, with no base-directory check. The line-based check missed it because
|
|
6
|
-
nothing on the `open(...)` line was interpolated. v0.17.0 fixed it with
|
|
7
|
-
validate_safe_path(). Also covers a false positive from the same repo: browser
|
|
8
|
-
`window.open(` inside a Python HTML string.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
import unittest
|
|
12
|
-
|
|
13
|
-
from mcpscan.findings import Severity
|
|
14
|
-
from mcpscan.loaders import FileInfo
|
|
15
|
-
from mcpscan.rules.path_traversal import PathTraversal
|
|
16
|
-
|
|
17
|
-
VULNERABLE = '''
|
|
18
|
-
import os
|
|
19
|
-
|
|
20
|
-
def download_attachment(self, url, target_path):
|
|
21
|
-
if not os.path.isabs(target_path):
|
|
22
|
-
target_path = os.path.abspath(target_path)
|
|
23
|
-
with open(target_path, "wb") as f:
|
|
24
|
-
f.write(b"x")
|
|
25
|
-
'''
|
|
26
|
-
|
|
27
|
-
FIXED = '''
|
|
28
|
-
import os
|
|
29
|
-
from .io import validate_safe_path
|
|
30
|
-
|
|
31
|
-
def download_attachment(self, url, target_path):
|
|
32
|
-
if not os.path.isabs(target_path):
|
|
33
|
-
target_path = os.path.abspath(target_path)
|
|
34
|
-
validate_safe_path(target_path)
|
|
35
|
-
with open(target_path, "wb") as f:
|
|
36
|
-
f.write(b"x")
|
|
37
|
-
'''
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _file(text, relpath="server.py"):
|
|
41
|
-
return FileInfo(
|
|
42
|
-
relpath=relpath, abspath=relpath, text=text, kind="source", in_dot_claude=False
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def _check(text):
|
|
47
|
-
return PathTraversal().check([_file(text)])
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
class TestParamWriteSink(unittest.TestCase):
|
|
51
|
-
def test_cve_2026_27825_shape_is_flagged(self):
|
|
52
|
-
findings = _check(VULNERABLE)
|
|
53
|
-
self.assertEqual([f.line for f in findings], [7])
|
|
54
|
-
self.assertEqual(findings[0].severity, Severity.HIGH)
|
|
55
|
-
|
|
56
|
-
def test_validate_safe_path_fix_is_clean(self):
|
|
57
|
-
self.assertEqual(_check(FIXED), [])
|
|
58
|
-
|
|
59
|
-
def test_resolve_plus_prefix_check_is_clean(self):
|
|
60
|
-
self.assertEqual(_check(
|
|
61
|
-
"from pathlib import Path\n"
|
|
62
|
-
"def save(p, data):\n"
|
|
63
|
-
" q = Path(p).resolve()\n"
|
|
64
|
-
" if not q.is_relative_to(BASE):\n"
|
|
65
|
-
" raise ValueError(p)\n"
|
|
66
|
-
" q.write_bytes(data)\n"
|
|
67
|
-
), [])
|
|
68
|
-
|
|
69
|
-
def test_path_derived_write_text_and_shutil_are_flagged(self):
|
|
70
|
-
findings = _check(
|
|
71
|
-
"import shutil\nfrom pathlib import Path\n"
|
|
72
|
-
"def save(dest, src):\n"
|
|
73
|
-
" out = Path(dest)\n"
|
|
74
|
-
" out.write_text('x')\n"
|
|
75
|
-
" shutil.copy(src, dest)\n"
|
|
76
|
-
)
|
|
77
|
-
self.assertEqual([f.line for f in findings], [5, 6])
|
|
78
|
-
|
|
79
|
-
def test_read_mode_and_mkdir_are_not_write_sinks(self):
|
|
80
|
-
self.assertEqual(_check(
|
|
81
|
-
"from pathlib import Path\n"
|
|
82
|
-
"def load(p):\n"
|
|
83
|
-
" Path(p).mkdir(parents=True)\n"
|
|
84
|
-
" return open(p, 'rb').read()\n"
|
|
85
|
-
), [])
|
|
86
|
-
|
|
87
|
-
def test_non_parameter_path_is_not_flagged(self):
|
|
88
|
-
self.assertEqual(_check(
|
|
89
|
-
"def save(data):\n"
|
|
90
|
-
" with open('out.json', 'w') as f:\n"
|
|
91
|
-
" f.write(data)\n"
|
|
92
|
-
), [])
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
class TestWindowOpenFalsePositive(unittest.TestCase):
|
|
96
|
-
def test_js_window_open_in_python_string_is_not_flagged(self):
|
|
97
|
-
self.assertEqual(_check(
|
|
98
|
-
'html = f"""<script>window.open(\'\', \'_self\').close(); {status}</script>"""\n'
|
|
99
|
-
), [])
|
|
100
|
-
|
|
101
|
-
def test_interpolated_open_is_still_flagged(self):
|
|
102
|
-
findings = _check('open("data/" + name).read()\n') # mcpscan: ignore[MCP007]
|
|
103
|
-
self.assertEqual(len(findings), 1)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if __name__ == "__main__":
|
|
107
|
-
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|