fix(kvb-check-security): never copy a file through a symlinked folder
copy() checked is_symlink() only on the last path part. When a committed folder is replaced by a link to an outside directory, git diff still lists the files below it, so root / name resolved through the link and the scan copied and read a file outside the repository. A source whose resolved path leaves the repository is now skipped. Found by an independent skeptic review of a MEDUSA temp-file hit. 🎯 Fix: 96 % 💥 Break: 2 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 94 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
731d0df43b
commit
67cbe0ddd3
2 files changed
+16
-2
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user