Repository navigation
Expand file tree
/
Copy pathcheck-secrets.ts
More file actions
281 lines (262 loc) · 8.42 KB
/
Copy pathcheck-secrets.ts
File metadata and controls
281 lines (262 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Secret scanner.
*
* pnpm secrets:check
*
* Two jobs, both of which r
7438
un in CI and fail the build:
*
* 1. Scan tracked source for anything that looks like a live credential.
* 2. Scan the BUILT CLIENT BUNDLE for server-side secrets. This is the check
* that actually matters — a secret can be perfectly safe in a server file
* and catastrophic if a bundler inlines it into JavaScript the browser
* downloads.
*
* `.env.example` is scanned too, but its deliberate `dev-only-...` placeholders
* are allowlisted: they exist precisely so nobody is tempted to paste a real
* value there, and the config loader refuses to boot on them outside
* development.
*/
import { readdirSync, readFileSync, statSync } from "node:fs";
import path from "node:path";
import { repoRoot } from "./_env";
interface Rule {
name: string;
pattern: RegExp;
/** Matches that are known-safe (placeholders, test keys, examples). */
allow?: RegExp[];
/**
* Skip this rule inside test files.
*
* Only ever set on the low-confidence NAME-SHAPE heuristic. Test suites are
* full of literal fixture passwords by necessity — they have to sign in as
* somebody — and flagging every one of them trains people to ignore the
* scanner, which is worse than the narrow gap it closes.
*
* Every high-confidence rule (real key formats, private key blocks, live
* connection strings) still applies to test files, because a genuine
* credential pasted into a test is exactly as leaked as one pasted anywhere
* else.
*/
skipInTests?: boolean;
}
/**
* Test and fixture paths, for `skipInTests`.
*
* The second half of this pattern names the acceptance and drill harnesses in
* `scripts/` individually, and that is deliberate. They are test code by nature
* — every one of them seeds throwaway accounts on RFC-reserved domains and
* needs a literal password to sign in with — but `scripts/` as a whole is NOT
* exempt. It also holds real operational tooling (backup, bootstrap, migrate)
* where a hardcoded credential is exactly the finding this scanner exists for.
* Blanket-allowing the directory would close the gap by removing the check.
*/
const TEST_PATH =
/(?:__tests__|[./](?:test|spec)s?[./]|\.test\.|\.spec\.|^e2e\/|\/e2e\/|scripts\/(?:loadtest|capacity-test|drill-[a-z-]+|[a-z-]+-acceptance|verify-staging-fixes|restore-drill|failure-injection)\.)/;
const RULES: Rule[] = [
{
name: "AWS access key id",
pattern: /\bAKIA[0-9A-Z]{16}\b/,
},
{
name: "Private key block",
pattern: /-----BEGIN (?:RSA |EC |OPENSSH |PGP )?PRIVATE KEY-----/,
},
{
name: "Resend API key",
pattern: /\bre_[A-Za-z0-9]{20,}\b/,
},
{
name: "Stripe secret key",
pattern: /\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{20,}\b/,
},
{
name: "GitHub token",
pattern: /\bgh[pousr]_[A-Za-z0-9]{30,}\b/,
},
{
name: "Google OAuth client secret",
pattern: /\bGOCSPX-[A-Za-z0-9_-]{20,}\b/,
},
{
name: "Slack token",
pattern: /\bxox[baprs]-[A-Za-z0-9-]{10,}\b/,
},
{
name: "JSON Web Token",
pattern: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/,
},
{
name: "Postgres URL with a non-local password",
pattern: /postgres(?:ql)?:\/\/[^:\s]+:[^@\s]{6,}@(?!localhost|127\.0\.0\.1|postgres\b)/,
allow: [/inkloom_local_dev/],
},
{
name: "Hardcoded assignment to a secret-looking name",
skipInTests: true,
pattern:
/\b(?:SECRET|PEPPER|PASSWORD|PRIVATE_KEY|API_KEY|ACCESS_TOKEN|CLIENT_SECRET)\s*[:=]\s*["'`][^"'`\n]{12,}["'`]/i,
allow: [
// Deliberate placeholders in .env.example — never valid outside dev.
/dev-only-insecure/,
// Cloudflare's published always-passing Turnstile test keys.
/1x0{6,}/,
// Test fixtures, which are obviously fake by their own text.
/test-(?:secret|pepper|access-code|ip-hash)/,
/integration-test-pepper/,
/a-perfectly-fine-passphrase-1/,
// Reading from the environment is the correct pattern, not a finding.
/process\.env\./,
/env\[["']/,
// Type declarations and schema definitions, not values.
/z\.string\(\)/,
/:\s*string\b/,
],
},
];
const SKIP_DIRS = new Set([
"node_modules",
".git",
"dist",
"build",
".react-router",
".wrangler",
"coverage",
"playwright-report",
"test-results",
]);
const SCAN_EXTENSIONS = new Set([
".ts",
".tsx",
".js",
".jsx",
".mjs",
".cjs",
".json",
".yaml",
".yml",
".toml",
".jsonc",
".md",
".sql",
".example",
".sh",
".html",
".css",
]);
interface Finding {
file: string;
line: number;
rule: string;
excerpt: string;
}
function walk(dir: string, out: string[] = []): string[] {
for (const entry of readdirSync(dir)) {
if (SKIP_DIRS.has(entry)) continue;
const full = path.join(dir, entry);
const stat = statSync(full);
if (stat.isDirectory()) walk(full, out);
else if (SCAN_EXTENSIONS.has(path.extname(entry)) || entry === ".env.example") out.push(full);
}
return out;
}
function scanFile(file: string): Finding[] {
const findings: Finding[] = [];
const content = readFileSync(file, "utf8");
const lines = content.split("\n");
const relative = path.relative(repoRoot, file);
const isTest = TEST_PATH.test(relative);
lines.forEach((line, index) => {
for (const rule of RULES) {
if (rule.skipInTests && isTest) continue;
if (!rule.pattern.test(line)) continue;
if (rule.allow?.some((allow) => allow.test(line))) continue;
findings.push({
file: path.relative(repoRoot, file),
line: index + 1,
rule: rule.name,
// Truncated: the report itself must not become a place a secret lives.
excerpt: `${line.trim().slice(0, 60)}…`,
});
}
});
return findings;
}
/**
* Server-side values that must NEVER appear in a client bundle, matched by
* variable name. A bundler that inlines one of these has turned a server secret
* into a public one.
*/
const CLIENT_FORBIDDEN = [
"BETTER_AUTH_SECRET",
"ACCESS_CODE_PEPPER",
"IP_HASH_PEPPER",
"RESEND_API_KEY",
"TURNSTILE_SECRET_KEY",
"GOOGLE_CLIENT_SECRET",
"DATABASE_URL",
];
function scanClientBundle(): Finding[] {
const clientDir = path.join(repoRoot, "apps", "web", "build", "client");
let files: string[];
try {
files = walk(clientDir);
} catch {
console.log(" (no client build found — run `pnpm build` to scan the bundle)");
return [];
}
const findings: Finding[] = [];
for (const file of files) {
const content = readFileSync(file, "utf8");
for (const name of CLIENT_FORBIDDEN) {
if (content.includes(name)) {
findings.push({
file: path.relative(repoRoot, file),
line: 0,
rule: `Server secret name "${name}" present in the CLIENT bundle`,
excerpt: "(a server-only value must never be bundled for the browser)",
});
}
}
// The pepper and auth secret are high-entropy; catch a leaked VALUE even if
// the variable name was minified away.
for (const envKey of ["ACCESS_CODE_PEPPER", "BETTER_AUTH_SECRET", "IP_HASH_PEPPER"]) {
const value = process.env[envKey];
if (
value &&
value.length >= 16 &&
!value.startsWith("dev-only-") &&
content.includes(value)
) {
findings.push({
file: path.relative(repoRoot, file),
line: 0,
rule: `VALUE of ${envKey} found in the CLIENT bundle`,
excerpt: "(redacted)",
});
}
}
}
return findings;
}
function main() {
console.log("\n Scanning source for committed secrets…");
const sourceFindings = walk(repoRoot).flatMap(scanFile);
console.log(" Scanning the client bundle for server-side secrets…");
const bundleFindings = scanClientBundle();
const findings = [...sourceFindings, ...bundleFindings];
if (findings.length === 0) {
console.log("\n No secrets found in source or client bundle.\n");
process.exit(0);
}
console.log(`\n ${findings.length} potential secret(s):\n`);
for (const finding of findings) {
const where = finding.line > 0 ? `${finding.file}:${finding.line}` : finding.file;
console.log(` ${where}`);
console.log(` ${finding.rule}`);
console.log(` ${finding.excerpt}\n`);
}
console.log(" If one is a false positive, add an `allow` pattern in scripts/check-secrets.ts.");
console.log(" If one is real: rotate it first, then remove it from history.\n");
process.exit(1);
}
main();