fix: inspect indexed blobs omitted by sparse checkout

This commit is contained in:
xXJsonDeruloXx committed 2026-11-22 09:10:37 -05:00
1 parent 72a7c18797
commit 486b1d3758
2 files changed
+63 -15

No files matched your search

+50 -14
View File
@@ -33,9 +33,19 @@ VALID_STATES = {"prebuilt-exception", "binary-data", "build-tool"}
def tracked_paths(root: Path) -> list[str]:
data = subprocess.check_output(
["git", "ls-files", "--cached", "-z"], cwd=root
["git", "ls-files", "--cached", "--stage", "-z"], cwd=root
)
return sorted(os.fsdecode(item) for item in data.split(b"\0") if item)
paths = []
for item in data.split(b"\0"):
if not item:
continue
meta, path = item.split(b"\t", 1)
# Gitlinks contain an object ID, not a file blob. They are checked
# separately by the pinned submodule source/build process.
if meta.split()[0] == b"160000":
continue
paths.append(os.fsdecode(path))
return sorted(paths)
def is_binary_payload(relpath: str, prefix: bytes) -> bool:
@@ -47,22 +57,48 @@ def is_binary_payload(relpath: str, prefix: bytes) -> bool:
)
def hash_content(rel: str, handle) -> str | None:
prefix = handle.read(8)
if not is_binary_payload(rel, prefix):
return None
digest = hashlib.sha256()
digest.update(prefix)
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def discover(root: Path) -> dict[str, str]:
"""Hash binary-like tracked files. Sparse-checkout omissions are skipped."""
"""Hash tracked binary payloads, including paths omitted by sparse checkout.
Git's index still holds blob IDs for sparse entries, so read those entries
from Git rather than silently skipping them. A missing blob fails the gate.
"""
result: dict[str, str] = {}
for rel in tracked_paths(root):
file = root / rel
if not file.is_file():
continue
with file.open("rb") as handle:
prefix = handle.read(8)
if not is_binary_payload(rel, prefix):
continue
digest = hashlib.sha256()
digest.update(prefix)
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
result[rel] = digest.hexdigest()
if file.is_file():
with file.open("rb") as handle:
digest = hash_content(rel, handle)
else:
# The source CI checkout deliberately omits docs/releases/media.
# Read the index version so an ELF added there cannot evade the gate.
with subprocess.Popen(
["git", "show", f":{rel}"], cwd=root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
) as proc:
assert proc.stdout is not None
digest = hash_content(rel, proc.stdout)
# Drain even non-binary files before checking the exit status.
for _ in iter(lambda: proc.stdout.read(1024 * 1024), b""):
pass
error = proc.stderr.read() if proc.stderr else b""
if proc.wait() != 0:
raise RuntimeError(
f"cannot read tracked Git blob for {rel}: {error.decode(errors='replace')}"
)
if digest is not None:
result[rel] = digest
return result
+13 -1
View File
@@ -65,8 +65,20 @@ class PrebuiltInventoryTest(unittest.TestCase):
(self.root / "untracked.so").write_bytes(b"\x7fELF\x00local")
self.assertEqual(check.validate(self.root, self.manifest), [])
def test_missing_binary_is_rejected(self):
def test_sparse_missing_binary_is_checked_from_git(self):
self.binary.unlink()
self.assertEqual(check.validate(self.root, self.manifest), [])
def test_sparse_unlisted_binary_is_rejected(self):
hidden = self.root / "docs" / "releases" / "media" / "payload.exe"
hidden.parent.mkdir(parents=True)
hidden.write_bytes(b"MZhidden")
subprocess.run(["git", "add", "docs/releases/media/payload.exe"], cwd=self.root, check=True)
hidden.unlink() # Mimics a sparse-checkout file missing on disk.
self.assertTrue(any("UNREVIEWED binary" in x for x in check.validate(self.root, self.manifest)))
def test_removed_index_entry_is_rejected(self):
subprocess.run(["git", "rm", "--cached", "vendor.so"], cwd=self.root, check=True, stdout=subprocess.DEVNULL)
self.assertTrue(any("missing/untracked" in x for x in check.validate(self.root, self.manifest)))
def test_duplicate_manifest_entry_is_rejected(self):