@yagyeshvyas/vibeguard 1.3.0 → 1.11.1
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/.vibeguard-mcp.example.json +51 -0
- package/CHANGELOG.md +604 -216
- package/MCP-FIREWALL.md +296 -0
- package/README.md +355 -31
- package/bin/cli.js +355 -27
- package/bin/cmd-agent-monitor.js +45 -0
- package/bin/cmd-agent-risk.js +64 -0
- package/bin/cmd-gif.js +62 -0
- package/bin/cmd-git-scan.js +74 -0
- package/bin/cmd-llm-proxy.js +164 -0
- package/bin/cmd-mcp-firewall.js +191 -0
- package/bin/cmd-pentest.js +103 -0
- package/bin/cmd-platform.js +137 -0
- package/bin/cmd-pr-scan.js +135 -0
- package/bin/cmd-sbom-diff.js +38 -0
- package/bin/cmd-skill-scan.js +35 -0
- package/docs/AISVS.md +25 -0
- package/docs/ARCHITECTURE.md +81 -0
- package/docs/INSTALLATION.md +139 -0
- package/docs/WINDOWS.md +74 -0
- package/docs/security-policy.strict.example.json +134 -0
- package/docs/security-policy.strict.example.pub.pem +3 -0
- package/package.json +45 -7
- package/src/.vibeguard/cache/file-hashes.json +60 -0
- package/src/.vibeguard/latest-snapshot.json +36 -0
- package/src/.vibeguard/mcp-pins.json +1 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/mcp-server.js +2051 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/manifest.json +37 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/mcp-server.js +2054 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/manifest.json +37 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/manifest.json +33 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/trace.js +220 -0
- package/src/.vibeguard-baseline.json +5226 -0
- package/src/action-guard.js +84 -6
- package/src/agent-monitor.js +262 -0
- package/src/agent-policy.js +642 -0
- package/src/agent-risk.js +271 -0
- package/src/agentic.js +1513 -0
- package/src/ast.js +165 -7
- package/src/audit-trail.js +212 -67
- package/src/auto.js +427 -427
- package/src/autofix.js +11 -4
- package/src/bundles.js +198 -0
- package/src/daemon.js +343 -343
- package/src/dashboard.js +278 -278
- package/src/engine.js +3 -0
- package/src/entropy.js +187 -0
- package/src/firewall.js +19 -3
- package/src/gif.js +134 -0
- package/src/gitscan.js +282 -0
- package/src/hook.js +151 -151
- package/src/injection-decode.js +136 -0
- package/src/install.js +298 -280
- package/src/integrity.js +16 -1
- package/src/integrity.json +43 -13
- package/src/interceptor.js +12 -9
- package/src/llm-proxy.js +986 -0
- package/src/mcp-audit.js +61 -5
- package/src/mcp-firewall.js +971 -0
- package/src/mcp-server.js +2500 -2047
- package/src/obfuscation.js +315 -0
- package/src/owasp-agentic.js +314 -0
- package/src/pentest.js +589 -0
- package/src/pii.js +4 -0
- package/src/platform/aisvs.js +38 -0
- package/src/platform/approval.js +187 -0
- package/src/platform/canonical.js +73 -0
- package/src/platform/contracts.js +158 -0
- package/src/platform/delegation.js +73 -0
- package/src/platform/http-gateway.js +177 -0
- package/src/platform/identity.js +253 -0
- package/src/platform/jwt.js +86 -0
- package/src/platform/mcp-http-bridge.js +199 -0
- package/src/platform/memory-guard.js +64 -0
- package/src/platform/os-sandbox.js +144 -0
- package/src/platform/policy.js +332 -0
- package/src/platform/sandbox-manager.js +92 -0
- package/src/platform/signed-audit.js +357 -0
- package/src/platform/ssrf.js +129 -0
- package/src/plugin.js +182 -168
- package/src/pre-deploy.js +170 -55
- package/src/privacy-audit.js +272 -272
- package/src/repl.js +150 -150
- package/src/report.js +7 -1
- package/src/routes.js +1210 -0
- package/src/rules-2026.js +1407 -0
- package/src/rules-pack.js +3068 -3019
- package/src/rules.js +376 -60
- package/src/sandbox.js +200 -10
- package/src/sbom.js +193 -164
- package/src/scanner.js +189 -33
- package/src/shell-guard.js +11 -0
- package/src/skill-scan.js +477 -0
- package/src/slopsquat.js +144 -144
- package/src/surface.js +408 -0
- package/src/taint-ast.js +204 -37
- package/src/taint.js +15 -3
- package/src/tokenlean.js +579 -0
- package/src/trace.js +220 -220
package/src/surface.js
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Attack-surface merge: entrypoints × findings × reachability.
|
|
5
|
+
*
|
|
6
|
+
* Every other scanner reports a flat list of "issues". The question a developer
|
|
7
|
+
* or an AI agent actually needs answered is narrower: which of these can be hit
|
|
8
|
+
* from outside, on which endpoint, with what access, and is the data flow real?
|
|
9
|
+
*
|
|
10
|
+
* This module joins three facts that normally live apart:
|
|
11
|
+
* 1. entrypoints (src/routes.js): route/action/RPC + guards + handler scope
|
|
12
|
+
* 2. findings: what the analyzers fired
|
|
13
|
+
* 3. import reachability: is the flagged file even loaded by that entrypoint
|
|
14
|
+
*
|
|
15
|
+
* Output: per-endpoint risk with direct (in-handler) vs inherited (imported)
|
|
16
|
+
* findings, plus an `exposed` list of endpoints that are reachable today.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
const SEV_WEIGHT = { critical: 40, high: 20, medium: 8, low: 2 };
|
|
22
|
+
const CONF_FACTOR = { high: 1, medium: 0.7, low: 0.4 };
|
|
23
|
+
|
|
24
|
+
// Which analyzer findings count as "the flow is real" (not a regex hint).
|
|
25
|
+
const DATAFLOW_PREFIXES = ['taint.', 'ast.', 'crosstaint', 'authz2026.', 'llm2026.', 'api2026.', 'obf.'];
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Import graph
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
const EXTS = ['', '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '/index.js', '/index.ts', '/index.tsx'];
|
|
32
|
+
|
|
33
|
+
function resolveSpec(fromRel, spec, known) {
|
|
34
|
+
if (!spec || !/^\.{1,2}\//.test(spec)) return null;
|
|
35
|
+
const dir = fromRel.includes('/') ? fromRel.slice(0, fromRel.lastIndexOf('/')) : '';
|
|
36
|
+
const base = path.posix.normalize(`${dir}/${spec}`).replace(/^\/+/, '');
|
|
37
|
+
for (const ext of EXTS) {
|
|
38
|
+
const cand = `${base}${ext}`;
|
|
39
|
+
if (known.has(cand)) return cand;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function buildImportGraph(files) {
|
|
45
|
+
const known = new Set(files.map((f) => f.rel));
|
|
46
|
+
const graph = new Map(); // rel -> [rel]
|
|
47
|
+
const specs = new Map(); // rel -> [raw specifiers]
|
|
48
|
+
for (const f of files) {
|
|
49
|
+
const deps = [];
|
|
50
|
+
const raw = [];
|
|
51
|
+
const patterns = [
|
|
52
|
+
/\bimport\s+[^;'"]*from\s*['"]([^'"]+)['"]/g,
|
|
53
|
+
/\bimport\s*['"]([^'"]+)['"]/g,
|
|
54
|
+
/\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
55
|
+
/\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
56
|
+
];
|
|
57
|
+
for (const re of patterns) {
|
|
58
|
+
let m;
|
|
59
|
+
while ((m = re.exec(f.content)) !== null) {
|
|
60
|
+
raw.push(m[1]);
|
|
61
|
+
const resolved = resolveSpec(f.rel, m[1], known);
|
|
62
|
+
if (resolved && !deps.includes(resolved)) deps.push(resolved);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
graph.set(f.rel, deps);
|
|
66
|
+
specs.set(f.rel, raw);
|
|
67
|
+
}
|
|
68
|
+
return { graph, specs };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// BFS: every file a given entrypoint file (transitively) loads.
|
|
72
|
+
function reachableFrom(startRel, graph) {
|
|
73
|
+
const seen = new Set([startRel]);
|
|
74
|
+
const queue = [startRel];
|
|
75
|
+
while (queue.length) {
|
|
76
|
+
const cur = queue.shift();
|
|
77
|
+
for (const dep of graph.get(cur) || []) {
|
|
78
|
+
if (seen.has(dep)) continue;
|
|
79
|
+
seen.add(dep);
|
|
80
|
+
queue.push(dep);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return seen;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
// Scoring
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
function findingReach(finding, entrypoints, reachSets) {
|
|
91
|
+
// Direct: the finding sits inside a handler body. For Express-style routes the
|
|
92
|
+
// handler may live in another file (resolved body), so check bodyFile first.
|
|
93
|
+
for (const e of entrypoints) {
|
|
94
|
+
if (e.bodyFile === finding.file && finding.line >= e.startLine && finding.line <= e.endLine) {
|
|
95
|
+
return { kind: 'direct', entrypoint: e };
|
|
96
|
+
}
|
|
97
|
+
if (e.file === finding.file && e.bodyFile !== e.file && finding.line >= e.line && finding.line <= e.line + 20) {
|
|
98
|
+
// Findings on the registration line itself (e.g. secrets in middleware args).
|
|
99
|
+
return { kind: 'direct', entrypoint: e };
|
|
100
|
+
}
|
|
101
|
+
if (e.file === finding.file && e.bodyFile === e.file && finding.line >= e.startLine && finding.line <= e.endLine) {
|
|
102
|
+
return { kind: 'direct', entrypoint: e };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Loaded-by: the finding's file is imported (transitively) by an entrypoint.
|
|
106
|
+
for (const e of entrypoints) {
|
|
107
|
+
const set = reachSets.get(e.file);
|
|
108
|
+
if (set && set.has(finding.file)) return { kind: 'imported', entrypoint: e };
|
|
109
|
+
if (e.bodyFile && e.bodyFile !== e.file) {
|
|
110
|
+
const bodySet = reachSets.get(e.bodyFile);
|
|
111
|
+
if (bodySet && bodySet.has(finding.file)) return { kind: 'imported', entrypoint: e };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { kind: 'dead', entrypoint: null };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function exposureMultiplier(entry, reachKind) {
|
|
118
|
+
if (reachKind === 'dead') return 0.25;
|
|
119
|
+
const open = entry && entry.auth === 'none';
|
|
120
|
+
if (reachKind === 'direct') {
|
|
121
|
+
if (open && entry.stateChanging) return 3;
|
|
122
|
+
if (open) return 2;
|
|
123
|
+
return 1.3;
|
|
124
|
+
}
|
|
125
|
+
return open ? 1.1 : 0.8;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function riskScore(finding, entry, reachKind) {
|
|
129
|
+
const base = SEV_WEIGHT[finding.severity] || 1;
|
|
130
|
+
const conf = CONF_FACTOR[finding.confidence] ?? 0.5;
|
|
131
|
+
const dataflow = DATAFLOW_PREFIXES.some((p) => String(finding.ruleId || '').startsWith(p));
|
|
132
|
+
const df = dataflow ? 1.25 : 1;
|
|
133
|
+
return Math.round(base * conf * exposureMultiplier(entry, reachKind) * df);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
// Build
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
function buildSurface({ files, findings }) {
|
|
141
|
+
const { extractEntrypoints } = require('./routes');
|
|
142
|
+
const { entrypoints, authCoverage, middleware } = extractEntrypoints(files);
|
|
143
|
+
const { graph } = buildImportGraph(files);
|
|
144
|
+
|
|
145
|
+
const reachSets = new Map();
|
|
146
|
+
for (const e of entrypoints) {
|
|
147
|
+
if (!reachSets.has(e.file)) reachSets.set(e.file, reachableFrom(e.file, graph));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Attach findings to endpoints.
|
|
151
|
+
const endpointById = new Map();
|
|
152
|
+
for (const e of entrypoints) {
|
|
153
|
+
endpointById.set(e.id, {
|
|
154
|
+
id: e.id,
|
|
155
|
+
kind: e.kind,
|
|
156
|
+
framework: e.framework,
|
|
157
|
+
method: e.method,
|
|
158
|
+
path: e.path,
|
|
159
|
+
file: e.file,
|
|
160
|
+
line: e.line,
|
|
161
|
+
bodyFile: e.bodyFile,
|
|
162
|
+
startLine: e.startLine,
|
|
163
|
+
endLine: e.endLine,
|
|
164
|
+
auth: e.auth,
|
|
165
|
+
guards: e.guards,
|
|
166
|
+
ownership: e.ownership,
|
|
167
|
+
stateChanging: e.stateChanging,
|
|
168
|
+
writes: e.writes,
|
|
169
|
+
readsObjectId: e.readsObjectId,
|
|
170
|
+
sensitive: e.sensitive,
|
|
171
|
+
rateLimited: e.rateLimited,
|
|
172
|
+
score: 0,
|
|
173
|
+
verdict: 'review',
|
|
174
|
+
risks: [],
|
|
175
|
+
});
|
|
176
|
+
if (e.ownership === 'missing') endpointById.get(e.id).score += 25; // IDOR base weight
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Reachability index per finding (memoized by file for non-direct lookups).
|
|
180
|
+
const fileEntryMap = new Map();
|
|
181
|
+
for (const e of entrypoints) {
|
|
182
|
+
if (!fileEntryMap.has(e.file)) fileEntryMap.set(e.file, []);
|
|
183
|
+
fileEntryMap.get(e.file).push(e);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
for (const f of findings) {
|
|
187
|
+
const reach = findingReach(f, entrypoints, reachSets);
|
|
188
|
+
f.surface = { reach: reach.kind };
|
|
189
|
+
if (reach.kind === 'dead') continue;
|
|
190
|
+
const ep = endpointById.get(reach.entrypoint.id);
|
|
191
|
+
if (!ep) continue;
|
|
192
|
+
const score = riskScore(f, reach.entrypoint, reach.kind);
|
|
193
|
+
ep.score += score;
|
|
194
|
+
ep.risks.push({
|
|
195
|
+
ruleId: f.ruleId,
|
|
196
|
+
severity: f.severity,
|
|
197
|
+
confidence: f.confidence,
|
|
198
|
+
title: f.title,
|
|
199
|
+
file: f.file,
|
|
200
|
+
line: f.line,
|
|
201
|
+
reach: reach.kind,
|
|
202
|
+
score,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Verdicts: open write/IDOR first, then anything with critical/high risk.
|
|
207
|
+
const endpoints = [...endpointById.values()].map((ep) => {
|
|
208
|
+
const directCritical = ep.risks.some((r) => r.severity === 'critical' && r.reach === 'direct');
|
|
209
|
+
const directHigh = ep.risks.some((r) => r.severity === 'high' && r.reach === 'direct');
|
|
210
|
+
const importedOnly = ep.risks.length > 0 && ep.risks.every((r) => r.reach === 'imported');
|
|
211
|
+
let verdict = 'clean';
|
|
212
|
+
if (ep.ownership === 'missing') verdict = 'critical'; // IDOR needs no finding to be a bug
|
|
213
|
+
else if (ep.risks.length === 0) verdict = ep.auth === 'none' && ep.stateChanging ? 'unguarded' : 'clean';
|
|
214
|
+
else if (directCritical || (ep.auth === 'none' && ep.stateChanging)) verdict = 'critical';
|
|
215
|
+
else if (directHigh) verdict = 'high';
|
|
216
|
+
else if (importedOnly && ep.auth === 'unknown') verdict = 'review';
|
|
217
|
+
else verdict = 'review';
|
|
218
|
+
return { ...ep, verdict, risks: ep.risks.sort((a, b) => b.score - a.score) };
|
|
219
|
+
});
|
|
220
|
+
endpoints.sort((a, b) => b.score - a.score || a.file.localeCompare(b.file));
|
|
221
|
+
|
|
222
|
+
const exposed = endpoints.filter(
|
|
223
|
+
(e) => e.verdict !== 'clean' && (e.auth === 'none' || e.ownership === 'missing' || e.risks.length > 0)
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// Category rollup helps the CLI and an agent decide where to start.
|
|
227
|
+
const byRule = {};
|
|
228
|
+
for (const ep of endpoints) {
|
|
229
|
+
for (const r of ep.risks) {
|
|
230
|
+
byRule[r.ruleId] = byRule[r.ruleId] || { count: 0, endpoints: 0, maxScore: 0, severity: r.severity };
|
|
231
|
+
byRule[r.ruleId].count++;
|
|
232
|
+
byRule[r.ruleId].maxScore = Math.max(byRule[r.ruleId].maxScore, r.score);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
for (const ruleId of Object.keys(byRule)) {
|
|
236
|
+
byRule[ruleId].endpoints = endpoints.filter((ep) => ep.risks.some((r) => r.ruleId === ruleId)).length;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const stats = {
|
|
240
|
+
entrypoints: endpoints.length,
|
|
241
|
+
byFramework: endpoints.reduce((acc, e) => ((acc[e.framework] = (acc[e.framework] || 0) + 1), acc), {}),
|
|
242
|
+
unguarded: endpoints.filter((e) => e.auth === 'none').length,
|
|
243
|
+
unknownAuth: endpoints.filter((e) => e.auth === 'unknown').length,
|
|
244
|
+
unguardedStateChanging: endpoints.filter((e) => e.auth === 'none' && e.stateChanging).length,
|
|
245
|
+
idorCandidates: endpoints.filter((e) => e.ownership === 'missing').length,
|
|
246
|
+
endpointsWithDirectRisk: endpoints.filter((e) => e.risks.some((r) => r.reach === 'direct')).length,
|
|
247
|
+
exposed: exposed.length,
|
|
248
|
+
deadFindings: findings.filter((f) => f.surface && f.surface.reach === 'dead').length,
|
|
249
|
+
reachableFindings: findings.filter((f) => f.surface && f.surface.reach !== 'dead').length,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const recommendations = buildRecommendations(stats, endpoints, middleware, authCoverage);
|
|
253
|
+
const chains = buildChains(endpoints);
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
entrypoints: endpoints,
|
|
257
|
+
exposed,
|
|
258
|
+
chains,
|
|
259
|
+
authCoverage,
|
|
260
|
+
middleware: middleware || null,
|
|
261
|
+
stats,
|
|
262
|
+
byRule,
|
|
263
|
+
recommendations,
|
|
264
|
+
diag: { importEdges: [...graph.values()].reduce((s, v) => s + v.length, 0) },
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function buildRecommendations(stats, endpoints, middleware, authCoverage) {
|
|
269
|
+
const out = [];
|
|
270
|
+
if (middleware && middleware.apiExcluded) {
|
|
271
|
+
out.push({
|
|
272
|
+
severity: 'high',
|
|
273
|
+
text: 'middleware.ts matcher excludes /api. Guard each route handler itself; middleware-only auth is bypassable (x-middleware-subrequest).',
|
|
274
|
+
file: middleware.file,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
const unguardedWrites = endpoints.filter((e) => e.auth === 'none' && e.stateChanging);
|
|
278
|
+
for (const e of unguardedWrites.slice(0, 6)) {
|
|
279
|
+
out.push({
|
|
280
|
+
severity: 'critical',
|
|
281
|
+
text: `${e.method} ${e.path} mutates state with no auth guard. Add the session check at the top of the handler.`,
|
|
282
|
+
file: e.file,
|
|
283
|
+
line: e.line,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
const idor = endpoints.filter((e) => e.ownership === 'missing');
|
|
287
|
+
for (const e of idor.slice(0, 6)) {
|
|
288
|
+
out.push({
|
|
289
|
+
severity: 'high',
|
|
290
|
+
text: `${e.method} ${e.path} fetches by request id without an owner/tenant predicate. Add the ownership constraint to the same query.`,
|
|
291
|
+
file: e.file,
|
|
292
|
+
line: e.line,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
if (authCoverage.unthrottledSensitive > 0) {
|
|
296
|
+
out.push({
|
|
297
|
+
severity: 'medium',
|
|
298
|
+
text: `${authCoverage.unthrottledSensitive} credential-shaped endpoint(s) have no rate limiting. Add a limiter and lockout.`,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
if (stats.deadFindings > 0 && stats.deadFindings >= stats.reachableFindings) {
|
|
302
|
+
out.push({
|
|
303
|
+
severity: 'low',
|
|
304
|
+
text: `${stats.deadFindings} finding(s) sit outside every entrypoint's import graph (dead/tooling code). Fix the reachable ones first; do not chase noise.`,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ---------------------------------------------------------------------------
|
|
311
|
+
// Deterministic exploit chains
|
|
312
|
+
// ---------------------------------------------------------------------------
|
|
313
|
+
|
|
314
|
+
// Impact classification per rule family. Static analysis cannot "prove" an
|
|
315
|
+
// exploit, but for these families the consequence is fixed by the sink type.
|
|
316
|
+
function impactOf(ruleId) {
|
|
317
|
+
const id = String(ruleId || '');
|
|
318
|
+
if (/^(?:taint\.(?:command|code)-injection|code\.eval|ast\.function-constructor|ast\.eval-dynamic|deser\.|deser2026\.|obf\.)/.test(id)) {
|
|
319
|
+
return { impact: 'remote code execution', cwe: 'CWE-94' };
|
|
320
|
+
}
|
|
321
|
+
if (/sql|orm|injection\.nosql|injection\.prototype|injection\.ssti|deser2026\.template/.test(id)) {
|
|
322
|
+
return { impact: 'database compromise / auth bypass', cwe: 'CWE-89' };
|
|
323
|
+
}
|
|
324
|
+
if (/authz2026\.|auth\.idor|authz\.idor|idor/i.test(id)) {
|
|
325
|
+
return { impact: 'cross-user data access (IDOR)', cwe: 'CWE-639' };
|
|
326
|
+
}
|
|
327
|
+
if (/ssrf/i.test(id)) return { impact: 'internal network / cloud metadata access', cwe: 'CWE-918' };
|
|
328
|
+
if (/path-traversal/i.test(id)) return { impact: 'arbitrary file read/write', cwe: 'CWE-22' };
|
|
329
|
+
if (/xss/i.test(id)) return { impact: 'session compromise via script injection', cwe: 'CWE-79' };
|
|
330
|
+
if (/llm2026\.|^ai\./.test(id)) return { impact: 'model-driven exploitation (prompt injection → action)', cwe: 'CWE-1427' };
|
|
331
|
+
if (/secret/i.test(id)) return { impact: 'credential exposure', cwe: 'CWE-798' };
|
|
332
|
+
if (/csrf|jwt2026\.|cookie/.test(id)) return { impact: 'account takeover / request forgery', cwe: 'CWE-352' };
|
|
333
|
+
if (/supply|npmrc|postinstall/.test(id)) return { impact: 'build/install-time code execution', cwe: 'CWE-829' };
|
|
334
|
+
return { impact: 'security impact', cwe: '' };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const DATAFLOW_PREFERENCE = ['taint.', 'ast.', 'authz2026.', 'llm2026.', 'api2026.'];
|
|
338
|
+
|
|
339
|
+
function buildChains(endpoints, limit = 25) {
|
|
340
|
+
const chains = [];
|
|
341
|
+
for (const ep of endpoints) {
|
|
342
|
+
if (ep.auth === 'unknown') continue; // cannot state the precondition
|
|
343
|
+
const unauthenticated = ep.auth === 'none';
|
|
344
|
+
|
|
345
|
+
// 1) A real sink reachable from this endpoint (direct preferred).
|
|
346
|
+
const sinks = ep.risks
|
|
347
|
+
.filter((r) => r.reach === 'direct' && (r.severity === 'critical' || r.severity === 'high'))
|
|
348
|
+
.sort((a, b) => {
|
|
349
|
+
const da = DATAFLOW_PREFERENCE.some((p) => a.ruleId.startsWith(p)) ? 1 : 0;
|
|
350
|
+
const db = DATAFLOW_PREFERENCE.some((p) => b.ruleId.startsWith(p)) ? 1 : 0;
|
|
351
|
+
return db - da || b.score - a.score;
|
|
352
|
+
});
|
|
353
|
+
if (sinks.length > 0) {
|
|
354
|
+
const sink = sinks[0];
|
|
355
|
+
const { impact, cwe } = impactOf(sink.ruleId);
|
|
356
|
+
chains.push({
|
|
357
|
+
kind: 'sink',
|
|
358
|
+
entrypoint: { method: ep.method, path: ep.path, file: ep.file, line: ep.line, auth: ep.auth },
|
|
359
|
+
sink: { ruleId: sink.ruleId, file: sink.file, line: sink.line, severity: sink.severity },
|
|
360
|
+
precondition: unauthenticated ? 'no authentication' : 'authenticated user',
|
|
361
|
+
impact,
|
|
362
|
+
cwe,
|
|
363
|
+
score: Math.round(ep.score + (unauthenticated ? 25 : 0) + (sink.severity === 'critical' ? 20 : 0)),
|
|
364
|
+
narrative:
|
|
365
|
+
`${unauthenticated ? 'Unauthenticated' : 'Authenticated'} ${ep.method} ${ep.path} reaches ${sink.ruleId} ` +
|
|
366
|
+
`(${sink.file}:${sink.line}) → ${impact}.`,
|
|
367
|
+
});
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// 2) Object-level authorization absent: the endpoint is the vulnerability.
|
|
372
|
+
if (ep.ownership === 'missing') {
|
|
373
|
+
chains.push({
|
|
374
|
+
kind: 'idor',
|
|
375
|
+
entrypoint: { method: ep.method, path: ep.path, file: ep.file, line: ep.line, auth: ep.auth },
|
|
376
|
+
sink: null,
|
|
377
|
+
precondition: unauthenticated ? 'no authentication' : 'authenticated user',
|
|
378
|
+
impact: 'cross-user data access (IDOR): any caller can address another user\'s record by id',
|
|
379
|
+
cwe: 'CWE-639',
|
|
380
|
+
score: Math.round(ep.score + (unauthenticated ? 25 : 0)),
|
|
381
|
+
narrative:
|
|
382
|
+
`${ep.method} ${ep.path} reads an object id from the request with no owner/tenant filter ` +
|
|
383
|
+
`→ any authenticated user can access other users' records.`,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return chains.sort((a, b) => b.score - a.score).slice(0, limit);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function summarizeSurface(surface) {
|
|
391
|
+
const s = surface.stats;
|
|
392
|
+
return (
|
|
393
|
+
`attack surface: ${s.entrypoints} entrypoints (${Object.entries(s.byFramework)
|
|
394
|
+
.map(([k, v]) => `${k} ${v}`)
|
|
395
|
+
.join(', ')}) · ${s.unguarded} unguarded · ${s.unknownAuth || 0} unresolved guards · ` +
|
|
396
|
+
`${s.unguardedStateChanging} unguarded writes · ${s.idorCandidates} IDOR candidates · ` +
|
|
397
|
+
`${s.reachableFindings} reachable findings`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
module.exports = {
|
|
402
|
+
buildSurface,
|
|
403
|
+
buildImportGraph,
|
|
404
|
+
reachableFrom,
|
|
405
|
+
buildChains,
|
|
406
|
+
impactOf,
|
|
407
|
+
summarizeSurface,
|
|
408
|
+
};
|