|
| 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 | +} |
0 commit comments