|
5 | 5 | * Checks for common secret patterns in files that would be published. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { readFileSync, readdirSync, statSync } from 'fs'; |
9 | | -import { join, extname } from 'path'; |
| 8 | +import { existsSync, readdirSync, readFileSync, statSync } from 'fs'; |
| 9 | +import { extname, join } from 'path'; |
10 | 10 |
|
11 | 11 | const SECRET_PATTERNS = [ |
12 | 12 | { pattern: /(?:api[_-]?key|apikey)\s*[:=]\s*['"][A-Za-z0-9_\-]{20,}['"]/i, name: 'API key' }, |
13 | | - { pattern: /(?:secret|token|password|passwd|pwd)\s*[:=]\s*['"][^\s'"]{8,}['"]/i, name: 'Secret/token' }, |
| 13 | + { |
| 14 | + pattern: /(?:secret|token|password|passwd|pwd)\s*[:=]\s*['"][^\s'"]{8,}['"]/i, |
| 15 | + name: 'Secret/token', |
| 16 | + }, |
14 | 17 | { pattern: /-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----/, name: 'Private key' }, |
15 | 18 | { pattern: /ghp_[A-Za-z0-9]{36}/, name: 'GitHub token' }, |
16 | 19 | { pattern: /sk-[A-Za-z0-9]{20,}/, name: 'OpenAI key' }, |
17 | 20 | { pattern: /xoxb-[0-9]+-[A-Za-z0-9]+/, name: 'Slack token' }, |
18 | 21 | { pattern: /AKIA[0-9A-Z]{16}/, name: 'AWS access key' }, |
19 | 22 | ]; |
20 | 23 |
|
21 | | -const SKIP_DIRS = new Set(['node_modules', '.git', 'dist']); |
22 | | -const TEXT_EXTENSIONS = new Set(['.js', '.ts', '.json', '.md', '.txt', '.yml', '.yaml', '.toml']); |
| 24 | +const TEXT_EXTENSIONS = new Set([ |
| 25 | + '.cjs', |
| 26 | + '.js', |
| 27 | + '.jsx', |
| 28 | + '.mjs', |
| 29 | + '.ts', |
| 30 | + '.tsx', |
| 31 | + '.json', |
| 32 | + '.md', |
| 33 | + '.txt', |
| 34 | + '.yml', |
| 35 | + '.yaml', |
| 36 | + '.toml', |
| 37 | +]); |
23 | 38 | const README_IMAGE_PATTERN = /\b(?:src|srcset)=["'](?:\.\/)?img\//; |
24 | 39 |
|
25 | | -function* walkFiles(dir) { |
26 | | - for (const entry of readdirSync(dir)) { |
27 | | - const full = join(dir, entry); |
28 | | - const stat = statSync(full); |
29 | | - if (stat.isDirectory()) { |
30 | | - if (!SKIP_DIRS.has(entry)) { |
31 | | - yield* walkFiles(full); |
| 40 | +function normalized(value) { |
| 41 | + return value.replaceAll('\\', '/').replace(/^\.\//u, '').replace(/\/+/gu, '/'); |
| 42 | +} |
| 43 | + |
| 44 | +function* walkIncludedPath(relativePath) { |
| 45 | + const stat = statSync(relativePath); |
| 46 | + if (stat.isFile()) { |
| 47 | + yield normalized(relativePath); |
| 48 | + return; |
| 49 | + } |
| 50 | + if (!stat.isDirectory()) return; |
| 51 | + |
| 52 | + for (const entry of readdirSync(relativePath)) { |
| 53 | + yield* walkIncludedPath(join(relativePath, entry)); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function readPackageFileList() { |
| 58 | + const packageJson = JSON.parse(readFileSync('package.json', 'utf-8')); |
| 59 | + const files = Array.isArray(packageJson.files) ? packageJson.files : []; |
| 60 | + const includes = files.filter((entry) => typeof entry === 'string' && !entry.startsWith('!')); |
| 61 | + const excludes = files |
| 62 | + .filter((entry) => typeof entry === 'string' && entry.startsWith('!')) |
| 63 | + .map((entry) => normalized(entry.slice(1))); |
| 64 | + return { includes, excludes }; |
| 65 | +} |
| 66 | + |
| 67 | +function isExcludedFromPackage(filePath, excludes) { |
| 68 | + const path = normalized(filePath); |
| 69 | + for (const pattern of excludes) { |
| 70 | + if (pattern === 'dist/**/*.test.js' && path.startsWith('dist/') && path.endsWith('.test.js')) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + if ( |
| 74 | + pattern === 'dist/**/__tests__' && |
| 75 | + path.startsWith('dist/') && |
| 76 | + path.split('/').includes('__tests__') |
| 77 | + ) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + if (path === pattern || path.startsWith(`${pattern}/`)) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | +} |
| 86 | + |
| 87 | +function alwaysIncludedPackageFiles() { |
| 88 | + const entries = readdirSync('.'); |
| 89 | + return entries.filter((entry) => { |
| 90 | + const lower = entry.toLowerCase(); |
| 91 | + return ( |
| 92 | + lower === 'package.json' || |
| 93 | + lower.startsWith('readme') || |
| 94 | + lower.startsWith('license') || |
| 95 | + lower.startsWith('licence') |
| 96 | + ); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function publishedFiles() { |
| 101 | + const { includes, excludes } = readPackageFileList(); |
| 102 | + const paths = new Set(); |
| 103 | + |
| 104 | + for (const entry of [...alwaysIncludedPackageFiles(), ...includes]) { |
| 105 | + const relativePath = normalized(entry); |
| 106 | + if (!relativePath || relativePath.startsWith('!') || !existsSync(relativePath)) continue; |
| 107 | + for (const filePath of walkIncludedPath(relativePath)) { |
| 108 | + if (!isExcludedFromPackage(filePath, excludes)) { |
| 109 | + paths.add(filePath); |
32 | 110 | } |
33 | | - } else if (stat.isFile()) { |
34 | | - yield full; |
35 | 111 | } |
36 | 112 | } |
| 113 | + |
| 114 | + return [...paths].sort(); |
37 | 115 | } |
38 | 116 |
|
39 | 117 | let found = 0; |
40 | 118 |
|
41 | | -for (const filePath of walkFiles('.')) { |
| 119 | +for (const filePath of publishedFiles()) { |
42 | 120 | const ext = extname(filePath); |
43 | 121 | if (!TEXT_EXTENSIONS.has(ext)) continue; |
44 | 122 |
|
|
0 commit comments