Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,32 @@ def test_load_manifest_flat_dictionary(self) -> None:
entries = mv.load_manifest(flat_path)
self.assertEqual(len(entries), len(SYNTHETIC_SIZES))

def test_flat_manifest_verifies_a_file_named_files(self) -> None:
directory = os.path.join(self.root, "flat-files")
os.mkdir(directory)
payload = b"synthetic payload"
with open(os.path.join(directory, "files"), "wb") as f:
f.write(payload)
digest = hashlib.sha256(payload).hexdigest()
for entry in ({"size": len(payload), "sha256": digest}, digest):
with self.subTest(entry=entry):
path = os.path.join(self.root, "files_manifest.json")
with open(path, "w", encoding="utf-8", newline="\n") as f:
json.dump({"files": entry}, f)
report = mv.verify_manifest(directory, path, strict=True)
self.assertTrue(report.passed)
self.assertEqual(report.matched, ["files"])

def test_wrapped_manifest_accepts_files_named_sha256_and_size(self) -> None:
digest = hashlib.sha256(b"synthetic payload").hexdigest()
path = os.path.join(self.root, "wrapped_manifest.json")
with open(path, "w", encoding="utf-8", newline="\n") as f:
json.dump({"files": {"sha256": digest, "size": digest}}, f)
self.assertEqual(mv.load_manifest(path), {
"sha256": {"size": None, "sha256": digest},
"size": {"size": None, "sha256": digest},
})

def test_load_manifest_flat_skips_metadata_keys(self) -> None:
flat_path = os.path.join(self.root, "manifest_flat_meta.json")
doc = {
Expand Down
18 changes: 15 additions & 3 deletions tools/manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
CHUNK = 8 * 1024 * 1024
DEFAULT_MANIFEST = os.path.join(os.path.dirname(__file__), "..", "reports", "manifest.json")
CACHE_VERSION = 1
SHA256_HEX = re.compile(r"[0-9a-fA-F]{64}")
# don't cache files written this recently, a write in the same timestamp tick wouldn't change the stamp
# (FAT and some network shares only keep 2s times)
RACY_WINDOW_NS = 3_000_000_000
Expand Down Expand Up @@ -194,6 +195,8 @@ def load_manifest(path: str) -> dict[str, dict]:
{<rel_path>: {"size": <int>, "sha256": <str>}, ...}
In a flat manifest, `created`, `title`, `version` and `note` are metadata unless
their value is an explicit file object with a `sha256` field.
A flat entry named `files` needs a digest string or an object with an integer
`size` and a `sha256` string to distinguish it from the wrapper.

Rejects repeated JSON keys and normalizes relative paths to forward
slashes and hashes to lowercase hex.
Expand Down Expand Up @@ -226,9 +229,18 @@ def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
if not isinstance(doc, dict):
raise ManifestError(f"{path}: root must be a JSON object")

nested = "files" in doc
files_value = doc.get("files")
# A wrapped manifest can itself list files named `sha256` or `size`.
flat_files_entry = (
isinstance(files_value, str) and SHA256_HEX.fullmatch(files_value) is not None
) or (
isinstance(files_value, dict)
and type(files_value.get("size")) is int
and isinstance(files_value.get("sha256"), str)
)
nested = "files" in doc and not flat_files_entry
if nested:
raw_files = doc["files"]
raw_files = files_value
if not isinstance(raw_files, dict):
raise ManifestError(f"{path}: 'files' must be a JSON object")
else:
Expand All @@ -253,7 +265,7 @@ def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
size, digest = None, info
else:
raise ManifestError(f"{path}: entry {rel!r} must be an object or digest string")
if re.fullmatch(r"[0-9a-fA-F]{64}", digest) is None:
if SHA256_HEX.fullmatch(digest) is None:
raise ManifestError(f"{path}: entry {rel!r} needs a SHA-256 digest of 64 hexadecimal characters")
entries[rel] = {"size": size, "sha256": digest.lower()}
return entries
Expand Down