Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/tests_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ jobs:
- run: npm run build
- run: ./utils/docker/build.sh --${{ matrix.docker_arch }} ${{ matrix.docker_tag }} playwright:localbuild
shell: bash
# 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:localbuild
scanners: secret
exit-code: '1'
# The secret scan needs no vulnerability database.
cache: 'false'
- run: ./utils/docker/test_check_layers.sh
shell: bash
- name: Launch container
Expand Down
2 changes: 2 additions & 0 deletions utils/docker/Dockerfile.jammy
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ARG PLAYWRIGHT_VERSION
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
# so no image layer keeps them. The image itself uses the default ~/.npm.
ARG npm_config_cache=/dev/shm/npm
# npm turns on the Node.js compile cache, which writes to /tmp. Turn it off during the build.
ARG NODE_DISABLE_COMPILE_CACHE=1

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
Expand Down
2 changes: 2 additions & 0 deletions utils/docker/Dockerfile.noble
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ARG PLAYWRIGHT_VERSION
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
# so no image layer keeps them. The image itself uses the default ~/.npm.
ARG npm_config_cache=/dev/shm/npm
# npm turns on the Node.js compile cache, which writes to /tmp. Turn it off during the build.
ARG NODE_DISABLE_COMPILE_CACHE=1

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
Expand Down
2 changes: 2 additions & 0 deletions utils/docker/Dockerfile.resolute
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ARG PLAYWRIGHT_VERSION
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
# so no image layer keeps them. The image itself uses the default ~/.npm.
ARG npm_config_cache=/dev/shm/npm
# npm turns on the Node.js compile cache, which writes to /tmp. Turn it off during the build.
ARG NODE_DISABLE_COMPILE_CACHE=1

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
Expand Down
58 changes: 20 additions & 38 deletions utils/docker/check_layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* 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');
Expand All @@ -22,55 +25,35 @@ 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, and
// everyone who pulls the image can read it there. So the check reads every layer,
// not the file system of the final image.
// 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 and yarn: the cache index and the debug logs record download URLs,
// including signed URLs of a private registry.
// 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\//,
/(^|\/)\.cache\/yarn\//,
// Registry and package manager credentials.
/(^|\/)\.npmrc$/,
/(^|\/)\.yarnrc(\.yml)?$/,
/(^|\/)\.pypirc$/,
/(^|\/)\.netrc$/,
// The cache directory of a user: pip, virtualenv, yarn and other tools write here.
/(^|\/)\.cache\//,
// Source control.
/(^|\/)\.git\//,
/(^|\/)\.git-credentials$/,
// Cloud, container and ssh credentials.
/(^|\/)\.ssh\//,
/(^|\/)\.aws\//,
/(^|\/)\.azure\//,
/(^|\/)\.kube\//,
/(^|\/)\.docker\//,
/(^|\/)\.config\/gcloud\//,
// Private keys and key stores. Certificates (.pem, .crt) are public, and the
// image needs the CA certificates, so they are not in this list.
/\.key$/,
/\.(p12|pfx|jks|keystore)$/,
/(^|\/)id_(rsa|dsa|ecdsa|ed25519)$/,
// Local environment files.
/(^|\/)\.env(\.[^/]*)?$/,
];

// Files that match a pattern above, but are a part of a package and hold no secret.
const ALLOWED = [
// The npm package has its own .npmrc with settings for npm development.
/(^|\/)node_modules\/npm\/\.npmrc$/,
];

function usage() {
return `usage: ${SCRIPT_NAME} [--list-files] playwright:localbuild-noble

Check that no layer of the docker image contains files that can hold
tokens or credentials, such as the npm cache, .npmrc, a .git directory or a private key.
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.
Expand All @@ -93,7 +76,7 @@ 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 files that can hold credentials...`);
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' });
Expand All @@ -115,7 +98,7 @@ layers.forEach((layer, index) => {
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)) && !ALLOWED.some(re => re.test(file)));
const matches = files.filter(file => FORBIDDEN.some(re => re.test(file)));
if (!matches.length)
return;
failed = true;
Expand Down Expand Up @@ -162,7 +145,6 @@ function listLayerFiles(layerFile) {
return output.split('\n')
// Paths in a layer can start with "./".
.map(line => line.replace(/^\.\//, ''))
// Skip directory entries: an empty directory, such as the ~/.ssh that a
// package creates, holds no secret.
// Skip directory entries: an empty directory is not a problem.
.filter(line => line && !line.endsWith('/'));
}
32 changes: 20 additions & 12 deletions utils/docker/test_check_layers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if [[ ($1 == '--help') || ($1 == '-h') ]]; then
fi

# The images that we publish are clean, so a build never sees check_layers.js fail.
# This test gives it an image with a forbidden file, to know that it still finds one.
# 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)"
Expand All @@ -26,34 +26,42 @@ cd "${TMP_DIR}"
echo "placeholder" > file

# 1. Forbidden files in a lower layer must fail the check.
cat > Dockerfile.bad <<'DOCKERFILE'
FROM scratch
COPY file /root/.npmrc
COPY file /certs/server.key
COPY file /home/pwuser/id_ed25519
COPY file /readme
DOCKERFILE
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 root/.npmrc certs/server.key home/pwuser/id_ed25519; do
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 only allowed files must pass the check.
# 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 /home/pwuser/.ssh-notes/id_ed25519.pub
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
Expand All @@ -63,4 +71,4 @@ if ! node "${SCRIPT_DIR}/check_layers.js" playwright:check-layers-good > output.
exit 1
fi

echo "PASSED: check_layers.js finds a forbidden file and accepts a clean image"
echo "PASSED: check_layers.js finds forbidden files and accepts a clean image"
Loading