From 2160a27dd97460d99c619d04311b6ca80788620b Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:33:15 +0000 Subject: [PATCH] Admit Sample's read-only PHP launcher for its existing callers Bind the exact sample-app f069b118 PHP CI runtime source (12 files by SHA-256 and Git mode, plus the per-Dockerfile ignore absence) for its existing callers only: ci.yml test and microservice-test on the literal PHP 8.4/8.5 matrix, and the 8.4 setup in composer-lock-audit.yml audit and playground-journeys.yml journeys. The preflight validates every sample-app workflow, so all four callers need the same launcher record. The retired writable record stays withdrawn; its launcher bytes refuse. Fixtures copy the three caller workflows, the audit request helper and the reviewed and retired launcher pairs for the focused controls. Co-Authored-By: Claude Opus 5.5 --- qualification/composer-container-trust.json | 148 ++++++++++ .../799df847/run-php-container.sh | 67 +++++ .../799df847/setup-php-container.sh | 49 ++++ tests/fixtures/sample-app-php-ci/ci.yml | 270 ++++++++++++++++++ .../sample-app-php-ci/composer-lock-audit.yml | 68 +++++ .../audit-committed-composer-locks.sh | 35 +++ .../f069b118/run-php-container.sh | 128 +++++++++ .../f069b118/setup-php-container.sh | 49 ++++ .../sample-app-php-ci/playground-journeys.yml | 103 +++++++ tests/test_composer_trust_contract.py | 145 ++++++++++ 10 files changed, 1062 insertions(+) create mode 100644 tests/fixtures/sample-app-php-ci/799df847/run-php-container.sh create mode 100644 tests/fixtures/sample-app-php-ci/799df847/setup-php-container.sh create mode 100644 tests/fixtures/sample-app-php-ci/ci.yml create mode 100644 tests/fixtures/sample-app-php-ci/composer-lock-audit.yml create mode 100644 tests/fixtures/sample-app-php-ci/f069b118/audit-committed-composer-locks.sh create mode 100644 tests/fixtures/sample-app-php-ci/f069b118/run-php-container.sh create mode 100644 tests/fixtures/sample-app-php-ci/f069b118/setup-php-container.sh create mode 100644 tests/fixtures/sample-app-php-ci/playground-journeys.yml diff --git a/qualification/composer-container-trust.json b/qualification/composer-container-trust.json index 21f3382..c3d3eeb 100644 --- a/qualification/composer-container-trust.json +++ b/qualification/composer-container-trust.json @@ -915,6 +915,154 @@ "mode": 420 } } + }, + "sample-app-php-ci": { + "target": "sample-app", + "matrix": { + "php": [ + "8.4", + "8.5" + ] + }, + "workflows": { + "ci.yml": [ + "test", + "microservice-test" + ] + }, + "commands": [ + "bash \"$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh\" \"${{ matrix.php }}\"" + ], + "environment": [ + "SAMPLE_APP_PHP_CI_IMAGE", + "SAMPLE_APP_PHP_CI_ROOT", + "SAMPLE_APP_PHP_CI_STATE" + ], + "absent_files": [ + "docker/php-ci/Dockerfile.dockerignore" + ], + "files": { + ".dockerignore": { + "sha256": "3d3b99f9bbf9d4ce35b7dc8462c4da1c87983b2b65a3a829d8ec9d80f43551cc", + "mode": 420 + }, + "docker/php-ci/Dockerfile": { + "sha256": "dcdd1e2f99db98825f9cf6b89ab2b0191252d2816001af09f391b6fb1a8b035a", + "mode": 420 + }, + "scripts/ci/php-container-images.json": { + "sha256": "fc4d410589d1fdb322107e404bfcf9f4593e6ac7e3fcce729b1d78fafd9be1c4", + "mode": 420 + }, + "scripts/ci/run-php-container.sh": { + "sha256": "f4030f583b9bf148cb12209ad5d709308593c60b9e04a08acc4d07c614aa8883", + "mode": 493 + }, + "scripts/ci/setup-php-container.sh": { + "sha256": "051301e9af4a65503a34c513a1e91c802a59e66c3b0c00c1425c87e30797bb08", + "mode": 493 + }, + "tests/ci/test_php_container_tls.py": { + "sha256": "673e53e9b1f9a91f7c986cad50ca96aeff38a530fb426aefd1a7cf585d102b2c", + "mode": 420 + }, + ".devcontainer/docker/verify-php-trust.php": { + "sha256": "bd8682984a757eb28887e996f77c9cafed32634cc7e19155b9885da1bb5b48c9", + "mode": 420 + }, + "docker/install-phpredis.sh": { + "sha256": "17b21ea9724199f502605abeeff6856f8af721027691e1de74d12cc6ccb7d33b", + "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 + } + } + }, + "sample-app-php84-ci": { + "target": "sample-app", + "workflows": { + "composer-lock-audit.yml": [ + "audit" + ], + "playground-journeys.yml": [ + "journeys" + ] + }, + "commands": [ + "bash \"$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh\" \"8.4\"" + ], + "environment": [ + "SAMPLE_APP_PHP_CI_IMAGE", + "SAMPLE_APP_PHP_CI_ROOT", + "SAMPLE_APP_PHP_CI_STATE" + ], + "absent_files": [ + "docker/php-ci/Dockerfile.dockerignore" + ], + "files": { + ".dockerignore": { + "sha256": "3d3b99f9bbf9d4ce35b7dc8462c4da1c87983b2b65a3a829d8ec9d80f43551cc", + "mode": 420 + }, + "docker/php-ci/Dockerfile": { + "sha256": "dcdd1e2f99db98825f9cf6b89ab2b0191252d2816001af09f391b6fb1a8b035a", + "mode": 420 + }, + "scripts/ci/php-container-images.json": { + "sha256": "fc4d410589d1fdb322107e404bfcf9f4593e6ac7e3fcce729b1d78fafd9be1c4", + "mode": 420 + }, + "scripts/ci/run-php-container.sh": { + "sha256": "f4030f583b9bf148cb12209ad5d709308593c60b9e04a08acc4d07c614aa8883", + "mode": 493 + }, + "scripts/ci/setup-php-container.sh": { + "sha256": "051301e9af4a65503a34c513a1e91c802a59e66c3b0c00c1425c87e30797bb08", + "mode": 493 + }, + "tests/ci/test_php_container_tls.py": { + "sha256": "673e53e9b1f9a91f7c986cad50ca96aeff38a530fb426aefd1a7cf585d102b2c", + "mode": 420 + }, + ".devcontainer/docker/verify-php-trust.php": { + "sha256": "bd8682984a757eb28887e996f77c9cafed32634cc7e19155b9885da1bb5b48c9", + "mode": 420 + }, + "docker/install-phpredis.sh": { + "sha256": "17b21ea9724199f502605abeeff6856f8af721027691e1de74d12cc6ccb7d33b", + "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 + } + } } } } diff --git a/tests/fixtures/sample-app-php-ci/799df847/run-php-container.sh b/tests/fixtures/sample-app-php-ci/799df847/run-php-container.sh new file mode 100644 index 0000000..b8d84f6 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/799df847/run-php-container.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +set -euo pipefail + +command="${0##*/}" +case "$command" in php|composer) ;; *) echo 'Invoke this launcher as php or composer.' >&2; exit 2 ;; esac +root="${SAMPLE_APP_PHP_CI_ROOT:?PHP CI repository root is required}" +state="${SAMPLE_APP_PHP_CI_STATE:?PHP CI state directory is required}" +image="${SAMPLE_APP_PHP_CI_IMAGE:?PHP CI image is required}" +if [[ ! "$image" =~ ^sha256:[0-9a-f]{64}$ ]]; then + echo 'PHP CI must use the inspected immutable image config ID.' >&2 + exit 1 +fi + +options=(--rm --init --interactive --name "sample-app-php-ci-${command}-${BASHPID}" + --cpus 2 --memory 2g --pids-limit 512 --network host + --cap-drop ALL --security-opt no-new-privileges + --user "$(id -u):$(id -g)" --workdir "$PWD" + --mount "type=bind,source=$root,target=$root" + --mount "type=bind,source=$state,target=$state") + +# Composer's guard is a composite action outside the repository checkout. +if [[ -n "${GITHUB_ACTION_PATH:-}" ]]; then + options+=(--mount "type=bind,source=$GITHUB_ACTION_PATH,target=$GITHUB_ACTION_PATH,readonly") +fi +# A local worktree has its Git metadata outside the source mount. +git_directory="$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)" +if [[ "$git_directory" != "$root/"* ]]; then + options+=(--mount "type=bind,source=$git_directory,target=$git_directory,readonly") +fi + +declare -A mounted=() +for variable in SSL_CERT_FILE CURL_CA_BUNDLE GIT_SSL_CAINFO COMPOSER_CAFILE \ + REQUESTS_CA_BUNDLE PIP_CERT NPM_CONFIG_CAFILE CARGO_HTTP_CAINFO CARGO_HTTP_PROXY_CAINFO \ + SSL_CERT_DIR GIT_SSL_CAPATH COMPOSER_CAPATH; do + if [[ -n "${!variable:-}" ]]; then + selected="${!variable}" + if [[ -z "${mounted[$selected]:-}" ]]; then + options+=(--mount "type=bind,source=$selected,target=$selected,readonly") + mounted[$selected]=1 + fi + options+=(--env "$variable") + fi +done + +# Pass the CI configuration and the playground's separate synthetic role credentials. +while IFS= read -r variable; do + case "$variable" in + APP_*|DB_*|SHARED_DB_*|REDIS_*|CACHE_*|QUEUE_*|SESSION_*|MAIL_*|BCRYPT_ROUNDS|\ + COMPOSER_HOME|COMPOSER_CACHE_DIR|COMPOSER_MAX_PARALLEL_*|COMPOSER_IPRESOLVE|\ + SAMPLE_APP_ROOT|SAMPLE_APP_PLAYGROUND_SCENARIO|SAMPLE_APP_PLAYGROUND_WORKFLOW_ID_PREFIX|\ + DURABLE_WORKFLOW_RUNTIME_URL|DURABLE_WORKFLOW_SERVER_URL|DURABLE_WORKFLOW_NAMESPACE|\ + DURABLE_WORKFLOW_TASK_QUEUE|DURABLE_WORKFLOW_WORKER_ID|\ + DURABLE_WORKFLOW_WORKER_TOKEN|DURABLE_WORKFLOW_CLIENT_TOKEN|\ + DW_CI_CA_SHA256|GITHUB_ACTIONS|CI|TMPDIR|TZ) + options+=(--env "$variable") ;; + esac +done < <(compgen -e) + +# The host verifier discovers the launcher, but must inspect the real PHAR. +arguments=() +for argument in "$@"; do + if [[ "$argument" == "$state/bin/composer" ]]; then + argument=/usr/local/bin/composer + fi + arguments+=("$argument") +done +exec docker run "${options[@]}" "$image" "$command" "${arguments[@]}" diff --git a/tests/fixtures/sample-app-php-ci/799df847/setup-php-container.sh b/tests/fixtures/sample-app-php-ci/799df847/setup-php-container.sh new file mode 100644 index 0000000..2a1dee7 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/799df847/setup-php-container.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +minor="${1:?Usage: setup-php-container.sh 8.4|8.5}" +image="$(python3 -I -S -B - "$root/scripts/ci/php-container-images.json" "$minor" <<'PY' +import json,re,sys +images=json.load(open(sys.argv[1])) +image=images[sys.argv[2]] +assert re.fullmatch(r'php:8\.[45]\.\d+-cli-bookworm@sha256:[0-9a-f]{64}', image) +print(image) +PY +)" +test -f "$root/.central-apt-source/docker/apt/require-fixed-bookworm-apt.sh" +test "$(git -C "$root/.central-apt-source" rev-parse HEAD)" = 18783be6bef737cc778e4c7282a8797e39daa61a +echo '2a36a2417e8947357ea6f6cbc2f6c53a834f3c069f8013a34d6d861b7123d576 '"$root/.central-apt-source/docker/apt/require-fixed-bookworm-apt.sh" | sha256sum --check --strict + +runtime="sample-app-php-ci:${minor}-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-1}" +docker build --file "$root/docker/php-ci/Dockerfile" --build-arg "PHP_IMAGE=$image" --tag "$runtime" "$root" +runtime_id="$(docker image inspect "$runtime" --format '{{.Id}}')" +docker run --rm --init --name "sample-app-php-ci-probe-${minor}-${GITHUB_RUN_ID:-local}" \ + --cpus 1 --memory 512m --pids-limit 64 --network none "$runtime_id" \ + php -r ' + if (PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION !== $argv[1]) { throw new RuntimeException("The PHP matrix minor does not match the image."); } + foreach (["mbstring", "intl", "pdo_mysql", "redis", "bcmath", "gd", "zip", "curl", "pcntl"] as $extension) { + if (!extension_loaded($extension)) { throw new RuntimeException("Missing extension: ".$extension); } + } + if (extension_loaded("xdebug") || extension_loaded("pcov")) { throw new RuntimeException("Coverage must remain none."); } + if (php_ini_loaded_file() !== "/usr/local/etc/php/php.ini") { throw new RuntimeException("Production INI must be loaded."); } + echo json_encode(["php" => PHP_VERSION, "extensions" => get_loaded_extensions(), "ini" => php_ini_loaded_file(), "coverage" => "none"]), PHP_EOL; + ' "$minor" +printf 'php_ci_image=%s\n' "$runtime_id" + +state="${RUNNER_TEMP:?RUNNER_TEMP is required}/sample-app-php-ci" +mkdir -p "$state/bin" "$state/tmp" "$state/composer-home/cache" +ln -sf "$root/scripts/ci/run-php-container.sh" "$state/bin/php" +ln -sf "$root/scripts/ci/run-php-container.sh" "$state/bin/composer" +# Direct isolated execution keeps discovery from adding the checkout to sys.path. +SAMPLE_APP_PHP_CI_TEST_IMAGE="$runtime_id" SAMPLE_APP_PHP_CI_ROOT="$root" SAMPLE_APP_PHP_CI_STATE="$state" \ + python3 -I -S -B "$root/tests/ci/test_php_container_tls.py" -v +{ + echo "SAMPLE_APP_PHP_CI_IMAGE=$runtime_id" + echo "SAMPLE_APP_PHP_CI_ROOT=$root" + echo "SAMPLE_APP_PHP_CI_STATE=$state" + echo "COMPOSER_HOME=$state/composer-home" + echo "COMPOSER_CACHE_DIR=$state/composer-home/cache" + echo "TMPDIR=$state/tmp" +} >> "${GITHUB_ENV:?GITHUB_ENV is required}" +echo "$state/bin" >> "${GITHUB_PATH:?GITHUB_PATH is required}" diff --git a/tests/fixtures/sample-app-php-ci/ci.yml b/tests/fixtures/sample-app-php-ci/ci.yml new file mode 100644 index 0000000..93f62d5 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/ci.yml @@ -0,0 +1,270 @@ +name: ci + +on: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +jobs: + test: + name: php artisan test (php ${{ matrix.php }}) + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + php: ['8.4', '8.5'] + services: + mysql: + image: mysql:8.0 + env: + MYSQL_ROOT_PASSWORD: secret + MYSQL_DATABASE: sample + MYSQL_USER: laravel + MYSQL_PASSWORD: password + ports: + - 3306/tcp + options: >- + --health-cmd="mysqladmin ping -h 127.0.0.1 -u root --password=secret" + --health-interval=5s + --health-timeout=5s + --health-retries=20 + redis: + image: redis:7-alpine + ports: + - 6379/tcp + options: >- + --health-cmd="redis-cli ping" + --health-interval=3s + --health-timeout=3s + --health-retries=10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - name: Select authenticated dependency trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + with: + clients: pip,python,composer,curl,git,npm + + - 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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: durable-workflow/.github + ref: 18783be6bef737cc778e4c7282a8797e39daa61a + path: .central-apt-source + persist-credentials: false + - name: Build the pinned PHP runtime and install launchers + run: bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "${{ matrix.php }}" + + - name: Verify actual Composer trust before dependency requests + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-interaction + + - name: Verify the playground Composer configuration + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + with: + working-directory: playground/php-runtime + + - name: Validate packages and stable artifact tuple + run: | + composer validate --strict --no-check-all + composer audit --locked + composer install --working-dir=playground/php-runtime --prefer-dist --no-dev --no-progress --no-interaction + scripts/resolve-current-artifacts.sh + php scripts/ci/validate-composer-artifact-graph.php + composer docs:check-ai-contract + + - name: Prepare application + run: | + cp .env.example .env + php artisan key:generate + if getent hosts mysql >/dev/null 2>&1; then + { + echo "DB_HOST=mysql" + echo "DB_PORT=3306" + echo "SHARED_DB_HOST=mysql" + echo "SHARED_DB_PORT=3306" + echo "REDIS_HOST=redis" + echo "REDIS_PORT=6379" + } >> "$GITHUB_ENV" + else + { + echo "DB_HOST=127.0.0.1" + echo "DB_PORT=${{ job.services.mysql.ports['3306'] }}" + echo "SHARED_DB_HOST=127.0.0.1" + echo "SHARED_DB_PORT=${{ job.services.mysql.ports['3306'] }}" + echo "REDIS_HOST=127.0.0.1" + echo "REDIS_PORT=${{ job.services.redis.ports['6379'] }}" + } >> "$GITHUB_ENV" + fi + + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '22.23.4' + - name: Verify Node before running application tooling + run: node docker/verify-node-trust.mjs + + - name: Run application tests + env: + DB_CONNECTION: mysql + DB_DATABASE: sample + DB_USERNAME: laravel + DB_PASSWORD: password + SHARED_DB_DATABASE: sample + SHARED_DB_USERNAME: laravel + SHARED_DB_PASSWORD: password + REDIS_CLIENT: phpredis + run: | + php artisan test + npm run test:node + + microservice-test: + name: microservice composer and tests (php ${{ matrix.php }}) + runs-on: ubuntu-latest + timeout-minutes: 15 + defaults: + run: + working-directory: microservice + strategy: + fail-fast: false + matrix: + php: ['8.4', '8.5'] + services: + mysql: + image: mysql:8.0 + env: + MYSQL_ROOT_PASSWORD: secret + MYSQL_DATABASE: microservice + MYSQL_USER: laravel + MYSQL_PASSWORD: password + ports: + - 3306/tcp + options: >- + --health-cmd="mysqladmin ping -h 127.0.0.1 -u root --password=secret" + --health-interval=5s + --health-timeout=5s + --health-retries=20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - name: Select authenticated dependency trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # 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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: durable-workflow/.github + ref: 18783be6bef737cc778e4c7282a8797e39daa61a + path: .central-apt-source + persist-credentials: false + - name: Build the pinned PHP runtime and install launchers + run: bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "${{ matrix.php }}" + + - name: Verify actual Composer trust before dependency requests + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + with: + working-directory: microservice + + - name: Install and test + env: + DB_CONNECTION: mysql + DB_HOST: 127.0.0.1 + DB_PORT: ${{ job.services.mysql.ports['3306'] }} + DB_DATABASE: microservice + DB_USERNAME: laravel + DB_PASSWORD: password + SHARED_DB_HOST: 127.0.0.1 + SHARED_DB_PORT: ${{ job.services.mysql.ports['3306'] }} + SHARED_DB_DATABASE: microservice + SHARED_DB_USERNAME: laravel + SHARED_DB_PASSWORD: password + run: | + composer validate --strict --check-lock --no-check-all + composer install --prefer-dist --no-progress --no-interaction + composer audit --locked + php artisan test + + action-policy: + name: Central action policy preflight + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: durable-workflow/.github + ref: main + path: .central-action-policy + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - name: Bootstrap patched pip trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + - run: 'python -m pip install --only-binary :all: PyYAML==6.0.2' + - name: Require centrally approved immutable action commits + run: >- + python .central-action-policy/scripts/qualification_policy.py validate + --policy .central-action-policy/qualification/policy.json + --target sample-app + --workflow-directory .github/workflows + + image-security-tests: + name: Image security behavior + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - run: python -m unittest discover -s tests/ci -p 'test_*.py' + + target-branch-qualification: + name: Target branch qualification + if: always() + needs: [test, microservice-test, action-policy, image-security-tests] + runs-on: ubuntu-latest + timeout-minutes: 2 + steps: + - name: Require application tests + run: test "${{ needs.test.result }}" = success + - name: Require microservice tests + run: test "${{ needs.microservice-test.result }}" = success + - name: Require action policy + env: + ACTION_POLICY_RESULT: ${{ needs.action-policy.result }} + run: test "$ACTION_POLICY_RESULT" = success + - name: Require image security behavior + env: + IMAGE_SECURITY_RESULT: ${{ needs.image-security-tests.result }} + run: test "$IMAGE_SECURITY_RESULT" = success diff --git a/tests/fixtures/sample-app-php-ci/composer-lock-audit.yml b/tests/fixtures/sample-app-php-ci/composer-lock-audit.yml new file mode 100644 index 0000000..51f5bd0 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/composer-lock-audit.yml @@ -0,0 +1,68 @@ +name: Committed Composer lock audit + +on: + pull_request: + paths: + - '**/composer.json' + - '**/composer.lock' + - composer.json + - composer.lock + - scripts/ci/audit-committed-composer-locks.sh + - .github/workflows/composer-lock-audit.yml + push: + branches: [main] + paths: + - '**/composer.json' + - '**/composer.lock' + - composer.json + - composer.lock + - scripts/ci/audit-committed-composer-locks.sh + - .github/workflows/composer-lock-audit.yml + schedule: + - cron: '43 5 * * *' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: committed-composer-audit-${{ github.ref }} + cancel-in-progress: true + +jobs: + audit: + name: Audit every committed Composer lock + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + + - name: Select authenticated dependency trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + with: + clients: pip,python,composer,curl,git,npm + + - 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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: durable-workflow/.github + ref: 18783be6bef737cc778e4c7282a8797e39daa61a + path: .central-apt-source + persist-credentials: false + - name: Build the pinned PHP runtime and install launchers + run: bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "8.4" + + - name: Verify actual Composer trust before advisory requests + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + + - name: Audit the committed lock inventory + run: bash scripts/ci/audit-committed-composer-locks.sh diff --git a/tests/fixtures/sample-app-php-ci/f069b118/audit-committed-composer-locks.sh b/tests/fixtures/sample-app-php-ci/f069b118/audit-committed-composer-locks.sh new file mode 100644 index 0000000..dd87d6d --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/f069b118/audit-committed-composer-locks.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(git rev-parse --show-toplevel)" +composer_lock_list="$(mktemp)" +trap 'rm -f "$composer_lock_list"' EXIT +git ls-files -z -- composer.lock '**/composer.lock' > "$composer_lock_list" + +if [[ ! -s "$composer_lock_list" ]]; then + printf '%s\n' 'No committed Composer locks were found.' >&2 + exit 1 +fi + +audit_count=0 +audit_failed=0 +while IFS= read -r -d '' composer_lock; do + audit_count=$((audit_count + 1)) + if (( audit_count > 64 )); then + printf '%s\n' 'The Composer lock inventory exceeds the 64-lock audit bound.' >&2 + exit 1 + fi + if [[ -L "$composer_lock" || ! -f "$composer_lock" ]]; then + printf 'Composer lock must be a regular committed file: %s\n' "$composer_lock" >&2 + exit 1 + fi + + printf '\nAuditing %s\n' "$composer_lock" + if ! composer --no-plugins --no-scripts audit --locked --no-interaction \ + --working-dir="$(dirname -- "$composer_lock")"; then + audit_failed=1 + fi +done < "$composer_lock_list" + +printf '\nAudited %s committed Composer locks.\n' "$audit_count" +exit "$audit_failed" diff --git a/tests/fixtures/sample-app-php-ci/f069b118/run-php-container.sh b/tests/fixtures/sample-app-php-ci/f069b118/run-php-container.sh new file mode 100644 index 0000000..101854c --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/f069b118/run-php-container.sh @@ -0,0 +1,128 @@ +#!/usr/bin/env bash +set -euo pipefail + +command="${0##*/}" +case "$command" in php|composer) ;; *) echo 'Invoke this launcher as php or composer.' >&2; exit 2 ;; esac +root="${SAMPLE_APP_PHP_CI_ROOT:?PHP CI repository root is required}" +state="${SAMPLE_APP_PHP_CI_STATE:?PHP CI state directory is required}" +image="${SAMPLE_APP_PHP_CI_IMAGE:?PHP CI image is required}" +if [[ ! "$image" =~ ^sha256:[0-9a-f]{64}$ ]]; then + echo 'PHP CI must use the inspected immutable image config ID.' >&2 + exit 1 +fi + +# Container writes must never reach a host executable, wrapper or verifier. +# Canonical paths also prevent a writable-data symlink becoming a control mount. +require_directory() { + local path="$1" + if [[ "$path" != /* || "$path" == / || "$path" == *','* \ + || "$path" == *$'\n'* || "$path" == *$'\r'* \ + || -L "$path" || "$(realpath -m -- "$path")" != "$path" \ + || ( -e "$path" && ! -d "$path" ) ]]; then + echo 'PHP CI data directories must be canonical and contain no symlinks.' >&2 + exit 1 + fi + mkdir -p -- "$path" +} + +require_directory "$root" +require_directory "$state" +if [[ "$state/" == "$root/"* || "$root/" == "$state/"* ]]; then + echo 'PHP CI source and wrapper state must be separate directories.' >&2 + exit 1 +fi + +options=(--rm --init --interactive --name "sample-app-php-ci-${command}-${BASHPID}" + --cpus 2 --memory 2g --pids-limit 512 --network host + --cap-drop ALL --security-opt no-new-privileges + --user "$(id -u):$(id -g)" --workdir "$PWD" + --mount "type=bind,source=$root,target=$root,readonly" + --mount "type=bind,source=$state,target=$state,readonly") + +for relative in vendor bootstrap/cache storage .phpunit.cache \ + microservice/vendor microservice/bootstrap/cache microservice/storage microservice/.phpunit.cache \ + playground/php-runtime/vendor; do + require_directory "$root/$relative" + options+=(--mount "type=bind,source=$root/$relative,target=$root/$relative") +done +for relative in .env microservice/.env; do + path="$root/$relative" + if [[ -L "$path" || "$(realpath -m -- "$path")" != "$path" \ + || ( -e "$path" && ! -f "$path" ) ]]; then + echo 'PHP CI environment data must be a regular file without symlinks.' >&2 + exit 1 + fi + if [[ ! -e "$path" ]]; then touch -- "$path"; fi + options+=(--mount "type=bind,source=$path,target=$path") +done +for relative in data/php-tmp composer-home source/php; do + require_directory "$state/$relative" + options+=(--mount "type=bind,source=$state/$relative,target=$state/$relative") +done +# The host runs the Python/Rust scaffolds. PHP may write only its own source. +require_directory "$state/source/python" +require_directory "$state/source/rust" +# Host temporary files can contain command inputs for other languages. PHP gets +# its own temporary-data directory instead of write access to the host TMPDIR. +options+=(--env "TMPDIR=$state/data/php-tmp") + +# Preview tests create/remove public/hot. Keep these writes in disposable data, +# rather than exposing the checkout's public scripts/assets to container writes. +require_directory "$state/data" +for application in root microservice; do + public="$root/public" + if [[ "$application" == microservice ]]; then public="$root/microservice/public"; fi + require_directory "$public" + snapshot="$state/data/public-$application" + if [[ ! -e "$snapshot" && ! -L "$snapshot" ]]; then cp -a -- "$public" "$snapshot"; fi + require_directory "$snapshot" + options+=(--mount "type=bind,source=$snapshot,target=$public") +done + +# Composer's guard is a composite action outside the repository checkout. +if [[ -n "${GITHUB_ACTION_PATH:-}" ]]; then + options+=(--mount "type=bind,source=$GITHUB_ACTION_PATH,target=$GITHUB_ACTION_PATH,readonly") +fi +# A local worktree has its Git metadata outside the source mount. +git_directory="$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)" +if [[ "$git_directory" != "$root/"* ]]; then + options+=(--mount "type=bind,source=$git_directory,target=$git_directory,readonly") +fi + +declare -A mounted=() +for variable in SSL_CERT_FILE CURL_CA_BUNDLE GIT_SSL_CAINFO COMPOSER_CAFILE \ + REQUESTS_CA_BUNDLE PIP_CERT NPM_CONFIG_CAFILE CARGO_HTTP_CAINFO CARGO_HTTP_PROXY_CAINFO \ + SSL_CERT_DIR GIT_SSL_CAPATH COMPOSER_CAPATH; do + if [[ -n "${!variable:-}" ]]; then + selected="${!variable}" + if [[ -z "${mounted[$selected]:-}" ]]; then + options+=(--mount "type=bind,source=$selected,target=$selected,readonly") + mounted[$selected]=1 + fi + options+=(--env "$variable") + fi +done + +# Pass the CI configuration and the playground's separate synthetic role credentials. +while IFS= read -r variable; do + case "$variable" in + APP_*|DB_*|SHARED_DB_*|REDIS_*|CACHE_*|QUEUE_*|SESSION_*|MAIL_*|BCRYPT_ROUNDS|\ + COMPOSER_HOME|COMPOSER_CACHE_DIR|COMPOSER_MAX_PARALLEL_*|COMPOSER_IPRESOLVE|\ + SAMPLE_APP_ROOT|SAMPLE_APP_PLAYGROUND_SCENARIO|SAMPLE_APP_PLAYGROUND_WORKFLOW_ID_PREFIX|\ + DURABLE_WORKFLOW_RUNTIME_URL|DURABLE_WORKFLOW_SERVER_URL|DURABLE_WORKFLOW_NAMESPACE|\ + DURABLE_WORKFLOW_TASK_QUEUE|DURABLE_WORKFLOW_WORKER_ID|\ + DURABLE_WORKFLOW_WORKER_TOKEN|DURABLE_WORKFLOW_CLIENT_TOKEN|\ + DW_CI_CA_SHA256|GITHUB_ACTIONS|CI|TZ) + options+=(--env "$variable") ;; + esac +done < <(compgen -e) + +# The host verifier discovers the launcher, but must inspect the real PHAR. +arguments=() +for argument in "$@"; do + if [[ "$argument" == "$state/bin/composer" ]]; then + argument=/usr/local/bin/composer + fi + arguments+=("$argument") +done +exec docker run "${options[@]}" "$image" "$command" "${arguments[@]}" diff --git a/tests/fixtures/sample-app-php-ci/f069b118/setup-php-container.sh b/tests/fixtures/sample-app-php-ci/f069b118/setup-php-container.sh new file mode 100644 index 0000000..d8b0e98 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/f069b118/setup-php-container.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +minor="${1:?Usage: setup-php-container.sh 8.4|8.5}" +image="$(python3 -I -S -B - "$root/scripts/ci/php-container-images.json" "$minor" <<'PY' +import json,re,sys +images=json.load(open(sys.argv[1])) +image=images[sys.argv[2]] +assert re.fullmatch(r'php:8\.[45]\.\d+-cli-bookworm@sha256:[0-9a-f]{64}', image) +print(image) +PY +)" +test -f "$root/.central-apt-source/docker/apt/require-fixed-bookworm-apt.sh" +test "$(git -C "$root/.central-apt-source" rev-parse HEAD)" = 18783be6bef737cc778e4c7282a8797e39daa61a +echo '2a36a2417e8947357ea6f6cbc2f6c53a834f3c069f8013a34d6d861b7123d576 '"$root/.central-apt-source/docker/apt/require-fixed-bookworm-apt.sh" | sha256sum --check --strict + +runtime="sample-app-php-ci:${minor}-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-1}" +docker build --file "$root/docker/php-ci/Dockerfile" --build-arg "PHP_IMAGE=$image" --tag "$runtime" "$root" +runtime_id="$(docker image inspect "$runtime" --format '{{.Id}}')" +docker run --rm --init --name "sample-app-php-ci-probe-${minor}-${GITHUB_RUN_ID:-local}" \ + --cpus 1 --memory 512m --pids-limit 64 --network none "$runtime_id" \ + php -r ' + if (PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION !== $argv[1]) { throw new RuntimeException("The PHP matrix minor does not match the image."); } + foreach (["mbstring", "intl", "pdo_mysql", "redis", "bcmath", "gd", "zip", "curl", "pcntl"] as $extension) { + if (!extension_loaded($extension)) { throw new RuntimeException("Missing extension: ".$extension); } + } + if (extension_loaded("xdebug") || extension_loaded("pcov")) { throw new RuntimeException("Coverage must remain none."); } + if (php_ini_loaded_file() !== "/usr/local/etc/php/php.ini") { throw new RuntimeException("Production INI must be loaded."); } + echo json_encode(["php" => PHP_VERSION, "extensions" => get_loaded_extensions(), "ini" => php_ini_loaded_file(), "coverage" => "none"]), PHP_EOL; + ' "$minor" +printf 'php_ci_image=%s\n' "$runtime_id" + +state="${RUNNER_TEMP:?RUNNER_TEMP is required}/sample-app-php-ci" +mkdir -p "$state/bin" "$state/tmp" "$state/data/php-tmp" "$state/composer-home/cache" +ln -sf "$root/scripts/ci/run-php-container.sh" "$state/bin/php" +ln -sf "$root/scripts/ci/run-php-container.sh" "$state/bin/composer" +# Direct isolated execution keeps discovery from adding the checkout to sys.path. +SAMPLE_APP_PHP_CI_TEST_IMAGE="$runtime_id" SAMPLE_APP_PHP_CI_ROOT="$root" SAMPLE_APP_PHP_CI_STATE="$state" \ + python3 -I -S -B "$root/tests/ci/test_php_container_tls.py" -v +{ + echo "SAMPLE_APP_PHP_CI_IMAGE=$runtime_id" + echo "SAMPLE_APP_PHP_CI_ROOT=$root" + echo "SAMPLE_APP_PHP_CI_STATE=$state" + echo "COMPOSER_HOME=$state/composer-home" + echo "COMPOSER_CACHE_DIR=$state/composer-home/cache" + echo "TMPDIR=$state/tmp" +} >> "${GITHUB_ENV:?GITHUB_ENV is required}" +echo "$state/bin" >> "${GITHUB_PATH:?GITHUB_PATH is required}" diff --git a/tests/fixtures/sample-app-php-ci/playground-journeys.yml b/tests/fixtures/sample-app-php-ci/playground-journeys.yml new file mode 100644 index 0000000..c9da739 --- /dev/null +++ b/tests/fixtures/sample-app-php-ci/playground-journeys.yml @@ -0,0 +1,103 @@ +name: playground journeys + +on: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: playground-journeys-${{ github.event_name }}-${{ github.event_name == 'pull_request' && github.ref || github.sha }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + journeys: + name: Playground journeys (PHP/Python/Rust) on the published tuple + runs-on: ubuntu-latest + timeout-minutes: 30 + env: + PLAYGROUND_COMPOSE_PROJECT: sample-app-playground-${{ github.run_id }}-${{ github.run_attempt }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - name: Select authenticated dependency trust + uses: durable-workflow/.github/.github/actions/bootstrap-patched-pip@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # 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@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: durable-workflow/.github + ref: 18783be6bef737cc778e4c7282a8797e39daa61a + path: .central-apt-source + persist-credentials: false + - name: Build the pinned PHP runtime and install launchers + run: bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "8.4" + + - name: Verify actual Composer trust before dependency requests + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + + - name: Verify the playground Composer configuration + uses: durable-workflow/.github/.github/actions/verify-composer-trust@52283e2e8ea9a2cf7e3865ee30f2a72a38b098a2 # clean-trust + with: + working-directory: playground/php-runtime + + - name: Install the exact published tuple + run: | + while IFS= read -r assignment; do export "$assignment"; done \ + < <(scripts/resolve-current-artifacts.sh) + composer install --prefer-dist --no-progress --no-interaction + composer install --working-dir=playground/php-runtime --prefer-dist --no-dev --no-progress --no-interaction + python -m pip install "durable-workflow==${DURABLE_WORKFLOW_PYTHON_SDK_VERSION}" + curl --proto "=https" --tlsv1.2 -fsSL https://durable-workflow.com/install.sh -o "$RUNNER_TEMP/dw-install.sh" + mkdir -p "$RUNNER_TEMP/bin" + # The installer refuses to finish unless its directory is already on PATH. + export PATH="$RUNNER_TEMP/bin:$PATH" + VERSION="$DURABLE_WORKFLOW_CLI_VERSION" DURABLE_WORKFLOW_INSTALL_DIR="$RUNNER_TEMP/bin" \ + sh "$RUNNER_TEMP/dw-install.sh" + # The development image provides these two wrappers on PATH. + install -m 0755 .devcontainer/docker/with-disposable-composer-state \ + .devcontainer/docker/with-group-shared-umask "$RUNNER_TEMP/bin/" + echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" + + - name: Check the playground toolchain + run: scripts/playground doctor + + - name: Run each SDK journey against the published native Server + env: + PLAYGROUND_SOURCE_ROOT: ${{ env.SAMPLE_APP_PHP_CI_STATE }}/source + run: | + mkdir -p "$RUNNER_TEMP/playground-evidence" + for language in php python rust; do + scripts/playground "$language" \ + --evidence "$RUNNER_TEMP/playground-evidence/$language.json" + scripts/playground down "$language" + done + python scripts/ci/validate-playground-evidence.py "$RUNNER_TEMP"/playground-evidence/*.json + + - name: Remove playground services + if: always() + run: | + for language in php python rust; do scripts/playground down "$language" || true; done + + - name: Retain journey evidence + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: playground-journeys-${{ github.run_id }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/playground-evidence/*.json + if-no-files-found: warn + retention-days: 30 diff --git a/tests/test_composer_trust_contract.py b/tests/test_composer_trust_contract.py index 0847b6a..e70ee74 100644 --- a/tests/test_composer_trust_contract.py +++ b/tests/test_composer_trust_contract.py @@ -461,6 +461,151 @@ def test_retired_callers_cannot_borrow_their_old_runtime_admission(self): verify_composer_network_sinks(policy, recipe["target"], sources, root) +class SampleAppReadOnlyRuntimeContractTest(unittest.TestCase): + """Sample's read-only launcher at f069b118 is admitted only for its existing callers.""" + + LAUNCHER = ("scripts/ci/run-php-container.sh", "scripts/ci/setup-php-container.sh") + NAMES = ("sample-app-php-ci", "sample-app-php84-ci") + + def setUp(self): + repository = Path(__file__).resolve().parents[1] + self.fixtures = repository / "tests/fixtures/sample-app-php-ci" + self.policy = json.loads((repository / "qualification/policy.json").read_text()) + self.catalog = json.loads((repository / "qualification/composer-container-trust.json").read_text()) + self.recorded = deepcopy(self.catalog) + self.documents = {name: yaml.safe_load((self.fixtures / name).read_text()) + for name in ("ci.yml", "composer-lock-audit.yml", "playground-journeys.yml")} + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) + # The launcher pair uses the reviewed f069b118 bytes and recorded hashes. + # Unchanged image inputs are inert; independent review binds their real blobs. + for name, expected in self.catalog["runtime_setups"]["sample-app-php-ci"]["files"].items(): + path = self.root / name + path.parent.mkdir(parents=True, exist_ok=True) + if name in self.LAUNCHER: + path.write_bytes((self.fixtures / "f069b118" / Path(name).name).read_bytes()) + else: + path.write_text("# inert reviewed runtime input\n") + path.chmod(expected["mode"]) + for recipe in self.NAMES: + self.catalog["runtime_setups"][recipe]["files"][name]["sha256"] = hashlib.sha256(path.read_bytes()).hexdigest() + # The audit caller's actual request helper keeps its request enumerable. + helper = self.root / "scripts/ci/audit-committed-composer-locks.sh" + helper.write_bytes((self.fixtures / "f069b118" / helper.name).read_bytes()) + patch = mock.patch("scripts.qualification_policy._composer_container_catalog", return_value=self.catalog) + patch.start() + self.addCleanup(patch.stop) + + def check(self, documents=None, target="sample-app"): + selected = self.documents if documents is None else documents + sources = {name: yaml.safe_dump(document) for name, document in selected.items()} + return verify_composer_network_sinks(self.policy, target, sources, self.root) + + def setup_step(self, document, job): + return next(step for step in document["jobs"][job]["steps"] + if "setup-php-container.sh" in step.get("run", "")) + + def test_recorded_launcher_hashes_are_the_exact_reviewed_bytes(self): + retired = json.loads((self.fixtures.parent / "retired-writable-runtime-setups.json").read_text())["sample-app-php-ci"] + for recipe in self.NAMES: + files = self.recorded["runtime_setups"][recipe]["files"] + self.assertEqual(set(retired["files"]), set(files)) + for name in self.LAUNCHER: + with self.subTest(recipe=recipe, file=name): + self.assertEqual(hashlib.sha256((self.fixtures / "f069b118" / Path(name).name).read_bytes()).hexdigest(), + files[name]["sha256"]) + self.assertEqual(hashlib.sha256((self.fixtures / "799df847" / Path(name).name).read_bytes()).hexdigest(), + retired["files"][name]["sha256"]) + self.assertNotEqual(retired["files"][name]["sha256"], files[name]["sha256"]) + + def test_exact_callers_enumerate_every_guarded_request(self): + self.assertEqual(sorted(["ci.yml:test:step-8", "ci.yml:test:step-10", "ci.yml:microservice-test:step-8", + "composer-lock-audit.yml:audit:step-8", "playground-journeys.yml:journeys:step-9"]), + sorted(self.check())) + + def test_retired_writable_launcher_or_any_changed_input_refuses(self): + for name in self.LAUNCHER: + with self.subTest(retired=name): + path = self.root / name + before = path.read_bytes() + path.write_bytes((self.fixtures / "799df847" / Path(name).name).read_bytes()) + with self.assertRaisesRegex(PolicyError, "runtime source differs"): + self.check() + path.write_bytes(before) + launcher = self.root / self.LAUNCHER[0] + before = launcher.read_bytes() + writable = before.replace(b'target=$root,readonly"', b'target=$root"', 1) + self.assertNotEqual(before, writable) + launcher.write_bytes(writable) + with self.assertRaisesRegex(PolicyError, "runtime source differs"): + self.check() + launcher.write_bytes(before) + for name in self.catalog["runtime_setups"]["sample-app-php-ci"]["files"]: + with self.subTest(changed=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) + launcher.chmod(0o644) + with self.assertRaises(PolicyError): + self.check() + launcher.chmod(0o755) + (self.root / "docker/php-ci/Dockerfile.dockerignore").touch() + with self.assertRaisesRegex(PolicyError, "Unreviewed Composer runtime input"): + self.check() + + def test_admission_does_not_extend_to_other_jobs_commands_matrices_or_targets(self): + with self.assertRaises(PolicyError): + self.check(target="cli") + moved = deepcopy(self.documents) + moved["ci.yml"]["jobs"]["other"] = moved["ci.yml"]["jobs"].pop("test") + with self.assertRaises(PolicyError): + self.check(moved) + moved = deepcopy(self.documents) + moved["other.yml"] = moved.pop("composer-lock-audit.yml") + with self.assertRaises(PolicyError): + self.check(moved) + literal = deepcopy(self.documents) + self.setup_step(literal["ci.yml"], "test")["run"] = 'bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "8.4"' + with self.assertRaises(PolicyError): + self.check(literal) + literal = deepcopy(self.documents) + self.setup_step(literal["composer-lock-audit.yml"], "audit")["run"] = 'bash "$GITHUB_WORKSPACE/scripts/ci/setup-php-container.sh" "8.5"' + with self.assertRaises(PolicyError): + self.check(literal) + for matrix in ({"php": ["8.4", "8.5", "8.3"]}, {"php": ["8.4", "8.5"], "include": [{"php": "8.3"}]}): + altered = deepcopy(self.documents) + altered["ci.yml"]["jobs"]["microservice-test"]["strategy"]["matrix"] = matrix + with self.subTest(matrix=matrix), self.assertRaisesRegex(PolicyError, "literal PHP matrix"): + self.check(altered) + extra = deepcopy(self.documents) + extra["ci.yml"]["jobs"]["unguarded"] = {"steps": [{"run": "composer install --no-interaction"}]} + with self.assertRaises(PolicyError): + self.check(extra) + + def test_setup_overrides_or_verifier_removal_fail_closed(self): + for workflow, job in (("ci.yml", "test"), ("ci.yml", "microservice-test"), + ("composer-lock-audit.yml", "audit"), ("playground-journeys.yml", "journeys")): + for field, value in (("if", "false"), ("continue-on-error", True), + ("env", {"SAMPLE_APP_PHP_CI_IMAGE": "unreviewed"})): + altered = deepcopy(self.documents) + self.setup_step(altered[workflow], job)[field] = value + with self.subTest(job=job, field=field), self.assertRaises(PolicyError): + self.check(altered) + altered = deepcopy(self.documents) + steps = altered[workflow]["jobs"][job]["steps"] + steps[:] = [step for step in steps if "verify-composer-trust@" not in step.get("uses", "")] + with self.subTest(job=job, verifier="removed"), self.assertRaises(PolicyError): + self.check(altered) + altered = deepcopy(self.documents) + altered[workflow]["jobs"][job]["env"] = {"SAMPLE_APP_PHP_CI_ROOT": "/unreviewed"} + with self.subTest(job=job, job_env="override"), self.assertRaises(PolicyError): + self.check(altered) + + class ComposerRuntimeSetupContractTest(unittest.TestCase): def setUp(self): temporary = tempfile.TemporaryDirectory()