code-intelligence 0.0.0-stage → 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/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +154 -2
- package/THIRD_PARTY.md +17 -0
- package/dist/cli.js +7 -0
- package/dist/commands/doctor.js +80 -0
- package/dist/commands/impact.js +148 -0
- package/dist/commands/init.js +27 -0
- package/dist/commands/mutate.js +319 -0
- package/dist/commands/scan.js +118 -0
- package/dist/core/baseline.js +54 -0
- package/dist/core/cache.js +148 -0
- package/dist/core/config.js +317 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/exec.js +154 -0
- package/dist/core/glob.js +306 -0
- package/dist/core/hashing.js +44 -0
- package/dist/core/logger.js +29 -0
- package/dist/core/normalize.js +133 -0
- package/dist/core/planner.js +106 -0
- package/dist/core/runner.js +126 -0
- package/dist/core/safefs.js +87 -0
- package/dist/core/suppressions.js +27 -0
- package/dist/core/targets.js +286 -0
- package/dist/engines/ast-grep.js +71 -0
- package/dist/engines/engine.js +25 -0
- package/dist/engines/gitleaks.js +135 -0
- package/dist/engines/osv-scanner.js +100 -0
- package/dist/engines/registry.js +7 -0
- package/dist/engines/semgrep.js +139 -0
- package/dist/engines/trivy.js +71 -0
- package/dist/engines/util.js +129 -0
- package/dist/main.js +301 -0
- package/dist/report/json.js +1 -0
- package/dist/report/sarif.js +73 -0
- package/dist/report/text.js +86 -0
- package/dist/types.js +9 -0
- package/dist/version.js +12 -0
- package/package.json +56 -4
- package/schema/config.schema.json +86 -0
- package/schema/report.schema.json +126 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Glob matcher for repo-relative POSIX paths, with no dependencies and no regular expressions.
|
|
3
|
+
*
|
|
4
|
+
* Why not a regex: patterns can come from an untrusted repository's configuration, and a regex built
|
|
5
|
+
* from a hostile glob can run for minutes (JavaScript has no regex timeout). Capping the wildcard count
|
|
6
|
+
* only lowers the polynomial degree. This matcher is a dynamic program over (pattern tokens x path
|
|
7
|
+
* characters), so its cost is O(tokens x path length) for any input.
|
|
8
|
+
*
|
|
9
|
+
* Syntax: `**` (any characters including `/`), `**` followed by `/` (zero or more directories), `*` (any
|
|
10
|
+
* characters except `/`), `?` (one character except `/`), `[abc]` / `[a-z]` / `[!abc]`, and `{a,b}`.
|
|
11
|
+
* A pattern without `/` matches that name at any depth (gitignore-style), and a match on a directory
|
|
12
|
+
* excludes everything beneath it.
|
|
13
|
+
*/
|
|
14
|
+
import { UsageError } from "./errors.js";
|
|
15
|
+
export const MAX_GLOB_LENGTH = 200;
|
|
16
|
+
export const MAX_PATTERNS = 100;
|
|
17
|
+
export const MAX_TOTAL_ALTERNATIVES = 64;
|
|
18
|
+
/** Upper bound on matcher work (tokens x path length, summed over every call) before a scan is aborted. */
|
|
19
|
+
const WORK_BUDGET = 400_000_000;
|
|
20
|
+
export const MAX_DOUBLE_STAR = 3;
|
|
21
|
+
const MAX_WILDCARDS = 12;
|
|
22
|
+
const MAX_BRACE_EXPANSIONS = 32;
|
|
23
|
+
/** Expand non-nested `{a,b}` alternatives. Unbalanced braces stay literal. */
|
|
24
|
+
function expandBraces(p) {
|
|
25
|
+
if (p.split("{").length !== p.split("}").length)
|
|
26
|
+
return [p];
|
|
27
|
+
const open = p.indexOf("{");
|
|
28
|
+
if (open === -1)
|
|
29
|
+
return [p];
|
|
30
|
+
let depth = 0;
|
|
31
|
+
let close = -1;
|
|
32
|
+
for (let i = open; i < p.length; i++) {
|
|
33
|
+
if (p[i] === "{")
|
|
34
|
+
depth += 1;
|
|
35
|
+
else if (p[i] === "}") {
|
|
36
|
+
depth -= 1;
|
|
37
|
+
if (depth === 0) {
|
|
38
|
+
close = i;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (close === -1)
|
|
44
|
+
return [p];
|
|
45
|
+
const parts = [];
|
|
46
|
+
let cur = "";
|
|
47
|
+
depth = 0;
|
|
48
|
+
for (const ch of p.slice(open + 1, close)) {
|
|
49
|
+
if (ch === "{")
|
|
50
|
+
depth += 1;
|
|
51
|
+
if (ch === "}")
|
|
52
|
+
depth -= 1;
|
|
53
|
+
if (ch === "," && depth === 0) {
|
|
54
|
+
parts.push(cur);
|
|
55
|
+
cur = "";
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
cur += ch;
|
|
59
|
+
}
|
|
60
|
+
parts.push(cur);
|
|
61
|
+
const head = p.slice(0, open);
|
|
62
|
+
const tail = p.slice(close + 1);
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const part of parts) {
|
|
65
|
+
for (const rest of expandBraces(`${part}${tail}`)) {
|
|
66
|
+
out.push(`${head}${rest}`);
|
|
67
|
+
if (out.length > MAX_BRACE_EXPANSIONS)
|
|
68
|
+
throw new Error("too many brace alternatives");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
function tokenize(p) {
|
|
74
|
+
const toks = [];
|
|
75
|
+
let i = 0;
|
|
76
|
+
while (i < p.length) {
|
|
77
|
+
const ch = p[i];
|
|
78
|
+
if (ch === "*") {
|
|
79
|
+
if (p[i + 1] === "*") {
|
|
80
|
+
if (p[i + 2] === "/") {
|
|
81
|
+
toks.push({ t: "gslash" });
|
|
82
|
+
i += 3;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
toks.push({ t: "gstar" });
|
|
86
|
+
i += 2;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
toks.push({ t: "star" });
|
|
91
|
+
i += 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (ch === "?") {
|
|
95
|
+
toks.push({ t: "any" });
|
|
96
|
+
i += 1;
|
|
97
|
+
}
|
|
98
|
+
else if (ch === "[") {
|
|
99
|
+
const close = p.indexOf("]", i + 2);
|
|
100
|
+
if (close === -1) {
|
|
101
|
+
toks.push({ t: "lit", c: "[" });
|
|
102
|
+
i += 1;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
let body = p.slice(i + 1, close);
|
|
106
|
+
const neg = body.startsWith("!") || body.startsWith("^");
|
|
107
|
+
if (neg)
|
|
108
|
+
body = body.slice(1);
|
|
109
|
+
const items = [];
|
|
110
|
+
for (let k = 0; k < body.length; k++) {
|
|
111
|
+
const a = body[k];
|
|
112
|
+
if (body[k + 1] === "-" && k + 2 < body.length) {
|
|
113
|
+
items.push([a, body[k + 2]]);
|
|
114
|
+
k += 2;
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
items.push([a, a]);
|
|
118
|
+
}
|
|
119
|
+
toks.push({ t: "cls", neg, items });
|
|
120
|
+
i = close + 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
toks.push({ t: "lit", c: ch });
|
|
125
|
+
i += 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return toks;
|
|
129
|
+
}
|
|
130
|
+
function classHas(tok, ch) {
|
|
131
|
+
const hit = tok.items.some(([a, b]) => ch >= a && ch <= b);
|
|
132
|
+
return tok.neg ? !hit : hit;
|
|
133
|
+
}
|
|
134
|
+
/** O(tokens x path length): reach[j] means "the tokens so far can consume path[0..j)". */
|
|
135
|
+
function matchTokens(toks, path) {
|
|
136
|
+
const n = path.length;
|
|
137
|
+
let reach = new Uint8Array(n + 1);
|
|
138
|
+
reach[0] = 1;
|
|
139
|
+
for (const tok of toks) {
|
|
140
|
+
const next = new Uint8Array(n + 1);
|
|
141
|
+
let any = false;
|
|
142
|
+
switch (tok.t) {
|
|
143
|
+
case "lit":
|
|
144
|
+
for (let j = 0; j < n; j++)
|
|
145
|
+
if (reach[j] && path[j] === tok.c)
|
|
146
|
+
next[j + 1] = 1;
|
|
147
|
+
break;
|
|
148
|
+
case "any":
|
|
149
|
+
for (let j = 0; j < n; j++)
|
|
150
|
+
if (reach[j] && path[j] !== "/")
|
|
151
|
+
next[j + 1] = 1;
|
|
152
|
+
break;
|
|
153
|
+
case "cls":
|
|
154
|
+
for (let j = 0; j < n; j++) {
|
|
155
|
+
const ch = path[j];
|
|
156
|
+
if (reach[j] && ch !== "/" && classHas(tok, ch))
|
|
157
|
+
next[j + 1] = 1;
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
case "star": {
|
|
161
|
+
let on = false;
|
|
162
|
+
for (let k = 0; k <= n; k++) {
|
|
163
|
+
on = Boolean(reach[k]) || (on && k > 0 && path[k - 1] !== "/");
|
|
164
|
+
if (on)
|
|
165
|
+
next[k] = 1;
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case "gstar": {
|
|
170
|
+
let on = false;
|
|
171
|
+
for (let k = 0; k <= n; k++) {
|
|
172
|
+
on = on || Boolean(reach[k]);
|
|
173
|
+
if (on)
|
|
174
|
+
next[k] = 1;
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case "gslash": {
|
|
179
|
+
let seen = false;
|
|
180
|
+
for (let k = 0; k <= n; k++) {
|
|
181
|
+
if (k > 0 && reach[k - 1])
|
|
182
|
+
seen = true;
|
|
183
|
+
if (reach[k] || (seen && k > 0 && path[k - 1] === "/"))
|
|
184
|
+
next[k] = 1;
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (let j = 0; j <= n; j++) {
|
|
190
|
+
if (next[j]) {
|
|
191
|
+
any = true;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (!any)
|
|
196
|
+
return false;
|
|
197
|
+
reach = next;
|
|
198
|
+
}
|
|
199
|
+
// The pattern may end at the path's end or at a directory boundary (everything beneath matches).
|
|
200
|
+
for (let j = 0; j <= n; j++)
|
|
201
|
+
if (reach[j] && (j === n || path[j] === "/"))
|
|
202
|
+
return true;
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
function normalize(pattern) {
|
|
206
|
+
let p = pattern.replace(/\\/g, "/");
|
|
207
|
+
if (p.startsWith("./"))
|
|
208
|
+
p = p.slice(2);
|
|
209
|
+
if (p.endsWith("/"))
|
|
210
|
+
p = p.slice(0, -1);
|
|
211
|
+
return p;
|
|
212
|
+
}
|
|
213
|
+
const WILD = /[*?[\]{}]/;
|
|
214
|
+
export function compileGlobs(patterns) {
|
|
215
|
+
const names = new Set(); // bare literal names: `node_modules`
|
|
216
|
+
const suffixes = []; // bare `*.ext` patterns
|
|
217
|
+
const general = [];
|
|
218
|
+
for (const raw of patterns) {
|
|
219
|
+
let alternatives;
|
|
220
|
+
try {
|
|
221
|
+
alternatives = expandBraces(normalize(raw));
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
alternatives = [normalize(raw)]; // validation rejects these; never throw while matching
|
|
225
|
+
}
|
|
226
|
+
for (const p of alternatives) {
|
|
227
|
+
if (!p.includes("/")) {
|
|
228
|
+
if (!WILD.test(p)) {
|
|
229
|
+
names.add(p);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (p.startsWith("*") && !p.startsWith("**") && !WILD.test(p.slice(1))) {
|
|
233
|
+
suffixes.push(p.slice(1));
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
general.push(tokenize(`**/${p}`));
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
general.push(tokenize(p.replace(/^\//, "")));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
let work = 0;
|
|
244
|
+
return (path) => {
|
|
245
|
+
if (names.size > 0 || suffixes.length > 0) {
|
|
246
|
+
for (const seg of path.split("/")) {
|
|
247
|
+
if (names.has(seg))
|
|
248
|
+
return true;
|
|
249
|
+
for (const s of suffixes)
|
|
250
|
+
if (seg.length >= s.length && seg.endsWith(s))
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const toks of general) {
|
|
255
|
+
// Even linear-time matching can be made slow by thousands of patterns: bound the total work.
|
|
256
|
+
work += toks.length * (path.length + 1);
|
|
257
|
+
if (work > WORK_BUDGET) {
|
|
258
|
+
throw new UsageError("The exclude patterns are too expensive to evaluate on this repository; use fewer or simpler patterns");
|
|
259
|
+
}
|
|
260
|
+
if (matchTokens(toks, path))
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
return false;
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/** A path or pattern is unsafe if it is absolute or climbs out of the repo. */
|
|
267
|
+
export function isSafeGlob(pattern) {
|
|
268
|
+
const p = pattern.replace(/\\/g, "/");
|
|
269
|
+
if (p.startsWith("/") || /^[a-zA-Z]:/.test(p))
|
|
270
|
+
return false;
|
|
271
|
+
return !p.split("/").includes("..");
|
|
272
|
+
}
|
|
273
|
+
/** Why a user-supplied glob must be rejected, or null if it is fine. */
|
|
274
|
+
export function globProblem(pattern) {
|
|
275
|
+
if (!isSafeGlob(pattern))
|
|
276
|
+
return "must be relative and must not contain '..'";
|
|
277
|
+
if (pattern.length > MAX_GLOB_LENGTH)
|
|
278
|
+
return `must be at most ${MAX_GLOB_LENGTH} characters`;
|
|
279
|
+
const collapsed = pattern.replace(/(?:\*\*\/){2,}/g, "**/");
|
|
280
|
+
if ((collapsed.match(/\*\*/g) ?? []).length > MAX_DOUBLE_STAR) {
|
|
281
|
+
return `must use '**' at most ${MAX_DOUBLE_STAR} times`;
|
|
282
|
+
}
|
|
283
|
+
if ((collapsed.match(/[*?{[]/g) ?? []).length > MAX_WILDCARDS) {
|
|
284
|
+
return `must use at most ${MAX_WILDCARDS} wildcard characters`;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
expandBraces(normalize(pattern));
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return `must not expand to more than ${MAX_BRACE_EXPANSIONS} brace alternatives`;
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
/** How many concrete patterns the given globs expand to once `{a,b}` groups are multiplied out. */
|
|
295
|
+
export function totalAlternatives(patterns) {
|
|
296
|
+
let n = 0;
|
|
297
|
+
for (const p of patterns) {
|
|
298
|
+
try {
|
|
299
|
+
n += expandBraces(normalize(p)).length;
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
n += MAX_TOTAL_ALTERNATIVES + 1;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return n;
|
|
306
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
const MAX_FILES = 2000;
|
|
5
|
+
/**
|
|
6
|
+
* Content hash of repo-relative files or directories (recursive, sorted, links skipped). Used for cache
|
|
7
|
+
* keys: a result may only be reused if the configuration that produced it is byte-identical, including
|
|
8
|
+
* rule files that live in the repository.
|
|
9
|
+
*/
|
|
10
|
+
export function hashPaths(root, rels) {
|
|
11
|
+
const h = createHash("sha256");
|
|
12
|
+
let count = 0;
|
|
13
|
+
const visit = (rel) => {
|
|
14
|
+
if (count > MAX_FILES)
|
|
15
|
+
return;
|
|
16
|
+
let st;
|
|
17
|
+
try {
|
|
18
|
+
st = lstatSync(join(root, rel));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
h.update(`${rel}\0missing\0`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (st.isSymbolicLink()) {
|
|
25
|
+
h.update(`${rel}\0link\0`);
|
|
26
|
+
}
|
|
27
|
+
else if (st.isDirectory()) {
|
|
28
|
+
for (const name of readdirSync(join(root, rel)).sort())
|
|
29
|
+
visit(`${rel}/${name}`);
|
|
30
|
+
}
|
|
31
|
+
else if (st.isFile()) {
|
|
32
|
+
count += 1;
|
|
33
|
+
h.update(`${rel}\0`);
|
|
34
|
+
h.update(createHash("sha256")
|
|
35
|
+
.update(readFileSync(join(root, rel)))
|
|
36
|
+
.digest());
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
for (const r of [...rels].sort())
|
|
40
|
+
visit(r);
|
|
41
|
+
if (count > MAX_FILES)
|
|
42
|
+
h.update("truncated");
|
|
43
|
+
return h.digest("hex");
|
|
44
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const ORDER = { silent: 0, error: 1, warn: 2, info: 3, debug: 4 };
|
|
2
|
+
export function isLogLevel(v) {
|
|
3
|
+
return v in ORDER;
|
|
4
|
+
}
|
|
5
|
+
/** Structured logger. Always writes to the given sink (stderr by default), never stdout. */
|
|
6
|
+
export function createLogger(level, format, write = (l) => process.stderr.write(`${l}\n`)) {
|
|
7
|
+
const emit = (lvl, msg, fields) => {
|
|
8
|
+
if (ORDER[lvl] > ORDER[level])
|
|
9
|
+
return;
|
|
10
|
+
if (format === "json") {
|
|
11
|
+
write(JSON.stringify({ ts: new Date().toISOString(), level: lvl, msg, ...fields }));
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
const extra = fields
|
|
15
|
+
? ` ${Object.entries(fields)
|
|
16
|
+
.map(([k, v]) => `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`)
|
|
17
|
+
.join(" ")}`
|
|
18
|
+
: "";
|
|
19
|
+
write(`[${lvl}] ${msg}${extra}`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
error: (m, f) => emit("error", m, f),
|
|
24
|
+
warn: (m, f) => emit("warn", m, f),
|
|
25
|
+
info: (m, f) => emit("info", m, f),
|
|
26
|
+
debug: (m, f) => emit("debug", m, f),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export const silentLogger = createLogger("silent", "text", () => { });
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, join } from "node:path";
|
|
4
|
+
import { severityRank } from "../types.js";
|
|
5
|
+
export function hash16(...parts) {
|
|
6
|
+
return createHash("sha256").update(parts.join("\u0000")).digest("hex").slice(0, 16);
|
|
7
|
+
}
|
|
8
|
+
/** Collapse whitespace so reformatting does not change a fingerprint. */
|
|
9
|
+
const squash = (s) => s.replace(/\s+/g, " ").trim();
|
|
10
|
+
class LineReader {
|
|
11
|
+
cache = new Map();
|
|
12
|
+
root;
|
|
13
|
+
constructor(root) {
|
|
14
|
+
this.root = root;
|
|
15
|
+
}
|
|
16
|
+
line(file, n) {
|
|
17
|
+
let lines = this.cache.get(file);
|
|
18
|
+
if (lines === undefined) {
|
|
19
|
+
try {
|
|
20
|
+
lines = readFileSync(join(this.root, file), "utf8").split("\n");
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
lines = null;
|
|
24
|
+
}
|
|
25
|
+
this.cache.set(file, lines);
|
|
26
|
+
}
|
|
27
|
+
return lines?.[n - 1] ?? null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Derive stable fingerprints (engine + rule + path + normalized source line, or the
|
|
32
|
+
* adapter's explicit key) so a finding keeps its identity when lines above it move.
|
|
33
|
+
* The source line is only hashed, never emitted, so secrets cannot leak through it.
|
|
34
|
+
*/
|
|
35
|
+
export function toFindings(raws, root) {
|
|
36
|
+
const reader = new LineReader(root);
|
|
37
|
+
const exact = new Set();
|
|
38
|
+
const occurrences = new Map();
|
|
39
|
+
const out = [];
|
|
40
|
+
// Deterministic order, so the occurrence index of identical lines is stable between runs.
|
|
41
|
+
const ordered = [...raws].sort((a, b) => a.file.localeCompare(b.file) || (a.line ?? 0) - (b.line ?? 0));
|
|
42
|
+
for (const r of ordered) {
|
|
43
|
+
const { key, ...rest } = r;
|
|
44
|
+
// Last line of defense: a path outside the repository must never reach a report verbatim.
|
|
45
|
+
if (isAbsolute(rest.file) || rest.file.split("/").includes("..")) {
|
|
46
|
+
rest.file = `<outside-repository>/${rest.file.split(/[\\/]/).pop() ?? "unknown"}`;
|
|
47
|
+
}
|
|
48
|
+
const basis = key ??
|
|
49
|
+
(r.line !== null ? squash(reader.line(r.file, r.line) ?? r.message) : squash(r.message));
|
|
50
|
+
// The same engine reporting the same thing twice is one finding.
|
|
51
|
+
const exactKey = `${r.engine}|${r.ruleId}|${r.file}|${r.line ?? ""}|${basis}`;
|
|
52
|
+
if (exact.has(exactKey))
|
|
53
|
+
continue;
|
|
54
|
+
exact.add(exactKey);
|
|
55
|
+
// Identical lines in one file are different findings: the n-th copy gets its own identity, so a
|
|
56
|
+
// baselined `eval(x)` cannot hide a newly added copy of the same line. The first keeps the plain hash.
|
|
57
|
+
const baseKey = `${r.engine}|${r.ruleId}|${r.file}|${basis}`;
|
|
58
|
+
const index = occurrences.get(baseKey) ?? 0;
|
|
59
|
+
occurrences.set(baseKey, index + 1);
|
|
60
|
+
const fingerprint = index === 0
|
|
61
|
+
? hash16(r.engine, r.ruleId, r.file, basis)
|
|
62
|
+
: hash16(r.engine, r.ruleId, r.file, basis, String(index));
|
|
63
|
+
out.push({ fingerprint, ...rest, alsoReportedBy: [] });
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
/** Engines whose secrets findings win when several report the same line (dedicated scanner first). */
|
|
68
|
+
const SECRETS_PRIORITY = ["gitleaks"];
|
|
69
|
+
/**
|
|
70
|
+
* The same hardcoded secret is often reported by more than one engine (gitleaks and semgrep's
|
|
71
|
+
* secrets rules). Report it once, credit the others in `alsoReportedBy`, and keep the highest severity.
|
|
72
|
+
*/
|
|
73
|
+
export function mergeDuplicateSecrets(findings) {
|
|
74
|
+
const groups = new Map();
|
|
75
|
+
const rest = [];
|
|
76
|
+
for (const f of findings) {
|
|
77
|
+
if (f.category !== "secrets" || f.line === null) {
|
|
78
|
+
rest.push(f);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const k = `${f.file}:${f.line}`;
|
|
82
|
+
const g = groups.get(k);
|
|
83
|
+
if (g)
|
|
84
|
+
g.push(f);
|
|
85
|
+
else
|
|
86
|
+
groups.set(k, [f]);
|
|
87
|
+
}
|
|
88
|
+
for (const g of groups.values()) {
|
|
89
|
+
const rank = (f) => {
|
|
90
|
+
const i = SECRETS_PRIORITY.indexOf(f.engine);
|
|
91
|
+
return i === -1 ? SECRETS_PRIORITY.length : i;
|
|
92
|
+
};
|
|
93
|
+
const sorted = [...g].sort((a, b) => rank(a) - rank(b));
|
|
94
|
+
const keep = { ...sorted[0] };
|
|
95
|
+
const others = sorted.slice(1).filter((o) => o.engine !== keep.engine);
|
|
96
|
+
keep.alsoReportedBy = [...new Set([...keep.alsoReportedBy, ...others.map((o) => o.engine)])];
|
|
97
|
+
for (const o of others)
|
|
98
|
+
if (severityRank(o.severity) > severityRank(keep.severity))
|
|
99
|
+
keep.severity = o.severity;
|
|
100
|
+
rest.push(keep, ...sorted.slice(1).filter((o) => o.engine === keep.engine));
|
|
101
|
+
}
|
|
102
|
+
return rest;
|
|
103
|
+
}
|
|
104
|
+
export function sortFindings(findings) {
|
|
105
|
+
return [...findings].sort((a, b) => severityRank(b.severity) - severityRank(a.severity) ||
|
|
106
|
+
a.file.localeCompare(b.file) ||
|
|
107
|
+
(a.line ?? 0) - (b.line ?? 0) ||
|
|
108
|
+
a.ruleId.localeCompare(b.ruleId));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Drop SAST findings on lines the change did not touch. Findings with no line, in files
|
|
112
|
+
* without a diff entry (untracked = whole file), or outside SAST are always kept.
|
|
113
|
+
* Returns the number suppressed so reports can say so instead of hiding it.
|
|
114
|
+
*/
|
|
115
|
+
export function filterToChangedLines(findings, target, enabled) {
|
|
116
|
+
if (!enabled || target.mode === "all")
|
|
117
|
+
return { kept: findings, suppressed: 0 };
|
|
118
|
+
const kept = [];
|
|
119
|
+
let suppressed = 0;
|
|
120
|
+
for (const f of findings) {
|
|
121
|
+
const ranges = target.changedLines.get(f.file);
|
|
122
|
+
if (f.category !== "sast" || f.line === null || ranges === undefined) {
|
|
123
|
+
kept.push(f);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const end = Math.max(f.line, f.endLine ?? f.line); // tolerate adapters reporting endLine < line
|
|
127
|
+
if (ranges.some((r) => f.line !== null && f.line <= r.end && end >= r.start))
|
|
128
|
+
kept.push(f);
|
|
129
|
+
else
|
|
130
|
+
suppressed += 1;
|
|
131
|
+
}
|
|
132
|
+
return { kept, suppressed };
|
|
133
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { CATEGORIES } from "../types.js";
|
|
2
|
+
import { UsageError } from "./errors.js";
|
|
3
|
+
/** Expand selectors (category or engine names) into the categories to cover, plus pinned engines. */
|
|
4
|
+
export function resolveSelectors(selectors, engines) {
|
|
5
|
+
const cats = new Set();
|
|
6
|
+
const pinned = new Set();
|
|
7
|
+
for (const s of selectors) {
|
|
8
|
+
if (CATEGORIES.includes(s)) {
|
|
9
|
+
cats.add(s);
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
const e = engines.find((x) => x.name === s);
|
|
13
|
+
if (!e) {
|
|
14
|
+
const known = [...CATEGORIES, ...engines.map((x) => x.name)].join(", ");
|
|
15
|
+
throw new UsageError(`Unknown engine or category '${s}'`, [`Known: ${known}`]);
|
|
16
|
+
}
|
|
17
|
+
cats.add(e.category);
|
|
18
|
+
pinned.add(e.name);
|
|
19
|
+
}
|
|
20
|
+
return { categories: CATEGORIES.filter((c) => cats.has(c)), pinned };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* For each requested category, walk its engines by priority and pick the first that has work
|
|
24
|
+
* to do and is installed. Nothing is dropped silently: every category yields exactly one
|
|
25
|
+
* entry that says run, skip (with why), or unavailable (with how to fix it).
|
|
26
|
+
*/
|
|
27
|
+
export async function plan(engines, selectors, target, profile) {
|
|
28
|
+
const { categories, pinned } = resolveSelectors(selectors, engines);
|
|
29
|
+
const entries = [];
|
|
30
|
+
for (const cat of categories) {
|
|
31
|
+
let candidates = engines
|
|
32
|
+
.filter((e) => e.category === cat)
|
|
33
|
+
.sort((a, b) => a.priority - b.priority);
|
|
34
|
+
const pins = candidates.filter((e) => pinned.has(e.name));
|
|
35
|
+
if (pins.length > 0)
|
|
36
|
+
candidates = pins;
|
|
37
|
+
const skips = [];
|
|
38
|
+
const missing = [];
|
|
39
|
+
let chosen = null;
|
|
40
|
+
for (const engine of candidates) {
|
|
41
|
+
const sel = engine.select(target, profile);
|
|
42
|
+
if (sel.files === null) {
|
|
43
|
+
skips.push(sel.skipReason ?? "nothing to scan");
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Locate only: reading versions spawns every tool (semgrep alone is ~0.5 s) and would delay all engines.
|
|
47
|
+
const detection = await engine.detect({ withVersion: false });
|
|
48
|
+
if (detection.available) {
|
|
49
|
+
chosen = {
|
|
50
|
+
name: engine.name,
|
|
51
|
+
category: cat,
|
|
52
|
+
engine,
|
|
53
|
+
action: "run",
|
|
54
|
+
detection,
|
|
55
|
+
files: sel.files,
|
|
56
|
+
reason: null,
|
|
57
|
+
};
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
missing.push({ engine, detection, files: sel.files });
|
|
61
|
+
}
|
|
62
|
+
if (chosen) {
|
|
63
|
+
entries.push(chosen);
|
|
64
|
+
}
|
|
65
|
+
else if (missing.length > 0) {
|
|
66
|
+
const first = missing[0];
|
|
67
|
+
const hints = missing
|
|
68
|
+
.map((m) => m.detection.reason)
|
|
69
|
+
.filter(Boolean)
|
|
70
|
+
.join(" | ");
|
|
71
|
+
entries.push({
|
|
72
|
+
name: first.engine.name,
|
|
73
|
+
category: cat,
|
|
74
|
+
engine: first.engine,
|
|
75
|
+
action: "unavailable",
|
|
76
|
+
detection: first.detection,
|
|
77
|
+
files: first.files,
|
|
78
|
+
reason: hints,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else if (candidates.length === 0) {
|
|
82
|
+
entries.push({
|
|
83
|
+
name: cat,
|
|
84
|
+
category: cat,
|
|
85
|
+
engine: null,
|
|
86
|
+
action: "skip",
|
|
87
|
+
detection: null,
|
|
88
|
+
files: [],
|
|
89
|
+
reason: `no engine implemented for '${cat}'`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const first = candidates[0];
|
|
94
|
+
entries.push({
|
|
95
|
+
name: first.name,
|
|
96
|
+
category: cat,
|
|
97
|
+
engine: first,
|
|
98
|
+
action: "skip",
|
|
99
|
+
detection: null,
|
|
100
|
+
files: [],
|
|
101
|
+
reason: skips[0] ?? "nothing to scan",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return entries;
|
|
106
|
+
}
|