@neuralaxis/vibedoctor 0.2.1 → 0.2.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 +49 -23
- package/dist/adapters/biome.js +3 -2
- package/dist/adapters/flowDoctor.d.ts +7 -0
- package/dist/adapters/flowDoctor.js +195 -0
- package/dist/adapters/gitleaks.js +4 -2
- package/dist/adapters/index.d.ts +1 -0
- package/dist/adapters/index.js +3 -1
- package/dist/adapters/osvScanner.js +4 -2
- package/dist/adapters/ruff.js +7 -3
- package/dist/adapters/shared.d.ts +4 -0
- package/dist/cli/commands/init.js +7 -0
- package/dist/cli/commands/scan.d.ts +6 -0
- package/dist/cli/commands/scan.js +16 -2
- package/dist/cli/index.js +4 -4
- package/dist/core/agentDiagnosis.d.ts +39 -0
- package/dist/core/agentDiagnosis.js +96 -0
- package/dist/core/config.d.ts +11 -0
- package/dist/core/config.js +31 -2
- package/dist/core/engine.d.ts +8 -1
- package/dist/core/engine.js +54 -51
- package/dist/core/finding.d.ts +1 -1
- package/dist/core/finding.js +8 -3
- package/dist/core/findingCorrelator.d.ts +19 -0
- package/dist/core/findingCorrelator.js +112 -0
- package/dist/core/findingNormalizer.js +1 -0
- package/dist/core/managedTools.d.ts +25 -0
- package/dist/core/managedTools.js +186 -0
- package/dist/core/repoGraph.d.ts +62 -0
- package/dist/core/repoGraph.js +408 -0
- package/dist/core/riskRanker.d.ts +6 -0
- package/dist/core/riskRanker.js +127 -0
- package/dist/core/scanContext.d.ts +26 -0
- package/dist/core/scanContext.js +44 -0
- package/dist/core/scanPlanner.d.ts +1 -0
- package/dist/core/scanPlanner.js +10 -6
- package/dist/core/toolRegistry.js +12 -5
- package/dist/core/toolRunner.d.ts +4 -0
- package/dist/core/toolRunner.js +18 -0
- package/dist/core/toolRuntime.d.ts +40 -0
- package/dist/core/toolRuntime.js +417 -0
- package/dist/core/toolVerification.d.ts +3 -1
- package/dist/core/toolVerification.js +58 -3
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/reporters/agent.d.ts +1 -1
- package/dist/reporters/agent.js +7 -0
- package/package.json +80 -80
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pinned managed-tool catalog.
|
|
4
|
+
*
|
|
5
|
+
* Adapters ask for `tool: "ruff"`; ToolRuntime maps that name onto a
|
|
6
|
+
* versioned, checksummed artifact in the VibeDoctor cache. Other engines plug
|
|
7
|
+
* into the same shape without changing adapters.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.MANAGED_TOOLS = void 0;
|
|
11
|
+
exports.isManagedTool = isManagedTool;
|
|
12
|
+
exports.getManagedTool = getManagedTool;
|
|
13
|
+
exports.currentPlatformKey = currentPlatformKey;
|
|
14
|
+
exports.artifactFor = artifactFor;
|
|
15
|
+
const RUFF_VERSION = "0.16.3";
|
|
16
|
+
const RUFF_BASE = `https://releases.astral.sh/github/ruff/releases/download/${RUFF_VERSION}`;
|
|
17
|
+
const GITLEAKS_VERSION = "8.30.1";
|
|
18
|
+
const GITLEAKS_BASE = `https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}`;
|
|
19
|
+
const OSV_VERSION = "2.5.0";
|
|
20
|
+
const OSV_BASE = `https://github.com/google/osv-scanner/releases/download/v${OSV_VERSION}`;
|
|
21
|
+
exports.MANAGED_TOOLS = {
|
|
22
|
+
ruff: {
|
|
23
|
+
id: "ruff",
|
|
24
|
+
version: RUFF_VERSION,
|
|
25
|
+
artifacts: {
|
|
26
|
+
"win32-x64": {
|
|
27
|
+
url: `${RUFF_BASE}/ruff-x86_64-pc-windows-msvc.zip`,
|
|
28
|
+
sha256: "f10c709755b393fd9821506b21070bcca969b9966504edd1e490efd08e3662ba",
|
|
29
|
+
archive: "zip",
|
|
30
|
+
binary: "ruff.exe"
|
|
31
|
+
},
|
|
32
|
+
"win32-arm64": {
|
|
33
|
+
url: `${RUFF_BASE}/ruff-aarch64-pc-windows-msvc.zip`,
|
|
34
|
+
sha256: "9a9915c54b27b26c972271fbd603845610bb379a7f29571daf923fd8d2bda33e",
|
|
35
|
+
archive: "zip",
|
|
36
|
+
binary: "ruff.exe"
|
|
37
|
+
},
|
|
38
|
+
"darwin-x64": {
|
|
39
|
+
url: `${RUFF_BASE}/ruff-x86_64-apple-darwin.tar.gz`,
|
|
40
|
+
sha256: "05c2a6705e7c0c056d6d93ff538978583f0c47b4c28d334ab9d58d2e8daf4c24",
|
|
41
|
+
archive: "tar.gz",
|
|
42
|
+
binary: "ruff"
|
|
43
|
+
},
|
|
44
|
+
"darwin-arm64": {
|
|
45
|
+
url: `${RUFF_BASE}/ruff-aarch64-apple-darwin.tar.gz`,
|
|
46
|
+
sha256: "136a4db6512d9b16dda56ac8604696ed65c3b1a914a142de029e7f8d5006f1d9",
|
|
47
|
+
archive: "tar.gz",
|
|
48
|
+
binary: "ruff"
|
|
49
|
+
},
|
|
50
|
+
"linux-x64": {
|
|
51
|
+
url: `${RUFF_BASE}/ruff-x86_64-unknown-linux-gnu.tar.gz`,
|
|
52
|
+
sha256: "7ab3b978d2c0b1c96b2323d4e5c4f35284ae1cdf35d2f7399595c74c805f5fa3",
|
|
53
|
+
archive: "tar.gz",
|
|
54
|
+
binary: "ruff"
|
|
55
|
+
},
|
|
56
|
+
"linux-arm64": {
|
|
57
|
+
url: `${RUFF_BASE}/ruff-aarch64-unknown-linux-gnu.tar.gz`,
|
|
58
|
+
sha256: "b9cc833f5db856484b38718c9da195a6ec990707307bda30530913a09705419a",
|
|
59
|
+
archive: "tar.gz",
|
|
60
|
+
binary: "ruff"
|
|
61
|
+
},
|
|
62
|
+
"linux-x64-musl": {
|
|
63
|
+
url: `${RUFF_BASE}/ruff-x86_64-unknown-linux-musl.tar.gz`,
|
|
64
|
+
sha256: "d67c9b5949981698c48915abf65e0b3406ba9184ad73521cdf20a926bc889c73",
|
|
65
|
+
archive: "tar.gz",
|
|
66
|
+
binary: "ruff"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
gitleaks: {
|
|
71
|
+
id: "gitleaks",
|
|
72
|
+
version: GITLEAKS_VERSION,
|
|
73
|
+
artifacts: {
|
|
74
|
+
"win32-x64": {
|
|
75
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_windows_x64.zip`,
|
|
76
|
+
sha256: "d29144deff3a68aa93ced33dddf84b7fdc26070add4aa0f4513094c8332afc4e",
|
|
77
|
+
archive: "zip",
|
|
78
|
+
binary: "gitleaks.exe"
|
|
79
|
+
},
|
|
80
|
+
"win32-arm64": {
|
|
81
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_windows_arm64.zip`,
|
|
82
|
+
sha256: "b95f5e4f5c425cedca7ee203d9afd29597e692c4924a12ed42f970537c72cc0f",
|
|
83
|
+
archive: "zip",
|
|
84
|
+
binary: "gitleaks.exe"
|
|
85
|
+
},
|
|
86
|
+
"darwin-x64": {
|
|
87
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_darwin_x64.tar.gz`,
|
|
88
|
+
sha256: "dfe101a4db2255fc85120ac7f3d25e4342c3c20cf749f2c20a18081af1952709",
|
|
89
|
+
archive: "tar.gz",
|
|
90
|
+
binary: "gitleaks"
|
|
91
|
+
},
|
|
92
|
+
"darwin-arm64": {
|
|
93
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_darwin_arm64.tar.gz`,
|
|
94
|
+
sha256: "b40ab0ae55c505963e365f271a8d3846efbc170aa17f2607f13df610a9aeb6a5",
|
|
95
|
+
archive: "tar.gz",
|
|
96
|
+
binary: "gitleaks"
|
|
97
|
+
},
|
|
98
|
+
"linux-x64": {
|
|
99
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz`,
|
|
100
|
+
sha256: "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb",
|
|
101
|
+
archive: "tar.gz",
|
|
102
|
+
binary: "gitleaks"
|
|
103
|
+
},
|
|
104
|
+
"linux-arm64": {
|
|
105
|
+
url: `${GITLEAKS_BASE}/gitleaks_${GITLEAKS_VERSION}_linux_arm64.tar.gz`,
|
|
106
|
+
sha256: "e4a487ee7ccd7d3a7f7ec08657610aa3606637dab924210b3aee62570fb4b080",
|
|
107
|
+
archive: "tar.gz",
|
|
108
|
+
binary: "gitleaks"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"osv-scanner": {
|
|
113
|
+
id: "osv-scanner",
|
|
114
|
+
version: OSV_VERSION,
|
|
115
|
+
artifacts: {
|
|
116
|
+
"win32-x64": {
|
|
117
|
+
url: `${OSV_BASE}/osv-scanner_windows_amd64.exe`,
|
|
118
|
+
sha256: "4342285bd8be36b9f113468f3eea86e7900befbcd19ca8dc6ac4f0f6cbe7c362",
|
|
119
|
+
archive: "none",
|
|
120
|
+
binary: "osv-scanner.exe"
|
|
121
|
+
},
|
|
122
|
+
"win32-arm64": {
|
|
123
|
+
url: `${OSV_BASE}/osv-scanner_windows_arm64.exe`,
|
|
124
|
+
sha256: "b8bb8d499e0636e193688c96834a11a8f74e353734053f9641e546d76a0cb857",
|
|
125
|
+
archive: "none",
|
|
126
|
+
binary: "osv-scanner.exe"
|
|
127
|
+
},
|
|
128
|
+
"darwin-x64": {
|
|
129
|
+
url: `${OSV_BASE}/osv-scanner_darwin_amd64`,
|
|
130
|
+
sha256: "baef4f4a4ce2924a9241869c36d4bd9d6c04b632cae6637a0f6347ab9272eb16",
|
|
131
|
+
archive: "none",
|
|
132
|
+
binary: "osv-scanner"
|
|
133
|
+
},
|
|
134
|
+
"darwin-arm64": {
|
|
135
|
+
url: `${OSV_BASE}/osv-scanner_darwin_arm64`,
|
|
136
|
+
sha256: "fff5a2e351b7f0a60001e87cbf862e82fb82e2792d368b533fec7a5865a73da2",
|
|
137
|
+
archive: "none",
|
|
138
|
+
binary: "osv-scanner"
|
|
139
|
+
},
|
|
140
|
+
"linux-x64": {
|
|
141
|
+
url: `${OSV_BASE}/osv-scanner_linux_amd64`,
|
|
142
|
+
sha256: "edcfc41d257db36148f065055655fe3fcfc434b0b423ea67468a84c207524e0c",
|
|
143
|
+
archive: "none",
|
|
144
|
+
binary: "osv-scanner"
|
|
145
|
+
},
|
|
146
|
+
"linux-arm64": {
|
|
147
|
+
url: `${OSV_BASE}/osv-scanner_linux_arm64`,
|
|
148
|
+
sha256: "fe152e1a546af223e6c557cc3111a8bb3e5dc02fcbf7dbe95d26567c0f0041f2",
|
|
149
|
+
archive: "none",
|
|
150
|
+
binary: "osv-scanner"
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
function isManagedTool(id) {
|
|
156
|
+
return Boolean(exports.MANAGED_TOOLS[id]);
|
|
157
|
+
}
|
|
158
|
+
function getManagedTool(id) {
|
|
159
|
+
return exports.MANAGED_TOOLS[id];
|
|
160
|
+
}
|
|
161
|
+
function currentPlatformKey() {
|
|
162
|
+
const os = process.platform === "win32" || process.platform === "darwin" || process.platform === "linux" ? process.platform : "linux";
|
|
163
|
+
const arch = process.arch === "x64" || process.arch === "arm64" || process.arch === "ia32" || process.arch === "arm" ? process.arch : "x64";
|
|
164
|
+
if (os === "linux" && arch === "x64" && typeof process.report?.getReport === "function" && isMusl()) {
|
|
165
|
+
return "linux-x64-musl";
|
|
166
|
+
}
|
|
167
|
+
if (os === "win32" && arch === "ia32") {
|
|
168
|
+
return "win32-x64";
|
|
169
|
+
}
|
|
170
|
+
return `${os}-${arch === "ia32" ? "x64" : arch}`;
|
|
171
|
+
}
|
|
172
|
+
function isMusl() {
|
|
173
|
+
try {
|
|
174
|
+
const report = process.report?.getReport();
|
|
175
|
+
if (report && typeof report === "object") {
|
|
176
|
+
return !report.header?.glibcVersionRuntime;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// Best-effort; fall back to gnu.
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
function artifactFor(spec, platformKey = currentPlatformKey()) {
|
|
185
|
+
return spec.artifacts[platformKey];
|
|
186
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { FileIndex } from "./scanContext";
|
|
2
|
+
export type HttpCall = {
|
|
3
|
+
file: string;
|
|
4
|
+
line: number;
|
|
5
|
+
method: string;
|
|
6
|
+
/** False when fetch() had no visible method — do not guess GET. */
|
|
7
|
+
methodExplicit: boolean;
|
|
8
|
+
path: string;
|
|
9
|
+
dynamic: boolean;
|
|
10
|
+
absolute: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type RouteDef = {
|
|
13
|
+
file: string;
|
|
14
|
+
line: number;
|
|
15
|
+
method: string;
|
|
16
|
+
path: string;
|
|
17
|
+
/** Simple identifier used as the handler, when one is visible. */
|
|
18
|
+
handler?: string;
|
|
19
|
+
/** True when the handler is `controllers.create` or similar — not statically bound. */
|
|
20
|
+
handlerMember?: boolean;
|
|
21
|
+
handlerFile?: string;
|
|
22
|
+
handlerLine?: number;
|
|
23
|
+
handlerExternal?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export type ImportEdge = {
|
|
26
|
+
from: string;
|
|
27
|
+
specifier: string;
|
|
28
|
+
names: string[];
|
|
29
|
+
external: boolean;
|
|
30
|
+
};
|
|
31
|
+
export type SymbolDef = {
|
|
32
|
+
file: string;
|
|
33
|
+
name: string;
|
|
34
|
+
kind: "function" | "class" | "route" | "handler";
|
|
35
|
+
line: number;
|
|
36
|
+
exported: boolean;
|
|
37
|
+
};
|
|
38
|
+
export type CallSite = {
|
|
39
|
+
file: string;
|
|
40
|
+
callee: string;
|
|
41
|
+
line: number;
|
|
42
|
+
};
|
|
43
|
+
export type ErrorPath = {
|
|
44
|
+
file: string;
|
|
45
|
+
line: number;
|
|
46
|
+
kind: "empty_handler" | "success_after_failure";
|
|
47
|
+
detail: string;
|
|
48
|
+
};
|
|
49
|
+
export type RepoGraph = {
|
|
50
|
+
files: string[];
|
|
51
|
+
imports: ImportEdge[];
|
|
52
|
+
symbols: SymbolDef[];
|
|
53
|
+
calls: CallSite[];
|
|
54
|
+
httpCalls: HttpCall[];
|
|
55
|
+
routes: RouteDef[];
|
|
56
|
+
errorPaths: ErrorPath[];
|
|
57
|
+
};
|
|
58
|
+
export declare function normalizeRoutePath(raw: string): string;
|
|
59
|
+
export declare function pathsCompatible(left: string, right: string): boolean;
|
|
60
|
+
export declare function linkRepoGraph(graph: RepoGraph): RepoGraph;
|
|
61
|
+
export declare function buildRepoGraph(root: string, files: FileIndex | readonly string[]): Promise<RepoGraph>;
|
|
62
|
+
export declare function routeMatchesCall(call: HttpCall, route: RouteDef): boolean;
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalizeRoutePath = normalizeRoutePath;
|
|
7
|
+
exports.pathsCompatible = pathsCompatible;
|
|
8
|
+
exports.linkRepoGraph = linkRepoGraph;
|
|
9
|
+
exports.buildRepoGraph = buildRepoGraph;
|
|
10
|
+
exports.routeMatchesCall = routeMatchesCall;
|
|
11
|
+
const node_fs_1 = require("node:fs");
|
|
12
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
13
|
+
const SOURCE_EXTS = new Set([".ts", ".tsx", ".js", ".jsx", ".py"]);
|
|
14
|
+
const MAX_FILE_BYTES = 512_000;
|
|
15
|
+
function normalizeMethod(method) {
|
|
16
|
+
return method.toUpperCase();
|
|
17
|
+
}
|
|
18
|
+
function normalizeRoutePath(raw) {
|
|
19
|
+
const withoutQuery = raw.split("?")[0].trim();
|
|
20
|
+
const withSlash = withoutQuery.startsWith("/") ? withoutQuery : `/${withoutQuery}`;
|
|
21
|
+
return withSlash
|
|
22
|
+
.replace(/\/+$/, "")
|
|
23
|
+
.replace(/\{[^}]+\}/g, ":param")
|
|
24
|
+
.replace(/:[A-Za-z_][\w]*/g, ":param")
|
|
25
|
+
.replace(/\[[^\]]+\]/g, ":param")
|
|
26
|
+
|| "/";
|
|
27
|
+
}
|
|
28
|
+
function stripCommonApiPrefix(normalized) {
|
|
29
|
+
return normalized.replace(/^\/api(?=\/|$)/, "") || "/";
|
|
30
|
+
}
|
|
31
|
+
function pathsCompatible(left, right) {
|
|
32
|
+
const a = normalizeRoutePath(left);
|
|
33
|
+
const b = normalizeRoutePath(right);
|
|
34
|
+
if (a === b) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
// SPA clients typically call /api/users while Express mounts /users.
|
|
38
|
+
return stripCommonApiPrefix(a) === stripCommonApiPrefix(b);
|
|
39
|
+
}
|
|
40
|
+
function isCommentLine(line, python) {
|
|
41
|
+
const trimmed = line.trim();
|
|
42
|
+
return python ? trimmed.startsWith("#") : trimmed.startsWith("//") || trimmed.startsWith("*") || trimmed.startsWith("/*");
|
|
43
|
+
}
|
|
44
|
+
function parseJsTs(file, content) {
|
|
45
|
+
const imports = [];
|
|
46
|
+
const symbols = [];
|
|
47
|
+
const calls = [];
|
|
48
|
+
const httpCalls = [];
|
|
49
|
+
const routes = [];
|
|
50
|
+
const errorPaths = [];
|
|
51
|
+
const lines = content.split(/\r?\n/);
|
|
52
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
53
|
+
const line = lines[index];
|
|
54
|
+
const lineNo = index + 1;
|
|
55
|
+
if (isCommentLine(line, false)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
imports.push(...parseJsImports(file, line));
|
|
59
|
+
const fnMatch = /(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/.exec(line);
|
|
60
|
+
if (fnMatch) {
|
|
61
|
+
symbols.push({ file, name: fnMatch[1], kind: "function", line: lineNo, exported: /^\s*export\b/.test(line) });
|
|
62
|
+
}
|
|
63
|
+
const constFn = /(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*=>)/.exec(line);
|
|
64
|
+
if (constFn) {
|
|
65
|
+
symbols.push({ file, name: constFn[1], kind: "function", line: lineNo, exported: /^\s*export\b/.test(line) });
|
|
66
|
+
}
|
|
67
|
+
const exportList = /^\s*export\s*\{([^}]+)\}/.exec(line);
|
|
68
|
+
if (exportList) {
|
|
69
|
+
for (const raw of exportList[1].split(",")) {
|
|
70
|
+
const local = raw.trim().split(/\s+as\s+/)[0]?.trim();
|
|
71
|
+
const existing = symbols.find((symbol) => symbol.file === file && symbol.name === local);
|
|
72
|
+
if (existing) {
|
|
73
|
+
existing.exported = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const callMatch of line.matchAll(/\b([A-Za-z_$][\w$]*)\s*\(/g)) {
|
|
78
|
+
if (!/^(if|for|while|switch|catch|function|return|async)$/.test(callMatch[1])) {
|
|
79
|
+
calls.push({ file, callee: callMatch[1], line: lineNo });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const fetchMatch = /\bfetch\s*\(\s*(['"`])([^'"`]+)\1/.exec(line) ??
|
|
83
|
+
/\baxios\.(get|post|put|patch|delete)\s*\(\s*(['"`])([^'"`]+)\2/i.exec(line);
|
|
84
|
+
if (fetchMatch) {
|
|
85
|
+
const method = fetchMatch.length === 4 ? fetchMatch[1] : methodFromFetchOptions(lines, index);
|
|
86
|
+
const rawPath = fetchMatch.length === 4 ? fetchMatch[3] : fetchMatch[2];
|
|
87
|
+
httpCalls.push(httpCallFrom(file, lineNo, method, rawPath, line));
|
|
88
|
+
}
|
|
89
|
+
const routeMatch = /\b(?:app|router|server)\.(get|post|put|patch|delete|all)\s*\(\s*(['"`])([^'"`]+)\2\s*(?:,\s*(?:async\s+)?([A-Za-z_$][\w$]*)(\.[A-Za-z_$][\w$]*)?)?/i.exec(line);
|
|
90
|
+
if (routeMatch && /get|post|put|patch|delete|all/i.test(routeMatch[1])) {
|
|
91
|
+
const handlerName = routeMatch[4];
|
|
92
|
+
const reserved = handlerName && /^(function|async|await|return|if)$/i.test(handlerName);
|
|
93
|
+
routes.push({
|
|
94
|
+
file,
|
|
95
|
+
line: lineNo,
|
|
96
|
+
method: normalizeMethod(routeMatch[1] === "all" ? "*" : routeMatch[1]),
|
|
97
|
+
path: routeMatch[3],
|
|
98
|
+
handler: reserved ? undefined : handlerName,
|
|
99
|
+
handlerMember: Boolean(routeMatch[5])
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const exportedHandler = /export\s+async\s+function\s+(GET|POST|PUT|PATCH|DELETE)\b/.exec(line);
|
|
103
|
+
if (exportedHandler) {
|
|
104
|
+
const fromFile = fileBasedRoute(file);
|
|
105
|
+
if (fromFile) {
|
|
106
|
+
routes.push({
|
|
107
|
+
file,
|
|
108
|
+
line: lineNo,
|
|
109
|
+
method: exportedHandler[1],
|
|
110
|
+
path: fromFile,
|
|
111
|
+
handler: exportedHandler[1],
|
|
112
|
+
handlerFile: file,
|
|
113
|
+
handlerLine: lineNo
|
|
114
|
+
});
|
|
115
|
+
symbols.push({ file, name: exportedHandler[1], kind: "handler", line: lineNo, exported: true });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
errorPaths.push(...detectJsErrorPaths(file, content));
|
|
120
|
+
return { imports, symbols, calls, httpCalls, routes, errorPaths };
|
|
121
|
+
}
|
|
122
|
+
function parsePython(file, content) {
|
|
123
|
+
const imports = [];
|
|
124
|
+
const symbols = [];
|
|
125
|
+
const calls = [];
|
|
126
|
+
const httpCalls = [];
|
|
127
|
+
const routes = [];
|
|
128
|
+
const errorPaths = [];
|
|
129
|
+
const pendingPythonRoute = new Map();
|
|
130
|
+
const lines = content.split(/\r?\n/);
|
|
131
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
132
|
+
const line = lines[index];
|
|
133
|
+
const lineNo = index + 1;
|
|
134
|
+
if (isCommentLine(line, true)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const fromImport = /^\s*from\s+([\w.]+)\s+import\s+(.+)$/.exec(line);
|
|
138
|
+
if (fromImport) {
|
|
139
|
+
const names = fromImport[2]
|
|
140
|
+
.split(",")
|
|
141
|
+
.map((part) => part.trim().split(/\s+as\s+/).pop()?.trim())
|
|
142
|
+
.filter((name) => Boolean(name) && name !== "*");
|
|
143
|
+
imports.push({ from: file, specifier: fromImport[1], names, external: !fromImport[1].startsWith(".") });
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const importMatch = /^\s*import\s+([\w.]+)/.exec(line);
|
|
147
|
+
if (importMatch) {
|
|
148
|
+
imports.push({ from: file, specifier: importMatch[1], names: [importMatch[1].split(".").pop() ?? importMatch[1]], external: !importMatch[1].startsWith(".") });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const fnMatch = /^\s*(?:async\s+)?def\s+([A-Za-z_][\w]*)/.exec(line);
|
|
152
|
+
if (fnMatch) {
|
|
153
|
+
symbols.push({ file, name: fnMatch[1], kind: "function", line: lineNo, exported: true });
|
|
154
|
+
const pending = pendingPythonRoute.get(file);
|
|
155
|
+
if (pending) {
|
|
156
|
+
pending.handler = fnMatch[1];
|
|
157
|
+
pending.handlerFile = file;
|
|
158
|
+
pending.handlerLine = lineNo;
|
|
159
|
+
pendingPythonRoute.delete(file);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (const callMatch of line.matchAll(/\b([A-Za-z_][\w]*)\s*\(/g)) {
|
|
163
|
+
if (!/^(if|for|while|except|def|return|print|len|range)$/.test(callMatch[1])) {
|
|
164
|
+
calls.push({ file, callee: callMatch[1], line: lineNo });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const client = /\b(?:requests|httpx)\.(get|post|put|patch|delete)\s*\(\s*(['"])([^'"]+)\2/i.exec(line);
|
|
168
|
+
if (client) {
|
|
169
|
+
httpCalls.push(httpCallFrom(file, lineNo, client[1], client[3], line));
|
|
170
|
+
}
|
|
171
|
+
const decorator = /@(?:app|router|api)\.(get|post|put|patch|delete)\s*\(\s*(['"])([^'"]+)\2/i.exec(line);
|
|
172
|
+
if (decorator) {
|
|
173
|
+
const route = { file, line: lineNo, method: normalizeMethod(decorator[1]), path: decorator[3] };
|
|
174
|
+
routes.push(route);
|
|
175
|
+
pendingPythonRoute.set(file, route);
|
|
176
|
+
}
|
|
177
|
+
const flask = /\b(?:app|blueprint)\.add_url_rule\s*\(\s*(['"])([^'"]+)\1[^)]*methods\s*=\s*\[[^\]]*['"](GET|POST|PUT|PATCH|DELETE)['"]/i.exec(line);
|
|
178
|
+
if (flask) {
|
|
179
|
+
routes.push({ file, line: lineNo, method: normalizeMethod(flask[3]), path: flask[2] });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
errorPaths.push(...detectPyErrorPaths(file, content));
|
|
183
|
+
return { imports, symbols, calls, httpCalls, routes, errorPaths };
|
|
184
|
+
}
|
|
185
|
+
function parseJsImports(file, line) {
|
|
186
|
+
const edges = [];
|
|
187
|
+
const named = /import\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]/.exec(line);
|
|
188
|
+
if (named) {
|
|
189
|
+
const names = named[1]
|
|
190
|
+
.split(",")
|
|
191
|
+
.map((part) => {
|
|
192
|
+
const bits = part.trim().split(/\s+as\s+/);
|
|
193
|
+
return (bits[1] ?? bits[0])?.trim();
|
|
194
|
+
})
|
|
195
|
+
.filter((name) => Boolean(name));
|
|
196
|
+
edges.push({ from: file, specifier: named[2], names, external: !named[2].startsWith(".") });
|
|
197
|
+
}
|
|
198
|
+
const def = /import\s+(?:type\s+)?([A-Za-z_$][\w$]*)\s+from\s+['"]([^'"]+)['"]/.exec(line);
|
|
199
|
+
if (def) {
|
|
200
|
+
edges.push({ from: file, specifier: def[2], names: [def[1]], external: !def[2].startsWith(".") });
|
|
201
|
+
}
|
|
202
|
+
const req = /require\s*\(\s*['"]([^'"]+)['"]/.exec(line);
|
|
203
|
+
if (req && !named && !def) {
|
|
204
|
+
edges.push({ from: file, specifier: req[1], names: [], external: !req[1].startsWith(".") });
|
|
205
|
+
}
|
|
206
|
+
return edges;
|
|
207
|
+
}
|
|
208
|
+
const JS_RESOLVE_EXTS = [".ts", ".tsx", ".js", ".jsx", ""];
|
|
209
|
+
function resolveSpecifier(fromFile, specifier, files) {
|
|
210
|
+
if (!specifier.startsWith(".")) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
const fromDir = fromFile.includes("/") ? fromFile.slice(0, fromFile.lastIndexOf("/")) : "";
|
|
214
|
+
const joined = node_path_1.default.posix.normalize(`${fromDir}/${specifier}`).replace(/^\.\//, "");
|
|
215
|
+
const candidates = [
|
|
216
|
+
...JS_RESOLVE_EXTS.map((ext) => `${joined}${ext}`),
|
|
217
|
+
`${joined}/index.ts`,
|
|
218
|
+
`${joined}/index.js`,
|
|
219
|
+
`${joined}/index.tsx`,
|
|
220
|
+
`${joined}.py`
|
|
221
|
+
];
|
|
222
|
+
return candidates.find((candidate) => files.has(candidate));
|
|
223
|
+
}
|
|
224
|
+
function linkRepoGraph(graph) {
|
|
225
|
+
const files = new Set(graph.files);
|
|
226
|
+
for (const route of graph.routes) {
|
|
227
|
+
const handlerName = route.handler;
|
|
228
|
+
if (!handlerName || route.handlerMember || route.handlerFile) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const local = graph.symbols.find((symbol) => symbol.file === route.file && symbol.name === handlerName);
|
|
232
|
+
if (local) {
|
|
233
|
+
route.handlerFile = local.file;
|
|
234
|
+
route.handlerLine = local.line;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const imported = graph.imports.find((edge) => edge.from === route.file && edge.names.includes(handlerName));
|
|
238
|
+
if (imported?.external) {
|
|
239
|
+
route.handlerExternal = true;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (imported) {
|
|
243
|
+
const target = resolveSpecifier(route.file, imported.specifier, files);
|
|
244
|
+
if (target) {
|
|
245
|
+
const remote = graph.symbols.find((symbol) => symbol.file === target && symbol.name === handlerName);
|
|
246
|
+
if (remote) {
|
|
247
|
+
route.handlerFile = remote.file;
|
|
248
|
+
route.handlerLine = remote.line;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return graph;
|
|
254
|
+
}
|
|
255
|
+
function methodFromFetchOptions(lines, startIndex) {
|
|
256
|
+
for (let index = startIndex; index < lines.length && index < startIndex + 10; index += 1) {
|
|
257
|
+
const match = /method\s*:\s*['"](GET|POST|PUT|PATCH|DELETE)['"]/i.exec(lines[index]);
|
|
258
|
+
if (match) {
|
|
259
|
+
return match[1];
|
|
260
|
+
}
|
|
261
|
+
if (index > startIndex && /^\s*\)\s*;?\s*$/.test(lines[index])) {
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
function httpCallFrom(file, line, method, rawPath, sourceLine) {
|
|
268
|
+
const dynamic = /\$\{|%[sdf]|f["']/.test(rawPath) || /\$\{/.test(sourceLine);
|
|
269
|
+
const absolute = /^https?:\/\//i.test(rawPath);
|
|
270
|
+
return {
|
|
271
|
+
file,
|
|
272
|
+
line,
|
|
273
|
+
method: normalizeMethod(method ?? "GET"),
|
|
274
|
+
methodExplicit: Boolean(method),
|
|
275
|
+
path: rawPath,
|
|
276
|
+
dynamic,
|
|
277
|
+
absolute
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function fileBasedRoute(file) {
|
|
281
|
+
const posix = file.replaceAll("\\", "/");
|
|
282
|
+
const appApi = /(?:^|\/)app\/(api\/.+)\/route\.(t|j)sx?$/.exec(posix);
|
|
283
|
+
if (appApi) {
|
|
284
|
+
return `/${appApi[1]}`.replace(/\/route$/, "");
|
|
285
|
+
}
|
|
286
|
+
const pagesApi = /(?:^|\/)pages\/(api\/.+)\.(t|j)sx?$/.exec(posix);
|
|
287
|
+
if (pagesApi) {
|
|
288
|
+
return `/${pagesApi[1]}`;
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
function detectJsErrorPaths(file, content) {
|
|
293
|
+
const findings = [];
|
|
294
|
+
const emptyCatch = /catch\s*(?:\([^)]*\))?\s*\{\s*\}/g;
|
|
295
|
+
let match;
|
|
296
|
+
while ((match = emptyCatch.exec(content))) {
|
|
297
|
+
findings.push({
|
|
298
|
+
file,
|
|
299
|
+
line: lineNumberAt(content, match.index),
|
|
300
|
+
kind: "empty_handler",
|
|
301
|
+
detail: "Empty catch swallows the error."
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
const successAfter = /catch\s*(?:\([^)]*\))?\s*\{[^}]*(?:status\s*\(\s*20\d\s*\)|\bok\s*:\s*true|\bsuccess\s*:\s*true)[^}]*\}/gis;
|
|
305
|
+
while ((match = successAfter.exec(content))) {
|
|
306
|
+
findings.push({
|
|
307
|
+
file,
|
|
308
|
+
line: lineNumberAt(content, match.index),
|
|
309
|
+
kind: "success_after_failure",
|
|
310
|
+
detail: "Caught failure then emitted an HTTP 2xx or success payload."
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
return findings;
|
|
314
|
+
}
|
|
315
|
+
function detectPyErrorPaths(file, content) {
|
|
316
|
+
const findings = [];
|
|
317
|
+
const lines = content.split(/\r?\n/);
|
|
318
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
319
|
+
if (!/^\s*except\b/.test(lines[index])) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const body = [];
|
|
323
|
+
let cursor = index + 1;
|
|
324
|
+
const indent = /^\s*/.exec(lines[index])?.[0].length ?? 0;
|
|
325
|
+
while (cursor < lines.length) {
|
|
326
|
+
const next = lines[cursor];
|
|
327
|
+
if (next.trim() === "") {
|
|
328
|
+
cursor += 1;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const nextIndent = /^\s*/.exec(next)?.[0].length ?? 0;
|
|
332
|
+
if (nextIndent <= indent) {
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
body.push(next.trim());
|
|
336
|
+
cursor += 1;
|
|
337
|
+
}
|
|
338
|
+
const meaningful = body.filter((line) => line && !line.startsWith("#"));
|
|
339
|
+
if (meaningful.length === 0 || meaningful.every((line) => line === "pass" || line === "...")) {
|
|
340
|
+
findings.push({
|
|
341
|
+
file,
|
|
342
|
+
line: index + 1,
|
|
343
|
+
kind: "empty_handler",
|
|
344
|
+
detail: "except handler is empty or only pass."
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
else if (meaningful.some((line) => /return\s+.*\b(200|ok\s*=\s*True|success)/i.test(line))) {
|
|
348
|
+
findings.push({
|
|
349
|
+
file,
|
|
350
|
+
line: index + 1,
|
|
351
|
+
kind: "success_after_failure",
|
|
352
|
+
detail: "except handler returns a success response."
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return findings;
|
|
357
|
+
}
|
|
358
|
+
function lineNumberAt(content, index) {
|
|
359
|
+
return content.slice(0, index).split(/\r?\n/).length;
|
|
360
|
+
}
|
|
361
|
+
async function buildRepoGraph(root, files) {
|
|
362
|
+
const list = "files" in files ? [...files.files] : [...files];
|
|
363
|
+
const graph = {
|
|
364
|
+
files: [],
|
|
365
|
+
imports: [],
|
|
366
|
+
symbols: [],
|
|
367
|
+
calls: [],
|
|
368
|
+
httpCalls: [],
|
|
369
|
+
routes: [],
|
|
370
|
+
errorPaths: []
|
|
371
|
+
};
|
|
372
|
+
for (const file of list) {
|
|
373
|
+
const ext = node_path_1.default.extname(file).toLowerCase();
|
|
374
|
+
if (!SOURCE_EXTS.has(ext)) {
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
const absolute = node_path_1.default.join(root, file);
|
|
378
|
+
let content;
|
|
379
|
+
try {
|
|
380
|
+
const stat = await node_fs_1.promises.stat(absolute);
|
|
381
|
+
if (stat.size > MAX_FILE_BYTES) {
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
content = await node_fs_1.promises.readFile(absolute, "utf8");
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
graph.files.push(file);
|
|
390
|
+
const parsed = ext === ".py" ? parsePython(file, content) : parseJsTs(file, content);
|
|
391
|
+
graph.imports.push(...parsed.imports);
|
|
392
|
+
graph.symbols.push(...parsed.symbols);
|
|
393
|
+
graph.calls.push(...parsed.calls);
|
|
394
|
+
graph.httpCalls.push(...parsed.httpCalls);
|
|
395
|
+
graph.routes.push(...parsed.routes);
|
|
396
|
+
graph.errorPaths.push(...parsed.errorPaths);
|
|
397
|
+
}
|
|
398
|
+
return linkRepoGraph(graph);
|
|
399
|
+
}
|
|
400
|
+
function routeMatchesCall(call, route) {
|
|
401
|
+
if (call.dynamic || !call.methodExplicit) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
if (route.method !== "*" && route.method !== call.method) {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
return pathsCompatible(call.path, route.path);
|
|
408
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Finding, FindingCategory } from "./finding";
|
|
2
|
+
export declare function isStyleOrHygieneFinding(finding: Finding): boolean;
|
|
3
|
+
export declare function isLikelyRealBug(finding: Finding): boolean;
|
|
4
|
+
export declare function riskScore(finding: Finding): number;
|
|
5
|
+
export declare function rankFindings(findings: Finding[]): Finding[];
|
|
6
|
+
export declare const RANKING_CATEGORY_HINT: FindingCategory[];
|