shipclear 0.2.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -4
- package/adapters/claude-code/.claude-plugin/plugin.json +1 -1
- package/adapters/claude-code/commands/safe-to-ship.md +7 -1
- package/adapters/generic/INSTRUCTIONS.md +7 -1
- package/cli/bin/shipclear.js +111 -3
- package/cli/lib/badge.js +216 -0
- package/cli/lib/checks.js +13 -2
- package/cli/lib/git.js +21 -4
- package/cli/lib/infra.js +536 -0
- package/cli/lib/patterns.js +220 -5
- package/cli/lib/probe.js +175 -0
- package/cli/lib/report.js +77 -8
- package/core/gate-checklist.md +13 -1
- package/docs/quickstart.md +23 -0
- package/docs/troubleshooting.md +37 -0
- package/package.json +1 -1
package/cli/lib/infra.js
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// Deployment checks: the gap between "the code is clean" and "the thing you
|
|
2
|
+
// deployed is safe".
|
|
3
|
+
//
|
|
4
|
+
// Everything in checks.js asks whether a secret was written somewhere it
|
|
5
|
+
// shouldn't be. These checks ask a different question — whether the app is
|
|
6
|
+
// *running on* a value anyone can look up, whether a missing variable quietly
|
|
7
|
+
// becomes a known one, and whether the deployment hands a backing service,
|
|
8
|
+
// a storage bucket or a debug page straight to the internet. A scan can read
|
|
9
|
+
// all of that out of the config files that are already in the project.
|
|
10
|
+
//
|
|
11
|
+
// Unlike the rest of the gate these checks walk the working tree rather than
|
|
12
|
+
// git's tracked files: the live `.env` is the whole point of the first check
|
|
13
|
+
// and is (correctly) never tracked.
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import {
|
|
16
|
+
isSecretishKey, isKnownDefaultCredential, isLeftoverStandIn, isVariableReference,
|
|
17
|
+
placeholderShapedCredential, productionSignal, maskSecret,
|
|
18
|
+
SHELL_DEFAULT, CODE_DEFAULTS,
|
|
19
|
+
MC_ANONYMOUS, AWS_PUBLIC_ACL, TF_PUBLIC_ACL, TF_PUBLIC_ACCESS_BLOCK_OFF,
|
|
20
|
+
WILDCARD_PRINCIPAL, S3_ACTION, GCS_ALL_USERS, AZURE_PUBLIC_CONTAINER, FIREBASE_OPEN_RULE,
|
|
21
|
+
PROXY_UPSTREAM_LINE, PROXY_ROUTE_LINE, HOST_PORT, BACKING_SERVICES, BACKING_PORTS,
|
|
22
|
+
SENSITIVE_PATHS, ROUTE_DEFINITION, AUTH_NEARBY, NON_PRODUCTION_GUARD, PROXY_DENY,
|
|
23
|
+
CSP_HEADER, CSP_MENTION,
|
|
24
|
+
REQUEST_URL_SOURCE, OUTBOUND_FETCH, SSRF_GUARD, FOLLOWS_REDIRECTS,
|
|
25
|
+
} from './patterns.js';
|
|
26
|
+
import { walkFiles, isTextCandidate, readFileSafe } from './scan.js';
|
|
27
|
+
|
|
28
|
+
const base = (rel) => rel.split('/').pop();
|
|
29
|
+
const isEnvTemplate = (rel) => /^\.env(?:\..+)?\.(?:example|sample|template|dist)$|^\.env\.(?:example|sample|template|dist)$/.test(base(rel));
|
|
30
|
+
const isLiveEnv = (rel) => /^\.env(?:\..+)?$/.test(base(rel)) && !isEnvTemplate(rel);
|
|
31
|
+
const isCompose = (rel) => /^(?:docker-)?compose(?:[.-].+)?\.ya?ml$/i.test(base(rel));
|
|
32
|
+
const isTfVars = (rel) => /\.tfvars$/i.test(rel);
|
|
33
|
+
const isHelmValues = (rel) => /^values(?:[.-].+)?\.ya?ml$/i.test(base(rel));
|
|
34
|
+
const isYaml = (rel) => /\.ya?ml$/i.test(rel);
|
|
35
|
+
const isTerraform = (rel) => /\.tf$/i.test(rel);
|
|
36
|
+
const isDoc = (rel) => /\.(?:md|mdx|mdc|rst|txt)$/i.test(rel);
|
|
37
|
+
const isTestFile = (rel) => /(?:^|\/)(?:tests?|__tests__|spec|fixtures?)\//i.test(rel) || /\.(?:test|spec)\.[a-z]+$/i.test(rel);
|
|
38
|
+
const isProxyConfig = (rel) => /(?:^|\/)caddyfile|\.caddy$|nginx|traefik|envoy|haproxy|httpd\.conf$|\.vhost$|sites-(?:available|enabled)\//i.test(rel);
|
|
39
|
+
const isCodeFile = (rel) => /\.(?:js|cjs|mjs|jsx|ts|tsx|py|rb|go|php|java|kt|rs|cs|sh|bash)$/i.test(rel);
|
|
40
|
+
|
|
41
|
+
/** A dev-machine path like /media/* is a broad prefix; /media/logo.png isn't. */
|
|
42
|
+
const isBroadPrefix = (route) => !route || /[*]$|\/$|^\/[^/]*$/.test(route);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Deployment-configuration findings for a project. Same shape as the rest of
|
|
46
|
+
* the gate: { id, severity, file, line?, detail }.
|
|
47
|
+
*/
|
|
48
|
+
export function runInfraChecks(root, { ignored = () => false } = {}) {
|
|
49
|
+
const findings = [];
|
|
50
|
+
const files = walkFiles(root).filter((f) => !ignored(f) && isTextCandidate(f));
|
|
51
|
+
const cache = new Map();
|
|
52
|
+
const read = (rel) => {
|
|
53
|
+
if (!cache.has(rel)) cache.set(rel, readFileSafe(path.join(root, rel)) ?? '');
|
|
54
|
+
return cache.get(rel);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// --- is this project facing the public? ---
|
|
58
|
+
// The same known password is untidy on a laptop and urgent on a domain.
|
|
59
|
+
let production = null;
|
|
60
|
+
for (const rel of files) {
|
|
61
|
+
if (!isLiveEnv(rel) && !isCompose(rel) && !isYaml(rel) && !isTfVars(rel)) continue;
|
|
62
|
+
const signal = productionSignal(read(rel));
|
|
63
|
+
if (signal) { production = `${signal} in \`${rel}\``; break; }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const liveCredentials = inUseCredentials(files, read, production, findings);
|
|
67
|
+
silentDefaults(files, read, liveCredentials, findings);
|
|
68
|
+
publicStorage(files, read, findings);
|
|
69
|
+
proxyTopology(files, read, findings);
|
|
70
|
+
openDebugRoutes(files, read, findings);
|
|
71
|
+
headerCoverage(files, read, findings);
|
|
72
|
+
serverSideFetches(files, read, findings);
|
|
73
|
+
return findings;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// #1 The app is running on a credential anybody can look up
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
// KEY=VALUE, as .env and .tfvars write it.
|
|
81
|
+
function envAssignments(content) {
|
|
82
|
+
const out = [];
|
|
83
|
+
content.split('\n').forEach((text, i) => {
|
|
84
|
+
const m = text.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
85
|
+
if (!m || text.trim().startsWith('#')) return;
|
|
86
|
+
out.push({ key: m[1], value: cleanValue(m[2]), line: i + 1 });
|
|
87
|
+
});
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Environment entries as YAML writes them: `- KEY=value`, `KEY: value`, and
|
|
92
|
+
// the k8s `- name: KEY` / `value: v` pair. Bare `key:` is skipped — lowercase
|
|
93
|
+
// YAML keys are ordinary structure, not credentials (see isSecretishKey).
|
|
94
|
+
function yamlAssignments(content) {
|
|
95
|
+
const out = [];
|
|
96
|
+
const lines = content.split('\n');
|
|
97
|
+
lines.forEach((text, i) => {
|
|
98
|
+
if (text.trim().startsWith('#')) return;
|
|
99
|
+
let m = text.match(/^\s*-\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
100
|
+
if (m) { out.push({ key: m[1], value: cleanValue(m[2]), line: i + 1 }); return; }
|
|
101
|
+
m = text.match(/^\s*-?\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(\S.*)$/);
|
|
102
|
+
if (m) {
|
|
103
|
+
if (m[1] === 'name') {
|
|
104
|
+
// k8s env entry: the value is on one of the next couple of lines.
|
|
105
|
+
const value = (lines[i + 1] || '').match(/^\s*value\s*:\s*(\S.*)$/);
|
|
106
|
+
if (value) out.push({ key: m[2].trim(), value: cleanValue(value[1]), line: i + 1 });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
out.push({ key: m[1], value: cleanValue(m[2]), line: i + 1 });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function cleanValue(raw) {
|
|
116
|
+
let v = raw.trim();
|
|
117
|
+
const quoted = v.match(/^(['"])(.*)\1/);
|
|
118
|
+
if (quoted) return quoted[2];
|
|
119
|
+
return v.replace(/\s+#.*$/, '').trim();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function assignmentsIn(rel, content) {
|
|
123
|
+
if (isLiveEnv(rel) || isEnvTemplate(rel) || isTfVars(rel)) return envAssignments(content);
|
|
124
|
+
if (isYaml(rel)) return yamlAssignments(content);
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function inUseCredentials(files, read, production, findings) {
|
|
129
|
+
// What the project's own example file says — committed, therefore public.
|
|
130
|
+
const examples = new Map();
|
|
131
|
+
for (const rel of files.filter(isEnvTemplate)) {
|
|
132
|
+
for (const { key, value } of envAssignments(read(rel))) {
|
|
133
|
+
if (value) examples.set(`${key}=${value}`, rel);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const live = new Map(); // key -> severity, for the escalation in #2
|
|
138
|
+
const configFiles = files.filter(
|
|
139
|
+
(rel) => isLiveEnv(rel) || isCompose(rel) || isTfVars(rel) || isHelmValues(rel)
|
|
140
|
+
|| (isYaml(rel) && /kind:\s*(?:Secret|Deployment|StatefulSet|ConfigMap|Pod)/.test(read(rel)))
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
for (const rel of configFiles) {
|
|
144
|
+
const seen = new Set();
|
|
145
|
+
const dbNote = /volumes\s*:/.test(read(rel)) && (isCompose(rel) || isYaml(rel));
|
|
146
|
+
for (const { key, value, line } of assignmentsIn(rel, read(rel))) {
|
|
147
|
+
if (!value || seen.has(key) || !isSecretishKey(key) || isVariableReference(value)) continue;
|
|
148
|
+
|
|
149
|
+
let why = null;
|
|
150
|
+
let weak = false;
|
|
151
|
+
if (examples.has(`${key}=${value}`)) {
|
|
152
|
+
why = `\`${key}\` is the same value as in your \`${examples.get(`${key}=${value}`)}\`, which is committed to your repo`;
|
|
153
|
+
} else if (isKnownDefaultCredential(value)) {
|
|
154
|
+
why = `\`${key}\` is \`${value}\` — the documented default for that service, and the first thing anyone tries`;
|
|
155
|
+
} else if (isLeftoverStandIn(value)) {
|
|
156
|
+
why = `\`${key}\` is \`${value}\` — a stand-in value that was never replaced`;
|
|
157
|
+
} else if (placeholderShapedCredential(key, value)) {
|
|
158
|
+
why = `\`${key}\` is still a placeholder (\`${maskSecret(value)}\`), not a generated secret`;
|
|
159
|
+
weak = true;
|
|
160
|
+
}
|
|
161
|
+
if (!why) continue;
|
|
162
|
+
// A short or placeholder-shaped value on a project with no sign of
|
|
163
|
+
// being deployed anywhere is noise; a value published in the repo's
|
|
164
|
+
// own example file is worth saying either way.
|
|
165
|
+
if (weak && !production) continue;
|
|
166
|
+
|
|
167
|
+
seen.add(key);
|
|
168
|
+
const severity = production ? 'critical' : 'medium';
|
|
169
|
+
live.set(key.toUpperCase(), severity);
|
|
170
|
+
|
|
171
|
+
// One finding per file, not per variable: every value in a file needs
|
|
172
|
+
// the same treatment (generate one, set it in both places), and three
|
|
173
|
+
// copies of the same ten-line explanation read as three problems.
|
|
174
|
+
const existing = findings.find((f) => f.id === 'credential-in-use-is-public' && f.file === rel);
|
|
175
|
+
if (existing) {
|
|
176
|
+
existing.reasons.push(why);
|
|
177
|
+
existing.line = undefined;
|
|
178
|
+
existing.detail = `${existing.reasons.length} values in this file are publicly known — `
|
|
179
|
+
+ `${existing.reasons.join('; ')}.`;
|
|
180
|
+
existing.dbInContainer = existing.dbInContainer
|
|
181
|
+
|| (dbNote && /postgres|mysql|mariadb|mongo|redis|^db_|database/i.test(key));
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
findings.push({
|
|
185
|
+
id: 'credential-in-use-is-public',
|
|
186
|
+
severity,
|
|
187
|
+
file: rel,
|
|
188
|
+
line,
|
|
189
|
+
detail: why + '.',
|
|
190
|
+
reasons: [why],
|
|
191
|
+
production,
|
|
192
|
+
dbInContainer: dbNote && /postgres|mysql|mariadb|mongo|redis|^db_|database/i.test(key),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return live;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// #2 A missing secret silently becomes a known one
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
// One finding per variable, not per file. A JWT secret read in five route
|
|
204
|
+
// files is one problem with one answer — five copies of the same ten-line
|
|
205
|
+
// explanation read as "do this five times" (the same reasoning as the
|
|
206
|
+
// per-commit grouping of history findings).
|
|
207
|
+
function silentDefaults(files, read, liveCredentials, findings) {
|
|
208
|
+
const byKey = new Map();
|
|
209
|
+
for (const rel of files) {
|
|
210
|
+
if (isDoc(rel) || isEnvTemplate(rel) || isTestFile(rel)) continue;
|
|
211
|
+
if (!isCompose(rel) && !isYaml(rel) && !isCodeFile(rel) && !isTerraform(rel) && !isProxyConfig(rel)) continue;
|
|
212
|
+
const content = read(rel);
|
|
213
|
+
const seen = new Set();
|
|
214
|
+
|
|
215
|
+
const hit = (key, value, index) => {
|
|
216
|
+
if (!value || seen.has(key) || !isSecretishKey(key)) return;
|
|
217
|
+
seen.add(key);
|
|
218
|
+
const line = lineAt(content, index);
|
|
219
|
+
const existing = byKey.get(key.toUpperCase());
|
|
220
|
+
if (existing) { existing.alsoAt.push(`${rel}:${line}`); return; }
|
|
221
|
+
// Escalation: the fallback isn't hypothetical if check #1 found that
|
|
222
|
+
// same variable running on a known value.
|
|
223
|
+
const inUse = liveCredentials.get(key.toUpperCase());
|
|
224
|
+
const finding = {
|
|
225
|
+
id: 'secret-default-fallback',
|
|
226
|
+
severity: inUse === 'critical' ? 'critical' : 'medium',
|
|
227
|
+
file: rel,
|
|
228
|
+
line,
|
|
229
|
+
key,
|
|
230
|
+
detail: `\`${key}\` falls back to \`${maskSecret(value)}\` when it isn't set`
|
|
231
|
+
+ (inUse ? ' — and that fallback is the value your config is actually using' : '')
|
|
232
|
+
+ '.',
|
|
233
|
+
dbInContainer: /volumes\s*:/.test(content) && /postgres|mysql|mariadb|mongo|^db_|database/i.test(key),
|
|
234
|
+
alsoAt: [],
|
|
235
|
+
};
|
|
236
|
+
byKey.set(key.toUpperCase(), finding);
|
|
237
|
+
findings.push(finding);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
SHELL_DEFAULT.lastIndex = 0;
|
|
241
|
+
let m;
|
|
242
|
+
while ((m = SHELL_DEFAULT.exec(content))) hit(m[1], cleanValue(m[2]), m.index);
|
|
243
|
+
|
|
244
|
+
if (isCodeFile(rel)) {
|
|
245
|
+
for (const pattern of CODE_DEFAULTS) {
|
|
246
|
+
pattern.lastIndex = 0;
|
|
247
|
+
let c;
|
|
248
|
+
while ((c = pattern.exec(content))) {
|
|
249
|
+
// The JS pattern has two alternative name groups (dot and bracket
|
|
250
|
+
// access); the value is always the last captured group.
|
|
251
|
+
const groups = c.slice(1).filter((g) => g !== undefined);
|
|
252
|
+
hit(groups[0], groups[groups.length - 1], c.index);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
for (const f of byKey.values()) {
|
|
259
|
+
if (!f.alsoAt.length) continue;
|
|
260
|
+
const shown = f.alsoAt.slice(0, 4);
|
|
261
|
+
const more = f.alsoAt.length - shown.length;
|
|
262
|
+
f.detail += ` The same fallback is written in ${f.alsoAt.length} other `
|
|
263
|
+
+ `${f.alsoAt.length === 1 ? 'place' : 'places'}: ${shown.join(', ')}`
|
|
264
|
+
+ (more ? `, and ${more} more` : '') + ' — all of them need the same change.';
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
// #3 Storage anyone can read — or list
|
|
270
|
+
// ---------------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
function publicStorage(files, read, findings) {
|
|
273
|
+
const add = (rel, index, content, severity, detail, listing = false) =>
|
|
274
|
+
findings.push({ id: 'public-bucket', severity, file: rel, line: lineAt(content, index), detail, listing });
|
|
275
|
+
|
|
276
|
+
for (const rel of files) {
|
|
277
|
+
if (isDoc(rel) || isTestFile(rel)) continue;
|
|
278
|
+
const content = read(rel);
|
|
279
|
+
|
|
280
|
+
MC_ANONYMOUS.lastIndex = 0;
|
|
281
|
+
let m;
|
|
282
|
+
while ((m = MC_ANONYMOUS.exec(content))) {
|
|
283
|
+
const policy = m[1].toLowerCase();
|
|
284
|
+
if (policy === 'none') continue;
|
|
285
|
+
const writable = policy === 'upload' || policy === 'write' || policy === 'public';
|
|
286
|
+
add(rel, m.index, content,
|
|
287
|
+
'critical',
|
|
288
|
+
`\`${m[0].trim().replace(/["';]+$/, '')}\` — MinIO's \`${policy}\` policy lets anonymous users **list** the bucket`
|
|
289
|
+
+ (writable ? ' and upload to it' : '') + ', not just read a file they already have the link to.',
|
|
290
|
+
true);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
AWS_PUBLIC_ACL.lastIndex = 0;
|
|
294
|
+
while ((m = AWS_PUBLIC_ACL.exec(content))) {
|
|
295
|
+
add(rel, m.index, content, m[1] === 'public-read-write' ? 'critical' : 'medium',
|
|
296
|
+
`\`--acl ${m[1]}\` makes the object or bucket readable${m[1] === 'public-read-write' ? ' *and writable*' : ''} by anyone.`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
TF_PUBLIC_ACL.lastIndex = 0;
|
|
300
|
+
while ((m = TF_PUBLIC_ACL.exec(content))) {
|
|
301
|
+
add(rel, m.index, content, m[1] === 'public-read-write' ? 'critical' : 'medium',
|
|
302
|
+
`\`acl = "${m[1]}"\` on a bucket.`, m[1] === 'public-read-write');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
TF_PUBLIC_ACCESS_BLOCK_OFF.lastIndex = 0;
|
|
306
|
+
while ((m = TF_PUBLIC_ACCESS_BLOCK_OFF.exec(content))) {
|
|
307
|
+
add(rel, m.index, content, 'medium',
|
|
308
|
+
`\`${m[1]} = false\` switches off one of the guards that stops this bucket being made public by accident.`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// A bucket policy with a wildcard principal: the actions decide how bad.
|
|
312
|
+
WILDCARD_PRINCIPAL.lastIndex = 0;
|
|
313
|
+
while ((m = WILDCARD_PRINCIPAL.exec(content))) {
|
|
314
|
+
const scope = content.slice(m.index, m.index + 800);
|
|
315
|
+
S3_ACTION.lastIndex = 0;
|
|
316
|
+
const actions = [...scope.matchAll(S3_ACTION)].map((a) => a[1]);
|
|
317
|
+
const listing = actions.some((a) => /ListBucket|s3:\*/.test(a));
|
|
318
|
+
const writing = actions.some((a) => /PutObject|DeleteObject|s3:\*/.test(a));
|
|
319
|
+
if (!actions.length) continue;
|
|
320
|
+
add(rel, m.index, content, listing || writing ? 'critical' : 'medium',
|
|
321
|
+
`a bucket policy grants \`${actions.join('`, `')}\` to everyone (\`"Principal": "*"\`)`
|
|
322
|
+
+ (listing ? ' — including permission to list the whole bucket' : '')
|
|
323
|
+
+ (writing ? ' — including permission to write or delete objects' : '') + '.',
|
|
324
|
+
listing);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
GCS_ALL_USERS.lastIndex = 0;
|
|
328
|
+
while ((m = GCS_ALL_USERS.exec(content))) {
|
|
329
|
+
const nearby = content.slice(Math.max(0, m.index - 300), m.index + 300);
|
|
330
|
+
if (!/role|iam|member|gsutil|binding|storage/i.test(nearby)) continue;
|
|
331
|
+
add(rel, m.index, content, 'critical',
|
|
332
|
+
`\`${m[1]}\` in a storage IAM binding — that is literally "anyone on the internet", and the viewer role includes listing the bucket.`,
|
|
333
|
+
true);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
AZURE_PUBLIC_CONTAINER.lastIndex = 0;
|
|
337
|
+
while ((m = AZURE_PUBLIC_CONTAINER.exec(content))) {
|
|
338
|
+
const container = m[1].toLowerCase() === 'container';
|
|
339
|
+
add(rel, m.index, content, container ? 'critical' : 'medium',
|
|
340
|
+
`this container is public (\`${m[0].trim()}\`)`
|
|
341
|
+
+ (container ? ' — the `container` level also allows anonymous listing of everything inside' : '') + '.',
|
|
342
|
+
container);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (/\/(?:databases|storage|buckets)\/|rules_version|service\s+(?:cloud\.firestore|firebase\.storage)/.test(content)) {
|
|
346
|
+
FIREBASE_OPEN_RULE.lastIndex = 0;
|
|
347
|
+
while ((m = FIREBASE_OPEN_RULE.exec(content))) {
|
|
348
|
+
const ops = m[1].trim();
|
|
349
|
+
add(rel, m.index, content, /write/.test(ops) ? 'critical' : 'medium',
|
|
350
|
+
`\`allow ${ops}: if true\` — every user, signed in or not, can ${ops.replace(/,\s*/g, ' and ')} this data.`,
|
|
351
|
+
true);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ---------------------------------------------------------------------------
|
|
358
|
+
// #4 A public route that forwards into a backing service
|
|
359
|
+
// ---------------------------------------------------------------------------
|
|
360
|
+
|
|
361
|
+
function proxyTopology(files, read, findings) {
|
|
362
|
+
for (const rel of files) {
|
|
363
|
+
const content = read(rel);
|
|
364
|
+
const ingress = isYaml(rel) && /kind:\s*Ingress/.test(content);
|
|
365
|
+
if (!isProxyConfig(rel) && !ingress) continue;
|
|
366
|
+
if (isDoc(rel) || isTestFile(rel)) continue;
|
|
367
|
+
|
|
368
|
+
const lines = content.split('\n');
|
|
369
|
+
let route = null;
|
|
370
|
+
const seen = new Set();
|
|
371
|
+
lines.forEach((text, i) => {
|
|
372
|
+
const r = text.match(PROXY_ROUTE_LINE);
|
|
373
|
+
if (r) route = (r[1] || r[2] || '').trim();
|
|
374
|
+
if (!PROXY_UPSTREAM_LINE.test(text)) return;
|
|
375
|
+
|
|
376
|
+
HOST_PORT.lastIndex = 0;
|
|
377
|
+
let hp;
|
|
378
|
+
const candidates = [];
|
|
379
|
+
while ((hp = HOST_PORT.exec(text))) candidates.push([hp[1].toLowerCase(), Number(hp[2])]);
|
|
380
|
+
// Ingress and Traefik split host and port across lines; carry the
|
|
381
|
+
// service name from whichever nearby line names it.
|
|
382
|
+
if (!candidates.length) {
|
|
383
|
+
const name = text.match(/(?:serviceName|service_name|name)\s*:\s*['"]?([a-z0-9-]+)/i);
|
|
384
|
+
const port = (lines[i + 1] || '') + (lines[i + 2] || '');
|
|
385
|
+
const num = port.match(/(?:number|port)\s*:\s*(\d{2,5})/);
|
|
386
|
+
if (name) candidates.push([name[1].toLowerCase(), num ? Number(num[1]) : 0]);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
for (const [host, port] of candidates) {
|
|
390
|
+
const service = Object.keys(BACKING_SERVICES).find((s) => host === s || host.includes(s))
|
|
391
|
+
|| BACKING_PORTS[port];
|
|
392
|
+
if (!service || !BACKING_SERVICES[service]) continue;
|
|
393
|
+
if (seen.has(service + route)) continue;
|
|
394
|
+
seen.add(service + route);
|
|
395
|
+
findings.push({
|
|
396
|
+
id: 'proxy-exposes-service',
|
|
397
|
+
severity: BACKING_SERVICES[service],
|
|
398
|
+
file: rel,
|
|
399
|
+
line: i + 1,
|
|
400
|
+
detail: `the public route \`${route || '/'}\` forwards to \`${host}${port ? ':' + port : ''}\` (${service})`
|
|
401
|
+
+ (isBroadPrefix(route) ? ' — and it is a catch-all prefix, so that service\'s own admin and API endpoints are reachable too' : '')
|
|
402
|
+
+ '.',
|
|
403
|
+
service,
|
|
404
|
+
route: route || '/',
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ---------------------------------------------------------------------------
|
|
412
|
+
// #5 Debug and monitoring pages open to the public
|
|
413
|
+
// ---------------------------------------------------------------------------
|
|
414
|
+
|
|
415
|
+
function openDebugRoutes(files, read, findings) {
|
|
416
|
+
for (const rel of files) {
|
|
417
|
+
if (isDoc(rel) || isTestFile(rel)) continue;
|
|
418
|
+
const proxy = isProxyConfig(rel);
|
|
419
|
+
if (!isCodeFile(rel) && !proxy) continue;
|
|
420
|
+
const content = read(rel);
|
|
421
|
+
const lines = content.split('\n');
|
|
422
|
+
const seen = new Set();
|
|
423
|
+
|
|
424
|
+
const consider = (route, lineIndex) => {
|
|
425
|
+
const normalized = route.replace(/\/+$/, '').replace(/\*$/, '').toLowerCase() || '/';
|
|
426
|
+
const match = Object.keys(SENSITIVE_PATHS).find(
|
|
427
|
+
(p) => normalized === p || normalized.startsWith(p + '/') || normalized.endsWith(p)
|
|
428
|
+
);
|
|
429
|
+
if (!match || seen.has(match)) return;
|
|
430
|
+
const nearby = lines.slice(Math.max(0, lineIndex - 4), lineIndex + 5).join('\n');
|
|
431
|
+
// Already protected, or deliberately absent outside development —
|
|
432
|
+
// `docs_url="/api/docs" if not settings.is_production else None` is the
|
|
433
|
+
// pattern we *want* people to use, so it must stay quiet.
|
|
434
|
+
if (AUTH_NEARBY.test(nearby) || NON_PRODUCTION_GUARD.test(nearby)) return;
|
|
435
|
+
if (proxy && PROXY_DENY.test(nearby)) return;
|
|
436
|
+
seen.add(match);
|
|
437
|
+
findings.push({
|
|
438
|
+
id: 'debug-route-public',
|
|
439
|
+
severity: SENSITIVE_PATHS[match],
|
|
440
|
+
file: rel,
|
|
441
|
+
line: lineIndex + 1,
|
|
442
|
+
detail: `\`${route}\` is served with no sign of an authentication check or a production switch.`,
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
lines.forEach((text, i) => {
|
|
447
|
+
ROUTE_DEFINITION.lastIndex = 0;
|
|
448
|
+
let m;
|
|
449
|
+
while ((m = ROUTE_DEFINITION.exec(text))) {
|
|
450
|
+
const route = m[1] || m[2] || m[3] || m[4];
|
|
451
|
+
if (route && route.startsWith('/')) consider(route, i);
|
|
452
|
+
}
|
|
453
|
+
if (proxy) {
|
|
454
|
+
const r = text.match(PROXY_ROUTE_LINE);
|
|
455
|
+
const route = r && (r[1] || r[2]);
|
|
456
|
+
if (route && route.startsWith('/')) consider(route, i);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// ---------------------------------------------------------------------------
|
|
463
|
+
// #6 Security headers that only cover part of the site
|
|
464
|
+
// ---------------------------------------------------------------------------
|
|
465
|
+
|
|
466
|
+
function headerCoverage(files, read, findings) {
|
|
467
|
+
// (a) A proxy config that talks about CSP in its comments and sets none.
|
|
468
|
+
// The comment is the tell: someone believed this was handled.
|
|
469
|
+
for (const rel of files.filter(isProxyConfig)) {
|
|
470
|
+
const content = read(rel);
|
|
471
|
+
if (CSP_HEADER.test(content.replace(/^\s*[#;].*$/gm, ''))) continue;
|
|
472
|
+
const lines = content.split('\n');
|
|
473
|
+
const claim = lines.findIndex((l) => /^\s*[#;]/.test(l) && CSP_MENTION.test(l));
|
|
474
|
+
if (claim === -1) continue;
|
|
475
|
+
findings.push({
|
|
476
|
+
id: 'headers-html-uncovered',
|
|
477
|
+
severity: 'medium',
|
|
478
|
+
file: rel,
|
|
479
|
+
line: claim + 1,
|
|
480
|
+
detail: 'this file\'s comments describe a Content-Security-Policy, but it does not set one.',
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// (b) A CSP that exists, but only on the API — the half of the app that
|
|
485
|
+
// doesn't run scripts. The pages people load in a browser get nothing.
|
|
486
|
+
const withCsp = files.filter((rel) => !isDoc(rel) && !isTestFile(rel) && CSP_HEADER.test(read(rel)));
|
|
487
|
+
if (!withCsp.length) return; // no CSP anywhere is a different (semantic) conversation
|
|
488
|
+
const apiOnly = withCsp.every((rel) => /["'`]\/api/.test(read(rel)) || /\/api/.test(rel));
|
|
489
|
+
if (!apiOnly) return;
|
|
490
|
+
const htmlSurface = files.find(
|
|
491
|
+
(rel) => /^(?:frontend\/|web\/|client\/)?(?:next|nuxt|vite|svelte|remix|astro)\.config\.[a-z]+$/i.test(rel)
|
|
492
|
+
|| base(rel) === 'index.html'
|
|
493
|
+
) || files.find((rel) => isProxyConfig(rel) && /^\s*(?:handle|route|location|reverse_proxy)\s+\/\s*[{\n]/m.test(read(rel)));
|
|
494
|
+
if (!htmlSurface) return;
|
|
495
|
+
findings.push({
|
|
496
|
+
id: 'headers-html-uncovered',
|
|
497
|
+
severity: 'medium',
|
|
498
|
+
file: withCsp[0],
|
|
499
|
+
detail: `the Content-Security-Policy here is scoped to \`/api\`, but the pages a browser actually loads are served elsewhere (\`${htmlSurface}\`) with no policy.`,
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// ---------------------------------------------------------------------------
|
|
504
|
+
// #7 The server fetching any address a user hands it
|
|
505
|
+
// ---------------------------------------------------------------------------
|
|
506
|
+
|
|
507
|
+
function serverSideFetches(files, read, findings) {
|
|
508
|
+
for (const rel of files) {
|
|
509
|
+
if (!isCodeFile(rel) || isDoc(rel) || isTestFile(rel)) continue;
|
|
510
|
+
const content = read(rel);
|
|
511
|
+
if (!REQUEST_URL_SOURCE.test(content)) continue;
|
|
512
|
+
const lines = content.split('\n');
|
|
513
|
+
const call = lines.findIndex((l) => OUTBOUND_FETCH.test(l));
|
|
514
|
+
if (call === -1) continue;
|
|
515
|
+
|
|
516
|
+
const guarded = SSRF_GUARD.test(content);
|
|
517
|
+
const redirects = FOLLOWS_REDIRECTS.test(content);
|
|
518
|
+
if (guarded && !redirects) continue;
|
|
519
|
+
findings.push({
|
|
520
|
+
id: 'ssrf-unchecked-fetch',
|
|
521
|
+
severity: 'high',
|
|
522
|
+
file: rel,
|
|
523
|
+
line: call + 1,
|
|
524
|
+
detail: guarded
|
|
525
|
+
? 'this fetch of a user-supplied address follows redirects, which steps around the address check above it — a redirect to `169.254.169.254` or `localhost` passes.'
|
|
526
|
+
: 'a web address that arrives in a request is fetched by the server with no check that it points at the public internet.',
|
|
527
|
+
redirectsOnly: guarded,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function lineAt(content, index) {
|
|
533
|
+
let line = 1;
|
|
534
|
+
for (let i = 0; i < index && i < content.length; i++) if (content.charCodeAt(i) === 10) line++;
|
|
535
|
+
return line;
|
|
536
|
+
}
|