diff --git a/qualification/composer-container-trust.json b/qualification/composer-container-trust.json index c3d3eeb..8a96f3d 100644 --- a/qualification/composer-container-trust.json +++ b/qualification/composer-container-trust.json @@ -1063,6 +1063,157 @@ "mode": 420 } } + }, + "cli-php-repro": { + "target": "cli", + "workflows": { + "build.yml": [ + "reproducible-build" + ] + }, + "commands": [ + "bash \"$GITHUB_WORKSPACE/scripts/ci/setup-php-repro-container.sh\"" + ], + "environment": [ + "CLI_PHP_CI_IMAGE", + "CLI_PHP_CI_REPRO", + "CLI_PHP_CI_ROOT", + "CLI_PHP_CI_STATE" + ], + "absent_files": [ + "docker/php-ci/Dockerfile.dockerignore", + "docker/php-ci/cli.Dockerfile.dockerignore" + ], + "files": { + ".dockerignore": { + "sha256": "19f259d2473129b500ed107780da5b5bc4a7dea47fde4500539bff4b919d4aee", + "mode": 420 + }, + "docker/php-ci/Dockerfile": { + "sha256": "81d23fbbee7e4ee97ca14df9d17982aa664335b94499c5019662259fe0387eaa", + "mode": 420 + }, + "docker/php-ci/cli.Dockerfile": { + "sha256": "c2b64bd82040083fbd812a470b266861f04303ea800ba58908f1e4601fe4179f", + "mode": 420 + }, + "docker/php-ci/verify-php-trust.php": { + "sha256": "529c282e7422202bf25565431a6391de388f86dfe5bfd6c75cf21daa56ffa75e", + "mode": 420 + }, + "docker/brotli/install-vendor.sh": { + "sha256": "d7eb8b2a3e0af1c3072a56bcb81632b22f061f211d8fe5e57ee901c8bf94c92f", + "mode": 420 + }, + "docker/brotli/verify-runtime.php": { + "sha256": "580cc465e349f3872eca592355294e7320fe7f1a4560ff5d4c83d5900921c00e", + "mode": 420 + }, + "docker/brotli/verify-system-brotli.c": { + "sha256": "3353927d206aaf5e63157e46be715c6cbee46bac777e29f8f9e25c4c1f656667", + "mode": 420 + }, + "docker/brotli/version": { + "sha256": "e064953e075ddb99fb7b48ad29e215f2dfa075812c127eb1d6ab680e9920fba2", + "mode": 420 + }, + "scripts/ci/php-container-images.json": { + "sha256": "85c87a2dff21225109581876befc00177c62824e44c044765dc921879ca41a4d", + "mode": 420 + }, + "scripts/ci/setup-php-container.sh": { + "sha256": "a7007c0fbef886b7e29b62d20b76404839746620361a9dd9e6024f1ff546d49b", + "mode": 493 + }, + "scripts/ci/run-php-container.sh": { + "sha256": "baef1e07ac53ce31fed01eac37ced2f98e363fa677e129952f6e15e47213db0e", + "mode": 493 + }, + "scripts/ci/probe-php-test-runtime.php": { + "sha256": "a0bbef217128b8ffe81de5c126dfafbfd93ebed28244c2a69204a8f4991be264", + "mode": 420 + }, + "scripts/ci/test-php-container-tls.py": { + "sha256": "da11950ff46810ba425fea6bb6a453869995204388570d1f764a160154f3e7b0", + "mode": 420 + }, + "scripts/ci/test-php-container-boundary.py": { + "sha256": "8d4db79b20598e5e9f1fc64f6a730b574d6f2161cc42a01442ac0095dc8684e9", + "mode": 420 + }, + "scripts/ci/ensure-box.sh": { + "sha256": "fd25fab5e1b1bd5d15ed37cd9585e901831fc7662a27e0f968340e06167da07e", + "mode": 493 + }, + "docker/php-ci/phar.ini": { + "sha256": "ec099701f20a226c574c65279f7fc98368c740f764db3a505c4efcd1ea21700b", + "mode": 420 + }, + "scripts/ci/exec-box.php": { + "sha256": "d3566ee7430eeba8843036d30415091619cc37e5d326fc8a31b2a97008e728c9", + "mode": 420 + }, + "scripts/ci/prepare-php-phar-container.sh": { + "sha256": "dd8833e2d3b9ef21fc14ccbe27a4491d91819b4ba3dc16ca3d23e2a6b5238027", + "mode": 493 + }, + "scripts/ci/probe-php-phar-runtime.php": { + "sha256": "28deb9870754643ad6a89a6c1b107446f11fef18d0dd9f8f1c09170b7fec1ecf", + "mode": 420 + }, + "scripts/ci/run-php-phar-container.sh": { + "sha256": "2c273c5867c0c270829d13210fc83b28b2d211acaf9d1692f0b55de0953a950a", + "mode": 493 + }, + "scripts/ci/setup-php-phar-container.sh": { + "sha256": "8b2179e704d8207ecde82e631799b38a609773d081bc700d38ca1f1295e81c5a", + "mode": 493 + }, + "scripts/ci/test_php_phar_container.py": { + "sha256": "56e49fc95402bb48bda28f6d17d0aa69803a742526958d0a0309d078f28cf384", + "mode": 420 + }, + "rust/parity/test_php_phar_container.py": { + "sha256": "2cc387159b9c239c8757871283e17cd673733f16d05a43ac42d187395beeec13", + "mode": 420 + }, + "tests/fixtures/php-phar-smoke-workflow.yml": { + "sha256": "e9d0ecc505ac2b4cb4d2c636665385f806b9eec056301ceb6d000c63321e2545", + "mode": 420 + }, + "scripts/ci/prepare-php-repro-container.sh": { + "sha256": "c1e7ca8a6087a5fa497c50e2801622239beae00dfafbd3d26a38825b218e4ac2", + "mode": 493 + }, + "scripts/ci/prepare-php-repro-tree.sh": { + "sha256": "2824797a6c829c41650ec2d17bf89667c9a37832ce29f4694da179292b0e5a30", + "mode": 493 + }, + "scripts/ci/run-php-repro-container.sh": { + "sha256": "fc30fa3b6ff16ae1f74a9d1f87d6cb46621557372d78fb0ae3e34cf2986dde0a", + "mode": 493 + }, + "scripts/ci/setup-php-repro-container.sh": { + "sha256": "734752a895e26bfc335bb09b31de1a693ac126bc18bc1bc4c8056e1ad7ada422", + "mode": 493 + }, + "scripts/ci/verify-php-reproducible-build.sh": { + "sha256": "60566677c47d148591d0cc3d8523e1fc677fc52e7f703f7cdb5211297f488e16", + "mode": 493 + }, + "scripts/ci/test_php_repro_container.py": { + "sha256": "9df98c19baf64b8bbc2b3adb6426586923deaf745946b7b7289efb99268171a1", + "mode": 420 + }, + "rust/parity/test_php_repro_container.py": { + "sha256": "38facd816e730fcdea9f4b2673697d39a86ecb89f7601ca3258b9cd57438ea78", + "mode": 420 + }, + "tests/fixtures/php-repro-workflow.yml": { + "sha256": "df960bc18f02c14e994c897460bb6edc543ae38da130e1ff2473ba79c09f3040", + "mode": 420 + } + } } } } diff --git a/tests/fixtures/cli-php-repro.yml b/tests/fixtures/cli-php-repro.yml new file mode 100644 index 0000000..4b66bd7 --- /dev/null +++ b/tests/fixtures/cli-php-repro.yml @@ -0,0 +1,35 @@ +# Reviewed caller, active in build.yml. Keep both copies byte-identical in job content. +jobs: + reproducible-build: + name: Reproducible build (PHAR is bit-identical across rebuilds) + needs: [action-policy] + if: ${{ github.server_url == 'https://github.com' }} + runs-on: ubuntu-24.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 + with: + python-version: '3.13' + - name: Bootstrap verified dependency trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@75587fc33ff40c34f32ef777293dad56bb6f48c7 # clean-trust + with: + clients: pip,python,composer,curl,git + - name: Disable APT download retries (CVE-2026-109518) + run: echo 'Acquire::Retries "0";' | sudo tee /etc/apt/apt.conf.d/99-no-retries >/dev/null + - name: Check out the accepted PHP container build source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + repository: durable-workflow/.github + ref: 18783be6bef737cc778e4c7282a8797e39daa61a + path: .central-apt-source + persist-credentials: false + - name: Build and prepare the pinned PHP reproducibility profile + run: bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-repro-container.sh" + - name: Verify actual Composer dependency trust + uses: durable-workflow/.github/.github/actions/verify-composer-trust@75587fc33ff40c34f32ef777293dad56bb6f48c7 # clean-trust + - name: Verify dw.phar reproduces bit-for-bit + run: scripts/ci/verify-php-reproducible-build.sh diff --git a/tests/test_cli_php_repro_runtime.py b/tests/test_cli_php_repro_runtime.py new file mode 100644 index 0000000..2792c3a --- /dev/null +++ b/tests/test_cli_php_repro_runtime.py @@ -0,0 +1,129 @@ +"""The PHAR reproducibility recipe admits one caller without weakening whole-target trust.""" +from copy import deepcopy +import hashlib +import json +from pathlib import Path +import tempfile +import unittest +from unittest import mock + +import yaml + +from scripts.qualification_policy import PolicyError, verify_composer_network_sinks + + +class CliPhpReproRuntimeTests(unittest.TestCase): + def setUp(self): + repository = Path(__file__).resolve().parents[1] + self.policy = json.loads((repository/'qualification/policy.json').read_text()) + self.catalog = json.loads((repository/'qualification/composer-container-trust.json').read_text()) + self.document = yaml.safe_load((repository/'tests/fixtures/cli-php-repro.yml').read_text()) + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) + self.recipe = self.catalog['runtime_setups']['cli-php-repro'] + # Inert bodies test the actual catalog selectors/refusals. Independent + # source review binds the public consumer's real blobs and Git modes. + for name, expected in self.recipe['files'].items(): + path = self.root/name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text('# inert reviewed input\n') + path.chmod(expected['mode']) + expected['sha256'] = hashlib.sha256(path.read_bytes()).hexdigest() + # The driver's Composer request lives in the unbound original build + # script it runs in each archived tree, as in the real consumer. + driver = self.root/'scripts/ci/verify-php-reproducible-build.sh' + driver.write_text('#!/usr/bin/env bash\n(cd "$tree" && scripts/build.sh phar)\n') + self.recipe['files']['scripts/ci/verify-php-reproducible-build.sh']['sha256'] = ( + hashlib.sha256(driver.read_bytes()).hexdigest()) + build = self.root/'scripts/build.sh' + build.write_text('#!/usr/bin/env bash\ncomposer install --no-dev --prefer-dist --no-interaction\n') + build.chmod(0o755) + + def check(self, document=None, filename='build.yml'): + sources = {filename:yaml.safe_dump(self.document if document is None else document)} + with mock.patch('scripts.qualification_policy._composer_container_catalog', return_value=self.catalog): + return verify_composer_network_sinks(self.policy, 'cli', sources, self.root) + + def test_exact_proposed_caller_uses_the_registered_setup_and_actual_guard(self): + self.assertEqual('ubuntu-24.04', self.document['jobs']['reproducible-build']['runs-on']) + self.assertEqual(1, len(self.check())) + # Without the live verifier the original driver's request refuses. + steps = self.document['jobs']['reproducible-build']['steps'] + unguarded = deepcopy(self.document) + unguarded['jobs']['reproducible-build']['steps'] = [ + step for step in steps if 'verify-composer-trust@' not in step.get('uses', '')] + with self.assertRaises(PolicyError): + self.check(unguarded) + + def test_wrong_job_workflow_or_unguarded_extra_job_cannot_borrow_admission(self): + moved = deepcopy(self.document) + moved['jobs']['other'] = moved['jobs'].pop('reproducible-build') + with self.assertRaises(PolicyError): + self.check(moved) + with self.assertRaises(PolicyError): + self.check(filename='other.yml') + extra = deepcopy(self.document) + extra['jobs']['unguarded'] = {'steps':[{'run':'composer install --no-interaction'}]} + with self.assertRaises(PolicyError): + self.check(extra) + + def test_every_bound_file_and_absence_is_required(self): + for name in self.recipe['files']: + with self.subTest(file=name): + path = self.root/name + before = path.read_bytes() + path.write_bytes(before+b'# changed\n') + with self.assertRaises(PolicyError): + self.check() + path.write_bytes(before) + for name in self.recipe['absent_files']: + with self.subTest(absence=name): + path = self.root/name + path.parent.mkdir(parents=True, exist_ok=True) + path.touch() + with self.assertRaises(PolicyError): + self.check() + path.unlink() + + def test_setup_overrides_or_all_verifier_deletion_fail_closed(self): + for field, value in (('if','false'), ('continue-on-error',True), ('env',{'CLI_PHP_CI_IMAGE':'unreviewed'})): + altered = deepcopy(self.document) + setup = next(step for step in altered['jobs']['reproducible-build']['steps'] + if step.get('run') == self.recipe['commands'][0]) + setup[field] = value + with self.subTest(field=field), self.assertRaises(PolicyError): + self.check(altered) + altered = deepcopy(self.document) + steps = altered['jobs']['reproducible-build']['steps'] + steps[:] = [step for step in steps if 'verify-composer-trust@' not in step.get('uses','')] + with self.assertRaises(PolicyError): + self.check(altered) + altered = deepcopy(self.document) + altered['jobs']['reproducible-build']['env'] = {'CLI_PHP_CI_STATE':'unreviewed'} + with self.assertRaises(PolicyError): + self.check(altered) + + def test_repro_marker_cannot_be_overridden_or_borrowed_by_another_job(self): + altered = deepcopy(self.document) + altered['jobs']['reproducible-build']['env'] = {'CLI_PHP_CI_REPRO': '0'} + with self.assertRaises(PolicyError): + self.check(altered) + borrowed = deepcopy(self.document) + borrowed['jobs']['build-phar'] = borrowed['jobs'].pop('reproducible-build') + with self.assertRaises(PolicyError): + self.check(borrowed) + + def test_caller_keeps_the_original_two_tree_command_after_the_verifier(self): + steps = self.document['jobs']['reproducible-build']['steps'] + runs = [step.get('run', '') for step in steps] + uses = [step.get('uses', '') for step in steps] + setup = runs.index(self.recipe['commands'][0]) + verifier = next(index for index, value in enumerate(uses) if 'verify-composer-trust@' in value) + self.assertLess(setup, verifier) + self.assertEqual('scripts/ci/verify-php-reproducible-build.sh', runs[verifier + 1]) + self.assertFalse(any('setup-php@' in value for value in uses)) + + +if __name__ == '__main__': + unittest.main()