@yagyeshvyas/vibeguard 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +600 -0
- package/bin/cli.js +1640 -0
- package/ci/vibeguard.yml +41 -0
- package/ci-templates/Jenkinsfile +26 -0
- package/ci-templates/azure-pipelines.yml +29 -0
- package/ci-templates/circleci.yml +24 -0
- package/ci-templates/gitlab.yml +19 -0
- package/hooks/pre-commit +31 -0
- package/package.json +94 -0
- package/src/ai-guard.js +205 -0
- package/src/ast.js +319 -0
- package/src/audit-trail.js +154 -0
- package/src/autofix.js +524 -0
- package/src/badge.js +42 -0
- package/src/behavior.js +205 -0
- package/src/compliance.js +151 -0
- package/src/crosstaint.js +324 -0
- package/src/cve-intel.js +136 -0
- package/src/dashboard.js +279 -0
- package/src/deps.js +159 -0
- package/src/doctor.js +111 -0
- package/src/engine.js +234 -0
- package/src/external.js +160 -0
- package/src/firewall.js +517 -0
- package/src/history.js +87 -0
- package/src/hook.js +151 -0
- package/src/install.js +248 -0
- package/src/interceptor.js +476 -0
- package/src/mcp-server.js +1804 -0
- package/src/net-audit.js +208 -0
- package/src/pii.js +212 -0
- package/src/plugin.js +80 -0
- package/src/policy-gen.js +288 -0
- package/src/pre-deploy.js +282 -0
- package/src/presets.js +113 -0
- package/src/privacy-audit.js +273 -0
- package/src/repl.js +150 -0
- package/src/report.js +312 -0
- package/src/rules-pack.js +2753 -0
- package/src/rules.js +1446 -0
- package/src/sandbox.js +139 -0
- package/src/scanner.js +651 -0
- package/src/secure-prompt.js +140 -0
- package/src/slopsquat.js +144 -0
- package/src/supply-firewall.js +243 -0
- package/src/suppress.js +89 -0
- package/src/taint.js +168 -0
- package/src/trace.js +220 -0
- package/src/urlscan.js +142 -0
- package/src/vault.js +145 -0
- package/src/verify.js +98 -0
- package/src/verifykeys.js +87 -0
package/src/autofix.js
ADDED
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Safe auto-fix engine.
|
|
5
|
+
*
|
|
6
|
+
* Principle: VibeGuard only auto-applies fixes that are MECHANICAL and reversible
|
|
7
|
+
* and cannot change program behavior in a surprising way. Everything else (secret
|
|
8
|
+
* removal, query rewrites, auth changes) stays in the manual fix prompt for a
|
|
9
|
+
* human/AI to apply under review — auto-rewriting security-sensitive logic is how
|
|
10
|
+
* you turn one bug into two.
|
|
11
|
+
*
|
|
12
|
+
* Every apply is:
|
|
13
|
+
* 1. shown as a dry-run diff first (never silent),
|
|
14
|
+
* 2. snapshotted before writing (one-command rollback),
|
|
15
|
+
* 3. reported after.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
const SNAP_DIR = '.vibeguard';
|
|
22
|
+
|
|
23
|
+
// Which rules have a safe, mechanical auto-fix. Keep this list conservative.
|
|
24
|
+
const AUTO_FIXABLE = new Set([
|
|
25
|
+
'project.env-not-ignored',
|
|
26
|
+
'secret.openai-key',
|
|
27
|
+
'secret.stripe-key',
|
|
28
|
+
'secret.github-token',
|
|
29
|
+
'secret.anthropic-key',
|
|
30
|
+
'secret.slack-token',
|
|
31
|
+
'secret.gitlab-token',
|
|
32
|
+
'secret.twilio-key',
|
|
33
|
+
'secret.sendgrid-key',
|
|
34
|
+
'secret.mailgun-key',
|
|
35
|
+
'secret.telegram-bot-token',
|
|
36
|
+
'secret.npm-token',
|
|
37
|
+
'secret.resend-key',
|
|
38
|
+
'secret.aws-access-key',
|
|
39
|
+
'secret.private-key',
|
|
40
|
+
'secret.generic-credential',
|
|
41
|
+
'secret.gcp-api-key',
|
|
42
|
+
'secret.azure-storage-key',
|
|
43
|
+
'secret.cloudflare-api-token',
|
|
44
|
+
'secret.datadog-api-key',
|
|
45
|
+
'secret.vercel-token',
|
|
46
|
+
'secret.heroku-api-key',
|
|
47
|
+
'secret.notion-token',
|
|
48
|
+
'injection.eval',
|
|
49
|
+
'error.empty-catch',
|
|
50
|
+
'config.weak-cookie',
|
|
51
|
+
'code.console-log-secret',
|
|
52
|
+
'auth.hardcoded-token',
|
|
53
|
+
'web.missing-hsts',
|
|
54
|
+
'web.missing-csp',
|
|
55
|
+
'crypto.tls-verification-disabled',
|
|
56
|
+
'net.tls-verification-disabled',
|
|
57
|
+
'code.cors-wildcard',
|
|
58
|
+
'cookie.insecure-flags',
|
|
59
|
+
'error.detail-to-client',
|
|
60
|
+
'code.insecure-http',
|
|
61
|
+
'csp.unsafe-inline',
|
|
62
|
+
'csp.unsafe-eval',
|
|
63
|
+
'ai.browser-api-key',
|
|
64
|
+
'py.debug-true',
|
|
65
|
+
'py.flask-debug',
|
|
66
|
+
'django.debug-true',
|
|
67
|
+
'flask.debug-true',
|
|
68
|
+
'supply.unpinned-docker',
|
|
69
|
+
'k8s.run-as-root',
|
|
70
|
+
'transport.mixed-content',
|
|
71
|
+
'header.no-referrer-policy',
|
|
72
|
+
'data.sensitive-in-localstorage',
|
|
73
|
+
'data.sensitive-in-sessionstorage',
|
|
74
|
+
'auth.password-in-query',
|
|
75
|
+
'auth.weak-jwt-secret',
|
|
76
|
+
'injection.log-injection',
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
function isAutoFixable(ruleId) {
|
|
80
|
+
return AUTO_FIXABLE.has(ruleId);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Build the set of proposed file changes for a scan result.
|
|
84
|
+
// Returns [{ file, absFile, ruleId, description, before, after }].
|
|
85
|
+
function computeAutoFixes(root, result) {
|
|
86
|
+
const changes = [];
|
|
87
|
+
const byFile = new Map();
|
|
88
|
+
|
|
89
|
+
for (const f of result.findings) {
|
|
90
|
+
if (!isAutoFixable(f.ruleId)) continue;
|
|
91
|
+
if (f.ruleId === 'project.env-not-ignored') {
|
|
92
|
+
const rel = '.gitignore';
|
|
93
|
+
const abs = path.join(root, rel);
|
|
94
|
+
let before = '';
|
|
95
|
+
try {
|
|
96
|
+
before = fs.readFileSync(abs, 'utf8');
|
|
97
|
+
} catch {
|
|
98
|
+
before = '';
|
|
99
|
+
}
|
|
100
|
+
const lines = before.split(/\r?\n/);
|
|
101
|
+
const has = (pat) => lines.some((l) => l.trim() === pat);
|
|
102
|
+
const additions = [];
|
|
103
|
+
if (!has('.env')) additions.push('.env');
|
|
104
|
+
if (!has('.env.*') && !has('.env*')) additions.push('.env.*');
|
|
105
|
+
if (additions.length === 0) continue;
|
|
106
|
+
|
|
107
|
+
const sep = before && !before.endsWith('\n') ? '\n' : '';
|
|
108
|
+
const after =
|
|
109
|
+
before + sep + '\n# Added by VibeGuard: keep secrets out of git\n' + additions.join('\n') + '\n';
|
|
110
|
+
|
|
111
|
+
byFile.set(rel, {
|
|
112
|
+
file: rel,
|
|
113
|
+
absFile: abs,
|
|
114
|
+
ruleId: f.ruleId,
|
|
115
|
+
description: `Add ${additions.join(', ')} to .gitignore`,
|
|
116
|
+
before,
|
|
117
|
+
after,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Secret redaction: replace with process.env reference
|
|
122
|
+
if (f.ruleId && f.ruleId.startsWith('secret.') && f.file && f.line) {
|
|
123
|
+
const rel = f.file;
|
|
124
|
+
const abs = path.join(root, rel);
|
|
125
|
+
let before;
|
|
126
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
127
|
+
const lines = before.split(/\r?\n/);
|
|
128
|
+
const idx = f.line - 1;
|
|
129
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
130
|
+
const orig = lines[idx];
|
|
131
|
+
// Replace hardcoded secret with process.env reference
|
|
132
|
+
let modified = orig;
|
|
133
|
+
const keyName = (f.ruleId.split('.')[1] || 'SECRET').toUpperCase().replace(/-/g, '_');
|
|
134
|
+
modified = modified.replace(/(['"`])(?:sk-|sk-ant-|ghp_|gho_|ghu_|ghs_|ghr_|github_pat_)?[A-Za-z0-9_\-]{16,}(['"`])/g, `'process.env.${keyName}'`);
|
|
135
|
+
if (modified === orig) continue;
|
|
136
|
+
lines[idx] = modified;
|
|
137
|
+
const after = lines.join('\n');
|
|
138
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Redact secret on line ${f.line} → process.env.${keyName}`, before, after });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Empty catch: add error logging
|
|
142
|
+
if (f.ruleId === 'error.empty-catch' && f.file && f.line) {
|
|
143
|
+
const rel = f.file;
|
|
144
|
+
const abs = path.join(root, rel);
|
|
145
|
+
let before;
|
|
146
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
147
|
+
const lines = before.split(/\r?\n/);
|
|
148
|
+
const idx = f.line - 1;
|
|
149
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
150
|
+
if (/\{\s*\}/.test(lines[idx])) {
|
|
151
|
+
lines[idx] = lines[idx].replace(/\{\s*\}/, '{ console.error(err); }');
|
|
152
|
+
} else if (idx + 1 < lines.length && /^\s*\}\s*$/.test(lines[idx + 1]) && !/console\.error|throw|return/.test(lines[idx])) {
|
|
153
|
+
lines[idx] = lines[idx] + '\n console.error(err);';
|
|
154
|
+
} else { continue; }
|
|
155
|
+
const after = lines.join('\n');
|
|
156
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Add error logging to empty catch on line ${f.line}`, before, after });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Console.log secret: remove or comment out
|
|
160
|
+
if (f.ruleId === 'code.console-log-secret' && f.file && f.line) {
|
|
161
|
+
const rel = f.file;
|
|
162
|
+
const abs = path.join(root, rel);
|
|
163
|
+
let before;
|
|
164
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
165
|
+
const lines = before.split(/\r?\n/);
|
|
166
|
+
const idx = f.line - 1;
|
|
167
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
168
|
+
lines[idx] = '// vibeguard: removed secret from log\n// ' + lines[idx];
|
|
169
|
+
const after = lines.join('\n');
|
|
170
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Comment out secret log on line ${f.line}`, before, after });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// TLS verification disabled: remove rejectUnauthorized: false
|
|
174
|
+
if ((f.ruleId === 'crypto.tls-verification-disabled' || f.ruleId === 'net.tls-verification-disabled') && f.file && f.line) {
|
|
175
|
+
const rel = f.file;
|
|
176
|
+
const abs = path.join(root, rel);
|
|
177
|
+
let before;
|
|
178
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
179
|
+
const lines = before.split(/\r?\n/);
|
|
180
|
+
const idx = f.line - 1;
|
|
181
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
182
|
+
let modified = lines[idx].replace(/rejectUnauthorized\s*:\s*false\s*,?\s*/g, '');
|
|
183
|
+
modified = modified.replace(/process\.env\.NODE_TLS_REJECT_UNAUTHORIZED\s*=\s*['"]0['"]\s*;?/g, '');
|
|
184
|
+
if (modified === lines[idx]) continue;
|
|
185
|
+
lines[idx] = modified;
|
|
186
|
+
const after = lines.join('\n');
|
|
187
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Remove TLS verification bypass on line ${f.line}`, before, after });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// CORS wildcard: replace origin: '*' with specific origin placeholder
|
|
191
|
+
if (f.ruleId === 'code.cors-wildcard' && f.file && f.line) {
|
|
192
|
+
const rel = f.file;
|
|
193
|
+
const abs = path.join(root, rel);
|
|
194
|
+
let before;
|
|
195
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
196
|
+
const lines = before.split(/\r?\n/);
|
|
197
|
+
const idx = f.line - 1;
|
|
198
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
199
|
+
let modified = lines[idx].replace(/origin\s*:\s*['"]\*['"]/g, "origin: process.env.CORS_ORIGIN || 'http://localhost:3000'");
|
|
200
|
+
if (modified === lines[idx]) continue;
|
|
201
|
+
lines[idx] = modified;
|
|
202
|
+
const after = lines.join('\n');
|
|
203
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Replace CORS wildcard with env var on line ${f.line}`, before, after });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Cookie insecure flags: add secure: true, httpOnly: true, sameSite: 'strict'
|
|
207
|
+
if (f.ruleId === 'cookie.insecure-flags' && f.file && f.line) {
|
|
208
|
+
const rel = f.file;
|
|
209
|
+
const abs = path.join(root, rel);
|
|
210
|
+
let before;
|
|
211
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
212
|
+
const lines = before.split(/\r?\n/);
|
|
213
|
+
const idx = f.line - 1;
|
|
214
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
215
|
+
let modified = lines[idx];
|
|
216
|
+
if (!/secure\s*:/.test(modified)) modified = modified.replace(/(\{)/, '$1 secure: true,');
|
|
217
|
+
if (!/httpOnly\s*:/.test(modified)) modified = modified.replace(/(\{)/, '$1 httpOnly: true,');
|
|
218
|
+
if (!/sameSite\s*:/.test(modified)) modified = modified.replace(/(\{)/, "$1 sameSite: 'strict',");
|
|
219
|
+
if (modified === lines[idx]) continue;
|
|
220
|
+
lines[idx] = modified;
|
|
221
|
+
const after = lines.join('\n');
|
|
222
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Add secure cookie flags on line ${f.line}`, before, after });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Error detail to client: replace with generic error
|
|
226
|
+
if (f.ruleId === 'error.detail-to-client' && f.file && f.line) {
|
|
227
|
+
const rel = f.file;
|
|
228
|
+
const abs = path.join(root, rel);
|
|
229
|
+
let before;
|
|
230
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
231
|
+
const lines = before.split(/\r?\n/);
|
|
232
|
+
const idx = f.line - 1;
|
|
233
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
234
|
+
let modified = lines[idx]
|
|
235
|
+
.replace(/res\.(?:json|send)\s*\(\s*\{\s*(?:error|err)\s*:\s*[^}]*\}\s*\)/gi, 'res.status(500).json({ error: "Internal server error" })')
|
|
236
|
+
.replace(/res\.(?:json|send)\s*\(\s*\{[^}]*(?:err\.stack|err\.message|error\.stack|error\.message)[^}]*\}\s*\)/gi, 'res.status(500).json({ error: "Internal server error" })');
|
|
237
|
+
if (modified === lines[idx]) continue;
|
|
238
|
+
lines[idx] = modified;
|
|
239
|
+
const after = lines.join('\n');
|
|
240
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Replace error details with generic message on line ${f.line}`, before, after });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Insecure HTTP: replace http:// with https://
|
|
244
|
+
if (f.ruleId === 'code.insecure-http' && f.file && f.line) {
|
|
245
|
+
const rel = f.file;
|
|
246
|
+
const abs = path.join(root, rel);
|
|
247
|
+
let before;
|
|
248
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
249
|
+
const lines = before.split(/\r?\n/);
|
|
250
|
+
const idx = f.line - 1;
|
|
251
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
252
|
+
let modified = lines[idx].replace(/http:\/\/(?!localhost|127\.0\.0\.1|0\.0\.0\.0)/g, 'https://');
|
|
253
|
+
if (modified === lines[idx]) continue;
|
|
254
|
+
lines[idx] = modified;
|
|
255
|
+
const after = lines.join('\n');
|
|
256
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Upgrade HTTP to HTTPS on line ${f.line}`, before, after });
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// CSP unsafe-inline/eval: add nonce-based CSP
|
|
260
|
+
if ((f.ruleId === 'csp.unsafe-inline' || f.ruleId === 'csp.unsafe-eval') && f.file && f.line) {
|
|
261
|
+
const rel = f.file;
|
|
262
|
+
const abs = path.join(root, rel);
|
|
263
|
+
let before;
|
|
264
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
265
|
+
const lines = before.split(/\r?\n/);
|
|
266
|
+
const idx = f.line - 1;
|
|
267
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
268
|
+
let modified = lines[idx]
|
|
269
|
+
.replace(/'unsafe-inline'/g, "'nonce-${nonce}'")
|
|
270
|
+
.replace(/'unsafe-eval'/g, "'strict-dynamic'");
|
|
271
|
+
if (modified === lines[idx]) continue;
|
|
272
|
+
lines[idx] = modified;
|
|
273
|
+
const after = lines.join('\n');
|
|
274
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Replace unsafe CSP directives with nonce-based on line ${f.line}`, before, after });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// AI browser API key: move to server-side env
|
|
278
|
+
if (f.ruleId === 'ai.browser-api-key' && f.file && f.line) {
|
|
279
|
+
const rel = f.file;
|
|
280
|
+
const abs = path.join(root, rel);
|
|
281
|
+
let before;
|
|
282
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
283
|
+
const lines = before.split(/\r?\n/);
|
|
284
|
+
const idx = f.line - 1;
|
|
285
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
286
|
+
let modified = lines[idx].replace(/NEXT_PUBLIC_(?:OPENAI|ANTHROPIC|AI)_API_KEY/g, 'API_KEY');
|
|
287
|
+
if (modified === lines[idx]) continue;
|
|
288
|
+
lines[idx] = modified;
|
|
289
|
+
const after = lines.join('\n');
|
|
290
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Remove NEXT_PUBLIC_ prefix on line ${f.line} — key should be server-side only`, before, after });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Python debug=True: set to False
|
|
294
|
+
if ((f.ruleId === 'py.debug-true' || f.ruleId === 'py.flask-debug' || f.ruleId === 'flask.debug-true' || f.ruleId === 'django.debug-true') && f.file && f.line) {
|
|
295
|
+
const rel = f.file;
|
|
296
|
+
const abs = path.join(root, rel);
|
|
297
|
+
let before;
|
|
298
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
299
|
+
const lines = before.split(/\r?\n/);
|
|
300
|
+
const idx = f.line - 1;
|
|
301
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
302
|
+
let modified = lines[idx].replace(/debug\s*=\s*True/i, 'debug = False # vibeguard: disabled in production');
|
|
303
|
+
if (modified === lines[idx]) continue;
|
|
304
|
+
lines[idx] = modified;
|
|
305
|
+
const after = lines.join('\n');
|
|
306
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Set debug=False on line ${f.line}`, before, after });
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Docker :latest: pin to specific version
|
|
310
|
+
if (f.ruleId === 'supply.unpinned-docker' && f.file && f.line) {
|
|
311
|
+
const rel = f.file;
|
|
312
|
+
const abs = path.join(root, rel);
|
|
313
|
+
let before;
|
|
314
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
315
|
+
const lines = before.split(/\r?\n/);
|
|
316
|
+
const idx = f.line - 1;
|
|
317
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
318
|
+
let modified = lines[idx].replace(/FROM\s+(\S+):latest\b/i, 'FROM $1:18-alpine # vibeguard: pin version');
|
|
319
|
+
if (modified === lines[idx]) continue;
|
|
320
|
+
lines[idx] = modified;
|
|
321
|
+
const after = lines.join('\n');
|
|
322
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Pin Docker image version on line ${f.line}`, before, after });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Mixed content: upgrade http:// to https:// in HTML
|
|
326
|
+
if (f.ruleId === 'transport.mixed-content' && f.file && f.line) {
|
|
327
|
+
const rel = f.file;
|
|
328
|
+
const abs = path.join(root, rel);
|
|
329
|
+
let before;
|
|
330
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
331
|
+
const lines = before.split(/\r?\n/);
|
|
332
|
+
const idx = f.line - 1;
|
|
333
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
334
|
+
let modified = lines[idx].replace(/src\s*=\s*['"]http:\/\/(?!localhost|127\.0\.0\.1)/gi, 'src="https://');
|
|
335
|
+
if (modified === lines[idx]) continue;
|
|
336
|
+
lines[idx] = modified;
|
|
337
|
+
const after = lines.join('\n');
|
|
338
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Upgrade mixed content to HTTPS on line ${f.line}`, before, after });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Sensitive data in localStorage: move to cookie
|
|
342
|
+
if ((f.ruleId === 'data.sensitive-in-localstorage' || f.ruleId === 'data.sensitive-in-sessionstorage') && f.file && f.line) {
|
|
343
|
+
const rel = f.file;
|
|
344
|
+
const abs = path.join(root, rel);
|
|
345
|
+
let before;
|
|
346
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
347
|
+
const lines = before.split(/\r?\n/);
|
|
348
|
+
const idx = f.line - 1;
|
|
349
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
350
|
+
let modified = lines[idx].replace(/(local|session)Storage\.setItem\s*\(\s*['"](?:token|password|secret|session|auth|jwt|api.?key|access.?token)['"]/i, "document.cookie = '$1=");
|
|
351
|
+
if (modified === lines[idx]) continue;
|
|
352
|
+
lines[idx] = '// vibeguard: move sensitive data to HTTP-only cookie\n' + modified;
|
|
353
|
+
const after = lines.join('\n');
|
|
354
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Move sensitive data from storage to cookie on line ${f.line}`, before, after });
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Password in query string: warn and suggest POST
|
|
358
|
+
if (f.ruleId === 'auth.password-in-query' && f.file && f.line) {
|
|
359
|
+
const rel = f.file;
|
|
360
|
+
const abs = path.join(root, rel);
|
|
361
|
+
let before;
|
|
362
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
363
|
+
const lines = before.split(/\r?\n/);
|
|
364
|
+
const idx = f.line - 1;
|
|
365
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
366
|
+
lines[idx] = '// vibeguard: never accept passwords in query strings — use POST body\n// ' + lines[idx];
|
|
367
|
+
const after = lines.join('\n');
|
|
368
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Comment out password-in-query on line ${f.line}`, before, after });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Weak JWT secret: replace with env var
|
|
372
|
+
if (f.ruleId === 'auth.weak-jwt-secret' && f.file && f.line) {
|
|
373
|
+
const rel = f.file;
|
|
374
|
+
const abs = path.join(root, rel);
|
|
375
|
+
let before;
|
|
376
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
377
|
+
const lines = before.split(/\r?\n/);
|
|
378
|
+
const idx = f.line - 1;
|
|
379
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
380
|
+
let modified = lines[idx].replace(/jwt\.sign\s*\(\s*([^,]+),\s*['"](?:secret|password|changeme|test|default|key|123456)['"]/gi, "jwt.sign($1, process.env.JWT_SECRET");
|
|
381
|
+
if (modified === lines[idx]) continue;
|
|
382
|
+
lines[idx] = modified;
|
|
383
|
+
const after = lines.join('\n');
|
|
384
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Replace weak JWT secret with env var on line ${f.line}`, before, after });
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Log injection: sanitize newlines from log input
|
|
388
|
+
if (f.ruleId === 'injection.log-injection' && f.file && f.line) {
|
|
389
|
+
const rel = f.file;
|
|
390
|
+
const abs = path.join(root, rel);
|
|
391
|
+
let before;
|
|
392
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
393
|
+
const lines = before.split(/\r?\n/);
|
|
394
|
+
const idx = f.line - 1;
|
|
395
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
396
|
+
let modified = lines[idx].replace(/(?:console\.log|logger\.\w+|log\.\w+)\s*\(\s*([^)]*)\)/, (m, arg) => {
|
|
397
|
+
return m.replace(arg, arg.replace(/(req\.headers|req\.url|req\.query|req\.body)/g, 'String($1).replace(/[\\r\\n]/g, "")'));
|
|
398
|
+
});
|
|
399
|
+
if (modified === lines[idx]) continue;
|
|
400
|
+
lines[idx] = modified;
|
|
401
|
+
const after = lines.join('\n');
|
|
402
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Sanitize log input on line ${f.line}`, before, after });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Header no-referrer-policy: add helmet referrerPolicy
|
|
406
|
+
if (f.ruleId === 'header.no-referrer-policy' && f.file && f.line) {
|
|
407
|
+
const rel = f.file;
|
|
408
|
+
const abs = path.join(root, rel);
|
|
409
|
+
let before;
|
|
410
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
411
|
+
const lines = before.split(/\r?\n/);
|
|
412
|
+
const idx = f.line - 1;
|
|
413
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
414
|
+
// Insert referrerPolicy after helmet() call
|
|
415
|
+
let modified = lines[idx];
|
|
416
|
+
if (/app\.use\s*\(\s*helmet\s*\(\s*\)\s*\)/.test(modified)) {
|
|
417
|
+
modified = modified + "\napp.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));";
|
|
418
|
+
}
|
|
419
|
+
if (modified === lines[idx]) continue;
|
|
420
|
+
lines[idx] = modified;
|
|
421
|
+
const after = lines.join('\n');
|
|
422
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Add referrerPolicy header on line ${f.line}`, before, after });
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// K8s run-as-root: add securityContext
|
|
426
|
+
if (f.ruleId === 'k8s.run-as-root' && f.file && f.line) {
|
|
427
|
+
const rel = f.file;
|
|
428
|
+
const abs = path.join(root, rel);
|
|
429
|
+
let before;
|
|
430
|
+
try { before = fs.readFileSync(abs, 'utf8'); } catch { continue; }
|
|
431
|
+
const lines = before.split(/\r?\n/);
|
|
432
|
+
const idx = f.line - 1;
|
|
433
|
+
if (idx < 0 || idx >= lines.length) continue;
|
|
434
|
+
// Add securityContext after containers: block
|
|
435
|
+
let insertIdx = idx + 1;
|
|
436
|
+
while (insertIdx < lines.length && !/^\s*-\s*(?:name|image):/.test(lines[insertIdx])) insertIdx++;
|
|
437
|
+
lines.splice(insertIdx, 0, ' securityContext:', ' runAsNonRoot: true');
|
|
438
|
+
const after = lines.join('\n');
|
|
439
|
+
if (!byFile.has(rel)) byFile.set(rel, { file: rel, absFile: abs, ruleId: f.ruleId, description: `Add runAsNonRoot securityContext near line ${f.line}`, before, after });
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
for (const c of byFile.values()) changes.push(c);
|
|
445
|
+
return changes;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// A tiny unified-ish diff (line level) for display. Not a full LCS — enough to
|
|
449
|
+
// show what changes without pulling a dependency.
|
|
450
|
+
function renderDiff(change) {
|
|
451
|
+
const beforeLines = change.before.split('\n');
|
|
452
|
+
const afterLines = change.after.split('\n');
|
|
453
|
+
const out = [];
|
|
454
|
+
out.push(`--- ${change.file}`);
|
|
455
|
+
out.push(`+++ ${change.file}`);
|
|
456
|
+
// Show trailing context: everything in `after` not identical-position in `before`.
|
|
457
|
+
const common = Math.min(beforeLines.length, afterLines.length);
|
|
458
|
+
let i = 0;
|
|
459
|
+
while (i < common && beforeLines[i] === afterLines[i]) i++;
|
|
460
|
+
// Unchanged head (last 2 lines for context).
|
|
461
|
+
for (let k = Math.max(0, i - 2); k < i; k++) out.push(` ${beforeLines[k]}`);
|
|
462
|
+
for (let k = i; k < beforeLines.length; k++) if (beforeLines[k]) out.push(`- ${beforeLines[k]}`);
|
|
463
|
+
for (let k = i; k < afterLines.length; k++) if (afterLines[k]) out.push(`+ ${afterLines[k]}`);
|
|
464
|
+
return out.join('\n');
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Snapshot the files a change set will touch, so a bad apply can be undone.
|
|
468
|
+
function snapshot(root, changes) {
|
|
469
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
470
|
+
const dir = path.join(root, SNAP_DIR, 'snapshots', stamp);
|
|
471
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
472
|
+
const manifest = { stamp, files: [] };
|
|
473
|
+
for (const c of changes) {
|
|
474
|
+
const dest = path.join(dir, c.file.split('/').join('__'));
|
|
475
|
+
fs.writeFileSync(dest, c.before);
|
|
476
|
+
manifest.files.push({ file: c.file, backup: path.relative(root, dest).split(path.sep).join('/') });
|
|
477
|
+
}
|
|
478
|
+
fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify(manifest, null, 2));
|
|
479
|
+
// Record "latest" pointer for a simple `rollback`.
|
|
480
|
+
fs.writeFileSync(
|
|
481
|
+
path.join(root, SNAP_DIR, 'latest-snapshot.json'),
|
|
482
|
+
JSON.stringify({ dir: path.relative(root, dir).split(path.sep).join('/'), manifest }, null, 2)
|
|
483
|
+
);
|
|
484
|
+
return dir;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function applyChanges(root, changes) {
|
|
488
|
+
for (const c of changes) {
|
|
489
|
+
fs.writeFileSync(c.absFile, c.after);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function rollback(root) {
|
|
494
|
+
const ptrPath = path.join(root, SNAP_DIR, 'latest-snapshot.json');
|
|
495
|
+
let ptr;
|
|
496
|
+
try {
|
|
497
|
+
ptr = JSON.parse(fs.readFileSync(ptrPath, 'utf8'));
|
|
498
|
+
} catch {
|
|
499
|
+
return { ok: false, reason: 'no snapshot found' };
|
|
500
|
+
}
|
|
501
|
+
const restored = [];
|
|
502
|
+
for (const f of ptr.manifest.files) {
|
|
503
|
+
const backupAbs = path.join(root, f.backup);
|
|
504
|
+
const targetAbs = path.join(root, f.file);
|
|
505
|
+
try {
|
|
506
|
+
const content = fs.readFileSync(backupAbs, 'utf8');
|
|
507
|
+
fs.writeFileSync(targetAbs, content);
|
|
508
|
+
restored.push(f.file);
|
|
509
|
+
} catch (err) {
|
|
510
|
+
return { ok: false, reason: `failed restoring ${f.file}: ${err.message}` };
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return { ok: true, restored, stamp: ptr.manifest.stamp };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
module.exports = {
|
|
517
|
+
computeAutoFixes,
|
|
518
|
+
renderDiff,
|
|
519
|
+
snapshot,
|
|
520
|
+
applyChanges,
|
|
521
|
+
rollback,
|
|
522
|
+
isAutoFixable,
|
|
523
|
+
SNAP_DIR,
|
|
524
|
+
};
|
package/src/badge.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Shareable badge — awarded ONLY on a genuinely clean scan (no critical AND no
|
|
5
|
+
* high severity findings, using real severities). The wording is deliberately
|
|
6
|
+
* modest: "passed VibeGuard checks as of <date>". It never claims the app is
|
|
7
|
+
* "safe" or "100% secure" — that would be a lie a scanner can't back up.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function isEligible(result) {
|
|
11
|
+
return result.counts.critical === 0 && result.counts.high === 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function passLabel(dateStr) {
|
|
15
|
+
return `VibeGuard: passed as of ${dateStr}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Minimal shields-style SVG (no dependency).
|
|
19
|
+
function svg(result, dateStr) {
|
|
20
|
+
const ok = isEligible(result);
|
|
21
|
+
const label = 'VibeGuard';
|
|
22
|
+
const status = ok ? `passed ${dateStr}` : `grade ${result.grade}`;
|
|
23
|
+
const color = ok ? '#3fb950' : '#d29922';
|
|
24
|
+
const lw = 8 + label.length * 6.2;
|
|
25
|
+
const sw = 8 + status.length * 6.2;
|
|
26
|
+
const w = lw + sw;
|
|
27
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${w.toFixed(0)}" height="20" role="img" aria-label="${label}: ${status}">
|
|
28
|
+
<rect width="${lw.toFixed(0)}" height="20" fill="#555"/>
|
|
29
|
+
<rect x="${lw.toFixed(0)}" width="${sw.toFixed(0)}" height="20" fill="${color}"/>
|
|
30
|
+
<g fill="#fff" font-family="Verdana,Geneva,sans-serif" font-size="11">
|
|
31
|
+
<text x="${(lw / 2).toFixed(0)}" y="14" text-anchor="middle">${label}</text>
|
|
32
|
+
<text x="${(lw + sw / 2).toFixed(0)}" y="14" text-anchor="middle">${status}</text>
|
|
33
|
+
</g>
|
|
34
|
+
</svg>`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function markdown(result, dateStr, svgPath) {
|
|
38
|
+
if (!isEligible(result)) return null;
|
|
39
|
+
return ``;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { isEligible, svg, markdown, passLabel };
|