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,319 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, rmdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { loadConfig } from "../core/config.js";
|
|
4
|
+
import { UsageError } from "../core/errors.js";
|
|
5
|
+
import { exec } from "../core/exec.js";
|
|
6
|
+
import { isSafeGlob } from "../core/glob.js";
|
|
7
|
+
import { isPlainFileInside } from "../core/safefs.js";
|
|
8
|
+
import { resolveTarget } from "../core/targets.js";
|
|
9
|
+
import { ENGINES } from "../engines/registry.js";
|
|
10
|
+
import { errorDetail } from "../engines/util.js";
|
|
11
|
+
import { TOOL_NAME, VERSION } from "../version.js";
|
|
12
|
+
import { isTestFile } from "./impact.js";
|
|
13
|
+
const JS_RE = /\.[cm]?[jt]sx?$/;
|
|
14
|
+
const OTHER_SRC_RE = /\.(?:py|go|rs|java|kt|cs|rb|php|swift)$/;
|
|
15
|
+
const STRYKER_BIN = ["node_modules", "@stryker-mutator", "core", "bin", "stryker.js"];
|
|
16
|
+
const CONFIG_NAMES = [
|
|
17
|
+
"stryker.config.json",
|
|
18
|
+
"stryker.config.js",
|
|
19
|
+
"stryker.config.mjs",
|
|
20
|
+
"stryker.config.cjs",
|
|
21
|
+
"stryker.conf.json",
|
|
22
|
+
"stryker.conf.js",
|
|
23
|
+
"stryker.conf.mjs",
|
|
24
|
+
"stryker.conf.cjs",
|
|
25
|
+
".stryker.conf.json",
|
|
26
|
+
];
|
|
27
|
+
const SURVIVOR_CAP = 25;
|
|
28
|
+
const score = (detected, valid) => valid === 0 ? null : Math.round((detected / valid) * 1000) / 10;
|
|
29
|
+
function summarize(file, mutants) {
|
|
30
|
+
const c = { killed: 0, survived: 0, noCoverage: 0, timeout: 0 };
|
|
31
|
+
const survivors = [];
|
|
32
|
+
for (const m of mutants) {
|
|
33
|
+
if (m.status === "Killed")
|
|
34
|
+
c.killed += 1;
|
|
35
|
+
else if (m.status === "Timeout")
|
|
36
|
+
c.timeout += 1;
|
|
37
|
+
else if (m.status === "Survived" || m.status === "NoCoverage") {
|
|
38
|
+
if (m.status === "Survived")
|
|
39
|
+
c.survived += 1;
|
|
40
|
+
else
|
|
41
|
+
c.noCoverage += 1;
|
|
42
|
+
survivors.push({
|
|
43
|
+
line: m.location.start.line,
|
|
44
|
+
mutator: m.mutatorName,
|
|
45
|
+
replacement: m.replacement ?? null,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
survivors.sort((a, b) => a.line - b.line);
|
|
50
|
+
const valid = c.killed + c.timeout + c.survived + c.noCoverage;
|
|
51
|
+
return {
|
|
52
|
+
file,
|
|
53
|
+
total: mutants.length,
|
|
54
|
+
...c,
|
|
55
|
+
score: score(c.killed + c.timeout, valid),
|
|
56
|
+
survivors,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function totalsOf(files) {
|
|
60
|
+
const t = { total: 0, killed: 0, survived: 0, noCoverage: 0, timeout: 0 };
|
|
61
|
+
for (const f of files) {
|
|
62
|
+
t.total += f.total;
|
|
63
|
+
t.killed += f.killed;
|
|
64
|
+
t.survived += f.survived;
|
|
65
|
+
t.noCoverage += f.noCoverage;
|
|
66
|
+
t.timeout += f.timeout;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
...t,
|
|
70
|
+
score: score(t.killed + t.timeout, t.killed + t.timeout + t.survived + t.noCoverage),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function pruneEmptyDirs(root, rels) {
|
|
74
|
+
for (const rel of rels) {
|
|
75
|
+
const dir = join(root, rel);
|
|
76
|
+
try {
|
|
77
|
+
if (existsSync(dir) && readdirSync(dir).length === 0)
|
|
78
|
+
rmdirSync(dir);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
/* leave it */
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export async function runMutate(opts) {
|
|
86
|
+
const started = performance.now();
|
|
87
|
+
const probe = await resolveTarget({ cwd: opts.cwd, mode: "all", ref: null, exclude: [] }).catch(() => null);
|
|
88
|
+
const root = probe?.root ?? opts.cwd;
|
|
89
|
+
const config = loadConfig(root, ENGINES.map((e) => e.name));
|
|
90
|
+
// 1. What to mutate: explicit files, or the changed files with their changed line ranges.
|
|
91
|
+
let candidates;
|
|
92
|
+
let changedLines = new Map();
|
|
93
|
+
let targetInfo;
|
|
94
|
+
if (opts.files.length > 0) {
|
|
95
|
+
for (const f of opts.files) {
|
|
96
|
+
if (!isSafeGlob(f) || !isPlainFileInside(root, f))
|
|
97
|
+
throw new UsageError(`Cannot mutate '${f}': not a regular file inside the repository`);
|
|
98
|
+
}
|
|
99
|
+
candidates = opts.files.map((f) => f.replace(/\\/g, "/").replace(/^\.\//, ""));
|
|
100
|
+
targetInfo = { mode: "files", ref: null };
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const t = await resolveTarget({
|
|
104
|
+
cwd: opts.cwd,
|
|
105
|
+
mode: opts.mode ?? "changed",
|
|
106
|
+
ref: opts.ref,
|
|
107
|
+
exclude: config.exclude,
|
|
108
|
+
});
|
|
109
|
+
candidates = t.files;
|
|
110
|
+
changedLines = t.changedLines;
|
|
111
|
+
targetInfo = { mode: t.mode, ref: t.ref };
|
|
112
|
+
}
|
|
113
|
+
const skippedFiles = [];
|
|
114
|
+
const toMutate = [];
|
|
115
|
+
for (const f of candidates) {
|
|
116
|
+
if (isTestFile(f) || f.endsWith(".d.ts"))
|
|
117
|
+
continue;
|
|
118
|
+
if (JS_RE.test(f)) {
|
|
119
|
+
if (f.includes(","))
|
|
120
|
+
skippedFiles.push({
|
|
121
|
+
file: f,
|
|
122
|
+
reason: "file name contains a comma, which Stryker cannot select",
|
|
123
|
+
});
|
|
124
|
+
else
|
|
125
|
+
toMutate.push(f);
|
|
126
|
+
}
|
|
127
|
+
else if (OTHER_SRC_RE.test(f)) {
|
|
128
|
+
skippedFiles.push({
|
|
129
|
+
file: f,
|
|
130
|
+
reason: "no mutation engine for this language (supported: JavaScript/TypeScript via StrykerJS)",
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const base = (over) => ({
|
|
135
|
+
schemaVersion: 1,
|
|
136
|
+
tool: { name: TOOL_NAME, version: VERSION },
|
|
137
|
+
engine: "stryker",
|
|
138
|
+
engineVersion: null,
|
|
139
|
+
status: "ok",
|
|
140
|
+
reason: null,
|
|
141
|
+
target: targetInfo,
|
|
142
|
+
files: [],
|
|
143
|
+
totals: totalsOf([]),
|
|
144
|
+
skippedFiles,
|
|
145
|
+
notes: [],
|
|
146
|
+
durationMs: 0,
|
|
147
|
+
minScore: opts.minScore,
|
|
148
|
+
exitCode: 0,
|
|
149
|
+
...over,
|
|
150
|
+
});
|
|
151
|
+
const finish = (r) => ({
|
|
152
|
+
...r,
|
|
153
|
+
durationMs: Math.round(performance.now() - started),
|
|
154
|
+
});
|
|
155
|
+
if (toMutate.length === 0) {
|
|
156
|
+
return finish(base({
|
|
157
|
+
status: "skipped",
|
|
158
|
+
reason: "no JavaScript/TypeScript source files to mutate in scope",
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
// 2. Is Stryker installed in the project? (It must be a project dependency: it runs the project's own tests.)
|
|
162
|
+
const bin = join(root, ...STRYKER_BIN);
|
|
163
|
+
if (!existsSync(bin)) {
|
|
164
|
+
return finish(base({
|
|
165
|
+
status: "unavailable",
|
|
166
|
+
reason: "StrykerJS is not installed in this project. Install: npm install --save-dev @stryker-mutator/core",
|
|
167
|
+
exitCode: 3,
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
let engineVersion = null;
|
|
171
|
+
try {
|
|
172
|
+
engineVersion = JSON.parse(readFileSync(join(root, "node_modules/@stryker-mutator/core/package.json"), "utf8")).version;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
/* version is informational */
|
|
176
|
+
}
|
|
177
|
+
// 3. How to run the tests: the project's Stryker config, else its `npm test` script.
|
|
178
|
+
const hasConfig = CONFIG_NAMES.some((n) => existsSync(join(root, n)));
|
|
179
|
+
const args = [
|
|
180
|
+
bin,
|
|
181
|
+
"run",
|
|
182
|
+
"--reporters",
|
|
183
|
+
"json",
|
|
184
|
+
// `--mutate=<list>` as one argument: a file named like an option can never be read as a flag.
|
|
185
|
+
`--mutate=${toMutate
|
|
186
|
+
.map((f) => {
|
|
187
|
+
const ranges = changedLines.get(f);
|
|
188
|
+
return ranges && ranges.length > 0
|
|
189
|
+
? ranges.map((r) => `${f}:${r.start}-${r.end}`).join(",")
|
|
190
|
+
: f;
|
|
191
|
+
})
|
|
192
|
+
.join(",")}`,
|
|
193
|
+
];
|
|
194
|
+
if (!hasConfig) {
|
|
195
|
+
let testScript = "";
|
|
196
|
+
try {
|
|
197
|
+
testScript = (JSON.parse(readFileSync(join(root, "package.json"), "utf8")).scripts?.test ?? "").trim();
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
/* handled below */
|
|
201
|
+
}
|
|
202
|
+
if (!testScript || /no test specified/.test(testScript)) {
|
|
203
|
+
return finish(base({
|
|
204
|
+
engineVersion,
|
|
205
|
+
status: "error",
|
|
206
|
+
reason: "No Stryker config and no usable `test` script in package.json; add one or create a stryker.config.json",
|
|
207
|
+
exitCode: 3,
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
args.push("--testRunner", "command", "--commandRunner.command", "npm test", "--coverageAnalysis", "off");
|
|
211
|
+
}
|
|
212
|
+
const reportDir = join(root, "reports", "mutation");
|
|
213
|
+
const reportFile = join(reportDir, "mutation.json");
|
|
214
|
+
const hadReport = existsSync(reportFile);
|
|
215
|
+
const hadReports = existsSync(join(root, "reports"));
|
|
216
|
+
const hadTmp = existsSync(join(root, ".stryker-tmp"));
|
|
217
|
+
opts.log.info("running stryker", { files: toMutate.length });
|
|
218
|
+
const r = await exec(process.execPath, args, {
|
|
219
|
+
cwd: root,
|
|
220
|
+
timeoutMs: opts.timeoutMs,
|
|
221
|
+
env: { CI: "1" },
|
|
222
|
+
});
|
|
223
|
+
let report = null;
|
|
224
|
+
try {
|
|
225
|
+
if (existsSync(reportFile))
|
|
226
|
+
report = JSON.parse(readFileSync(reportFile, "utf8"));
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
report = null;
|
|
230
|
+
}
|
|
231
|
+
// Leave the project as we found it.
|
|
232
|
+
if (!hadReport)
|
|
233
|
+
rmSync(reportFile, { force: true });
|
|
234
|
+
if (!hadTmp)
|
|
235
|
+
rmSync(join(root, ".stryker-tmp"), { recursive: true, force: true });
|
|
236
|
+
if (!hadReports)
|
|
237
|
+
pruneEmptyDirs(root, ["reports/mutation", "reports"]);
|
|
238
|
+
if (r.spawnError)
|
|
239
|
+
return finish(base({
|
|
240
|
+
engineVersion,
|
|
241
|
+
status: "error",
|
|
242
|
+
reason: `cannot start node: ${r.spawnError.message}`,
|
|
243
|
+
exitCode: 3,
|
|
244
|
+
}));
|
|
245
|
+
if (r.timedOut) {
|
|
246
|
+
return finish(base({
|
|
247
|
+
engineVersion,
|
|
248
|
+
status: "timeout",
|
|
249
|
+
reason: `mutation run exceeded ${Math.round(opts.timeoutMs / 1000)}s and was killed; no partial results are available. Narrow the scope or raise --timeout`,
|
|
250
|
+
exitCode: 3,
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
if (r.code !== 0) {
|
|
254
|
+
// Stryker logs to stdout and uncaught errors to stderr; strip color codes and pick the real error line.
|
|
255
|
+
const strip = (s) => s.replace(/\u001b\[[0-9;]*m/g, "");
|
|
256
|
+
const fromErr = errorDetail(strip(r.stderr));
|
|
257
|
+
const detail = fromErr !== "no error output"
|
|
258
|
+
? fromErr
|
|
259
|
+
: errorDetail(strip(r.stdout
|
|
260
|
+
.split("\n")
|
|
261
|
+
.filter((l) => /error/i.test(l))
|
|
262
|
+
.join("\n")));
|
|
263
|
+
return finish(base({
|
|
264
|
+
engineVersion,
|
|
265
|
+
status: "error",
|
|
266
|
+
reason: `stryker exited with code ${r.code}${detail ? ` (${detail})` : ""}`,
|
|
267
|
+
exitCode: 3,
|
|
268
|
+
}));
|
|
269
|
+
}
|
|
270
|
+
const files = report
|
|
271
|
+
? Object.entries(report.files).map(([f, v]) => summarize(f.replace(/\\/g, "/"), v.mutants))
|
|
272
|
+
: [];
|
|
273
|
+
const totals = totalsOf(files);
|
|
274
|
+
const notes = [];
|
|
275
|
+
if (files.length === 0)
|
|
276
|
+
notes.push("Stryker generated no mutants for the selected code (nothing mutable in the changed lines).");
|
|
277
|
+
if (totals.score === 0 && totals.total > 0) {
|
|
278
|
+
notes.push("Score is 0%: no mutant was detected. Check that your test command really runs tests (a command that runs zero tests still exits 0).");
|
|
279
|
+
}
|
|
280
|
+
if (changedLines.size > 0 && opts.files.length === 0)
|
|
281
|
+
notes.push("Mutants were limited to the changed lines of each file.");
|
|
282
|
+
if (files.some((f) => f.survivors.length > SURVIVOR_CAP))
|
|
283
|
+
notes.push(`Only the first ${SURVIVOR_CAP} survivors per file are listed; totals are complete.`);
|
|
284
|
+
for (const f of files)
|
|
285
|
+
f.survivors = f.survivors.slice(0, SURVIVOR_CAP);
|
|
286
|
+
const below = opts.minScore !== null && totals.score !== null && totals.score < opts.minScore;
|
|
287
|
+
return finish(base({ engineVersion, files, totals, notes, exitCode: below ? 1 : 0 }));
|
|
288
|
+
}
|
|
289
|
+
export function renderMutation(r) {
|
|
290
|
+
const L = [
|
|
291
|
+
`${r.tool.name} ${r.tool.version} | mutate | ${r.engine}${r.engineVersion ? ` ${r.engineVersion}` : ""} | ${(r.durationMs / 1000).toFixed(1)}s`,
|
|
292
|
+
"",
|
|
293
|
+
];
|
|
294
|
+
if (r.status !== "ok")
|
|
295
|
+
L.push(`Status: ${r.status.toUpperCase()}${r.reason ? `: ${r.reason}` : ""}`, "");
|
|
296
|
+
for (const f of r.files) {
|
|
297
|
+
L.push(`${f.file}: score ${f.score === null ? "n/a" : `${f.score}%`} (${f.killed + f.timeout} detected, ${f.survived} survived, ${f.noCoverage} uncovered, ${f.total} total)`);
|
|
298
|
+
for (const s of f.survivors)
|
|
299
|
+
L.push(` survived line ${s.line} ${s.mutator}${s.replacement ? ` -> ${s.replacement.slice(0, 60)}` : ""}`);
|
|
300
|
+
}
|
|
301
|
+
if (r.status === "ok") {
|
|
302
|
+
L.push("", `Mutation score: ${r.totals.score === null ? "n/a" : `${r.totals.score}%`} (${r.totals.survived + r.totals.noCoverage} undetected of ${r.totals.total} mutants)`);
|
|
303
|
+
if (r.totals.survived + r.totals.noCoverage > 0)
|
|
304
|
+
L.push("A survivor is a behavior change your tests did not notice: add or strengthen a test for that line.");
|
|
305
|
+
}
|
|
306
|
+
for (const s of r.skippedFiles)
|
|
307
|
+
L.push(`Skipped ${s.file}: ${s.reason}`);
|
|
308
|
+
for (const n of r.notes)
|
|
309
|
+
L.push(`Note: ${n}`);
|
|
310
|
+
if (r.exitCode === 1)
|
|
311
|
+
L.push(`Result: FAIL (exit 1): mutation score below --min-score ${r.minScore}.`);
|
|
312
|
+
else if (r.exitCode === 3)
|
|
313
|
+
L.push("Result: INCOMPLETE (exit 3): mutation testing did not run to completion.");
|
|
314
|
+
else if (r.status === "skipped")
|
|
315
|
+
L.push("Result: SKIPPED (exit 0): nothing to mutate.");
|
|
316
|
+
else
|
|
317
|
+
L.push("Result: OK (exit 0)");
|
|
318
|
+
return `${L.join("\n")}\n`;
|
|
319
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { applyBaseline, loadBaseline } from "../core/baseline.js";
|
|
3
|
+
import { ResultCache } from "../core/cache.js";
|
|
4
|
+
import { CONFIG_FILE, describeNonDefaults, loadConfig } from "../core/config.js";
|
|
5
|
+
import { UsageError } from "../core/errors.js";
|
|
6
|
+
import { filterToChangedLines, mergeDuplicateSecrets, sortFindings, toFindings, } from "../core/normalize.js";
|
|
7
|
+
import { plan } from "../core/planner.js";
|
|
8
|
+
import { runPlan } from "../core/runner.js";
|
|
9
|
+
import { lexists } from "../core/safefs.js";
|
|
10
|
+
import { countInlineSuppressions } from "../core/suppressions.js";
|
|
11
|
+
import { findGitRoot, resolveTarget } from "../core/targets.js";
|
|
12
|
+
import { ENGINES } from "../engines/registry.js";
|
|
13
|
+
import { SEVERITIES, severityRank, } from "../types.js";
|
|
14
|
+
import { TOOL_NAME, VERSION } from "../version.js";
|
|
15
|
+
function configDisclosure(root, config, baselineInUse, inlineSuppressions) {
|
|
16
|
+
const file = lexists(join(root, CONFIG_FILE)) ? CONFIG_FILE : null;
|
|
17
|
+
// Engine-native suppressors that no tool flag can switch off are listed in every mode.
|
|
18
|
+
const native = [];
|
|
19
|
+
if (lexists(join(root, ".semgrepignore")))
|
|
20
|
+
native.push(".semgrepignore present (honored by semgrep)");
|
|
21
|
+
if (inlineSuppressions > 0) {
|
|
22
|
+
native.push(`${inlineSuppressions} inline suppression comment(s) in scanned files (gitleaks:allow / nosemgrep)`);
|
|
23
|
+
}
|
|
24
|
+
if (config.repoConfigIgnored)
|
|
25
|
+
return { file, ignored: true, inEffect: native };
|
|
26
|
+
const inEffect = describeNonDefaults(config);
|
|
27
|
+
for (const f of [".gitleaks.toml", ".gitleaksignore"]) {
|
|
28
|
+
if (lexists(join(root, f)))
|
|
29
|
+
inEffect.push(`${f} present`);
|
|
30
|
+
}
|
|
31
|
+
if (baselineInUse)
|
|
32
|
+
inEffect.push("baseline in use");
|
|
33
|
+
inEffect.push(...native);
|
|
34
|
+
return { file, ignored: false, inEffect };
|
|
35
|
+
}
|
|
36
|
+
export const countBySeverity = (findings) => {
|
|
37
|
+
const out = Object.fromEntries(SEVERITIES.map((s) => [s, 0]));
|
|
38
|
+
for (const f of findings)
|
|
39
|
+
out[f.severity] += 1;
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
export async function runScan(opts) {
|
|
43
|
+
const started = performance.now();
|
|
44
|
+
const engines = opts.engineSet ?? ENGINES;
|
|
45
|
+
const root = (await findGitRoot(opts.cwd)) ?? opts.cwd;
|
|
46
|
+
if (opts.ignoreRepoConfig && opts.newOnly) {
|
|
47
|
+
throw new UsageError("--new-only needs the repository's baseline, which --ignore-repo-config discards");
|
|
48
|
+
}
|
|
49
|
+
const config = loadConfig(root, engines.map((e) => e.name), { ignoreRepoConfig: opts.ignoreRepoConfig });
|
|
50
|
+
const profile = config.profiles[opts.profile];
|
|
51
|
+
const selectors = opts.engines ?? profile.engines;
|
|
52
|
+
if (selectors.length === 0)
|
|
53
|
+
throw new UsageError("No engines selected");
|
|
54
|
+
const target = await resolveTarget({
|
|
55
|
+
cwd: opts.cwd,
|
|
56
|
+
mode: opts.mode,
|
|
57
|
+
ref: opts.ref,
|
|
58
|
+
exclude: config.exclude,
|
|
59
|
+
});
|
|
60
|
+
opts.log.info("target resolved", {
|
|
61
|
+
mode: target.mode,
|
|
62
|
+
files: target.files.length,
|
|
63
|
+
excluded: target.excludedCount,
|
|
64
|
+
});
|
|
65
|
+
const entries = await plan(engines, selectors, target, opts.profile);
|
|
66
|
+
const { results, findings: raws } = await runPlan(entries, {
|
|
67
|
+
root: target.root,
|
|
68
|
+
config,
|
|
69
|
+
mode: target.mode,
|
|
70
|
+
timeoutMs: opts.timeoutMs ?? profile.timeoutMs,
|
|
71
|
+
cache: new ResultCache(target.root, opts.cache),
|
|
72
|
+
log: opts.log,
|
|
73
|
+
});
|
|
74
|
+
const allowMissing = opts.allowMissing || config.allowMissingEngines;
|
|
75
|
+
const complete = results.every((r) => r.status === "ok" ||
|
|
76
|
+
r.status === "findings" ||
|
|
77
|
+
r.status === "skipped" ||
|
|
78
|
+
(r.status === "unavailable" && allowMissing));
|
|
79
|
+
const normalized = mergeDuplicateSecrets(toFindings(raws, target.root));
|
|
80
|
+
const { kept: inDiff, suppressed: outsideChangedLines } = filterToChangedLines(normalized, target, config.lineFilter);
|
|
81
|
+
const baseline = opts.newOnly ? loadBaseline(target.root, config.baseline) : null;
|
|
82
|
+
if (opts.newOnly && baseline === null) {
|
|
83
|
+
opts.log.warn("--new-only requested but no baseline exists; reporting every finding", {
|
|
84
|
+
path: config.baseline,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const { kept, suppressed: baselineSuppressed } = applyBaseline(inDiff, baseline);
|
|
88
|
+
const findings = sortFindings(kept);
|
|
89
|
+
const failOn = opts.failOn ?? config.failOn;
|
|
90
|
+
const blocking = findings.some((f) => severityRank(f.severity) >= severityRank(failOn));
|
|
91
|
+
const exitCode = blocking ? 1 : complete ? 0 : 3;
|
|
92
|
+
const report = {
|
|
93
|
+
schemaVersion: 1,
|
|
94
|
+
tool: { name: TOOL_NAME, version: VERSION },
|
|
95
|
+
profile: opts.profile,
|
|
96
|
+
target: {
|
|
97
|
+
mode: target.mode,
|
|
98
|
+
ref: target.ref,
|
|
99
|
+
isGit: target.isGit,
|
|
100
|
+
fileCount: target.files.length,
|
|
101
|
+
excludedCount: target.excludedCount,
|
|
102
|
+
},
|
|
103
|
+
complete,
|
|
104
|
+
durationMs: Math.round(performance.now() - started),
|
|
105
|
+
engines: results,
|
|
106
|
+
summary: {
|
|
107
|
+
total: findings.length,
|
|
108
|
+
bySeverity: countBySeverity(findings),
|
|
109
|
+
failOn,
|
|
110
|
+
newOnly: opts.newOnly,
|
|
111
|
+
suppressed: { outsideChangedLines, baseline: baselineSuppressed },
|
|
112
|
+
},
|
|
113
|
+
findings,
|
|
114
|
+
config: configDisclosure(target.root, config, Boolean(baseline), countInlineSuppressions(target.root, target.files)),
|
|
115
|
+
exitCode,
|
|
116
|
+
};
|
|
117
|
+
return { report, root: target.root, config };
|
|
118
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { UsageError } from "./errors.js";
|
|
4
|
+
import { assertPlainPath, safeWrite, UnsafePathError } from "./safefs.js";
|
|
5
|
+
export function loadBaseline(root, path) {
|
|
6
|
+
const abs = join(root, path);
|
|
7
|
+
try {
|
|
8
|
+
assertPlainPath(root, path); // no symlink anywhere in the path, not only at the end
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
if (e instanceof UnsafePathError)
|
|
12
|
+
throw new UsageError(`Refusing to read baseline ${path}: ${e.message}`);
|
|
13
|
+
throw e;
|
|
14
|
+
}
|
|
15
|
+
if (!existsSync(abs))
|
|
16
|
+
return null;
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(readFileSync(abs, "utf8"));
|
|
19
|
+
if (parsed.schemaVersion !== 1 || !Array.isArray(parsed.entries))
|
|
20
|
+
throw new Error("unsupported schema");
|
|
21
|
+
return new Set(parsed.entries.map((e) => e.fingerprint));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// Never echo the parser's message: it quotes the file's text.
|
|
25
|
+
throw new UsageError(`Cannot read baseline ${path}`, ["not a valid code-intel baseline file"]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function saveBaseline(root, path, findings) {
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
const entries = [];
|
|
31
|
+
for (const f of findings) {
|
|
32
|
+
if (seen.has(f.fingerprint))
|
|
33
|
+
continue;
|
|
34
|
+
seen.add(f.fingerprint);
|
|
35
|
+
entries.push({ fingerprint: f.fingerprint, engine: f.engine, ruleId: f.ruleId, file: f.file });
|
|
36
|
+
}
|
|
37
|
+
entries.sort((a, b) => a.fingerprint.localeCompare(b.fingerprint));
|
|
38
|
+
const body = { schemaVersion: 1, createdAt: new Date().toISOString(), entries };
|
|
39
|
+
try {
|
|
40
|
+
safeWrite(root, path, `${JSON.stringify(body, null, 2)}\n`);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (e instanceof UnsafePathError)
|
|
44
|
+
throw new UsageError(`Cannot write baseline: ${e.message}`);
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
return entries.length;
|
|
48
|
+
}
|
|
49
|
+
export function applyBaseline(findings, baseline) {
|
|
50
|
+
if (!baseline)
|
|
51
|
+
return { kept: findings, suppressed: 0 };
|
|
52
|
+
const kept = findings.filter((f) => !baseline.has(f.fingerprint));
|
|
53
|
+
return { kept, suppressed: findings.length - kept.length };
|
|
54
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { CATEGORIES, isSeverity } from "../types.js";
|
|
6
|
+
import { VERSION } from "../version.js";
|
|
7
|
+
import { isSafeGlob } from "./glob.js";
|
|
8
|
+
const isStr = (v) => typeof v === "string";
|
|
9
|
+
const isIntOrNull = (v) => v === null || (Number.isInteger(v) && v >= 0);
|
|
10
|
+
/**
|
|
11
|
+
* Cache files live in the repository, so they are untrusted input: a committed entry could claim
|
|
12
|
+
* "no findings" for a file that has some. Anything that is not exactly the shape we write is a miss.
|
|
13
|
+
*/
|
|
14
|
+
export function isCacheEntry(x) {
|
|
15
|
+
if (typeof x !== "object" || x === null)
|
|
16
|
+
return false;
|
|
17
|
+
const e = x;
|
|
18
|
+
if (!Array.isArray(e.warnings) || !e.warnings.every(isStr))
|
|
19
|
+
return false;
|
|
20
|
+
if (e.version !== undefined && !isStr(e.version))
|
|
21
|
+
return false;
|
|
22
|
+
if (!Array.isArray(e.findings))
|
|
23
|
+
return false;
|
|
24
|
+
return e.findings.every((f) => {
|
|
25
|
+
if (typeof f !== "object" || f === null)
|
|
26
|
+
return false;
|
|
27
|
+
const r = f;
|
|
28
|
+
return (isStr(r.engine) &&
|
|
29
|
+
isStr(r.ruleId) &&
|
|
30
|
+
CATEGORIES.includes(r.category) &&
|
|
31
|
+
isSeverity(r.severity) &&
|
|
32
|
+
isStr(r.file) &&
|
|
33
|
+
isSafeGlob(r.file) &&
|
|
34
|
+
isIntOrNull(r.line) &&
|
|
35
|
+
isIntOrNull(r.endLine) &&
|
|
36
|
+
isStr(r.message) &&
|
|
37
|
+
(r.remediation === null || isStr(r.remediation)) &&
|
|
38
|
+
Array.isArray(r.references) &&
|
|
39
|
+
r.references.every(isStr) &&
|
|
40
|
+
(r.key === undefined || isStr(r.key)));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Where cached results live: the user's own cache directory, never the repository. A cache inside a repository
|
|
45
|
+
* you scan is attacker-controlled (the key is predictable), so it cannot be trusted; one outside it can.
|
|
46
|
+
* `CODE_INTEL_CACHE_DIR` overrides the location (tests, CI, or a RAM disk).
|
|
47
|
+
*/
|
|
48
|
+
export function defaultCacheDir(root) {
|
|
49
|
+
const override = process.env.CODE_INTEL_CACHE_DIR;
|
|
50
|
+
let base;
|
|
51
|
+
if (override)
|
|
52
|
+
base = resolve(override);
|
|
53
|
+
else if (process.platform === "win32") {
|
|
54
|
+
base = join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), "code-intelligence");
|
|
55
|
+
}
|
|
56
|
+
else if (process.platform === "darwin") {
|
|
57
|
+
base = join(homedir(), "Library", "Caches", "code-intelligence");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
base = join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "code-intelligence");
|
|
61
|
+
}
|
|
62
|
+
let real = root;
|
|
63
|
+
try {
|
|
64
|
+
real = realpathSync(root);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
/* keep the logical root */
|
|
68
|
+
}
|
|
69
|
+
return join(base, createHash("sha256").update(real).digest("hex").slice(0, 16));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Exact-match result cache. A key covers everything that can change an engine's output: this tool's
|
|
73
|
+
* version, the repository's real path, the engine binary identity, its config inputs, and the content
|
|
74
|
+
* hash of every input file. Any difference is a miss; there is no partial reuse and no TTL guesswork.
|
|
75
|
+
* Entries are still schema-validated on read (defense in depth).
|
|
76
|
+
*/
|
|
77
|
+
export class ResultCache {
|
|
78
|
+
dir;
|
|
79
|
+
enabled;
|
|
80
|
+
constructor(root, enabled, dir = defaultCacheDir(root)) {
|
|
81
|
+
this.enabled = enabled;
|
|
82
|
+
this.dir = dir;
|
|
83
|
+
}
|
|
84
|
+
static binaryIdentity(path) {
|
|
85
|
+
if (!path)
|
|
86
|
+
return "none";
|
|
87
|
+
try {
|
|
88
|
+
const s = statSync(path);
|
|
89
|
+
return `${path}:${s.size}:${s.mtimeMs}`;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return path;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
static key(engine, identity, inputs, root, files) {
|
|
96
|
+
let realRoot = root;
|
|
97
|
+
try {
|
|
98
|
+
realRoot = realpathSync(root);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
/* keep the logical root */
|
|
102
|
+
}
|
|
103
|
+
const h = createHash("sha256");
|
|
104
|
+
h.update(`v1\0${VERSION}\0${realRoot}\0${engine}\0${identity}\0${inputs.join("\u0001")}\0`);
|
|
105
|
+
for (const f of [...files].sort()) {
|
|
106
|
+
try {
|
|
107
|
+
h.update(`${f}\0`);
|
|
108
|
+
h.update(createHash("sha256")
|
|
109
|
+
.update(readFileSync(join(root, f)))
|
|
110
|
+
.digest());
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
h.update("unreadable");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return h.digest("hex");
|
|
117
|
+
}
|
|
118
|
+
get(key) {
|
|
119
|
+
if (!this.enabled)
|
|
120
|
+
return null;
|
|
121
|
+
try {
|
|
122
|
+
const parsed = JSON.parse(readFileSync(join(this.dir, `${key}.json`), "utf8"));
|
|
123
|
+
return isCacheEntry(parsed) ? parsed : null;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
put(key, entry) {
|
|
130
|
+
if (!this.enabled)
|
|
131
|
+
return;
|
|
132
|
+
const tmp = join(this.dir, `.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
133
|
+
try {
|
|
134
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
135
|
+
writeFileSync(tmp, JSON.stringify(entry), { flag: "wx", mode: 0o600 });
|
|
136
|
+
renameSync(tmp, join(this.dir, `${key}.json`)); // atomic: readers never see partial files
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// A cache write failure must never fail a scan, and neither may cleaning up after it.
|
|
140
|
+
try {
|
|
141
|
+
rmSync(tmp, { force: true });
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
/* nothing more to do */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|