Repository navigation
Expand file tree
/
Copy pathruntime_inputs.py
More file actions
98 lines (85 loc) · 4.2 KB
/
Copy pathruntime_inputs.py
File metadata and controls
98 lines (85 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Validation helpers for caller-supplied, public runtime provenance."""
from __future__ import annotations
import hashlib
import json
from pathlib import Path
import re
TARGET_IDS = {
"macos": "macos-arm64",
"windows": "windows-x64",
"linux": "linux-x64",
}
TARGET_PLATFORM_ARCH = {
"macos-arm64": ("darwin", "arm64"),
"windows-x64": ("win32", "x64"),
"linux-x64": ("linux", "x64"),
}
SHA256 = re.compile(r"^[0-9a-f]{64}$")
def _read_object(path: Path, label: str) -> dict:
try:
value = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
raise ValueError(f"{label} is unreadable JSON") from exc
if not isinstance(value, dict) or value.get("schema_version") != 1:
raise ValueError(f"{label} has an unsupported schema")
return value
def _target(document: dict, target_id: str, field: str) -> dict:
values = document.get(field)
if not isinstance(values, list):
raise ValueError(f"runtime metadata is missing {field}")
matches = [entry for entry in values if isinstance(entry, dict) and entry.get("id") == target_id]
if len(matches) != 1:
raise ValueError(f"runtime metadata must contain exactly one {target_id} entry in {field}")
return matches[0]
def load_runtime_evidence(
archive: Path,
provenance_path: Path,
inputs_path: Path,
platform: str,
) -> tuple[dict, dict]:
"""Validate archive bytes against public provenance and official source metadata."""
try:
target_id = TARGET_IDS[platform]
except KeyError as exc:
raise ValueError(f"unsupported runtime platform: {platform}") from exc
provenance = _read_object(provenance_path, "runtime provenance")
inputs = _read_object(inputs_path, "runtime inputs")
source_release = inputs.get("source_release")
if (not isinstance(source_release, dict)
or not isinstance(source_release.get("tag"), str)
or not isinstance(source_release.get("project"), str)):
raise ValueError("runtime inputs are missing the pinned upstream release")
if provenance.get("upstream_runtime") != f"{source_release['project']} {source_release['tag']}":
raise ValueError("runtime documents do not identify the same upstream release")
if provenance.get("widevine_cdm_bundled") is not False:
raise ValueError("runtime provenance must explicitly confirm no CDM is bundled")
source_target = _target(inputs, target_id, "targets")
expected_platform, expected_arch = TARGET_PLATFORM_ARCH[target_id]
if source_target.get("platform") != expected_platform or source_target.get("architecture") != expected_arch:
raise ValueError("official runtime source metadata does not match the selected target")
official_digest = source_target.get("archive_sha256")
if not isinstance(official_digest, str) or not SHA256.fullmatch(official_digest):
raise ValueError("runtime inputs contain an invalid official archive digest")
expected = provenance.get("final_runtime_archive_sha256")
if not isinstance(expected, str) or not SHA256.fullmatch(expected):
raise ValueError("runtime provenance is missing the final archive digest")
digest = hashlib.sha256()
try:
with archive.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
except OSError as exc:
raise ValueError("runtime archive is unreadable") from exc
if digest.hexdigest() != expected:
raise ValueError("runtime archive does not match the supplied provenance digest")
# Linux has no VMP signing entry in the prepared-target provenance. Accept
# only its byte-identical official runtime archive; macOS and Windows must
# continue to have an explicit prepared-runtime provenance entry.
if target_id == "linux-x64":
if expected != official_digest:
raise ValueError("Linux runtime must match the official unmodified archive digest")
else:
prepared_target = _target(provenance, target_id, "targets")
if prepared_target.get("id") != target_id:
raise ValueError("runtime provenance target does not match the selected platform")
return provenance, inputs