Verify separate node game files against the published catalog

This commit is contained in:
waozi committed 2026-10-25 11:49:03 -03:00
1 parent 32d87470b3
commit 64a050fbc6
6 files changed
+195

No files matched your search

+6
View File
@@ -83,6 +83,12 @@ jobs:
git -C "$work/apps" checkout -q FETCH_HEAD
gh release download "$TAG" --repo "$REPOSITORY" --pattern "$APP.zx" --dir "$work/bundles"
echo "$SHA256 $work/bundles/$APP.zx" | sha256sum --check --strict
python3 scripts/node_app_games.py --bundle "$work/bundles/$APP.zx" --list > "$work/required-games.txt"
if [ -s "$work/required-games.txt" ]; then
gh release download "$TAG" --repo "$REPOSITORY" --pattern "$APP.games.tar" --dir "$work/bundles"
python3 scripts/node_app_games.py --bundle "$work/bundles/$APP.zx" \
--archive "$work/bundles/$APP.games.tar" --output "$work/bundles/$APP.games"
fi
- name: Install signing dependencies
run: sudo apt-get update && sudo apt-get install -y python3-cryptography
- name: Configure publisher
+4
View File
@@ -79,6 +79,10 @@ package-version-test:
package-workflow-test:
python3 tests/package_workflow_test.py
.PHONY: node-app-games-test
node-app-games-test:
python3 tests/node_app_games_test.py
.PHONY: package-manifest-test
package-manifest-test:
python3 tests/package_manifests_test.py
+1
View File
@@ -139,6 +139,7 @@ test: app-preferences-sync-test
test: package-version-test
test: play-screenshots-test
test: package-workflow-test
test: node-app-games-test
test: package-manifest-test
test: package-upload-test
test: package-receiver-test
+88
View File
@@ -0,0 +1,88 @@
#!/usr/bin/env python3
"""Verify a release's separate games against its authenticated catalog."""
import argparse
from hashlib import sha256
import json
from pathlib import Path
import re
import shutil
import tarfile
import tempfile
from practice_content import bundle_assets
GAME_LIMIT = 16
PAYLOAD_LIMIT = 64 * 1024 * 1024
def required_games(bundle):
assets = bundle_assets(bundle.read_bytes())
catalog = assets.get('assets/apps/catalog.json')
if catalog is None:
return set()
children = json.loads(catalog)['children']
if len(children) > GAME_LIMIT:
raise ValueError('collection has too many games')
names = set()
for child in children:
if child['bundle'] in assets:
continue
digest = child['sha256']
if not re.fullmatch(r'[0-9a-f]{64}', digest):
raise ValueError('invalid game digest in catalog')
names.add(digest + '.zx')
return names
def stage_games(bundle, archive, output):
expected = required_games(bundle)
output.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='node-games-', dir=output.parent) as directory:
staging = Path(directory) / 'games'
staging.mkdir()
seen = set()
with tarfile.open(archive) as source:
for member in source:
if (member.name not in expected or member.name in seen or not member.isfile()
or not 8 <= member.size <= PAYLOAD_LIMIT):
raise ValueError('unexpected, repeated or invalid game archive entry')
digest = sha256()
total = 0
with source.extractfile(member) as data, (staging / member.name).open('wb') as target:
while chunk := data.read(1024 * 1024):
total += len(chunk)
if total > member.size:
raise ValueError('game exceeds its declared archive size')
digest.update(chunk)
target.write(chunk)
if total != member.size or digest.hexdigest() + '.zx' != member.name:
raise ValueError('game bytes differ from the authenticated catalog')
with (staging / member.name).open('rb') as data:
if data.read(4) != b'ZX\0\0':
raise ValueError('game is not a ZX bundle')
seen.add(member.name)
if seen != expected:
raise ValueError('game archive is missing a declared game')
if output.exists():
shutil.rmtree(output)
staging.replace(output)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--bundle', type=Path, required=True)
parser.add_argument('--list', action='store_true')
parser.add_argument('--archive', type=Path)
parser.add_argument('--output', type=Path)
args = parser.parse_args()
if args.list:
for name in sorted(required_games(args.bundle)):
print(name)
else:
if args.archive is None or args.output is None:
parser.error('give --archive and --output')
stage_games(args.bundle, args.archive, args.output)
if __name__ == '__main__':
main()
+91
View File
@@ -0,0 +1,91 @@
"""Separate game downloads stay bound to the checked collection's digests."""
from hashlib import sha256
import io
import json
from pathlib import Path
import struct
import sys
import tarfile
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from node_app_games import required_games, stage_games
def fixture_bundle(assets):
def text(value):
data = value.encode()
return struct.pack('<I', len(data)) + data
data = b'ZX\0\0\x1a\0\0\0' + text('collection') + text('Main')
data += struct.pack('<IIIII', 0, 0, 0, 0, len(assets))
for name, content in assets.items():
data += text(name) + struct.pack('<I', len(content)) + content
return data
class GameArchiveTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix='node-game-archive-')
self.addCleanup(self.temporary.cleanup)
self.root = Path(self.temporary.name)
self.game = b'ZX\0\0game bytes'
self.name = sha256(self.game).hexdigest() + '.zx'
self.child = dict(bundle='assets/games/example.zx', sha256=self.name[:-3])
self.bundle = self.root / 'arcade.zx'
self.bundle.write_bytes(fixture_bundle({'assets/apps/catalog.json':
json.dumps({'children': [self.child]}).encode()}))
self.archive = self.root / 'arcade.games.tar'
self.output = self.root / 'arcade.games'
def archive_games(self, entries):
with tarfile.open(self.archive, 'w') as archive:
for name, data in entries:
entry = tarfile.TarInfo(name)
entry.size = len(data)
archive.addfile(entry, io.BytesIO(data))
def test_only_unpacked_children_need_downloads(self):
self.assertEqual(required_games(self.bundle), {self.name})
assets = {'assets/apps/catalog.json': json.dumps({'children': [self.child]}).encode(),
self.child['bundle']: self.game}
self.bundle.write_bytes(fixture_bundle(assets))
self.assertEqual(required_games(self.bundle), set())
self.bundle.write_bytes(fixture_bundle({}))
self.assertEqual(required_games(self.bundle), set())
def test_exact_download_is_staged_without_stale_games(self):
self.output.mkdir()
(self.output / 'old.zx').write_bytes(b'old')
self.archive_games([(self.name, self.game)])
stage_games(self.bundle, self.archive, self.output)
self.assertEqual(list(self.output.iterdir()), [self.output / self.name])
self.assertEqual((self.output / self.name).read_bytes(), self.game)
def test_wrong_or_missing_bytes_preserve_previous_output(self):
self.output.mkdir()
marker = self.output / 'previous.zx'
marker.write_bytes(b'previous')
for entries in ([], [(self.name, b'ZX\0\0different')],
[(self.name, self.game), (self.name, self.game)],
[('../escape.zx', self.game)], [('extra.zx', self.game)]):
with self.subTest(entries=entries):
self.archive_games(entries)
with self.assertRaises(ValueError):
stage_games(self.bundle, self.archive, self.output)
self.assertEqual(marker.read_bytes(), b'previous')
self.assertFalse((self.root / 'escape.zx').exists())
def test_link_is_rejected(self):
with tarfile.open(self.archive, 'w') as archive:
entry = tarfile.TarInfo(self.name)
entry.type = tarfile.SYMTYPE
entry.linkname = '../elsewhere'
archive.addfile(entry)
with self.assertRaises(ValueError):
stage_games(self.bundle, self.archive, self.output)
self.assertFalse(self.output.exists())
if __name__ == '__main__':
unittest.main()
+5
View File
@@ -17,6 +17,11 @@ assert int(os.environ['DISPLAY'].split(':')[-1].split('.')[0]) >= 400
assert os.environ['YUE_DESKTOP_RECOVERY'] == '0'
root = Path(__file__).resolve().parents[1]
binary, bundle = map(lambda value: Path(value).resolve(), sys.argv[1:3])
sys.path.insert(0, str(root / 'scripts'))
from practice_content import bundle_assets
catalog = json.loads(bundle_assets(bundle.read_bytes())['assets/apps/catalog.json'])
assert catalog['id'] == 'inbe.arcade', 'Pass the Arcade collection, not standalone Shovey'
output = root / 'build/shared-progress-ui-test'
output.mkdir(parents=True, exist_ok=True)
driver = UiDriver(output / 'driver')