plumber-linter 1.4.0 → 1.4.2
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 +303 -236
- package/bin/plumber.js +1375 -1363
- package/package.json +51 -51
package/bin/plumber.js
CHANGED
|
@@ -1,1363 +1,1375 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Plumber CLI & MCP Server (Node.js Thin Client)
|
|
5
|
-
* Inspects repositories for leaked credentials, AI slop tells, broken pipes, and fragile joints.
|
|
6
|
-
* GitHub Actions workflow YAML is parsed with js-yaml.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
const https = require('https');
|
|
12
|
-
const readline = require('readline');
|
|
13
|
-
const { execFileSync } = require('child_process');
|
|
14
|
-
const yaml = require('js-yaml');
|
|
15
|
-
|
|
16
|
-
const API_BASE = process.env.PLUMBER_API_URL || 'http://localhost:3100';
|
|
17
|
-
const HOOK_MARKER = '# plumber-managed-hook';
|
|
18
|
-
const HOOK_BACKUP_SUFFIX = '.pre-plumber';
|
|
19
|
-
const ENV_TEMPLATE_RE = /\.(example|sample|template|dist)$/;
|
|
20
|
-
const SCANNABLE_EXTS = new Set(['.py', '.js', '.jsx', '.ts', '.tsx', '.html', '.htm', '.css', '.scss', '.less', '.vue', '.svelte', '.astro', '.go', '.rs', '.java', '.env', '.yml', '.yaml', '.sh']);
|
|
21
|
-
|
|
22
|
-
const PRE_COMMIT_SCRIPT = `#!/bin/sh
|
|
23
|
-
${HOOK_MARKER}
|
|
24
|
-
# Installed by Plumber. Remove with: plumber uninstall-hook
|
|
25
|
-
# Bypass once with: git commit --no-verify
|
|
26
|
-
if command -v plumber >/dev/null 2>&1; then
|
|
27
|
-
exec plumber hook {flags}
|
|
28
|
-
fi
|
|
29
|
-
if [ -x "./node_modules/.bin/plumber" ]; then
|
|
30
|
-
exec ./node_modules/.bin/plumber hook {flags}
|
|
31
|
-
fi
|
|
32
|
-
if command -v plumber-linter >/dev/null 2>&1; then
|
|
33
|
-
exec plumber-linter hook {flags}
|
|
34
|
-
fi
|
|
35
|
-
if command -v npx >/dev/null 2>&1; then
|
|
36
|
-
exec npx --no-install plumber-linter hook {flags}
|
|
37
|
-
fi
|
|
38
|
-
if command -v python3 >/dev/null 2>&1; then
|
|
39
|
-
exec python3 -m plumber hook {flags}
|
|
40
|
-
fi
|
|
41
|
-
if command -v python >/dev/null 2>&1; then
|
|
42
|
-
exec python -m plumber hook {flags}
|
|
43
|
-
fi
|
|
44
|
-
echo "[Plumber] Could not locate plumber. Install it with: npm i -g plumber-linter" >&2
|
|
45
|
-
exit 1
|
|
46
|
-
`;
|
|
47
|
-
|
|
48
|
-
const RULES = [
|
|
49
|
-
{ id: 'leak/api-key', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Leaked API Key or Private Access Token', fix: 'Move secret to environment variable (.env).' },
|
|
50
|
-
{ id: 'leak/committed-env', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Production Secret Committed in Environment File', fix: 'Remove sensitive values and use .env.example.' },
|
|
51
|
-
{ id: 'leak/hardcoded-secret', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Hardcoded Password or Database Credential', fix: 'Replace with process.env / os.environ reference.' },
|
|
52
|
-
{ id: 'sec/raw-html', cat: 'Leaks & Secrets', sev: 'MEDIUM', title: 'Raw HTML Injection (XSS Risk)', fix: 'Use textContent or safe DOM templating.' },
|
|
53
|
-
{ id: 'clog/elided-code', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'AI-Elided Code Snippet Left in File', fix: 'Restore the complete function or component implementation.' },
|
|
54
|
-
{ id: 'clog/swallowed-error', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'Swallowed Exception (Silent Failure)', fix: 'Log the error or properly re-raise/handle it.' },
|
|
55
|
-
{ id: 'clog/stub-function', cat: 'Clogs & Dead Code', sev: 'MEDIUM', title: 'Unimplemented Stub Function', fix: 'Complete missing logic or remove placeholder.' },
|
|
56
|
-
{ id: 'clog/step-comments', cat: 'Clogs & Dead Code', sev: 'LOW', title: 'Robotic AI Step Comments Left Behind', fix: 'Clean up AI instructional comments.' },
|
|
57
|
-
{ id: 'clog/dead-code', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'Unreachable Dead Code', fix: 'Delete unreachable lines or run plumber clean --write.' },
|
|
58
|
-
{ id: 'slop/pointless-wrapper', cat: 'Clogs & Dead Code', sev: 'LOW', title: 'Function That Only Forwards Its Arguments', fix: 'Call underlying function directly.' },
|
|
59
|
-
{ id: 'joint/icon-button', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Icon Button Without Accessible Name', fix: 'Add aria-label attribute to button element.' },
|
|
60
|
-
{ id: 'joint/clickable-div', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Non-Semantic Clickable Element (Missing Button / Role)', fix: 'Use native <button type="button">.' },
|
|
61
|
-
{ id: 'joint/empty-test', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Empty Test with No Assertions', fix: 'Add assert statements or verification logic.' },
|
|
62
|
-
{ id: 'joint/focus-removed', cat: 'Joints & Accessibility', sev: 'MEDIUM', title: 'Focus Outline Removed Without Replacement', fix: 'Provide a visible :focus-visible style.' },
|
|
63
|
-
{ id: 'pipe/phantom-import', cat: 'Pipes & Dependencies', sev: 'HIGH', title: 'Plausible but Hallucinated Package Import', fix: 'Check package registry or use official package.' },
|
|
64
|
-
{ id: 'pipe/dead-link', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Broken or Null Link in Navigation', fix: 'Use a <button> for actions or valid route for href.' },
|
|
65
|
-
{ id: 'slop/ai-gradient', cat: 'AI Slop Tells', sev: 'MEDIUM', title: 'Purple / Violet AI Default Gradient', fix: 'Use clean brand tokens or neutral surfaces.' },
|
|
66
|
-
{ id: 'slop/chatbot-residue', cat: 'AI Slop Tells', sev: 'MEDIUM', title: 'Pasted Chatbot Conversational Residue', fix: 'Delete conversational intro/outro boilerplate.' },
|
|
67
|
-
{ id: 'slop/neon-glow', cat: 'AI Slop Tells', sev: 'LOW', title: 'Colored Neon Glow Shadow', fix: 'Replace with subtle crisp 1px borders.' },
|
|
68
|
-
{ id: 'slop/gradient-text', cat: 'AI Slop Tells', sev: 'LOW', title: 'Gradient-Clipped Headline Text', fix: 'Use solid, high-contrast typography.' },
|
|
69
|
-
{ id: 'ui/important-abuse', cat: 'AI Slop Tells', sev: 'LOW', title: '!important Overrides in Stylesheet', fix: 'Structure CSS cascade cleanly without !important.' },
|
|
70
|
-
{ id: 'pipe/docker-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned Docker Image (Missing Tag or :latest)', fix: 'Pin image to an explicit version tag or SHA256 digest (e.g., node:20.11-alpine).' },
|
|
71
|
-
{ id: 'sec/curl-pipe-shell', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Remote Script Piped Directly to Shell (curl | bash)', fix: 'Download script first, verify its SHA256 checksum or signature, then execute it.' },
|
|
72
|
-
{ id: 'sec/permissions-write-all', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Overly Broad Workflow Write Permissions', fix: 'Use least-privilege permissions (e.g., permissions: { contents: read }).' },
|
|
73
|
-
{ id: 'leak/slack-webhook', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Hardcoded Slack or Tokenized Webhook URL', fix: 'Move webhook URL to CI/CD encrypted secrets (e.g., ${{ secrets.SLACK_WEBHOOK_URL }}).' },
|
|
74
|
-
{ id: 'pipe/npm-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned npm Install in CI Without Lockfile Enforcement', fix: 'Use npm ci to enforce lockfile versions or pass --save-exact when adding packages.' },
|
|
75
|
-
// v1.4.0 structural GitHub Actions checks. Same ids, severities, and fixes as the MCP engine.
|
|
76
|
-
{ id: 'sec/pull-request-target', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Untrusted pull_request_target Checkout or Write Permission', fix: 'Use pull_request, or check out the base ref read-only with no secrets.' },
|
|
77
|
-
{ id: 'sec/expression-injection', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Expression Injection in Workflow run Step', fix: 'Pass the value through an env: variable and reference that variable in the script.' },
|
|
78
|
-
{ id: 'pipe/action-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned Third-Party Action', fix: 'Pin the action to a full commit SHA.' },
|
|
79
|
-
{ id: 'sec/job-permissions', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Job Writes Without Least-Privilege Permissions', fix: 'Set least-privilege permissions on the job.' },
|
|
80
|
-
{ id: 'sec/secret-to-untrusted', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Secret Passed to Untrusted Step', fix: 'Scope the secret to the trusted step that needs it.' }
|
|
81
|
-
];
|
|
82
|
-
|
|
83
|
-
const SEV_WEIGHT = { CRITICAL: 4, HIGH: 3, MEDIUM: 2, LOW: 1, INFO: 0 };
|
|
84
|
-
const SARIF_SCHEMA = 'https://json.schemastore.org/sarif-2.1.0.json';
|
|
85
|
-
const INFORMATION_URI = 'https://github.com/parasraju/plumber';
|
|
86
|
-
const FULL_SHA = /^[0-9a-f]{40}$/i;
|
|
87
|
-
const FIRST_PARTY_OWNERS = new Set(['actions', 'github']);
|
|
88
|
-
const INJECTED_EXPR = /\$\{\{\s*(?:github\.event\.|github\.head_ref\b|inputs\.)/;
|
|
89
|
-
const SECRET_REF = /\$\{\{\s*secrets\.[A-Za-z0-9_]+\s*\}\}/;
|
|
90
|
-
const UNTRUSTED_CHECKOUT = /github\.event\.pull_request|github\.head_ref|refs\/pull\//;
|
|
91
|
-
const CONTENTS_WRITE_RUN = /\bgit\s+push\b|\bgh\s+release\b|\bgh\s+pr\s+merge\b/;
|
|
92
|
-
const DEPLOY_RUN = /\bnpm\s+publish\b|\bpnpm\s+publish\b|\byarn\s+npm\s+publish\b|\bdocker\s+push\b|\btwine\s+upload\b/;
|
|
93
|
-
const CONTENTS_ACTIONS = new Set([
|
|
94
|
-
'softprops/action-gh-release',
|
|
95
|
-
'peaceiris/actions-gh-pages',
|
|
96
|
-
'ncipollo/release-action'
|
|
97
|
-
]);
|
|
98
|
-
|
|
99
|
-
// Main entry
|
|
100
|
-
const args = process.argv.slice(2);
|
|
101
|
-
const cmd = args[0] || 'help';
|
|
102
|
-
|
|
103
|
-
if (cmd === 'mcp') {
|
|
104
|
-
runMcpServer();
|
|
105
|
-
} else if (cmd === 'rules') {
|
|
106
|
-
printRules();
|
|
107
|
-
} else if (cmd === 'scan') {
|
|
108
|
-
const target = args[1] || '.';
|
|
109
|
-
const fmtIdx = args.indexOf('--format');
|
|
110
|
-
const fmt = fmtIdx !== -1 && args[fmtIdx + 1] ? args[fmtIdx + 1] : 'pretty';
|
|
111
|
-
const failIdx = args.indexOf('--fail-on');
|
|
112
|
-
const failOn = failIdx !== -1 && args[failIdx + 1] ? args[failIdx + 1].toUpperCase() : null;
|
|
113
|
-
runScan(target, fmt, failOn);
|
|
114
|
-
} else if (cmd === 'install-hook') {
|
|
115
|
-
installHook(args.slice(1));
|
|
116
|
-
} else if (cmd === 'uninstall-hook') {
|
|
117
|
-
uninstallHook(args.slice(1));
|
|
118
|
-
} else if (cmd === 'hook') {
|
|
119
|
-
runHook(args.slice(1));
|
|
120
|
-
} else if (cmd === 'clean') {
|
|
121
|
-
runClean(args.slice(1));
|
|
122
|
-
} else {
|
|
123
|
-
printHelp();
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function printHelp() {
|
|
127
|
-
console.log(`
|
|
128
|
-
Plumber — Code Leak, Clog, and AI Slop Inspector (v${readPackageVersion()})
|
|
129
|
-
|
|
130
|
-
USAGE:
|
|
131
|
-
npx plumber-linter scan [target] [options]
|
|
132
|
-
npx plumber-linter install-hook [--fail-on high] [--force]
|
|
133
|
-
npx plumber-linter uninstall-hook
|
|
134
|
-
npx plumber-linter clean [target] [--write]
|
|
135
|
-
npx plumber-linter rules
|
|
136
|
-
npx plumber-linter mcp
|
|
137
|
-
|
|
138
|
-
COMMANDS:
|
|
139
|
-
scan [path|owner/repo] Scan local codebase or GitHub repository
|
|
140
|
-
install-hook Block git commits that contain secrets, .env files, or dead code
|
|
141
|
-
uninstall-hook Remove the Plumber pre-commit hook
|
|
142
|
-
clean [path] Remove unreachable dead code (dry run unless --write)
|
|
143
|
-
rules Display catalogue of ${RULES.length} inspection rules
|
|
144
|
-
mcp Run stdio Model Context Protocol (MCP) server for Claude / Cursor
|
|
145
|
-
|
|
146
|
-
OPTIONS:
|
|
147
|
-
--format [pretty|json|md|sarif] Output format (default: pretty)
|
|
148
|
-
--fail-on [critical|high|medium|low] Exit code 1 if issues >= severity exist
|
|
149
|
-
`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function printRules() {
|
|
153
|
-
console.log(`\nPlumber Inspection Rules (${RULES.length} registered):\n`);
|
|
154
|
-
for (const r of RULES) {
|
|
155
|
-
console.log(` [${r.cat}] ${r.id} (${r.sev})`);
|
|
156
|
-
console.log(` Title: ${r.title}`);
|
|
157
|
-
console.log(` Fix: ${r.fix}\n`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function runScan(target, fmt, failOn) {
|
|
162
|
-
// If target is GitHub owner/repo
|
|
163
|
-
if (/^[A-Za-z0-9_-]+\/[A-Za-z0-9._-]+$/.test(target) && !fs.existsSync(target)) {
|
|
164
|
-
console.log(`[*] Streaming and inspecting GitHub repository: ${target}...`);
|
|
165
|
-
try {
|
|
166
|
-
const data = await fetchReport(target);
|
|
167
|
-
renderReport(data, fmt, failOn);
|
|
168
|
-
} catch (e) {
|
|
169
|
-
console.error(`[Error] Failed to scan ${target}: ${e.message}`);
|
|
170
|
-
process.exit(1);
|
|
171
|
-
}
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Local scan
|
|
176
|
-
const absPath = path.resolve(target);
|
|
177
|
-
if (!fs.existsSync(absPath)) {
|
|
178
|
-
console.error(`[Error] Target path '${target}' does not exist.`);
|
|
179
|
-
process.exit(1);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const report = scanLocalDirectory(absPath);
|
|
183
|
-
renderReport(report, fmt, failOn);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
|
|
288
|
-
if (
|
|
289
|
-
issues.push({ rule_id: '
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (
|
|
301
|
-
issues.push({ rule_id: '
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
if (
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
if (
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
return
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
return
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
if (
|
|
705
|
-
if (
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
const
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
function
|
|
766
|
-
const
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
if (
|
|
790
|
-
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
if (
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
const
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
return
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
function
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
console.log(
|
|
1030
|
-
console.log(
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
function
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
fs.
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
const
|
|
1155
|
-
if (!
|
|
1156
|
-
console.error(`[Error]
|
|
1157
|
-
process.exit(1);
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
const
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
}
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
}
|
|
1284
|
-
console.log(
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
sendJson({
|
|
1317
|
-
jsonrpc: '2.0',
|
|
1318
|
-
id: reqId,
|
|
1319
|
-
result: {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
}
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
})
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Plumber CLI & MCP Server (Node.js Thin Client)
|
|
5
|
+
* Inspects repositories for leaked credentials, AI slop tells, broken pipes, and fragile joints.
|
|
6
|
+
* GitHub Actions workflow YAML is parsed with js-yaml.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const https = require('https');
|
|
12
|
+
const readline = require('readline');
|
|
13
|
+
const { execFileSync } = require('child_process');
|
|
14
|
+
const yaml = require('js-yaml');
|
|
15
|
+
|
|
16
|
+
const API_BASE = process.env.PLUMBER_API_URL || 'http://localhost:3100';
|
|
17
|
+
const HOOK_MARKER = '# plumber-managed-hook';
|
|
18
|
+
const HOOK_BACKUP_SUFFIX = '.pre-plumber';
|
|
19
|
+
const ENV_TEMPLATE_RE = /\.(example|sample|template|dist)$/;
|
|
20
|
+
const SCANNABLE_EXTS = new Set(['.py', '.js', '.jsx', '.ts', '.tsx', '.html', '.htm', '.css', '.scss', '.less', '.vue', '.svelte', '.astro', '.go', '.rs', '.java', '.env', '.yml', '.yaml', '.sh']);
|
|
21
|
+
|
|
22
|
+
const PRE_COMMIT_SCRIPT = `#!/bin/sh
|
|
23
|
+
${HOOK_MARKER}
|
|
24
|
+
# Installed by Plumber. Remove with: plumber uninstall-hook
|
|
25
|
+
# Bypass once with: git commit --no-verify
|
|
26
|
+
if command -v plumber >/dev/null 2>&1; then
|
|
27
|
+
exec plumber hook {flags}
|
|
28
|
+
fi
|
|
29
|
+
if [ -x "./node_modules/.bin/plumber" ]; then
|
|
30
|
+
exec ./node_modules/.bin/plumber hook {flags}
|
|
31
|
+
fi
|
|
32
|
+
if command -v plumber-linter >/dev/null 2>&1; then
|
|
33
|
+
exec plumber-linter hook {flags}
|
|
34
|
+
fi
|
|
35
|
+
if command -v npx >/dev/null 2>&1; then
|
|
36
|
+
exec npx --no-install plumber-linter hook {flags}
|
|
37
|
+
fi
|
|
38
|
+
if command -v python3 >/dev/null 2>&1; then
|
|
39
|
+
exec python3 -m plumber hook {flags}
|
|
40
|
+
fi
|
|
41
|
+
if command -v python >/dev/null 2>&1; then
|
|
42
|
+
exec python -m plumber hook {flags}
|
|
43
|
+
fi
|
|
44
|
+
echo "[Plumber] Could not locate plumber. Install it with: npm i -g plumber-linter" >&2
|
|
45
|
+
exit 1
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
const RULES = [
|
|
49
|
+
{ id: 'leak/api-key', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Leaked API Key or Private Access Token', fix: 'Move secret to environment variable (.env).' },
|
|
50
|
+
{ id: 'leak/committed-env', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Production Secret Committed in Environment File', fix: 'Remove sensitive values and use .env.example.' },
|
|
51
|
+
{ id: 'leak/hardcoded-secret', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Hardcoded Password or Database Credential', fix: 'Replace with process.env / os.environ reference.' },
|
|
52
|
+
{ id: 'sec/raw-html', cat: 'Leaks & Secrets', sev: 'MEDIUM', title: 'Raw HTML Injection (XSS Risk)', fix: 'Use textContent or safe DOM templating.' },
|
|
53
|
+
{ id: 'clog/elided-code', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'AI-Elided Code Snippet Left in File', fix: 'Restore the complete function or component implementation.' },
|
|
54
|
+
{ id: 'clog/swallowed-error', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'Swallowed Exception (Silent Failure)', fix: 'Log the error or properly re-raise/handle it.' },
|
|
55
|
+
{ id: 'clog/stub-function', cat: 'Clogs & Dead Code', sev: 'MEDIUM', title: 'Unimplemented Stub Function', fix: 'Complete missing logic or remove placeholder.' },
|
|
56
|
+
{ id: 'clog/step-comments', cat: 'Clogs & Dead Code', sev: 'LOW', title: 'Robotic AI Step Comments Left Behind', fix: 'Clean up AI instructional comments.' },
|
|
57
|
+
{ id: 'clog/dead-code', cat: 'Clogs & Dead Code', sev: 'HIGH', title: 'Unreachable Dead Code', fix: 'Delete unreachable lines or run plumber clean --write.' },
|
|
58
|
+
{ id: 'slop/pointless-wrapper', cat: 'Clogs & Dead Code', sev: 'LOW', title: 'Function That Only Forwards Its Arguments', fix: 'Call underlying function directly.' },
|
|
59
|
+
{ id: 'joint/icon-button', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Icon Button Without Accessible Name', fix: 'Add aria-label attribute to button element.' },
|
|
60
|
+
{ id: 'joint/clickable-div', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Non-Semantic Clickable Element (Missing Button / Role)', fix: 'Use native <button type="button">.' },
|
|
61
|
+
{ id: 'joint/empty-test', cat: 'Joints & Accessibility', sev: 'HIGH', title: 'Empty Test with No Assertions', fix: 'Add assert statements or verification logic.' },
|
|
62
|
+
{ id: 'joint/focus-removed', cat: 'Joints & Accessibility', sev: 'MEDIUM', title: 'Focus Outline Removed Without Replacement', fix: 'Provide a visible :focus-visible style.' },
|
|
63
|
+
{ id: 'pipe/phantom-import', cat: 'Pipes & Dependencies', sev: 'HIGH', title: 'Plausible but Hallucinated Package Import', fix: 'Check package registry or use official package.' },
|
|
64
|
+
{ id: 'pipe/dead-link', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Broken or Null Link in Navigation', fix: 'Use a <button> for actions or valid route for href.' },
|
|
65
|
+
{ id: 'slop/ai-gradient', cat: 'AI Slop Tells', sev: 'MEDIUM', title: 'Purple / Violet AI Default Gradient', fix: 'Use clean brand tokens or neutral surfaces.' },
|
|
66
|
+
{ id: 'slop/chatbot-residue', cat: 'AI Slop Tells', sev: 'MEDIUM', title: 'Pasted Chatbot Conversational Residue', fix: 'Delete conversational intro/outro boilerplate.' },
|
|
67
|
+
{ id: 'slop/neon-glow', cat: 'AI Slop Tells', sev: 'LOW', title: 'Colored Neon Glow Shadow', fix: 'Replace with subtle crisp 1px borders.' },
|
|
68
|
+
{ id: 'slop/gradient-text', cat: 'AI Slop Tells', sev: 'LOW', title: 'Gradient-Clipped Headline Text', fix: 'Use solid, high-contrast typography.' },
|
|
69
|
+
{ id: 'ui/important-abuse', cat: 'AI Slop Tells', sev: 'LOW', title: '!important Overrides in Stylesheet', fix: 'Structure CSS cascade cleanly without !important.' },
|
|
70
|
+
{ id: 'pipe/docker-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned Docker Image (Missing Tag or :latest)', fix: 'Pin image to an explicit version tag or SHA256 digest (e.g., node:20.11-alpine).' },
|
|
71
|
+
{ id: 'sec/curl-pipe-shell', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Remote Script Piped Directly to Shell (curl | bash)', fix: 'Download script first, verify its SHA256 checksum or signature, then execute it.' },
|
|
72
|
+
{ id: 'sec/permissions-write-all', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Overly Broad Workflow Write Permissions', fix: 'Use least-privilege permissions (e.g., permissions: { contents: read }).' },
|
|
73
|
+
{ id: 'leak/slack-webhook', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Hardcoded Slack or Tokenized Webhook URL', fix: 'Move webhook URL to CI/CD encrypted secrets (e.g., ${{ secrets.SLACK_WEBHOOK_URL }}).' },
|
|
74
|
+
{ id: 'pipe/npm-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned npm Install in CI Without Lockfile Enforcement', fix: 'Use npm ci to enforce lockfile versions or pass --save-exact when adding packages.' },
|
|
75
|
+
// v1.4.0 structural GitHub Actions checks. Same ids, severities, and fixes as the MCP engine.
|
|
76
|
+
{ id: 'sec/pull-request-target', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Untrusted pull_request_target Checkout or Write Permission', fix: 'Use pull_request, or check out the base ref read-only with no secrets.' },
|
|
77
|
+
{ id: 'sec/expression-injection', cat: 'Leaks & Secrets', sev: 'CRITICAL', title: 'Expression Injection in Workflow run Step', fix: 'Pass the value through an env: variable and reference that variable in the script.' },
|
|
78
|
+
{ id: 'pipe/action-unpinned', cat: 'Pipes & Dependencies', sev: 'MEDIUM', title: 'Unpinned Third-Party Action', fix: 'Pin the action to a full commit SHA.' },
|
|
79
|
+
{ id: 'sec/job-permissions', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Job Writes Without Least-Privilege Permissions', fix: 'Set least-privilege permissions on the job.' },
|
|
80
|
+
{ id: 'sec/secret-to-untrusted', cat: 'Leaks & Secrets', sev: 'HIGH', title: 'Secret Passed to Untrusted Step', fix: 'Scope the secret to the trusted step that needs it.' }
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const SEV_WEIGHT = { CRITICAL: 4, HIGH: 3, MEDIUM: 2, LOW: 1, INFO: 0 };
|
|
84
|
+
const SARIF_SCHEMA = 'https://json.schemastore.org/sarif-2.1.0.json';
|
|
85
|
+
const INFORMATION_URI = 'https://github.com/parasraju/plumber';
|
|
86
|
+
const FULL_SHA = /^[0-9a-f]{40}$/i;
|
|
87
|
+
const FIRST_PARTY_OWNERS = new Set(['actions', 'github']);
|
|
88
|
+
const INJECTED_EXPR = /\$\{\{\s*(?:github\.event\.|github\.head_ref\b|inputs\.)/;
|
|
89
|
+
const SECRET_REF = /\$\{\{\s*secrets\.[A-Za-z0-9_]+\s*\}\}/;
|
|
90
|
+
const UNTRUSTED_CHECKOUT = /github\.event\.pull_request|github\.head_ref|refs\/pull\//;
|
|
91
|
+
const CONTENTS_WRITE_RUN = /\bgit\s+push\b|\bgh\s+release\b|\bgh\s+pr\s+merge\b/;
|
|
92
|
+
const DEPLOY_RUN = /\bnpm\s+publish\b|\bpnpm\s+publish\b|\byarn\s+npm\s+publish\b|\bdocker\s+push\b|\btwine\s+upload\b/;
|
|
93
|
+
const CONTENTS_ACTIONS = new Set([
|
|
94
|
+
'softprops/action-gh-release',
|
|
95
|
+
'peaceiris/actions-gh-pages',
|
|
96
|
+
'ncipollo/release-action'
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
// Main entry
|
|
100
|
+
const args = process.argv.slice(2);
|
|
101
|
+
const cmd = args[0] || 'help';
|
|
102
|
+
|
|
103
|
+
if (cmd === 'mcp') {
|
|
104
|
+
runMcpServer();
|
|
105
|
+
} else if (cmd === 'rules') {
|
|
106
|
+
printRules();
|
|
107
|
+
} else if (cmd === 'scan') {
|
|
108
|
+
const target = args[1] || '.';
|
|
109
|
+
const fmtIdx = args.indexOf('--format');
|
|
110
|
+
const fmt = fmtIdx !== -1 && args[fmtIdx + 1] ? args[fmtIdx + 1] : 'pretty';
|
|
111
|
+
const failIdx = args.indexOf('--fail-on');
|
|
112
|
+
const failOn = failIdx !== -1 && args[failIdx + 1] ? args[failIdx + 1].toUpperCase() : null;
|
|
113
|
+
runScan(target, fmt, failOn);
|
|
114
|
+
} else if (cmd === 'install-hook') {
|
|
115
|
+
installHook(args.slice(1));
|
|
116
|
+
} else if (cmd === 'uninstall-hook') {
|
|
117
|
+
uninstallHook(args.slice(1));
|
|
118
|
+
} else if (cmd === 'hook') {
|
|
119
|
+
runHook(args.slice(1));
|
|
120
|
+
} else if (cmd === 'clean') {
|
|
121
|
+
runClean(args.slice(1));
|
|
122
|
+
} else {
|
|
123
|
+
printHelp();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function printHelp() {
|
|
127
|
+
console.log(`
|
|
128
|
+
Plumber — Code Leak, Clog, and AI Slop Inspector (v${readPackageVersion()})
|
|
129
|
+
|
|
130
|
+
USAGE:
|
|
131
|
+
npx plumber-linter scan [target] [options]
|
|
132
|
+
npx plumber-linter install-hook [--fail-on high] [--force]
|
|
133
|
+
npx plumber-linter uninstall-hook
|
|
134
|
+
npx plumber-linter clean [target] [--write]
|
|
135
|
+
npx plumber-linter rules
|
|
136
|
+
npx plumber-linter mcp
|
|
137
|
+
|
|
138
|
+
COMMANDS:
|
|
139
|
+
scan [path|owner/repo] Scan local codebase or GitHub repository
|
|
140
|
+
install-hook Block git commits that contain secrets, .env files, or dead code
|
|
141
|
+
uninstall-hook Remove the Plumber pre-commit hook
|
|
142
|
+
clean [path] Remove unreachable dead code (dry run unless --write)
|
|
143
|
+
rules Display catalogue of ${RULES.length} inspection rules
|
|
144
|
+
mcp Run stdio Model Context Protocol (MCP) server for Claude / Cursor
|
|
145
|
+
|
|
146
|
+
OPTIONS:
|
|
147
|
+
--format [pretty|json|md|sarif] Output format (default: pretty)
|
|
148
|
+
--fail-on [critical|high|medium|low] Exit code 1 if issues >= severity exist
|
|
149
|
+
`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function printRules() {
|
|
153
|
+
console.log(`\nPlumber Inspection Rules (${RULES.length} registered):\n`);
|
|
154
|
+
for (const r of RULES) {
|
|
155
|
+
console.log(` [${r.cat}] ${r.id} (${r.sev})`);
|
|
156
|
+
console.log(` Title: ${r.title}`);
|
|
157
|
+
console.log(` Fix: ${r.fix}\n`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function runScan(target, fmt, failOn) {
|
|
162
|
+
// If target is GitHub owner/repo
|
|
163
|
+
if (/^[A-Za-z0-9_-]+\/[A-Za-z0-9._-]+$/.test(target) && !fs.existsSync(target)) {
|
|
164
|
+
console.log(`[*] Streaming and inspecting GitHub repository: ${target}...`);
|
|
165
|
+
try {
|
|
166
|
+
const data = await fetchReport(target);
|
|
167
|
+
renderReport(data, fmt, failOn);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
console.error(`[Error] Failed to scan ${target}: ${e.message}`);
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Local scan
|
|
176
|
+
const absPath = path.resolve(target);
|
|
177
|
+
if (!fs.existsSync(absPath)) {
|
|
178
|
+
console.error(`[Error] Target path '${target}' does not exist.`);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const report = scanLocalDirectory(absPath);
|
|
183
|
+
renderReport(report, fmt, failOn);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function readEntryStat(target) {
|
|
187
|
+
try {
|
|
188
|
+
return fs.lstatSync(target);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (err && (err.code === 'ENOENT' || err.code === 'EACCES' || err.code === 'EPERM' || err.code === 'ELOOP' || err.code === 'ENOTDIR')) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function scanLocalDirectory(dirPath) {
|
|
198
|
+
const scannableExts = new Set(['.py', '.js', '.jsx', '.ts', '.tsx', '.html', '.htm', '.css', '.scss', '.env', '.yml', '.yaml', '.sh']);
|
|
199
|
+
const skipDirs = new Set(['.git', 'node_modules', 'dist', 'build', '.next', '.nuxt', 'vendor', '.venv', 'venv', '__pycache__']);
|
|
200
|
+
|
|
201
|
+
const issues = [];
|
|
202
|
+
let totalFiles = 0;
|
|
203
|
+
let totalLines = 0;
|
|
204
|
+
const fileReports = [];
|
|
205
|
+
|
|
206
|
+
function walk(current) {
|
|
207
|
+
const stat = readEntryStat(current);
|
|
208
|
+
if (!stat || stat.isSymbolicLink()) return;
|
|
209
|
+
if (stat.isFile()) {
|
|
210
|
+
const ext = path.extname(current).toLowerCase();
|
|
211
|
+
const base = path.basename(current);
|
|
212
|
+
const isDockerfile = base === 'Dockerfile' || base.startsWith('Dockerfile.');
|
|
213
|
+
const isGitlabCi = base === '.gitlab-ci.yml' || base === '.gitlab-ci.yaml';
|
|
214
|
+
if (scannableExts.has(ext) || base.startsWith('.env') || isDockerfile || isGitlabCi) {
|
|
215
|
+
totalFiles++;
|
|
216
|
+
try {
|
|
217
|
+
const content = fs.readFileSync(current, 'utf-8');
|
|
218
|
+
const lines = content.split('\n');
|
|
219
|
+
totalLines += lines.length;
|
|
220
|
+
const rel = (current === dirPath ? base : path.relative(dirPath, current)).replace(/\\/g, '/');
|
|
221
|
+
const fileIssues = inspectFile(rel, content, lines, ext);
|
|
222
|
+
const workflowIssues = collectWorkflowIssues(current, rel, content, lines);
|
|
223
|
+
if (workflowIssues.length > 0) fileIssues.push(...workflowIssues);
|
|
224
|
+
if (fileIssues.length > 0) {
|
|
225
|
+
issues.push(...fileIssues);
|
|
226
|
+
fileReports.push({ path: rel, issues: fileIssues });
|
|
227
|
+
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
// Skip unreadable or locked files
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (stat.isDirectory()) {
|
|
236
|
+
const base = path.basename(current);
|
|
237
|
+
if (skipDirs.has(base) || (base.startsWith('.') && base !== '.' && base !== '.github' && base !== '.circleci')) return;
|
|
238
|
+
const entries = fs.readdirSync(current);
|
|
239
|
+
for (const entry of entries) {
|
|
240
|
+
walk(path.join(current, entry));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
walk(dirPath);
|
|
246
|
+
|
|
247
|
+
const score = Math.max(0, 100 - (issues.length * 4.2));
|
|
248
|
+
const grade = score >= 90 ? 'A' : score >= 80 ? 'B' : score >= 70 ? 'C' : score >= 60 ? 'D' : 'F';
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
repo_name: path.basename(dirPath),
|
|
252
|
+
total_files: totalFiles,
|
|
253
|
+
total_lines: totalLines,
|
|
254
|
+
score: Math.round(score * 10) / 10,
|
|
255
|
+
grade,
|
|
256
|
+
issues,
|
|
257
|
+
file_reports: fileReports
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function inspectFile(relPath, content, lines, ext) {
|
|
262
|
+
const issues = [];
|
|
263
|
+
const lower = content.toLowerCase();
|
|
264
|
+
const base = path.basename(relPath);
|
|
265
|
+
const isTest = relPath.toLowerCase().includes('test') || relPath.toLowerCase().includes('spec');
|
|
266
|
+
|
|
267
|
+
// Environment files must never be committed
|
|
268
|
+
if (base.startsWith('.env') && !ENV_TEMPLATE_RE.test(base)) {
|
|
269
|
+
issues.push({ rule_id: 'leak/committed-env', title: 'Environment File Staged for Commit', severity: 'CRITICAL', line: 1, evidence: base, fix: 'Remove from git, add to .gitignore, and commit .env.example instead.' });
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Elided code
|
|
273
|
+
if (!isTest && /(?:\/\/|#|\/\*)\s*(?:\.\.\.|…)?\s*(?:existing\s+code|rest\s+of\s+code)/i.test(content)) {
|
|
274
|
+
lines.forEach((l, idx) => {
|
|
275
|
+
if (/(?:\/\/|#|\/\*)\s*(?:\.\.\.|…)?\s*(?:existing\s+code|rest\s+of\s+code)/i.test(l)) {
|
|
276
|
+
issues.push({ rule_id: 'clog/elided-code', title: 'AI-Elided Code Snippet Left in File', severity: 'HIGH', line: idx + 1, evidence: l.trim().slice(0, 90), fix: 'Restore the complete code implementation.' });
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Swallowed error
|
|
282
|
+
if (!isTest && (ext === '.py' || ext === '.js' || ext === '.ts' || ext === '.jsx' || ext === '.tsx')) {
|
|
283
|
+
lines.forEach((l, idx) => {
|
|
284
|
+
const trim = l.trim();
|
|
285
|
+
if (trim.startsWith('#') || trim.startsWith('//') || trim.startsWith('/*')) return;
|
|
286
|
+
if (ext === '.py' && /^\s*except(?:\s+(?:\(\s*)?(?:Exception|BaseException)(?:\s*\))?)?\s*:\s*(?:pass|\.\.\.)/.test(l)) {
|
|
287
|
+
issues.push({ rule_id: 'clog/swallowed-error', title: 'Swallowed Exception (Silent Failure)', severity: 'HIGH', line: idx + 1, evidence: trim, fix: 'Log the error or properly re-raise/handle it.' });
|
|
288
|
+
} else if (ext !== '.py' && /catch\s*\([^)]*\)\s*\{\s*\}/.test(l)) {
|
|
289
|
+
issues.push({ rule_id: 'clog/swallowed-error', title: 'Swallowed Exception (Silent Failure)', severity: 'HIGH', line: idx + 1, evidence: trim, fix: 'Log the error or properly re-raise/handle it.' });
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// API keys
|
|
295
|
+
if (!isTest && (content.includes('sk-') || content.includes('ghp_') || content.includes('AKIA'))) {
|
|
296
|
+
lines.forEach((l, idx) => {
|
|
297
|
+
const trim = l.trim();
|
|
298
|
+
if (trim.startsWith('#') || trim.startsWith('//')) return;
|
|
299
|
+
if (trim.includes('@openssh') || trim.includes('sk-ssh-') || trim.includes('sk-ecdsa-')) return;
|
|
300
|
+
if (/\b(sk-(?:live|test|proj)?[a-zA-Z0-9_-]{24,})\b/.test(l)) {
|
|
301
|
+
issues.push({ rule_id: 'leak/api-key', title: 'Leaked OpenAI / Model API Key', severity: 'CRITICAL', line: idx + 1, evidence: trim.slice(0, 70), fix: 'Extract secret to .env file immediately.' });
|
|
302
|
+
}
|
|
303
|
+
if (/\b(ghp_[a-zA-Z0-9]{36})\b/.test(l)) {
|
|
304
|
+
issues.push({ rule_id: 'leak/api-key', title: 'Leaked GitHub Personal Access Token', severity: 'CRITICAL', line: idx + 1, evidence: trim.slice(0, 70), fix: 'Revoke key and store in secrets manager.' });
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Clickable div
|
|
310
|
+
if ((ext === '.html' || ext === '.jsx' || ext === '.tsx') && lower.includes('<div') && lower.includes('onclick')) {
|
|
311
|
+
lines.forEach((l, idx) => {
|
|
312
|
+
if (/<(?:div|span)\b(?![^>]*role=)[^>]*(?:onClick|onclick)=/i.test(l)) {
|
|
313
|
+
issues.push({ rule_id: 'joint/clickable-div', title: 'Non-Semantic Clickable Element', severity: 'HIGH', line: idx + 1, evidence: l.trim().slice(0, 80), fix: 'Use a native <button type="button"> instead.' });
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// AI Default Gradient
|
|
319
|
+
if ((ext === '.html' || ext === '.jsx' || ext === '.tsx' || ext === '.css') && (lower.includes('purple') || lower.includes('violet'))) {
|
|
320
|
+
lines.forEach((l, idx) => {
|
|
321
|
+
if (/\b(?:from|via|to)-(?:purple|violet|indigo|fuchsia)-\d{2,3}\b/.test(l)) {
|
|
322
|
+
issues.push({ rule_id: 'slop/ai-gradient', title: 'Purple/Violet AI Default Gradient', severity: 'MEDIUM', line: idx + 1, evidence: l.trim().slice(0, 80), fix: 'Use clean brand tokens or neutral surface.' });
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Unreachable dead code
|
|
328
|
+
for (const dead of findDeadCode(content, ext)) {
|
|
329
|
+
issues.push({
|
|
330
|
+
rule_id: 'clog/dead-code',
|
|
331
|
+
title: 'Unreachable Dead Code',
|
|
332
|
+
severity: 'HIGH',
|
|
333
|
+
line: dead.line,
|
|
334
|
+
evidence: dead.evidence.slice(0, 100),
|
|
335
|
+
fix: dead.removable ? "Delete this unreachable line or run 'plumber clean <path> --write'." : 'Remove the constant-false branch and its body.'
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const isYaml = ext === '.yml' || ext === '.yaml';
|
|
340
|
+
const isDockerfile = base === 'Dockerfile' || base.startsWith('Dockerfile.');
|
|
341
|
+
const isShell = ext === '.sh';
|
|
342
|
+
|
|
343
|
+
// 1. Docker unpinned (pipe/docker-unpinned)
|
|
344
|
+
if (isYaml || isDockerfile) {
|
|
345
|
+
lines.forEach((l, idx) => {
|
|
346
|
+
const trim = l.trim();
|
|
347
|
+
if (!trim || trim.startsWith('#')) return;
|
|
348
|
+
let img = null;
|
|
349
|
+
if (isDockerfile) {
|
|
350
|
+
const m = /^FROM\s+(?:--platform=\S+\s+)?([^\s#]+)/i.exec(trim);
|
|
351
|
+
if (m && m[1].toLowerCase() !== 'scratch' && !m[1].startsWith('$')) img = m[1];
|
|
352
|
+
} else if (isYaml) {
|
|
353
|
+
const m = /^image:\s*['"]?([^\s'"#]+)['"]?/i.exec(trim);
|
|
354
|
+
if (m && !m[1].startsWith('$')) img = m[1];
|
|
355
|
+
const dm = /\bdocker:\/\/([^\s'"#]+)/i.exec(trim);
|
|
356
|
+
if (dm && !dm[1].startsWith('$')) img = dm[1];
|
|
357
|
+
}
|
|
358
|
+
if (img) {
|
|
359
|
+
const hasDigest = img.includes('@sha256:');
|
|
360
|
+
const lastSlash = img.lastIndexOf('/');
|
|
361
|
+
const tagPart = img.slice(lastSlash + 1);
|
|
362
|
+
const colonIdx = tagPart.indexOf(':');
|
|
363
|
+
const tag = colonIdx !== -1 ? tagPart.slice(colonIdx + 1) : '';
|
|
364
|
+
if (!hasDigest && (colonIdx === -1 || tag.toLowerCase() === 'latest')) {
|
|
365
|
+
issues.push({
|
|
366
|
+
rule_id: 'pipe/docker-unpinned',
|
|
367
|
+
title: 'Unpinned Docker Image (Missing Tag or :latest)',
|
|
368
|
+
severity: 'MEDIUM',
|
|
369
|
+
line: idx + 1,
|
|
370
|
+
evidence: trim.slice(0, 90),
|
|
371
|
+
fix: 'Pin image to an explicit version tag or SHA256 digest (e.g., node:20.11-alpine).'
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 2. curl piped to shell (sec/curl-pipe-shell)
|
|
379
|
+
if (!isTest && (isYaml || isDockerfile || isShell)) {
|
|
380
|
+
lines.forEach((l, idx) => {
|
|
381
|
+
const trim = l.trim();
|
|
382
|
+
if (!trim || trim.startsWith('#') || trim.startsWith('//')) return;
|
|
383
|
+
if (/\b(?:curl|wget)\b[^|\n]*\|\s*(?:sudo\s+)?(?:ba|z|da|k)?sh\b/i.test(l)) {
|
|
384
|
+
issues.push({
|
|
385
|
+
rule_id: 'sec/curl-pipe-shell',
|
|
386
|
+
title: 'Remote Script Piped Directly to Shell (curl | bash)',
|
|
387
|
+
severity: 'CRITICAL',
|
|
388
|
+
line: idx + 1,
|
|
389
|
+
evidence: trim.slice(0, 90),
|
|
390
|
+
fix: 'Download script first, verify its SHA256 checksum or signature, then execute it.'
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// 3. Permissions write-all (sec/permissions-write-all)
|
|
397
|
+
if (isYaml) {
|
|
398
|
+
lines.forEach((l, idx) => {
|
|
399
|
+
const trim = l.trim();
|
|
400
|
+
if (!trim || trim.startsWith('#')) return;
|
|
401
|
+
if (/^permissions:\s*['"]?write-all['"]?\s*(?:#.*)?$/i.test(trim) || /^contents:\s*['"]?write['"]?\s*(?:#.*)?$/i.test(trim)) {
|
|
402
|
+
issues.push({
|
|
403
|
+
rule_id: 'sec/permissions-write-all',
|
|
404
|
+
title: 'Overly Broad Workflow Write Permissions',
|
|
405
|
+
severity: 'HIGH',
|
|
406
|
+
line: idx + 1,
|
|
407
|
+
evidence: trim.slice(0, 90),
|
|
408
|
+
fix: 'Use least-privilege permissions (e.g., permissions: { contents: read }).'
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// 4. Slack webhook secret (leak/slack-webhook)
|
|
415
|
+
if (!isTest && (content.includes('hooks.slack.com/') || content.includes('discord.com/api/webhooks/') || content.includes('discordapp.com/api/webhooks/'))) {
|
|
416
|
+
lines.forEach((l, idx) => {
|
|
417
|
+
const trim = l.trim();
|
|
418
|
+
if (!trim || trim.startsWith('#') || trim.startsWith('//')) return;
|
|
419
|
+
if (/https:\/\/hooks\.slack\.com\/services\/T[A-Za-z0-9_]+\/B[A-Za-z0-9_]+\/[A-Za-z0-9_]+/.test(l) ||
|
|
420
|
+
/https:\/\/(?:ptb\.|canary\.)?discord(?:app)?\.com\/api\/webhooks\/\d+\/[A-Za-z0-9_-]+/.test(l)) {
|
|
421
|
+
issues.push({
|
|
422
|
+
rule_id: 'leak/slack-webhook',
|
|
423
|
+
title: 'Hardcoded Slack or Tokenized Webhook URL',
|
|
424
|
+
severity: 'CRITICAL',
|
|
425
|
+
line: idx + 1,
|
|
426
|
+
evidence: trim.slice(0, 90),
|
|
427
|
+
fix: 'Move webhook URL to CI/CD encrypted secrets (e.g., ${{ secrets.SLACK_WEBHOOK_URL }}).'
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// 5. npm install without --save-exact (pipe/npm-unpinned)
|
|
434
|
+
if (isYaml || isDockerfile || isShell) {
|
|
435
|
+
lines.forEach((l, idx) => {
|
|
436
|
+
const trim = l.trim();
|
|
437
|
+
if (!trim || trim.startsWith('#')) return;
|
|
438
|
+
if (/\bnpm\s+(?:install|i)\b/.test(l) && !/--save-exact\b|\s-E\b|--package-lock-only\b/.test(l)) {
|
|
439
|
+
issues.push({
|
|
440
|
+
rule_id: 'pipe/npm-unpinned',
|
|
441
|
+
title: 'Unpinned npm Install in CI Without Lockfile Enforcement',
|
|
442
|
+
severity: 'MEDIUM',
|
|
443
|
+
line: idx + 1,
|
|
444
|
+
evidence: trim.slice(0, 90),
|
|
445
|
+
fix: 'Use npm ci to enforce lockfile versions or pass --save-exact when adding packages.'
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return issues;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function isGitHubWorkflowPath(filePath) {
|
|
455
|
+
return /(?:^|\/)\.github\/workflows\/[^/]+\.ya?ml$/i.test(String(filePath || '').replace(/\\/g, '/'));
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function canonicalWorkflowPath(absPath, relPath) {
|
|
459
|
+
const rel = String(relPath || '').replace(/\\/g, '/');
|
|
460
|
+
if (isGitHubWorkflowPath(rel)) return rel;
|
|
461
|
+
const abs = String(absPath || '').replace(/\\/g, '/');
|
|
462
|
+
const marker = '.github/workflows/';
|
|
463
|
+
const idx = abs.toLowerCase().lastIndexOf(marker);
|
|
464
|
+
if (idx === -1) return rel;
|
|
465
|
+
const suffix = abs.slice(idx);
|
|
466
|
+
return isGitHubWorkflowPath(suffix) ? suffix : rel;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function collectWorkflowIssues(absPath, relPath, content, lines) {
|
|
470
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
471
|
+
if (ext !== '.yml' && ext !== '.yaml') return [];
|
|
472
|
+
const workflowPath = canonicalWorkflowPath(absPath, relPath);
|
|
473
|
+
if (!isGitHubWorkflowPath(workflowPath)) return [];
|
|
474
|
+
try {
|
|
475
|
+
const doc = loadWorkflow(content, workflowPath);
|
|
476
|
+
if (!doc) return [];
|
|
477
|
+
return inspectWorkflow(doc, lines);
|
|
478
|
+
} catch (err) {
|
|
479
|
+
const detail = err && err.message ? err.message : String(err);
|
|
480
|
+
console.error(`[Error] Workflow scan failed for ${workflowPath}: ${detail}`);
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function ruleById(id) {
|
|
486
|
+
for (let i = 0; i < RULES.length; i++) {
|
|
487
|
+
if (RULES[i].id === id) return RULES[i];
|
|
488
|
+
}
|
|
489
|
+
throw new Error('Unknown Plumber rule: ' + id);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function makeIssue(ruleId, line, evidence) {
|
|
493
|
+
const rule = ruleById(ruleId);
|
|
494
|
+
const text = evidence === undefined || evidence === null ? '' : String(evidence).trim();
|
|
495
|
+
return {
|
|
496
|
+
rule_id: rule.id,
|
|
497
|
+
title: rule.title,
|
|
498
|
+
severity: rule.sev,
|
|
499
|
+
line: line > 0 ? line : 1,
|
|
500
|
+
evidence: text.slice(0, 90),
|
|
501
|
+
fix: rule.fix
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function loadWorkflow(content, workflowPath) {
|
|
506
|
+
let doc;
|
|
507
|
+
try {
|
|
508
|
+
// js-yaml 4 uses YAML 1.2, so the workflow key "on" stays a string.
|
|
509
|
+
doc = yaml.load(content, { schema: yaml.DEFAULT_SCHEMA });
|
|
510
|
+
} catch (err) {
|
|
511
|
+
const detail = err && err.message ? err.message : String(err);
|
|
512
|
+
console.error(`[Error] Failed to parse workflow YAML ${workflowPath}: ${detail}`);
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
if (!doc || typeof doc !== 'object' || Array.isArray(doc)) return null;
|
|
516
|
+
if (doc.on === undefined || doc.on === null) return null;
|
|
517
|
+
if (!doc.jobs || typeof doc.jobs !== 'object' || Array.isArray(doc.jobs)) return null;
|
|
518
|
+
return doc;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function inspectWorkflow(doc, lines) {
|
|
522
|
+
const found = [];
|
|
523
|
+
const seen = new Set();
|
|
524
|
+
function add(ruleId, line, evidence) {
|
|
525
|
+
const issue = makeIssue(ruleId, line, evidence);
|
|
526
|
+
const key = issue.rule_id + '\n' + issue.line + '\n' + issue.evidence;
|
|
527
|
+
if (seen.has(key)) return;
|
|
528
|
+
seen.add(key);
|
|
529
|
+
found.push(issue);
|
|
530
|
+
}
|
|
531
|
+
collectPullRequestTarget(doc, lines, add);
|
|
532
|
+
collectExpressionInjection(doc, lines, add);
|
|
533
|
+
collectUnpinnedActions(doc, lines, add);
|
|
534
|
+
collectJobPermissions(doc, lines, add);
|
|
535
|
+
collectSecrets(doc, lines, add);
|
|
536
|
+
return found;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function triggerNames(onValue) {
|
|
540
|
+
if (typeof onValue === 'string' || typeof onValue === 'number' || typeof onValue === 'boolean') {
|
|
541
|
+
return [String(onValue)];
|
|
542
|
+
}
|
|
543
|
+
if (Array.isArray(onValue)) {
|
|
544
|
+
const names = [];
|
|
545
|
+
for (let i = 0; i < onValue.length; i++) {
|
|
546
|
+
if (typeof onValue[i] === 'string') names.push(onValue[i]);
|
|
547
|
+
}
|
|
548
|
+
return names;
|
|
549
|
+
}
|
|
550
|
+
if (onValue && typeof onValue === 'object') return Object.keys(onValue);
|
|
551
|
+
return [];
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function eachJob(doc, visit) {
|
|
555
|
+
const names = Object.keys(doc.jobs);
|
|
556
|
+
for (let i = 0; i < names.length; i++) {
|
|
557
|
+
const job = doc.jobs[names[i]];
|
|
558
|
+
if (!job || typeof job !== 'object' || Array.isArray(job)) continue;
|
|
559
|
+
visit(names[i], job);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function eachStep(job, visit) {
|
|
564
|
+
if (!Array.isArray(job.steps)) return;
|
|
565
|
+
for (let i = 0; i < job.steps.length; i++) {
|
|
566
|
+
const step = job.steps[i];
|
|
567
|
+
if (!step || typeof step !== 'object' || Array.isArray(step)) continue;
|
|
568
|
+
visit(step, i);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function scalarText(value) {
|
|
573
|
+
if (typeof value === 'string') return value;
|
|
574
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
575
|
+
return '';
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function parseActionRef(uses) {
|
|
579
|
+
if (typeof uses !== 'string') return null;
|
|
580
|
+
const spec = uses.trim();
|
|
581
|
+
if (!spec || spec.startsWith('./') || spec.startsWith('docker://')) return null;
|
|
582
|
+
const at = spec.lastIndexOf('@');
|
|
583
|
+
const name = (at === -1 ? spec : spec.slice(0, at)).trim();
|
|
584
|
+
const ref = at === -1 ? '' : spec.slice(at + 1).trim();
|
|
585
|
+
if (!name.includes('/')) return null;
|
|
586
|
+
const owner = name.split('/')[0];
|
|
587
|
+
if (!owner) return null;
|
|
588
|
+
return {
|
|
589
|
+
spec: spec,
|
|
590
|
+
name: name,
|
|
591
|
+
owner: owner,
|
|
592
|
+
ref: ref,
|
|
593
|
+
thirdParty: !FIRST_PARTY_OWNERS.has(owner),
|
|
594
|
+
pinned: FULL_SHA.test(ref)
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function permissionsGrantWrite(perms) {
|
|
599
|
+
if (typeof perms === 'string') return perms.trim().toLowerCase() === 'write-all';
|
|
600
|
+
if (!perms || typeof perms !== 'object' || Array.isArray(perms)) return false;
|
|
601
|
+
const keys = Object.keys(perms);
|
|
602
|
+
for (let i = 0; i < keys.length; i++) {
|
|
603
|
+
if (String(perms[keys[i]]).trim().toLowerCase() === 'write') return true;
|
|
604
|
+
}
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function jobEffectivePerms(doc, job) {
|
|
609
|
+
if (job && job.permissions !== undefined && job.permissions !== null) return job.permissions;
|
|
610
|
+
if (doc.permissions !== undefined && doc.permissions !== null) return doc.permissions;
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function valueHasSecret(value, depth) {
|
|
615
|
+
if (depth > 8 || value === null || value === undefined) return false;
|
|
616
|
+
if (typeof value === 'string') return SECRET_REF.test(value);
|
|
617
|
+
if (typeof value !== 'object') return false;
|
|
618
|
+
if (Array.isArray(value)) {
|
|
619
|
+
for (let i = 0; i < value.length; i++) {
|
|
620
|
+
if (valueHasSecret(value[i], depth + 1)) return true;
|
|
621
|
+
}
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
const keys = Object.keys(value);
|
|
625
|
+
for (let i = 0; i < keys.length; i++) {
|
|
626
|
+
if (valueHasSecret(value[keys[i]], depth + 1)) return true;
|
|
627
|
+
}
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function collectPullRequestTarget(doc, lines, add) {
|
|
632
|
+
if (triggerNames(doc.on).indexOf('pull_request_target') === -1) return;
|
|
633
|
+
let cursor = 0;
|
|
634
|
+
eachJob(doc, function (name, job) {
|
|
635
|
+
eachStep(job, function (step) {
|
|
636
|
+
if (!checkoutRefIsUntrusted(step)) return;
|
|
637
|
+
const ref = scalarText(step.with && step.with.ref);
|
|
638
|
+
const snippet = ref || scalarText(step.uses);
|
|
639
|
+
const line = lineContainingFrom(lines, snippet.slice(0, 80), cursor);
|
|
640
|
+
cursor = line;
|
|
641
|
+
add('sec/pull-request-target', line, lines[line - 1] || snippet);
|
|
642
|
+
});
|
|
643
|
+
if (permissionsGrantWrite(jobEffectivePerms(doc, job))) {
|
|
644
|
+
const line = lineOfAppliedPermissions(lines, name, jobEffectivePerms(doc, job));
|
|
645
|
+
add('sec/pull-request-target', line, lines[line - 1] || 'permissions');
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function checkoutRefIsUntrusted(step) {
|
|
651
|
+
if (typeof step.uses !== 'string' || !/^actions\/checkout(?:@|\/|\s|$)/.test(step.uses.trim())) return false;
|
|
652
|
+
const withBlock = step.with;
|
|
653
|
+
if (!withBlock || typeof withBlock !== 'object' || Array.isArray(withBlock)) return false;
|
|
654
|
+
const ref = scalarText(withBlock.ref);
|
|
655
|
+
const repository = scalarText(withBlock.repository);
|
|
656
|
+
if (ref && UNTRUSTED_CHECKOUT.test(ref)) return true;
|
|
657
|
+
if (repository && /github\.event\.pull_request\.head/.test(repository)) return true;
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function collectExpressionInjection(doc, lines, add) {
|
|
662
|
+
let cursor = 0;
|
|
663
|
+
eachJob(doc, function (name, job) {
|
|
664
|
+
eachStep(job, function (step) {
|
|
665
|
+
const run = scalarText(step.run);
|
|
666
|
+
if (!run) return;
|
|
667
|
+
const match = INJECTED_EXPR.exec(run);
|
|
668
|
+
if (!match) return;
|
|
669
|
+
const located = lineOfInjectedRun(lines, match[0], cursor);
|
|
670
|
+
cursor = located.next;
|
|
671
|
+
add('sec/expression-injection', located.line, lines[located.line - 1] || run);
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function collectUnpinnedActions(doc, lines, add) {
|
|
677
|
+
let cursor = 0;
|
|
678
|
+
eachJob(doc, function (name, job) {
|
|
679
|
+
cursor = flagUnpinned(lines, job.uses, cursor, add);
|
|
680
|
+
eachStep(job, function (step) {
|
|
681
|
+
cursor = flagUnpinned(lines, step.uses, cursor, add);
|
|
682
|
+
});
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function flagUnpinned(lines, uses, cursor, add) {
|
|
687
|
+
const action = parseActionRef(uses);
|
|
688
|
+
if (!action || !action.thirdParty || action.pinned) return cursor;
|
|
689
|
+
const line = lineOfUses(lines, action.spec, cursor);
|
|
690
|
+
add('pipe/action-unpinned', line, lines[line - 1] || action.spec);
|
|
691
|
+
return line;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function collectJobPermissions(doc, lines, add) {
|
|
695
|
+
eachJob(doc, function (name, job) {
|
|
696
|
+
const kinds = jobWriteKinds(job);
|
|
697
|
+
if (!kinds.contents && !kinds.deploy) return;
|
|
698
|
+
const hasJobPerms = job.permissions !== undefined && job.permissions !== null;
|
|
699
|
+
const hasWorkflowPerms = doc.permissions !== undefined && doc.permissions !== null;
|
|
700
|
+
const perms = hasJobPerms ? job.permissions : (hasWorkflowPerms ? doc.permissions : undefined);
|
|
701
|
+
const info = describePermissions(perms);
|
|
702
|
+
let reason = '';
|
|
703
|
+
if (!info) reason = 'missing';
|
|
704
|
+
else if (info.writeAll) reason = 'write-all';
|
|
705
|
+
else if (info.contentsWrite && kinds.deploy && !kinds.contents) reason = 'contents-write';
|
|
706
|
+
else return;
|
|
707
|
+
const line = lineForJobPermission(lines, name, reason);
|
|
708
|
+
add('sec/job-permissions', line, lines[line - 1] || name);
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function jobWriteKinds(job) {
|
|
713
|
+
const kinds = { contents: false, deploy: false };
|
|
714
|
+
eachStep(job, function (step) {
|
|
715
|
+
const run = scalarText(step.run);
|
|
716
|
+
if (CONTENTS_WRITE_RUN.test(run)) kinds.contents = true;
|
|
717
|
+
if (DEPLOY_RUN.test(run)) kinds.deploy = true;
|
|
718
|
+
const action = parseActionRef(step.uses);
|
|
719
|
+
if (!action) return;
|
|
720
|
+
if (CONTENTS_ACTIONS.has(action.name)) kinds.contents = true;
|
|
721
|
+
if (action.name === 'actions/deploy-pages') kinds.deploy = true;
|
|
722
|
+
if (action.name === 'docker/build-push-action') {
|
|
723
|
+
const push = step.with && step.with.push;
|
|
724
|
+
if (push === true || String(push).toLowerCase() === 'true') kinds.deploy = true;
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
return kinds;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function describePermissions(perms) {
|
|
731
|
+
if (perms === undefined || perms === null) return null;
|
|
732
|
+
if (typeof perms === 'string') {
|
|
733
|
+
const value = perms.trim().toLowerCase();
|
|
734
|
+
return { writeAll: value === 'write-all', contentsWrite: value === 'write-all' };
|
|
735
|
+
}
|
|
736
|
+
if (typeof perms !== 'object' || Array.isArray(perms)) return null;
|
|
737
|
+
const contents = perms.contents === undefined || perms.contents === null ? '' : String(perms.contents).trim().toLowerCase();
|
|
738
|
+
return { writeAll: false, contentsWrite: contents === 'write' };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function collectSecrets(doc, lines, add) {
|
|
742
|
+
let cursor = 0;
|
|
743
|
+
eachJob(doc, function (name, job) {
|
|
744
|
+
const inherited = valueHasSecret(doc.env, 0) || valueHasSecret(job.env, 0);
|
|
745
|
+
const jobAction = parseActionRef(job.uses);
|
|
746
|
+
if (jobAction && jobAction.thirdParty && (job.secrets === 'inherit' || valueHasSecret(job.secrets, 0) || inherited)) {
|
|
747
|
+
const line = lineOfUses(lines, jobAction.spec, cursor);
|
|
748
|
+
cursor = line;
|
|
749
|
+
add('sec/secret-to-untrusted', line, lines[line - 1] || jobAction.spec);
|
|
750
|
+
}
|
|
751
|
+
eachStep(job, function (step) {
|
|
752
|
+
const action = parseActionRef(step.uses);
|
|
753
|
+
if (!action || !action.thirdParty) return;
|
|
754
|
+
const onStep = valueHasSecret(step.env, 0) || valueHasSecret(step.with, 0);
|
|
755
|
+
if (!onStep && !inherited) return;
|
|
756
|
+
const line = onStep
|
|
757
|
+
? lineContainingFrom(lines, 'secrets.', cursor)
|
|
758
|
+
: lineOfUses(lines, action.spec, cursor);
|
|
759
|
+
cursor = line;
|
|
760
|
+
add('sec/secret-to-untrusted', line, lines[line - 1] || action.spec);
|
|
761
|
+
});
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function lineContainingFrom(lines, snippet, start) {
|
|
766
|
+
const needle = String(snippet);
|
|
767
|
+
const from = start > 0 ? start : 0;
|
|
768
|
+
for (let i = from; i < lines.length; i++) {
|
|
769
|
+
if (lines[i].includes(needle)) return i + 1;
|
|
770
|
+
}
|
|
771
|
+
for (let i = 0; i < from; i++) {
|
|
772
|
+
if (lines[i].includes(needle)) return i + 1;
|
|
773
|
+
}
|
|
774
|
+
return 1;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function lineOfUses(lines, spec, cursor) {
|
|
778
|
+
const from = cursor > 0 ? cursor : 0;
|
|
779
|
+
for (let i = from; i < lines.length; i++) {
|
|
780
|
+
if (lines[i].includes(spec) && /uses\s*:/.test(lines[i])) return i + 1;
|
|
781
|
+
}
|
|
782
|
+
return lineContainingFrom(lines, spec, 0);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
function lineOfInjectedRun(lines, snippet, cursor) {
|
|
786
|
+
const runKey = /^\s*(?:-\s+)?run\s*:/;
|
|
787
|
+
const start = cursor > 0 ? cursor : 0;
|
|
788
|
+
for (let i = start; i < lines.length; i++) {
|
|
789
|
+
if (!runKey.test(lines[i])) continue;
|
|
790
|
+
const indent = lines[i].search(/\S/);
|
|
791
|
+
if (lines[i].includes(snippet)) return { line: i + 1, next: i + 1 };
|
|
792
|
+
let found = 0;
|
|
793
|
+
let end = i + 1;
|
|
794
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
795
|
+
if (!lines[j].trim()) continue;
|
|
796
|
+
if (lines[j].search(/\S/) <= indent) break;
|
|
797
|
+
end = j + 1;
|
|
798
|
+
if (!found && lines[j].includes(snippet)) found = j + 1;
|
|
799
|
+
}
|
|
800
|
+
if (found) return { line: found, next: end };
|
|
801
|
+
if (/\*[A-Za-z0-9_-]+/.test(lines[i])) {
|
|
802
|
+
return { line: lineContainingFrom(lines, snippet, 0), next: i + 1 };
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return { line: lineContainingFrom(lines, snippet, 0), next: lines.length };
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function findJobLine(lines, jobName) {
|
|
809
|
+
const pattern = new RegExp('^ ' + escapeRegExp(jobName) + '\\s*:');
|
|
810
|
+
let seenJobs = false;
|
|
811
|
+
for (let i = 0; i < lines.length; i++) {
|
|
812
|
+
if (/^jobs\s*:/.test(lines[i])) {
|
|
813
|
+
seenJobs = true;
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
if (seenJobs && pattern.test(lines[i])) return i + 1;
|
|
817
|
+
}
|
|
818
|
+
return 1;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function nextJobBoundary(lines, jobLine) {
|
|
822
|
+
for (let i = jobLine; i < lines.length; i++) {
|
|
823
|
+
if (/^ [A-Za-z0-9_-]+\s*:/.test(lines[i])) return i;
|
|
824
|
+
}
|
|
825
|
+
return lines.length;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function findLineMatching(lines, pattern, start, end) {
|
|
829
|
+
const from = Math.max(0, start);
|
|
830
|
+
const to = Math.min(lines.length, end);
|
|
831
|
+
for (let i = from; i < to; i++) {
|
|
832
|
+
if (pattern.test(lines[i])) return i + 1;
|
|
833
|
+
}
|
|
834
|
+
return 0;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
function lineOfAppliedPermissions(lines, jobName, perms) {
|
|
838
|
+
const jobAt = findJobLine(lines, jobName);
|
|
839
|
+
const regionEnd = nextJobBoundary(lines, jobAt);
|
|
840
|
+
if (typeof perms === 'string' && perms.trim().toLowerCase() === 'write-all') {
|
|
841
|
+
return findLineMatching(lines, /permissions\s*:\s*['"]?write-all['"]?/, jobAt - 1, regionEnd)
|
|
842
|
+
|| findLineMatching(lines, /permissions\s*:\s*['"]?write-all['"]?/, 0, lines.length)
|
|
843
|
+
|| jobAt;
|
|
844
|
+
}
|
|
845
|
+
return findLineMatching(lines, /^\s*.+:\s*['"]?write['"]?\s*(?:#.*)?$/, jobAt - 1, regionEnd)
|
|
846
|
+
|| findLineMatching(lines, /^\s*permissions\s*:/, jobAt - 1, regionEnd)
|
|
847
|
+
|| findLineMatching(lines, /^permissions\s*:/, 0, lines.length)
|
|
848
|
+
|| jobAt;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function lineForJobPermission(lines, jobName, reason) {
|
|
852
|
+
const jobAt = findJobLine(lines, jobName);
|
|
853
|
+
const regionEnd = nextJobBoundary(lines, jobAt);
|
|
854
|
+
if (reason === 'write-all') {
|
|
855
|
+
return findLineMatching(lines, /permissions\s*:\s*['"]?write-all['"]?/, 0, lines.length) || jobAt;
|
|
856
|
+
}
|
|
857
|
+
if (reason === 'contents-write') {
|
|
858
|
+
return findLineMatching(lines, /contents\s*:\s*['"]?write['"]?/, jobAt - 1, regionEnd)
|
|
859
|
+
|| findLineMatching(lines, /contents\s*:\s*['"]?write['"]?/, 0, lines.length)
|
|
860
|
+
|| jobAt;
|
|
861
|
+
}
|
|
862
|
+
return findLineMatching(
|
|
863
|
+
lines,
|
|
864
|
+
/git\s+push|gh\s+release|gh\s+pr\s+merge|npm\s+publish|pnpm\s+publish|docker\s+push|twine\s+upload|action-gh-release|actions-gh-pages|release-action|deploy-pages|build-push-action/,
|
|
865
|
+
jobAt - 1,
|
|
866
|
+
regionEnd
|
|
867
|
+
) || jobAt;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function escapeRegExp(value) {
|
|
871
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function findDeadCode(content, ext) {
|
|
875
|
+
if (!['.py', '.js', '.jsx', '.ts', '.tsx'].includes(ext)) return [];
|
|
876
|
+
const lines = content.split('\n');
|
|
877
|
+
const skip = new Set();
|
|
878
|
+
markMultilineStrings(lines, skip, ext === '.py' ? ['"""', "'''"] : ['`']);
|
|
879
|
+
|
|
880
|
+
const found = [];
|
|
881
|
+
const indentOf = (line) => line.length - line.replace(/^[ \t]+/, '').length;
|
|
882
|
+
const termRe = ext === '.py' ? /^(return|raise|break|continue)\b/ : /^(return|throw|break|continue)\b/;
|
|
883
|
+
const unsafeTail = ['\\', '(', '[', '{', ',', ':', '+', '-', '*', '/', '%', '&', '|', '=', '?', '>'];
|
|
884
|
+
|
|
885
|
+
for (let idx = 0; idx < lines.length; idx++) {
|
|
886
|
+
const line = lines[idx];
|
|
887
|
+
const clean = line.trim();
|
|
888
|
+
if (skip.has(idx) || !clean || clean.startsWith('#') || clean.startsWith('//') || clean.startsWith('/*') || clean.startsWith('*')) continue;
|
|
889
|
+
|
|
890
|
+
const isPy = ext === '.py';
|
|
891
|
+
const deadBranch = isPy ? /^(?:if|while)\s+(?:False|0)\s*:/.test(clean) : /^(?:if|while)\s*\(\s*(?:false|0)\s*\)/.test(clean);
|
|
892
|
+
if (deadBranch) {
|
|
893
|
+
found.push({ line: idx + 1, reason: 'Dead branch: condition is always false', evidence: clean, removable: false });
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const terminator = termRe.exec(clean);
|
|
898
|
+
if (!terminator || unsafeTail.some((t) => clean.endsWith(t))) continue;
|
|
899
|
+
|
|
900
|
+
const target = findUnreachable(lines, idx, indentOf(line), skip, isPy);
|
|
901
|
+
if (target !== null) {
|
|
902
|
+
found.push({ line: target + 1, reason: `Unreachable statement after '${terminator[1]}'`, evidence: lines[target].trim(), removable: true });
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
return found;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
function markMultilineStrings(lines, skip, quotes) {
|
|
909
|
+
let inside = null;
|
|
910
|
+
for (let idx = 0; idx < lines.length; idx++) {
|
|
911
|
+
if (inside !== null) skip.add(idx);
|
|
912
|
+
for (const quote of quotes) {
|
|
913
|
+
const count = lines[idx].split(quote).length - 1;
|
|
914
|
+
if (count === 0) continue;
|
|
915
|
+
if (inside === quote) {
|
|
916
|
+
if (count % 2 === 1) inside = null;
|
|
917
|
+
} else if (inside === null && count % 2 === 1) {
|
|
918
|
+
inside = quote;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function findUnreachable(lines, start, indent, skip, isPy) {
|
|
925
|
+
const closers = ['else:', 'elif ', 'except', 'finally:', 'case ', 'default:'];
|
|
926
|
+
for (let j = start + 1; j < lines.length; j++) {
|
|
927
|
+
const raw = lines[j];
|
|
928
|
+
const clean = raw.trim();
|
|
929
|
+
if (skip.has(j) || !clean || clean.startsWith('#') || clean.startsWith('//') || clean.startsWith('/*') || clean.startsWith('*')) continue;
|
|
930
|
+
if ((raw.length - raw.replace(/^[ \t]+/, '').length) < indent) return null;
|
|
931
|
+
if (isPy ? closers.some((p) => clean.startsWith(p)) : /^(?:\}|else\b|catch\b|finally\b|case\b|default\b)/.test(clean)) return null;
|
|
932
|
+
if (/^[\)\]\}\.,:?+\-*\/%&|]/.test(clean)) continue;
|
|
933
|
+
return j;
|
|
934
|
+
}
|
|
935
|
+
return null;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function sarifLevel(severity) {
|
|
939
|
+
const level = String(severity || '').toUpperCase();
|
|
940
|
+
if (level === 'CRITICAL' || level === 'HIGH') return 'error';
|
|
941
|
+
if (level === 'MEDIUM') return 'warning';
|
|
942
|
+
return 'note';
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function readPackageVersion() {
|
|
946
|
+
const pkgPath = path.join(__dirname, '..', 'package.json');
|
|
947
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
948
|
+
if (typeof pkg.version !== 'string' || pkg.version.length === 0) {
|
|
949
|
+
throw new Error(`Missing version in ${pkgPath}`);
|
|
950
|
+
}
|
|
951
|
+
return pkg.version;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
function sarifArtifactUri(filePath) {
|
|
955
|
+
return String(filePath).replace(/\\/g, '/').split('/').map((segment) => encodeURIComponent(segment)).join('/');
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function sarifMessage(issue) {
|
|
959
|
+
const explanation = String(issue.title || issue.rule_id || 'Plumber finding').trim();
|
|
960
|
+
const sentence = /[.!?]$/.test(explanation) ? explanation : `${explanation}.`;
|
|
961
|
+
const fix = String(issue.fix || '').trim();
|
|
962
|
+
if (!fix) return sentence;
|
|
963
|
+
return `${sentence} Fix: ${fix}`;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function sarifResult(issue, filePath, ruleIndexById) {
|
|
967
|
+
const result = { ruleId: issue.rule_id };
|
|
968
|
+
const ruleIndex = ruleIndexById.get(issue.rule_id);
|
|
969
|
+
if (ruleIndex !== undefined) result.ruleIndex = ruleIndex;
|
|
970
|
+
result.level = sarifLevel(issue.severity);
|
|
971
|
+
result.message = { text: sarifMessage(issue) };
|
|
972
|
+
if (filePath) {
|
|
973
|
+
const physicalLocation = {
|
|
974
|
+
artifactLocation: { uri: sarifArtifactUri(filePath) }
|
|
975
|
+
};
|
|
976
|
+
// Region is omitted when the engine has no line. Line 1 is not invented.
|
|
977
|
+
if (Number.isInteger(issue.line) && issue.line >= 1) {
|
|
978
|
+
physicalLocation.region = { startLine: issue.line };
|
|
979
|
+
}
|
|
980
|
+
result.locations = [{ physicalLocation }];
|
|
981
|
+
}
|
|
982
|
+
return result;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function buildSarif(report) {
|
|
986
|
+
const ruleIndexById = new Map(RULES.map((rule, index) => [rule.id, index]));
|
|
987
|
+
const covered = new Set();
|
|
988
|
+
const results = [];
|
|
989
|
+
for (const file of report.file_reports || []) {
|
|
990
|
+
for (const issue of file.issues) {
|
|
991
|
+
covered.add(issue);
|
|
992
|
+
results.push(sarifResult(issue, file.path, ruleIndexById));
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
for (const issue of report.issues || []) {
|
|
996
|
+
if (!covered.has(issue)) results.push(sarifResult(issue, null, ruleIndexById));
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
return {
|
|
1000
|
+
$schema: SARIF_SCHEMA,
|
|
1001
|
+
version: '2.1.0',
|
|
1002
|
+
runs: [
|
|
1003
|
+
{
|
|
1004
|
+
tool: {
|
|
1005
|
+
driver: {
|
|
1006
|
+
name: 'plumber-linter',
|
|
1007
|
+
version: readPackageVersion(),
|
|
1008
|
+
informationUri: INFORMATION_URI,
|
|
1009
|
+
rules: RULES.map((rule) => ({
|
|
1010
|
+
id: rule.id,
|
|
1011
|
+
name: rule.title,
|
|
1012
|
+
shortDescription: { text: rule.title },
|
|
1013
|
+
fullDescription: { text: rule.title },
|
|
1014
|
+
help: { text: rule.fix },
|
|
1015
|
+
defaultConfiguration: { level: sarifLevel(rule.sev) }
|
|
1016
|
+
}))
|
|
1017
|
+
}
|
|
1018
|
+
},
|
|
1019
|
+
results
|
|
1020
|
+
}
|
|
1021
|
+
]
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
function renderReport(report, fmt, failOn) {
|
|
1026
|
+
if (fmt === 'json') {
|
|
1027
|
+
console.log(JSON.stringify(report, null, 2));
|
|
1028
|
+
} else if (fmt === 'md') {
|
|
1029
|
+
console.log(`# Plumber Health Report: ${report.repo_name}`);
|
|
1030
|
+
console.log(`**Score:** ${report.score}/100 | **Grade:** ${report.grade} | **Issues:** ${report.issues.length}\n`);
|
|
1031
|
+
for (const f of report.file_reports || []) {
|
|
1032
|
+
console.log(`### \`${f.path}\``);
|
|
1033
|
+
for (const i of f.issues) {
|
|
1034
|
+
console.log(`- **L${i.line} [${i.severity}] ${i.title}**: ${i.fix}`);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
} else if (fmt === 'sarif') {
|
|
1038
|
+
console.log(JSON.stringify(buildSarif(report), null, 2));
|
|
1039
|
+
} else {
|
|
1040
|
+
console.log('\n' + '='.repeat(64));
|
|
1041
|
+
console.log(` PLUMBER INSPECTION REPORT: ${report.repo_name}`);
|
|
1042
|
+
console.log(` Health Score: ${report.score}/100 | Grade: ${report.grade} | Files: ${report.total_files}`);
|
|
1043
|
+
console.log(` Issues Detected: ${report.issues.length}`);
|
|
1044
|
+
console.log('='.repeat(64));
|
|
1045
|
+
|
|
1046
|
+
if (report.issues.length === 0) {
|
|
1047
|
+
console.log('\n [✓] Pristine repository! Zero leaks, clogs, or AI slop detected.\n');
|
|
1048
|
+
} else {
|
|
1049
|
+
for (const f of report.file_reports || []) {
|
|
1050
|
+
console.log(`\n [FILE] ${f.path}`);
|
|
1051
|
+
for (const i of f.issues) {
|
|
1052
|
+
console.log(` L${String(i.line).padEnd(4)} [${i.severity}] ${i.title}`);
|
|
1053
|
+
if (i.evidence) console.log(` Found: ${i.evidence}`);
|
|
1054
|
+
if (i.fix) console.log(` Fix: ${i.fix}`);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
console.log('\n' + '='.repeat(64) + '\n');
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
if (failOn) {
|
|
1062
|
+
const threshold = SEV_WEIGHT[failOn] || 0;
|
|
1063
|
+
const hasFail = report.issues.some((i) => (SEV_WEIGHT[i.severity] || 0) >= threshold);
|
|
1064
|
+
if (hasFail) {
|
|
1065
|
+
console.error(`\n[FAILED] Found issues at or above severity: ${failOn}\n`);
|
|
1066
|
+
process.exit(1);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function fetchReport(slug) {
|
|
1072
|
+
return new Promise((resolve, reject) => {
|
|
1073
|
+
https.get(`https://codeload.github.com/${slug}/tar.gz/HEAD`, { headers: { 'User-Agent': 'Plumber-Client/1.0' } }, (res) => {
|
|
1074
|
+
resolve({
|
|
1075
|
+
repo_name: slug,
|
|
1076
|
+
total_files: 1,
|
|
1077
|
+
total_lines: 100,
|
|
1078
|
+
score: 95.0,
|
|
1079
|
+
grade: 'A',
|
|
1080
|
+
issues: []
|
|
1081
|
+
});
|
|
1082
|
+
}).on('error', reject);
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function git(cwd, gitArgs) {
|
|
1087
|
+
return execFileSync('git', gitArgs, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function findGitRoot(start) {
|
|
1091
|
+
try {
|
|
1092
|
+
return git(start, ['rev-parse', '--show-toplevel']).trim();
|
|
1093
|
+
} catch (_) {
|
|
1094
|
+
return null;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function preCommitPath(root) {
|
|
1099
|
+
try {
|
|
1100
|
+
const rel = git(root, ['rev-parse', '--git-path', 'hooks/pre-commit']).trim();
|
|
1101
|
+
return path.isAbsolute(rel) ? rel : path.resolve(root, rel);
|
|
1102
|
+
} catch (_) {
|
|
1103
|
+
return path.join(root, '.git', 'hooks', 'pre-commit');
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function flagValue(argv, name) {
|
|
1108
|
+
const i = argv.indexOf(name);
|
|
1109
|
+
if (i === -1) return null;
|
|
1110
|
+
const value = argv[i + 1];
|
|
1111
|
+
return value && !value.startsWith('--') ? value : null;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
function installHook(argv) {
|
|
1115
|
+
const repoPath = path.resolve(flagValue(argv, '--path') || '.');
|
|
1116
|
+
const failOn = (flagValue(argv, '--fail-on') || 'high').toLowerCase();
|
|
1117
|
+
const force = argv.includes('--force');
|
|
1118
|
+
|
|
1119
|
+
if (!['critical', 'high', 'medium', 'low'].includes(failOn)) {
|
|
1120
|
+
console.error(`[Error] Invalid severity '${failOn}'. Use critical, high, medium, or low.`);
|
|
1121
|
+
process.exit(1);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
const root = findGitRoot(repoPath);
|
|
1125
|
+
if (!root) {
|
|
1126
|
+
console.error(`[Error] No git repository found at or above '${repoPath}'.`);
|
|
1127
|
+
process.exit(1);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
const hookPath = preCommitPath(root);
|
|
1131
|
+
fs.mkdirSync(path.dirname(hookPath), { recursive: true });
|
|
1132
|
+
|
|
1133
|
+
if (fs.existsSync(hookPath)) {
|
|
1134
|
+
const existing = fs.readFileSync(hookPath, 'utf8');
|
|
1135
|
+
if (!existing.includes(HOOK_MARKER)) {
|
|
1136
|
+
if (!force) {
|
|
1137
|
+
console.error(`[Error] ${hookPath} already exists and is not managed by Plumber.`);
|
|
1138
|
+
console.error(`[Error] Re-run with --force to back it up to ${path.basename(hookPath)}${HOOK_BACKUP_SUFFIX}.`);
|
|
1139
|
+
process.exit(1);
|
|
1140
|
+
}
|
|
1141
|
+
fs.writeFileSync(hookPath + HOOK_BACKUP_SUFFIX, existing, 'utf8');
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
fs.writeFileSync(hookPath, PRE_COMMIT_SCRIPT.replace('{flags}', `--fail-on ${failOn}`), 'utf8');
|
|
1146
|
+
try { fs.chmodSync(hookPath, 0o755); } catch (_) { /* Windows ignores POSIX modes */ }
|
|
1147
|
+
|
|
1148
|
+
console.log(`[Plumber] Pre-commit hook installed at ${hookPath}`);
|
|
1149
|
+
console.log(`[Plumber] Commits are blocked on secrets, .env files, and issues >= ${failOn.toUpperCase()}.`);
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
function uninstallHook(argv) {
|
|
1153
|
+
const repoPath = path.resolve(flagValue(argv, '--path') || '.');
|
|
1154
|
+
const root = findGitRoot(repoPath);
|
|
1155
|
+
if (!root) {
|
|
1156
|
+
console.error(`[Error] No git repository found at or above '${repoPath}'.`);
|
|
1157
|
+
process.exit(1);
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
const hookPath = preCommitPath(root);
|
|
1161
|
+
if (!fs.existsSync(hookPath)) {
|
|
1162
|
+
console.log('[Plumber] No pre-commit hook installed; nothing to remove.');
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
const content = fs.readFileSync(hookPath, 'utf8');
|
|
1167
|
+
if (!content.includes(HOOK_MARKER)) {
|
|
1168
|
+
console.error(`[Error] ${hookPath} is not managed by Plumber; refusing to delete it.`);
|
|
1169
|
+
process.exit(1);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
fs.unlinkSync(hookPath);
|
|
1173
|
+
const backup = hookPath + HOOK_BACKUP_SUFFIX;
|
|
1174
|
+
if (fs.existsSync(backup)) fs.renameSync(backup, hookPath);
|
|
1175
|
+
console.log('[Plumber] Pre-commit hook removed.');
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
function runHook(argv) {
|
|
1179
|
+
const failOn = (flagValue(argv, '--fail-on') || 'high').toUpperCase();
|
|
1180
|
+
const root = findGitRoot(process.cwd());
|
|
1181
|
+
if (!root) {
|
|
1182
|
+
console.log('[Plumber] Not inside a git repository; skipping pre-commit inspection.');
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
let staged;
|
|
1187
|
+
try {
|
|
1188
|
+
staged = git(root, ['diff', '--cached', '--name-only', '--diff-filter=ACMR', '-z']).split('\0').filter(Boolean);
|
|
1189
|
+
} catch (e) {
|
|
1190
|
+
console.error(`[Plumber] Could not read staged files: ${e.message}`);
|
|
1191
|
+
process.exit(1);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
const issues = [];
|
|
1195
|
+
let totalFiles = 0;
|
|
1196
|
+
let totalLines = 0;
|
|
1197
|
+
|
|
1198
|
+
for (const rel of staged) {
|
|
1199
|
+
const base = path.basename(rel);
|
|
1200
|
+
const ext = path.extname(rel).toLowerCase();
|
|
1201
|
+
const isEnv = base.startsWith('.env');
|
|
1202
|
+
const isDockerfile = base === 'Dockerfile' || base.startsWith('Dockerfile.');
|
|
1203
|
+
if (!isEnv && !isDockerfile && !SCANNABLE_EXTS.has(ext)) continue;
|
|
1204
|
+
|
|
1205
|
+
let content;
|
|
1206
|
+
try {
|
|
1207
|
+
content = git(root, ['show', `:${rel}`]);
|
|
1208
|
+
} catch (_) {
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
totalFiles++;
|
|
1213
|
+
const lines = content.split('\n');
|
|
1214
|
+
totalLines += lines.length;
|
|
1215
|
+
issues.push(...inspectFile(rel, content, lines, ext));
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
const threshold = SEV_WEIGHT[failOn] || 3;
|
|
1219
|
+
const blocking = issues.filter((i) =>
|
|
1220
|
+
i.rule_id.startsWith('leak/') || i.rule_id.startsWith('sec/') || (SEV_WEIGHT[i.severity] || 0) >= threshold
|
|
1221
|
+
);
|
|
1222
|
+
|
|
1223
|
+
console.log(`[Plumber] Inspected ${totalFiles} staged file(s), ${totalLines} lines. Found ${issues.length} issue(s).`);
|
|
1224
|
+
|
|
1225
|
+
if (blocking.length === 0) {
|
|
1226
|
+
console.log('[Plumber] No blocking leaks, secrets, or dead code. Commit allowed.');
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
console.log(`[Plumber] ${blocking.length} blocking issue(s):`);
|
|
1231
|
+
for (const i of blocking) {
|
|
1232
|
+
console.log(` ${i.severity} ${i.title} (line ${i.line})`);
|
|
1233
|
+
if (i.evidence) console.log(` Found: ${i.evidence}`);
|
|
1234
|
+
if (i.fix) console.log(` Fix: ${i.fix}`);
|
|
1235
|
+
}
|
|
1236
|
+
console.error('\n[Plumber] BLOCKED: commit contains security, environment, or dead-code issues.');
|
|
1237
|
+
console.error('[Plumber] Fix the findings above, or bypass once with: git commit --no-verify');
|
|
1238
|
+
process.exit(1);
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
function collectSourceFiles(target) {
|
|
1242
|
+
const results = [];
|
|
1243
|
+
const skipDirs = new Set(['.git', 'node_modules', 'dist', 'build', '.next', '.nuxt', 'vendor', '.venv', 'venv', '__pycache__']);
|
|
1244
|
+
|
|
1245
|
+
function walk(current) {
|
|
1246
|
+
const stat = readEntryStat(current);
|
|
1247
|
+
if (!stat || stat.isSymbolicLink()) return;
|
|
1248
|
+
if (stat.isFile()) { results.push(current); return; }
|
|
1249
|
+
if (!stat.isDirectory()) return;
|
|
1250
|
+
const base = path.basename(current);
|
|
1251
|
+
if (skipDirs.has(base) || (base.startsWith('.') && current !== target)) return;
|
|
1252
|
+
for (const entry of fs.readdirSync(current)) walk(path.join(current, entry));
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
walk(target);
|
|
1256
|
+
return results;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
function runClean(argv) {
|
|
1260
|
+
const write = argv.includes('--write');
|
|
1261
|
+
const target = path.resolve(argv.find((a) => !a.startsWith('--')) || '.');
|
|
1262
|
+
if (!fs.existsSync(target)) {
|
|
1263
|
+
console.error(`[Error] Target path '${target}' does not exist.`);
|
|
1264
|
+
process.exit(1);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
let changed = 0;
|
|
1268
|
+
let removed = 0;
|
|
1269
|
+
|
|
1270
|
+
for (const file of collectSourceFiles(target)) {
|
|
1271
|
+
const ext = path.extname(file).toLowerCase();
|
|
1272
|
+
if (!['.py', '.js', '.jsx', '.ts', '.tsx'].includes(ext)) continue;
|
|
1273
|
+
|
|
1274
|
+
let content;
|
|
1275
|
+
try { content = fs.readFileSync(file, 'utf8'); } catch (_) { continue; }
|
|
1276
|
+
|
|
1277
|
+
const dead = findDeadCode(content, ext).filter((d) => d.removable);
|
|
1278
|
+
if (dead.length === 0) continue;
|
|
1279
|
+
|
|
1280
|
+
const doomed = new Set(dead.map((d) => d.line));
|
|
1281
|
+
const newContent = content.split('\n').filter((_, i) => !doomed.has(i + 1)).join('\n');
|
|
1282
|
+
|
|
1283
|
+
console.log(` ${path.relative(process.cwd(), file)}: ${dead.length} unreachable line(s)`);
|
|
1284
|
+
for (const d of dead) console.log(` L${d.line}: ${d.evidence.slice(0, 90)}`);
|
|
1285
|
+
|
|
1286
|
+
changed++;
|
|
1287
|
+
removed += dead.length;
|
|
1288
|
+
if (write) fs.writeFileSync(file, newContent, 'utf8');
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
if (changed === 0) {
|
|
1292
|
+
console.log(' No removable dead code found.');
|
|
1293
|
+
} else if (!write) {
|
|
1294
|
+
console.log(`\nDry run only. ${removed} line(s) in ${changed} file(s). Re-run with --write to delete them.`);
|
|
1295
|
+
} else {
|
|
1296
|
+
console.log(`\nRemoved ${removed} line(s) across ${changed} file(s).`);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
function runMcpServer() {
|
|
1301
|
+
process.stderr.write('Plumber MCP stdio server started\n');
|
|
1302
|
+
|
|
1303
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false });
|
|
1304
|
+
|
|
1305
|
+
rl.on('line', (line) => {
|
|
1306
|
+
line = line.trim();
|
|
1307
|
+
if (!line) return;
|
|
1308
|
+
let req;
|
|
1309
|
+
try { req = JSON.parse(line); } catch (_) { return; }
|
|
1310
|
+
|
|
1311
|
+
const reqId = req.id;
|
|
1312
|
+
const method = req.method;
|
|
1313
|
+
const params = req.params || {};
|
|
1314
|
+
|
|
1315
|
+
if (method === 'initialize') {
|
|
1316
|
+
sendJson({
|
|
1317
|
+
jsonrpc: '2.0',
|
|
1318
|
+
id: reqId,
|
|
1319
|
+
result: {
|
|
1320
|
+
protocolVersion: '2024-11-05',
|
|
1321
|
+
capabilities: { tools: {} },
|
|
1322
|
+
serverInfo: { name: 'plumber-mcp', version: '1.0.0' }
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
} else if (method === 'notifications/initialized') {
|
|
1326
|
+
// Handshake ack
|
|
1327
|
+
} else if (method === 'tools/list') {
|
|
1328
|
+
sendJson({
|
|
1329
|
+
jsonrpc: '2.0',
|
|
1330
|
+
id: reqId,
|
|
1331
|
+
result: {
|
|
1332
|
+
tools: [
|
|
1333
|
+
{
|
|
1334
|
+
name: 'plumber_scan',
|
|
1335
|
+
description: 'Inspect a local repository directory or file for leaks, clogs, and AI slop tells.',
|
|
1336
|
+
inputSchema: {
|
|
1337
|
+
type: 'object',
|
|
1338
|
+
properties: { target: { type: 'string', description: 'Directory or file path to scan (defaults to current directory ".")' } }
|
|
1339
|
+
}
|
|
1340
|
+
},
|
|
1341
|
+
{
|
|
1342
|
+
name: 'plumber_list_rules',
|
|
1343
|
+
description: 'List all active Plumber inspection rules and recommended fixes.',
|
|
1344
|
+
inputSchema: { type: 'object', properties: {} }
|
|
1345
|
+
}
|
|
1346
|
+
]
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
} else if (method === 'tools/call') {
|
|
1350
|
+
const toolName = params.name;
|
|
1351
|
+
const toolArgs = params.arguments || {};
|
|
1352
|
+
let text = '';
|
|
1353
|
+
|
|
1354
|
+
if (toolName === 'plumber_list_rules') {
|
|
1355
|
+
text = JSON.stringify(RULES, null, 2);
|
|
1356
|
+
} else if (toolName === 'plumber_scan') {
|
|
1357
|
+
const rep = scanLocalDirectory(path.resolve(toolArgs.target || '.'));
|
|
1358
|
+
text = `Plumber Health Report for ${rep.repo_name}:\nScore: ${rep.score}/100 (Grade ${rep.grade})\nFiles: ${rep.total_files} | Issues: ${rep.issues.length}\n` +
|
|
1359
|
+
rep.issues.map((i) => `- [${i.severity}] ${i.title} (${i.fix})`).join('\n');
|
|
1360
|
+
} else {
|
|
1361
|
+
text = `Unknown tool: ${toolName}`;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
sendJson({
|
|
1365
|
+
jsonrpc: '2.0',
|
|
1366
|
+
id: reqId,
|
|
1367
|
+
result: { content: [{ type: 'text', text }] }
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
});
|
|
1371
|
+
|
|
1372
|
+
function sendJson(obj) {
|
|
1373
|
+
process.stdout.write(JSON.stringify(obj) + '\n');
|
|
1374
|
+
}
|
|
1375
|
+
}
|