diff --git a/skills/kvb-check-security/scripts/security.py b/skills/kvb-check-security/scripts/security.py index d6e3a73..494b32a 100755 --- a/skills/kvb-check-security/scripts/security.py +++ b/skills/kvb-check-security/scripts/security.py @@ -291,11 +291,12 @@ def copy(root: Path, paths: list[str], tree: Path) -> dict[str, str]: the tree path of every copied file mapped to its repository path. """ names = {} + top = root.resolve() taken = set(paths) | {parent.as_posix() for name in paths for parent in Path(name).parents} for name in paths: source = root / name - # Trip-wire: a symlink's target may lie outside the repository; even a copied link lets the scan read it. - if source.is_symlink() or not source.is_file(): + # Trip-wire: a symlink's target may lie outside the repository, also through a linked parent folder; even a copied link lets the scan read it. + if source.is_symlink() or not source.resolve().is_relative_to(top) or not source.is_file(): continue scratch = name if Path(name).suffix.lower() in SKIPPED: diff --git a/tests/test_security.py b/tests/test_security.py index 379202e..eadca0a 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -609,6 +609,19 @@ class TestSecurity(unittest.TestCase): unread = f"{never}: medusa's own types, exclusions and limits are unreadable from its source: {HAND}" self.assertEqual((result.stdout, result.returncode), (f"🛡️ security – ({unread})\n", 4)) + def test_a_file_behind_a_symlinked_folder_is_never_copied(self): + self.write("dir/a.py", "x = 1\n") + self.git("add", "-A") + self.git("commit", "-qm", "dir") + outside = self.base / "outside" + outside.mkdir() + (outside / "a.py").write_text("secret = 1 # MARK-CRITICAL\n", encoding="utf-8") + shutil.rmtree(self.repo / "dir") + (self.repo / "dir").symlink_to(outside) + self.write("app.py", "print(1)\nx = 1 # MARK-LOW\n") + found = json.loads(self.scan("--json", env=self.env).stdout) + self.assertEqual(([(f["path"], f["severity"]) for f in found["findings"]], found["files"]), ([("app.py", "low")], 1)) + def test_a_file_a_path_exclusion_drops_is_scanned_and_named_at_its_repository_path(self): self.write("app.py", "print(1)\nx = 1 # MARK-HIGH\n") self.write("utils/env/run.py", "y = 1 # MARK-LOW\n")