Merge commit 'faec5fd' into fix/kvb-check-security-partial-cache
Resolution: rules, limits, line/file counts and not-covered judge the aliased tree path medusa scans and name the repository path; findings, cache key (tree path, repository path, not covered) and accept rows keep the real path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ed45b35dd7
2 files changed
+143
-27
No files matched your search
+84
-2
@@ -27,7 +27,7 @@ out.mkdir(parents=True)
|
||||
limit = int(os.environ.get("FAKE_MEDUSA_FILE_LIMIT", str(2 * 1024 * 1024)))
|
||||
findings, taken, counted = [], 0, 0
|
||||
for path in sorted(p for p in target.rglob("*") if p.is_file()):
|
||||
if path.suffix not in (".py", ".json") and not path.name.startswith("Dockerfile"):
|
||||
if path.suffix.lower() not in (".py", ".json", ".js") and not path.name.startswith("Dockerfile"):
|
||||
continue
|
||||
if path.name == os.environ.get("FAKE_MEDUSA_LOSE") or any(p in str(path.relative_to(target)) for p in ("build", "env")):
|
||||
continue
|
||||
@@ -67,7 +67,7 @@ PARALLEL = """MAX_SCAN_FILE_SIZE = {size}
|
||||
|
||||
|
||||
class MedusaParallelScanner:
|
||||
FILE_SCANNERS = {{".py": "python", ".json": "json"}}
|
||||
FILE_SCANNERS = {{".py": "python", ".json": "json", ".js": "javascript"}}
|
||||
|
||||
def find_scannable_files(self):
|
||||
special_exact_names: Set[str] = {{"mcp.json"}}
|
||||
@@ -126,6 +126,7 @@ class TestSecurity(unittest.TestCase):
|
||||
subprocess.run(["git", *args], cwd=self.repo, env=self.env, capture_output=True, check=True)
|
||||
|
||||
def write(self, path: str, text: str) -> None:
|
||||
(self.repo / path).parent.mkdir(parents=True, exist_ok=True)
|
||||
(self.repo / path).write_text(text, encoding="utf-8")
|
||||
|
||||
def scan(self, *extra: str, env: dict[str, str]) -> subprocess.CompletedProcess:
|
||||
@@ -273,6 +274,87 @@ class TestSecurity(unittest.TestCase):
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual(result.stdout, "🛡️ security 100 % (medusa: 0 findings, 2 accepted in 1 file)\n")
|
||||
|
||||
def test_mjs_and_cjs_files_medusa_skips_are_scanned_and_reported_at_their_repository_path(self):
|
||||
line = "run(request.query) // MARK-HIGH"
|
||||
self.write("tool.mjs", f"import run from 'run';\n{line}\n")
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual(
|
||||
(result.stdout, result.returncode),
|
||||
("🛡️ security 75 % (medusa: 1 finding in 1 file: high tool.mjs:2)\n", 3),
|
||||
)
|
||||
sha1 = hashlib.sha1(line.encode()).hexdigest()
|
||||
result = self.accept(
|
||||
"--rule", "FAKE-HIGH", "--path", "tool.mjs", "--sha1", sha1, "--line", "2", "--reason", "fixture"
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.write("tool.mjs.js", "a // MARK-MEDIUM\n")
|
||||
self.write("lib.CJS", "b // MARK-LOW\n")
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual(
|
||||
(result.stdout, result.returncode),
|
||||
("🛡️ security 88 % (medusa: 2 findings, 1 accepted in 3 files: medium tool.mjs.js:1, low lib.CJS:1)\n", 3),
|
||||
)
|
||||
|
||||
def test_a_folder_named_like_the_alias_keeps_both_paths_apart(self):
|
||||
self.write("tool.mjs", "a // MARK-HIGH\n")
|
||||
self.write("tool.mjs.js/inner.py", "b # MARK-LOW\n")
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual(
|
||||
(result.stdout, result.returncode),
|
||||
("🛡️ security 73 % (medusa: 2 findings in 2 files: high tool.mjs:1, low tool.mjs.js/inner.py:1)\n", 3),
|
||||
)
|
||||
|
||||
def test_the_cache_keeps_the_repository_path_of_an_aliased_file(self):
|
||||
text = "a // MARK-HIGH\n"
|
||||
self.write("tool.mjs", text)
|
||||
first = json.loads(self.scan("--json", env=self.env).stdout)
|
||||
(self.repo / "tool.mjs").unlink()
|
||||
self.write("tool.mjs.js", text)
|
||||
second = json.loads(self.scan("--json", env=self.env).stdout)
|
||||
self.assertEqual([f["path"] for f in first["findings"] + second["findings"]], ["tool.mjs", "tool.mjs.js"])
|
||||
|
||||
def test_a_nested_mjs_file_is_reported_at_its_nested_path(self):
|
||||
self.write("a/b/tool.mjs", "a // MARK-MEDIUM\n")
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual(
|
||||
(result.stdout, result.returncode),
|
||||
("🛡️ security 90 % (medusa: 1 finding in 1 file: medium a/b/tool.mjs:1)\n", 3),
|
||||
)
|
||||
|
||||
def test_an_mjs_file_name_without_room_for_the_alias_is_scanned_under_a_short_one(self):
|
||||
name = "t" * 250 + ".mjs"
|
||||
self.write(name, "a // MARK-LOW\n")
|
||||
found = json.loads(self.scan("--json", env=self.env).stdout)
|
||||
self.assertEqual((found["files"], [(f["path"], f["line"]) for f in found["findings"]]), (1, [(name, 1)]))
|
||||
|
||||
def test_an_mjs_only_diff_scores_from_a_real_scan_and_only_a_complete_run_is_cached(self):
|
||||
argv = self.bin / "argv.json"
|
||||
folder = self.base / "council" / "security-cache"
|
||||
self.write("tool.mjs", "a // MARK-HIGH\n")
|
||||
rerun = "medusa run incomplete, nothing cached, rerun once the host is idle"
|
||||
timeout = "1 scanner error(s)/timeout(s) during scan: JavaScriptScanner (1)"
|
||||
cases = (
|
||||
({"FAKE_MEDUSA_STDOUT": f"⚠️ {timeout}\n"}, f"{rerun}: {timeout}"),
|
||||
({"FAKE_MEDUSA_FILE_LIMIT": "4"}, f"{rerun}: medusa counted 0 of 2 lines"),
|
||||
)
|
||||
for extra, problem in cases:
|
||||
with self.subTest(problem=problem):
|
||||
argv.unlink(missing_ok=True)
|
||||
result = self.scan(env={**self.env, **extra})
|
||||
self.assertEqual((result.stdout, result.returncode), (f"🛡️ security – ({problem}: {HAND})\n", 4))
|
||||
self.assertTrue(argv.exists())
|
||||
self.assertEqual(list(folder.glob("*.json")), [])
|
||||
scored = ("🛡️ security 75 % (medusa: 1 finding in 1 file: high tool.mjs:1)\n", 3)
|
||||
argv.unlink()
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual((result.stdout, result.returncode), scored)
|
||||
self.assertTrue(argv.exists())
|
||||
self.assertEqual(len(list(folder.glob("*.json"))), 1)
|
||||
argv.unlink()
|
||||
result = self.scan(env=self.env)
|
||||
self.assertEqual((result.stdout, result.returncode), scored)
|
||||
self.assertFalse(argv.exists())
|
||||
|
||||
def test_critical_exits_1_and_floors_at_0(self):
|
||||
self.write("app.py", "a # MARK-CRITICAL\nb # MARK-CRITICAL\nc # MARK-CRITICAL\n")
|
||||
result = self.scan(env=self.env)
|
||||
|
||||
Reference in new issue
Block a user