diff --git a/.github/workflows/test_docker.yml b/.github/workflows/test_docker.yml index 001275484..8fa87a0b1 100644 --- a/.github/workflows/test_docker.yml +++ b/.github/workflows/test_docker.yml @@ -5,6 +5,7 @@ on: - '.github/workflows/test_docker.yml' - 'setup.py' - '**/Dockerfile.*' + - 'utils/docker/**' branches: - main - release-* @@ -13,6 +14,7 @@ on: - '.github/workflows/test_docker.yml' - 'setup.py' - '**/Dockerfile.*' + - 'utils/docker/**' branches: - main - release-* @@ -67,6 +69,19 @@ jobs: # build.sh builds the wheels, and with them the driver. - name: Build Docker image run: bash utils/docker/build.sh --${{ matrix.arch }} ${{ matrix.docker-image-variant }} playwright-python:localbuild-${{ matrix.docker-image-variant }} + # check_layers.js looks at the paths of the files, for caches and other leftovers. + # This scan looks at the content of the files, in all layers, and finds secrets. + - name: Scan the image for secrets + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 + with: + image-ref: playwright-python:localbuild-${{ matrix.docker-image-variant }} + scanners: secret + version: v0.76.2 + exit-code: '1' + # The secret scan needs no vulnerability database. + cache: 'false' + - name: Test check_layers.js + run: bash utils/docker/test_check_layers.sh - name: Start the container id: container run: | diff --git a/utils/docker/Dockerfile.jammy b/utils/docker/Dockerfile.jammy index a2428c1d9..03291208c 100644 --- a/utils/docker/Dockerfile.jammy +++ b/utils/docker/Dockerfile.jammy @@ -5,6 +5,9 @@ ARG DEBIAN_FRONTEND=noninteractive ARG TZ=America/Los_Angeles ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright/python:v%version%-jammy" ARG UBUNTU_MIRROR_PREFIX +# pip and virtualenv write their caches here during the build. /dev/shm is a tmpfs, +# so no image layer keeps them. The image itself uses the default ~/.cache. +ARG XDG_CACHE_HOME=/dev/shm/cache ENV LANG=C.UTF-8 ENV LC_ALL=C.UTF-8 diff --git a/utils/docker/Dockerfile.noble b/utils/docker/Dockerfile.noble index fcd686bf2..427c4463f 100644 --- a/utils/docker/Dockerfile.noble +++ b/utils/docker/Dockerfile.noble @@ -5,6 +5,9 @@ ARG DEBIAN_FRONTEND=noninteractive ARG TZ=America/Los_Angeles ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright/python:v%version%-noble" ARG UBUNTU_MIRROR_PREFIX +# pip and virtualenv write their caches here during the build. /dev/shm is a tmpfs, +# so no image layer keeps them. The image itself uses the default ~/.cache. +ARG XDG_CACHE_HOME=/dev/shm/cache ENV LANG=C.UTF-8 ENV LC_ALL=C.UTF-8 diff --git a/utils/docker/Dockerfile.resolute b/utils/docker/Dockerfile.resolute index c8911634e..76a12e704 100644 --- a/utils/docker/Dockerfile.resolute +++ b/utils/docker/Dockerfile.resolute @@ -5,6 +5,9 @@ ARG DEBIAN_FRONTEND=noninteractive ARG TZ=America/Los_Angeles ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright/python:v%version%-resolute" ARG UBUNTU_MIRROR_PREFIX +# pip and virtualenv write their caches here during the build. /dev/shm is a tmpfs, +# so no image layer keeps them. The image itself uses the default ~/.cache. +ARG XDG_CACHE_HOME=/dev/shm/cache ENV LANG=C.UTF-8 ENV LC_ALL=C.UTF-8 diff --git a/utils/docker/build.sh b/utils/docker/build.sh index e9f6fb7c7..a7ebf85a9 100755 --- a/utils/docker/build.sh +++ b/utils/docker/build.sh @@ -69,6 +69,8 @@ for ((attempt = 1; attempt <= MAX_ATTEMPTS; attempt++)); do --build-arg UBUNTU_MIRROR_PREFIX="${UBUNTU_MIRROR_PREFIX}" \ "${SECRET_ARGS[@]}" \ -t "$3" -f "Dockerfile.$2" .; then + # A failure of this check is not transient, so it does not retry the build. + node ./check_layers.js "$3" exit 0 fi if (( attempt < MAX_ATTEMPTS )); then diff --git a/utils/docker/check_layers.js b/utils/docker/check_layers.js new file mode 100755 index 000000000..49fa44720 --- /dev/null +++ b/utils/docker/check_layers.js @@ -0,0 +1,150 @@ +#!/usr/bin/env node +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * The same file is in microsoft/playwright, microsoft/playwright-python and + * microsoft/playwright-mcp, in utils/docker/. Keep the copies the same. + */ + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { execFileSync, spawnSync } = require('child_process'); + +const SCRIPT_NAME = path.basename(__filename); + +// A file that a later layer deletes is still in the layer that created it. It makes +// the image larger, and everyone who pulls the image can read it there. So the check +// reads every layer, not the file system of the final image. +// +// This check looks only at the paths of the files. It does not look for secrets: +// the Trivy scan in the Docker tests does that, by content. +// +// Each pattern is matched against the path of a file in a layer, for example +// "root/.npm/_logs/2026-01-01-debug-0.log". +const FORBIDDEN = [ + // npm: the cache and the debug logs. + /(^|\/)\.npm\//, + /(^|\/)_cacache\//, + /(^|\/)_logs\/.*-debug-\d+\.log$/, + // Node.js: the compile cache that npm turns on. It is in the temporary directory. + /(^|\/)node-compile-cache\//, + // yarn. + /(^|\/)\.yarn\//, + // The cache directory of a user: pip, virtualenv, yarn and other tools write here. + /(^|\/)\.cache\//, + // Source control. + /(^|\/)\.git\//, +]; + +function usage() { + return `usage: ${SCRIPT_NAME} [--list-files] playwright:localbuild-noble + +Check that no layer of the docker image contains files that a build step +must not leave behind, such as a package manager cache or a .git directory. + + --list-files also print every file that the layers of our Dockerfile add. + Layers of the base image are not listed. +`; +} + +const args = process.argv.slice(2); +if (!args.length || args.includes('--help') || args.includes('-h')) { + console.log(usage()); + process.exit(args.length ? 0 : 1); +} +const listFiles = args.includes('--list-files'); +const images = args.filter(arg => arg !== '--list-files'); +if (images.length !== 1 || images[0].startsWith('-')) { + console.error(usage()); + process.exit(1); +} +const image = images[0]; + +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-layers-')); +process.on('exit', () => fs.rmSync(tmpDir, { recursive: true, force: true })); + +console.log(`Checking the layers of ${image} for caches and other files that must not be in the image...`); +// Unpack the image without a copy of the archive on disk. With pipefail, a +// failure of "docker save" fails the command too. +const saved = spawnSync('bash', ['-o', 'pipefail', '-c', 'docker save "$0" | tar -x -C "$1"', image, tmpDir], { stdio: 'inherit' }); +if (saved.status !== 0) { + console.error(`ERROR: cannot save and unpack ${image}`); + process.exit(1); +} + +let failed = false; +const layers = readLayers(tmpDir); +if (!layers.length) { + console.error(`ERROR: found no layers in ${image}`); + process.exit(1); +} +layers.forEach((layer, index) => { + const files = listLayerFiles(path.join(tmpDir, layer.file)); + if (listFiles && layer.ours) { + console.log(`\n=== layer ${index + 1}: ${layer.step}`); + console.log(`=== ${files.length} file(s). A ".wh." name marks a file that the layer deletes.`); + console.log(files.join('\n')); + } + const matches = files.filter(file => FORBIDDEN.some(re => re.test(file))); + if (!matches.length) + return; + failed = true; + console.log(`\nERROR: layer ${index + 1} contains ${matches.length} forbidden file(s).`); + console.log(` step: ${layer.step}`); + for (const file of matches.slice(0, 20)) + console.log(` ${file}`); +}); + +if (failed) { + console.error('\nThese files stay in the image even when a later layer deletes them.'); + console.error('Do not create them, or delete them in the RUN step that creates them.'); + process.exit(1); +} +console.log(`\nChecked ${layers.length} layer(s), found no forbidden files.`); + +/** + * Returns the layers of an unpacked "docker save" archive, bottom layer first. + * @returns {{ file: string, ours: boolean, step: string }[]} + */ +function readLayers(dir) { + const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'manifest.json'), 'utf8'))[0]; + const config = JSON.parse(fs.readFileSync(path.join(dir, manifest.Config), 'utf8')); + // The history also has the steps that make no layer, such as ENV and ARG. + const steps = (config.history || []).filter(step => !step.empty_layer); + return manifest.Layers.map((file, i) => { + const step = steps[i] || {}; + // BuildKit puts the build arguments in front of a RUN command. Remove them. + const command = (step.created_by || '?').replace(/^RUN \|\d+ .*? \/bin\/sh -c /, 'RUN ').replace(/\s+/g, ' '); + return { + file, + // BuildKit marks the steps of the Dockerfile that it runs. The base image has no mark. + ours: (step.comment || '').includes('buildkit.dockerfile'), + step: command.length > 100 ? command.slice(0, 100) + '...' : command, + }; + }); +} + +/** + * Returns the paths of the files in a layer. tar reads both plain and compressed layers. + */ +function listLayerFiles(layerFile) { + const output = execFileSync('tar', ['-tf', layerFile], { encoding: 'utf8', maxBuffer: 1024 * 1024 * 1024 }); + return output.split('\n') + // Paths in a layer can start with "./". + .map(line => line.replace(/^\.\//, '')) + // Skip directory entries: an empty directory is not a problem. + .filter(line => line && !line.endsWith('/')); +} diff --git a/utils/docker/test_check_layers.sh b/utils/docker/test_check_layers.sh new file mode 100755 index 000000000..0c81e9ed4 --- /dev/null +++ b/utils/docker/test_check_layers.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +set -e +set +x + +if [[ ($1 == '--help') || ($1 == '-h') ]]; then + echo "usage: $(basename $0)" + echo + echo "Test check_layers.js with two small images that this script builds." + echo + exit 0 +fi + +# The images that we publish are clean, so a build never sees check_layers.js fail. +# This test gives it an image with forbidden files, to know that it still finds them. + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" +TMP_DIR="$(mktemp -d)" + +function cleanup() { + rm -rf "${TMP_DIR}" + docker rmi -f playwright:check-layers-bad playwright:check-layers-good > /dev/null 2>&1 || true +} +trap cleanup EXIT + +cd "${TMP_DIR}" +echo "placeholder" > file + +# 1. Forbidden files in a lower layer must fail the check. +FORBIDDEN_FILES=( + root/.npm/_cacache/index-v5/aa/bb/cc + root/.npm/_logs/2026-01-01T00_00_00_000Z-debug-0.log + tmp/node-compile-cache/v26.0.0-x64/aabbcc + root/.yarn/berry/cache/a.zip + root/.cache/pip/http-v2/a/b/c + home/pwuser/.cache/virtualenv/a + src/.git/config +) +echo "FROM scratch" > Dockerfile.bad +for FORBIDDEN_FILE in "${FORBIDDEN_FILES[@]}"; do + echo "COPY file /${FORBIDDEN_FILE}" >> Dockerfile.bad +done +echo "COPY file /readme" >> Dockerfile.bad +docker build -q -t playwright:check-layers-bad -f Dockerfile.bad . > /dev/null +if node "${SCRIPT_DIR}/check_layers.js" playwright:check-layers-bad > output.txt 2>&1; then + cat output.txt + echo "FAILED: check_layers.js passed for an image with forbidden files in its layers" + exit 1 +fi +# The check must fail because it found the files, not because it crashed. +for FORBIDDEN_FILE in "${FORBIDDEN_FILES[@]}"; do + if ! grep -q -x -F " ${FORBIDDEN_FILE}" output.txt; then + cat output.txt + echo "FAILED: check_layers.js did not report ${FORBIDDEN_FILE}" + exit 1 + fi +done + +# 2. An image with no forbidden files must pass the check. Files that can hold +# a secret, such as .npmrc, pass too: the Trivy scan looks for secrets. +cat > Dockerfile.good <<'DOCKERFILE' +FROM scratch +COPY file /usr/local/lib/node_modules/npm/.npmrc +COPY file /etc/ssl/certs/ca.pem +COPY file /var/cache/fontconfig/aabbcc.cache-9 +COPY file /readme +DOCKERFILE +docker build -q -t playwright:check-layers-good -f Dockerfile.good . > /dev/null +if ! node "${SCRIPT_DIR}/check_layers.js" playwright:check-layers-good > output.txt 2>&1; then + cat output.txt + echo "FAILED: check_layers.js failed for an image with no forbidden files" + exit 1 +fi + +echo "PASSED: check_layers.js finds forbidden files and accepts a clean image"