Skip to content

Commit c86329f

Browse files
authored
fix(docker): keep the npm cache out of the image layers (#44181)
1 parent 765383d commit c86329f

7 files changed

Lines changed: 247 additions & 3 deletions

File tree

‎.github/workflows/tests_docker.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
- run: npm run build
4646
- run: ./utils/docker/build.sh --${{ matrix.docker_arch }} ${{ matrix.docker_tag }} playwright:localbuild
4747
shell: bash
48+
- run: ./utils/docker/test_check_layers.sh
49+
shell: bash
4850
- name: Launch container
4951
run: |
5052
docker run \

‎utils/docker/Dockerfile.jammy‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright:v%version%-jammy"
77
ARG NODE_VERSION=26.13.1 # autogenerated via ./update-playwright-node.mjs
88
ARG UBUNTU_MIRROR_PREFIX
99
ARG PLAYWRIGHT_VERSION
10+
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
11+
# so no image layer keeps them. The image itself uses the default ~/.npm.
12+
ARG npm_config_cache=/dev/shm/npm
1013

1114
ENV LANG=C.UTF-8
1215
ENV LC_ALL=C.UTF-8
@@ -78,7 +81,6 @@ RUN cd /ms-playwright-agent && \
7881
npm exec --no -- playwright-core install webkit && \
7982
rm /tmp/playwright-core.tar.gz && \
8083
rm -rf /ms-playwright-agent && \
81-
rm -rf ~/.npm/ && \
8284
chmod -R 777 /ms-playwright/webkit-*
8385

8486
# === IMAGE METADATA ===

‎utils/docker/Dockerfile.noble‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright:v%version%-noble"
77
ARG NODE_VERSION=26.13.1 # autogenerated via ./update-playwright-node.mjs
88
ARG UBUNTU_MIRROR_PREFIX
99
ARG PLAYWRIGHT_VERSION
10+
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
11+
# so no image layer keeps them. The image itself uses the default ~/.npm.
12+
ARG npm_config_cache=/dev/shm/npm
1013

1114
ENV LANG=C.UTF-8
1215
ENV LC_ALL=C.UTF-8
@@ -69,7 +72,6 @@ RUN cd /ms-playwright-agent && \
6972
npm exec --no -- playwright-core install webkit && \
7073
rm /tmp/playwright-core.tar.gz && \
7174
rm -rf /ms-playwright-agent && \
72-
rm -rf ~/.npm/ && \
7375
chmod -R 777 /ms-playwright/webkit-*
7476

7577
# === IMAGE METADATA ===

‎utils/docker/Dockerfile.resolute‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright:v%version%-resolute
77
ARG NODE_VERSION=26.13.1 # autogenerated via ./update-playwright-node.mjs
88
ARG UBUNTU_MIRROR_PREFIX
99
ARG PLAYWRIGHT_VERSION
10+
# npm writes its cache and logs here during the build. /dev/shm is a tmpfs,
11+
# so no image layer keeps them. The image itself uses the default ~/.npm.
12+
ARG npm_config_cache=/dev/shm/npm
1013

1114
ENV LANG=C.UTF-8
1215
ENV LC_ALL=C.UTF-8
@@ -70,7 +73,6 @@ RUN cd /ms-playwright-agent && \
7073
npm exec --no -- playwright-core install webkit && \
7174
rm /tmp/playwright-core.tar.gz && \
7275
rm -rf /ms-playwright-agent && \
73-
rm -rf ~/.npm/ && \
7476
chmod -R 777 /ms-playwright/webkit-*
7577

7678
# === IMAGE METADATA ===

‎utils/docker/build.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ for ((attempt = 1; attempt <= MAX_ATTEMPTS; attempt++)); do
6060
--build-arg PLAYWRIGHT_VERSION="${PLAYWRIGHT_VERSION}" \
6161
"${SECRET_ARGS[@]}" \
6262
-t "$3" -f "Dockerfile.$2" .; then
63+
# A failure of this check is not transient, so it does not retry the build.
64+
node ./check_layers.js "$3"
6365
exit 0
6466
fi
6567
if (( attempt < MAX_ATTEMPTS )); then

‎utils/docker/check_layers.js‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Copyright (c) Microsoft Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const fs = require('fs');
19+
const os = require('os');
20+
const path = require('path');
21+
const { execFileSync, spawnSync } = require('child_process');
22+
23+
const SCRIPT_NAME = path.basename(__filename);
24+
25+
// A file that a later layer deletes is still in the layer that created it, and
26+
// everyone who pulls the image can read it there. So the check reads every layer,
27+
// not the file system of the final image.
28+
//
29+
// Each pattern is matched against the path of a file in a layer, for example
30+
// "root/.npm/_logs/2026-01-01-debug-0.log".
31+
const FORBIDDEN = [
32+
// npm and yarn: the cache index and the debug logs record download URLs,
33+
// including signed URLs of a private registry.
34+
/(^|\/)\.npm\//,
35+
/(^|\/)_cacache\//,
36+
/(^|\/)_logs\/.*-debug-\d+\.log$/,
37+
/(^|\/)\.yarn\//,
38+
/(^|\/)\.cache\/yarn\//,
39+
// Registry and package manager credentials.
40+
/(^|\/)\.npmrc$/,
41+
/(^|\/)\.yarnrc(\.yml)?$/,
42+
/(^|\/)\.pypirc$/,
43+
/(^|\/)\.netrc$/,
44+
// Source control.
45+
/(^|\/)\.git\//,
46+
/(^|\/)\.git-credentials$/,
47+
// Cloud, container and ssh credentials.
48+
/(^|\/)\.ssh\//,
49+
/(^|\/)\.aws\//,
50+
/(^|\/)\.azure\//,
51+
/(^|\/)\.kube\//,
52+
/(^|\/)\.docker\//,
53+
/(^|\/)\.config\/gcloud\//,
54+
// Private keys and key stores. Certificates (.pem, .crt) are public, and the
55+
// image needs the CA certificates, so they are not in this list.
56+
/\.key$/,
57+
/\.(p12|pfx|jks|keystore)$/,
58+
/(^|\/)id_(rsa|dsa|ecdsa|ed25519)$/,
59+
// Local environment files.
60+
/(^|\/)\.env(\.[^/]*)?$/,
61+
];
62+
63+
// Files that match a pattern above, but are a part of a package and hold no secret.
64+
const ALLOWED = [
65+
// The npm package has its own .npmrc with settings for npm development.
66+
/(^|\/)node_modules\/npm\/\.npmrc$/,
67+
];
68+
69+
function usage() {
70+
return `usage: ${SCRIPT_NAME} [--list-files] playwright:localbuild-noble
71+
72+
Check that no layer of the docker image contains files that can hold
73+
tokens or credentials, such as the npm cache, .npmrc, a .git directory or a private key.
74+
75+
--list-files also print every file that the layers of our Dockerfile add.
76+
Layers of the base image are not listed.
77+
`;
78+
}
79+
80+
const args = process.argv.slice(2);
81+
if (!args.length || args.includes('--help') || args.includes('-h')) {
82+
console.log(usage());
83+
process.exit(args.length ? 0 : 1);
84+
}
85+
const listFiles = args.includes('--list-files');
86+
const images = args.filter(arg => arg !== '--list-files');
87+
if (images.length !== 1 || images[0].startsWith('-')) {
88+
console.error(usage());
89+
process.exit(1);
90+
}
91+
const image = images[0];
92+
93+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-layers-'));
94+
process.on('exit', () => fs.rmSync(tmpDir, { recursive: true, force: true }));
95+
96+
console.log(`Checking the layers of ${image} for files that can hold credentials...`);
97+
// Unpack the image without a copy of the archive on disk. With pipefail, a
98+
// failure of "docker save" fails the command too.
99+
const saved = spawnSync('bash', ['-o', 'pipefail', '-c', 'docker save "$0" | tar -x -C "$1"', image, tmpDir], { stdio: 'inherit' });
100+
if (saved.status !== 0) {
101+
console.error(`ERROR: cannot save and unpack ${image}`);
102+
process.exit(1);
103+
}
104+
105+
let failed = false;
106+
const layers = readLayers(tmpDir);
107+
if (!layers.length) {
108+
console.error(`ERROR: found no layers in ${image}`);
109+
process.exit(1);
110+
}
111+
layers.forEach((layer, index) => {
112+
const files = listLayerFiles(path.join(tmpDir, layer.file));
113+
if (listFiles && layer.ours) {
114+
console.log(`\n=== layer ${index + 1}: ${layer.step}`);
115+
console.log(`=== ${files.length} file(s). A ".wh." name marks a file that the layer deletes.`);
116+
console.log(files.join('\n'));
117+
}
118+
const matches = files.filter(file => FORBIDDEN.some(re => re.test(file)) && !ALLOWED.some(re => re.test(file)));
119+
if (!matches.length)
120+
return;
121+
failed = true;
122+
console.log(`\nERROR: layer ${index + 1} contains ${matches.length} forbidden file(s).`);
123+
console.log(` step: ${layer.step}`);
124+
for (const file of matches.slice(0, 20))
125+
console.log(` ${file}`);
126+
});
127+
128+
if (failed) {
129+
console.error('\nThese files stay in the image even when a later layer deletes them.');
130+
console.error('Do not create them, or delete them in the RUN step that creates them.');
131+
process.exit(1);
132+
}
133+
console.log(`\nChecked ${layers.length} layer(s), found no forbidden files.`);
134+
135+
/**
136+
* Returns the layers of an unpacked "docker save" archive, bottom layer first.
137+
* @returns {{ file: string, ours: boolean, step: string }[]}
138+
*/
139+
function readLayers(dir) {
140+
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'manifest.json'), 'utf8'))[0];
141+
const config = JSON.parse(fs.readFileSync(path.join(dir, manifest.Config), 'utf8'));
142+
// The history also has the steps that make no layer, such as ENV and ARG.
143+
const steps = (config.history || []).filter(step => !step.empty_layer);
144+
return manifest.Layers.map((file, i) => {
145+
const step = steps[i] || {};
146+
// BuildKit puts the build arguments in front of a RUN command. Remove them.
147+
const command = (step.created_by || '?').replace(/^RUN \|\d+ .*? \/bin\/sh -c /, 'RUN ').replace(/\s+/g, ' ');
148+
return {
149+
file,
150+
// BuildKit marks the steps of the Dockerfile that it runs. The base image has no mark.
151+
ours: (step.comment || '').includes('buildkit.dockerfile'),
152+
step: command.length > 100 ? command.slice(0, 100) + '...' : command,
153+
};
154+
});
155+
}
156+
157+
/**
158+
* Returns the paths of the files in a layer. tar reads both plain and compressed layers.
159+
*/
160+
function listLayerFiles(layerFile) {
161+
const output = execFileSync('tar', ['-tf', layerFile], { encoding: 'utf8', maxBuffer: 1024 * 1024 * 1024 });
162+
return output.split('\n')
163+
// Paths in a layer can start with "./".
164+
.map(line => line.replace(/^\.\//, ''))
165+
// Skip directory entries: an empty directory, such as the ~/.ssh that a
166+
// package creates, holds no secret.
167+
.filter(line => line && !line.endsWith('/'));
168+
}

‎utils/docker/test_check_layers.sh‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
set +x
4+
5+
if [[ ($1 == '--help') || ($1 == '-h') ]]; then
6+
echo "usage: $(basename $0)"
7+
echo
8+
echo "Test check_layers.js with two small images that this script builds."
9+
echo
10+
exit 0
11+
fi
12+
13+
# The images that we publish are clean, so a build never sees check_layers.js fail.
14+
# This test gives it an image with a forbidden file, to know that it still finds one.
15+
16+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
17+
TMP_DIR="$(mktemp -d)"
18+
19+
function cleanup() {
20+
rm -rf "${TMP_DIR}"
21+
docker rmi -f playwright:check-layers-bad playwright:check-layers-good > /dev/null 2>&1 || true
22+
}
23+
trap cleanup EXIT
24+
25+
cd "${TMP_DIR}"
26+
echo "placeholder" > file
27+
28+
# 1. Forbidden files in a lower layer must fail the check.
29+
cat > Dockerfile.bad <<'DOCKERFILE'
30+
FROM scratch
31+
COPY file /root/.npmrc
32+
COPY file /certs/server.key
33+
COPY file /home/pwuser/id_ed25519
34+
COPY file /readme
35+
DOCKERFILE
36+
docker build -q -t playwright:check-layers-bad -f Dockerfile.bad . > /dev/null
37+
if node "${SCRIPT_DIR}/check_layers.js" playwright:check-layers-bad > output.txt 2>&1; then
38+
cat output.txt
39+
echo "FAILED: check_layers.js passed for an image with forbidden files in its layers"
40+
exit 1
41+
fi
42+
# The check must fail because it found the files, not because it crashed.
43+
for FORBIDDEN_FILE in root/.npmrc certs/server.key home/pwuser/id_ed25519; do
44+
if ! grep -q -x -F " ${FORBIDDEN_FILE}" output.txt; then
45+
cat output.txt
46+
echo "FAILED: check_layers.js did not report ${FORBIDDEN_FILE}"
47+
exit 1
48+
fi
49+
done
50+
51+
# 2. An image with only allowed files must pass the check.
52+
cat > Dockerfile.good <<'DOCKERFILE'
53+
FROM scratch
54+
COPY file /usr/local/lib/node_modules/npm/.npmrc
55+
COPY file /etc/ssl/certs/ca.pem
56+
COPY file /home/pwuser/.ssh-notes/id_ed25519.pub
57+
COPY file /readme
58+
DOCKERFILE
59+
docker build -q -t playwright:check-layers-good -f Dockerfile.good . > /dev/null
60+
if ! node "${SCRIPT_DIR}/check_layers.js" playwright:check-layers-good > output.txt 2>&1; then
61+
cat output.txt
62+
echo "FAILED: check_layers.js failed for an image with no forbidden files"
63+
exit 1
64+
fi
65+
66+
echo "PASSED: check_layers.js finds a forbidden file and accepts a clean image"

0 commit comments

Comments
 (0)