diff --git a/.github/workflows/node-app-release.yml b/.github/workflows/node-app-release.yml index 5f77ba424..d974c221d 100644 --- a/.github/workflows/node-app-release.yml +++ b/.github/workflows/node-app-release.yml @@ -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 diff --git a/mk/tests-apps.mk b/mk/tests-apps.mk index 0aef392a3..628b2cd96 100644 --- a/mk/tests-apps.mk +++ b/mk/tests-apps.mk @@ -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 diff --git a/mk/tests-suite.mk b/mk/tests-suite.mk index 984421dc9..8aa8e6459 100644 --- a/mk/tests-suite.mk +++ b/mk/tests-suite.mk @@ -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 diff --git a/scripts/node_app_games.py b/scripts/node_app_games.py new file mode 100644 index 000000000..cedf3c1b6 --- /dev/null +++ b/scripts/node_app_games.py @@ -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() diff --git a/tests/node_app_games_test.py b/tests/node_app_games_test.py new file mode 100644 index 000000000..893bf596f --- /dev/null +++ b/tests/node_app_games_test.py @@ -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('= 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')