forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_release_manifest.py
More file actions
413 lines (349 loc) · 16.1 KB
/
Copy pathdesktop_release_manifest.py
File metadata and controls
413 lines (349 loc) · 16.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python3
"""Validate and digest the immutable Omi desktop release manifest v1.
This module is intentionally stdlib-only so candidate, beta, and stable
workflows can use the exact same contract on clean runners. The detached
manifest digest is SHA-256 over UTF-8 JSON with sorted keys, no insignificant
whitespace, and non-ASCII characters preserved. A detached digest avoids the
self-referential ambiguity of embedding a manifest hash inside the manifest.
``qualification_tier`` and ``qualification_passed`` are legacy evidence-class
fields on the frozen v1 wire schema; ``signed-smoke`` is the only tier new
manifests carry. The names must not be renamed: every stored manifest is
validated on every read.
"""
from __future__ import annotations
import argparse
from datetime import datetime, timezone
import hashlib
import hmac
import json
from pathlib import Path
import re
from typing import Any, NoReturn
from urllib.parse import unquote, urlparse
SCHEMA_VERSION = 1
PLATFORM = "macos"
BACKEND_MODES = frozenset({"app_only", "backend_required"})
SIGNATURE_PREFIX = "hmac-sha256:"
SIGNING_CONTEXT = b"omi-desktop-release-manifest-v1\0"
TOP_LEVEL_FIELDS = frozenset(
{
"schema_version",
"release_id",
"platform",
"version",
"build_number",
"app_source_sha",
"zip_url",
"zip_sha256",
"dmg_url",
"dmg_sha256",
"ed_signature",
"qualification_evidence_asset",
"qualification_evidence_sha256",
"qualification_tier",
"qualification_passed",
"backend_mode",
"desktop_backend_source_sha",
"desktop_backend_oci_index_digest",
"desktop_backend_platform_digest",
"compatibility_contract",
"environment_contract_version",
"created_at",
"published_at",
"changelog",
"mandatory",
}
)
REQUIRED_FIELDS = frozenset(
TOP_LEVEL_FIELDS
- {
"desktop_backend_source_sha",
"desktop_backend_oci_index_digest",
"desktop_backend_platform_digest",
"published_at",
"changelog",
"mandatory",
}
)
BACKEND_FIELDS = frozenset(
{
"desktop_backend_source_sha",
"desktop_backend_oci_index_digest",
"desktop_backend_platform_digest",
}
)
COMPATIBILITY_BASE_FIELDS = frozenset(
{
"schema_version",
"app_release_id",
"app_version",
"app_build_number",
"backend_mode",
"environment_contract_version",
}
)
TAG_RE = re.compile(r"^v(?P<version>[0-9]+\.[0-9]+(?:\.[0-9]+)?)\+(?P<build>[1-9][0-9]*)-macos$")
SOURCE_SHA_RE = re.compile(r"^[0-9a-f]{40}$")
SHA256_RE = re.compile(r"^sha256:[0-9a-f]{64}$")
EVIDENCE_ASSET_RE = re.compile(r"^qualification-evidence-[^/]+\.json$")
ENVIRONMENT_CONTRACT_RE = re.compile(r"^desktop-backend-env-v[1-9]\d*$")
class ManifestError(ValueError):
"""The release manifest violates the immutable v1 contract."""
def _fail(message: str) -> NoReturn:
raise ManifestError(message)
def _require_exact_fields(data: dict[str, Any], required: frozenset[str], allowed: frozenset[str], label: str) -> None:
missing = sorted(required - data.keys())
unknown = sorted(data.keys() - allowed)
if missing:
_fail(f"{label} is missing required field(s): {', '.join(missing)}")
if unknown:
_fail(f"{label} has unknown field(s): {', '.join(unknown)}")
def _require_string(data: dict[str, Any], key: str) -> str:
value = data.get(key)
if not isinstance(value, str) or not value.strip():
_fail(f"{key} must be a non-empty string")
if value != value.strip():
_fail(f"{key} must not have surrounding whitespace")
return value
def _require_int(data: dict[str, Any], key: str) -> int:
value = data.get(key)
if not isinstance(value, int) or isinstance(value, bool) or value <= 0:
_fail(f"{key} must be a positive integer")
return value
def _require_source_sha(data: dict[str, Any], key: str) -> str:
value = _require_string(data, key)
if not SOURCE_SHA_RE.fullmatch(value):
_fail(f"{key} must be a lowercase 40-character Git SHA")
return value
def _require_sha256(data: dict[str, Any], key: str) -> str:
value = _require_string(data, key)
if not SHA256_RE.fullmatch(value):
_fail(f"{key} must use sha256:<64 lowercase hex> form")
return value
def _require_release_asset_url(data: dict[str, Any], key: str, *, release_id: str, asset_name: str) -> str:
value = _require_string(data, key)
parsed = urlparse(value)
if (
parsed.scheme != "https"
or parsed.netloc != "github.com"
or parsed.username
or parsed.password
or parsed.query
or parsed.fragment
):
_fail(f"{key} must be a clean github.com release asset URL")
expected_prefix = "/BasedHardware/omi/releases/download/"
if not parsed.path.startswith(expected_prefix):
_fail(f"{key} must reference the BasedHardware/omi release")
suffix = parsed.path.removeprefix(expected_prefix)
try:
encoded_tag, actual_asset = suffix.rsplit("/", 1)
except ValueError:
_fail(f"{key} must include a release tag and asset name")
if unquote(encoded_tag) != release_id or actual_asset != asset_name:
_fail(f"{key} must reference {asset_name} on release {release_id}")
return value
def _require_timestamp(data: dict[str, Any], key: str) -> str:
value = _require_string(data, key)
if not value.endswith("Z"):
_fail(f"{key} must be an RFC 3339 UTC timestamp ending in Z")
try:
parsed = datetime.fromisoformat(value.removesuffix("Z") + "+00:00")
except ValueError as exc:
raise ManifestError(f"{key} must be an RFC 3339 timestamp") from exc
if parsed.tzinfo is None or parsed.utcoffset() != timezone.utc.utcoffset(parsed):
_fail(f"{key} must be UTC")
return value
def _require_changelog(data: dict[str, Any]) -> list[str]:
value = data.get("changelog")
if not isinstance(value, list) or any(not isinstance(item, str) or not item.strip() for item in value):
_fail("changelog must be a list of non-empty strings")
return value
def _validate_compatibility(manifest: dict[str, Any]) -> None:
raw = manifest.get("compatibility_contract")
if not isinstance(raw, dict):
_fail("compatibility_contract must be an object")
contract = raw
mode = manifest["backend_mode"]
allowed = COMPATIBILITY_BASE_FIELDS | (BACKEND_FIELDS if mode == "backend_required" else frozenset())
_require_exact_fields(contract, allowed, allowed, "compatibility_contract")
if contract.get("schema_version") != SCHEMA_VERSION:
_fail("compatibility_contract.schema_version must be 1")
exact_matches = {
"app_release_id": manifest["release_id"],
"app_version": manifest["version"],
"app_build_number": manifest["build_number"],
"backend_mode": mode,
"environment_contract_version": manifest["environment_contract_version"],
}
if mode == "backend_required":
exact_matches.update({field: manifest[field] for field in BACKEND_FIELDS})
for field, expected in exact_matches.items():
if contract.get(field) != expected:
_fail(f"compatibility_contract.{field} must exactly match {field}")
def validate_manifest(value: object) -> dict[str, Any]:
"""Validate one v1 manifest and return it unchanged when valid."""
if not isinstance(value, dict):
_fail("manifest must be a JSON object")
manifest = value
_require_exact_fields(manifest, REQUIRED_FIELDS, TOP_LEVEL_FIELDS, "manifest")
if manifest.get("schema_version") != SCHEMA_VERSION:
_fail("schema_version must be 1")
if manifest.get("platform") != PLATFORM:
_fail("platform must be macos")
release_id = _require_string(manifest, "release_id")
match = TAG_RE.fullmatch(release_id)
if not match:
_fail("release_id must use v<version>+<build>-macos form")
version = _require_string(manifest, "version")
build_number = _require_int(manifest, "build_number")
if version != match.group("version"):
_fail("version must match release_id")
if build_number != int(match.group("build")):
_fail("build_number must match release_id")
_require_source_sha(manifest, "app_source_sha")
_require_release_asset_url(manifest, "zip_url", release_id=release_id, asset_name="Omi.zip")
_require_sha256(manifest, "zip_sha256")
_require_release_asset_url(manifest, "dmg_url", release_id=release_id, asset_name="omi.dmg")
_require_sha256(manifest, "dmg_sha256")
_require_string(manifest, "ed_signature")
evidence_asset = _require_string(manifest, "qualification_evidence_asset")
if not EVIDENCE_ASSET_RE.fullmatch(evidence_asset) and evidence_asset not in {
"desktop-smoke-result.json",
"desktop-smoke-result-beta.json",
}:
_fail("qualification_evidence_asset must be a qualification evidence or signed-smoke asset name")
_require_sha256(manifest, "qualification_evidence_sha256")
qualification_tier = manifest.get("qualification_tier")
qualification_passed = manifest.get("qualification_passed")
if not isinstance(qualification_passed, bool) or (qualification_tier, qualification_passed) not in {
("T2", True),
("signed-smoke", False),
("emergency", False),
}:
_fail("release evidence must be T2, signed-smoke, or emergency truth")
if qualification_tier == "T2" and not EVIDENCE_ASSET_RE.fullmatch(evidence_asset):
_fail("T2 qualification requires a qualification-evidence-*.json asset")
if qualification_tier == "emergency" and evidence_asset != "desktop-smoke-result.json":
_fail("emergency qualification requires exact signed-smoke evidence")
if qualification_tier == "signed-smoke" and evidence_asset != "desktop-smoke-result-beta.json":
_fail("normal Beta promotion requires exact Codemagic Beta signed-smoke evidence")
mode = manifest.get("backend_mode")
if mode not in BACKEND_MODES:
_fail("backend_mode must be app_only or backend_required")
present_backend_fields = BACKEND_FIELDS & manifest.keys()
if mode == "app_only" and present_backend_fields:
_fail(f"app_only manifest must omit backend field(s): {', '.join(sorted(present_backend_fields))}")
if mode == "backend_required":
missing_backend_fields = BACKEND_FIELDS - manifest.keys()
if missing_backend_fields:
_fail(f"backend_required manifest is missing field(s): {', '.join(sorted(missing_backend_fields))}")
_require_source_sha(manifest, "desktop_backend_source_sha")
if manifest["desktop_backend_source_sha"] != manifest["app_source_sha"]:
_fail("desktop backend and app must come from the same source SHA")
_require_sha256(manifest, "desktop_backend_oci_index_digest")
_require_sha256(manifest, "desktop_backend_platform_digest")
if manifest["desktop_backend_oci_index_digest"] == manifest["desktop_backend_platform_digest"]:
_fail("OCI index and platform-child digests must identify distinct objects")
environment_contract = _require_string(manifest, "environment_contract_version")
if not ENVIRONMENT_CONTRACT_RE.fullmatch(environment_contract):
_fail("environment_contract_version must use desktop-backend-env-vN form")
_require_timestamp(manifest, "created_at")
if "published_at" in manifest:
_require_timestamp(manifest, "published_at")
if "changelog" in manifest:
_require_changelog(manifest)
if "mandatory" in manifest and not isinstance(manifest.get("mandatory"), bool):
_fail("mandatory must be a boolean")
_validate_compatibility(manifest)
return manifest
def canonical_bytes(manifest: object) -> bytes:
"""Return the deterministic bytes covered by the detached manifest digest."""
validated = validate_manifest(manifest)
return json.dumps(validated, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
def manifest_digest(manifest: object) -> str:
return f"sha256:{hashlib.sha256(canonical_bytes(manifest)).hexdigest()}"
def _require_signing_key(signing_key: bytes) -> None:
if len(signing_key) < 32:
_fail("manifest signing key must contain at least 32 bytes")
def manifest_signature(manifest: object, signing_key: bytes) -> str:
"""Sign canonical bytes with a separately controlled HMAC trust anchor."""
_require_signing_key(signing_key)
signature = hmac.new(signing_key, SIGNING_CONTEXT + canonical_bytes(manifest), hashlib.sha256).hexdigest()
return f"{SIGNATURE_PREFIX}{signature}"
def verify_manifest_signature(manifest: object, signature: str, signing_key: bytes) -> None:
_require_signing_key(signing_key)
expected = manifest_signature(manifest, signing_key)
if not hmac.compare_digest(expected, signature):
_fail("manifest signature mismatch")
def require_digest_match(expected: str, actual: str, *, label: str) -> None:
if not SHA256_RE.fullmatch(expected) or not SHA256_RE.fullmatch(actual):
_fail(f"{label} digest must use sha256:<64 lowercase hex> form")
if not hmac.compare_digest(expected, actual):
_fail(f"{label} digest mismatch: expected {expected}, got {actual}")
def verify_manifest_digest(manifest: object, expected_digest: str) -> None:
require_digest_match(expected_digest, manifest_digest(manifest), label="manifest")
def verify_manifest_integrity(
manifest: object,
expected_digest: str,
signature: str,
signing_key: bytes,
) -> None:
"""Verify both public drift detection and the independent trust anchor."""
verify_manifest_digest(manifest, expected_digest)
verify_manifest_signature(manifest, signature, signing_key)
def file_digest(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return f"sha256:{digest.hexdigest()}"
def verify_artifact(path: Path, expected_digest: str, *, label: str) -> None:
require_digest_match(expected_digest, file_digest(path), label=label)
def _load_manifest(path: Path) -> object:
def reject_duplicate_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
result: dict[str, Any] = {}
for key, value in pairs:
if key in result:
_fail(f"manifest JSON contains duplicate key: {key}")
result[key] = value
return result
return json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=reject_duplicate_keys)
def main() -> int:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command", required=True)
validate = subparsers.add_parser("validate")
validate.add_argument("manifest", type=Path)
digest = subparsers.add_parser("digest")
digest.add_argument("manifest", type=Path)
verify = subparsers.add_parser("verify")
verify.add_argument("manifest", type=Path)
verify.add_argument("--digest", required=True)
verify.add_argument("--signature", required=True)
verify.add_argument("--signing-key-file", required=True, type=Path)
sign = subparsers.add_parser("sign")
sign.add_argument("manifest", type=Path)
sign.add_argument("--signing-key-file", required=True, type=Path)
args = parser.parse_args()
try:
manifest = _load_manifest(args.manifest)
if args.command == "validate":
validate_manifest(manifest)
print(f"desktop release manifest v{SCHEMA_VERSION} valid: {args.manifest}")
elif args.command == "digest":
print(manifest_digest(manifest))
elif args.command == "verify":
verify_manifest_integrity(
manifest,
args.digest,
args.signature,
args.signing_key_file.read_bytes(),
)
print(f"desktop release manifest integrity verified: {args.manifest}")
else:
print(manifest_signature(manifest, args.signing_key_file.read_bytes()))
except (ManifestError, json.JSONDecodeError, OSError) as exc:
raise SystemExit(f"FAIL: {exc}") from exc
return 0
if __name__ == "__main__":
raise SystemExit(main())