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.
@@ -0,0 +1,317 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join, posix } from "node:path";
3
+ import { CATEGORIES, isSeverity, PROFILES } from "../types.js";
4
+ import { UsageError } from "./errors.js";
5
+ import { globProblem, isSafeGlob, MAX_PATTERNS, MAX_TOTAL_ALTERNATIVES, totalAlternatives, } from "./glob.js";
6
+ import { isSymlink } from "./safefs.js";
7
+ export const CONFIG_FILE = ".code-intel.json";
8
+ export const DEFAULT_EXCLUDE = [
9
+ ".git",
10
+ "node_modules",
11
+ "vendor",
12
+ "dist",
13
+ "build",
14
+ "coverage",
15
+ ".code-intel",
16
+ "*.min.js",
17
+ "*.map",
18
+ ];
19
+ export function defaultConfig() {
20
+ return {
21
+ failOn: "high",
22
+ exclude: [...DEFAULT_EXCLUDE],
23
+ allowMissingEngines: false,
24
+ baseline: ".code-intel/baseline.json",
25
+ lineFilter: true,
26
+ repoConfigIgnored: false,
27
+ profiles: {
28
+ quick: { engines: ["secrets", "sast", "deps"], timeoutMs: 60_000 },
29
+ deep: { engines: ["secrets", "sast", "deps", "architecture"], timeoutMs: 600_000 },
30
+ },
31
+ engines: {
32
+ gitleaks: { configPath: null },
33
+ semgrep: { configs: ["p/default"] },
34
+ },
35
+ };
36
+ }
37
+ const ROOT_KEYS = new Set([
38
+ "$schema",
39
+ "failOn",
40
+ "exclude",
41
+ "allowMissingEngines",
42
+ "baseline",
43
+ "lineFilter",
44
+ "profiles",
45
+ "engines",
46
+ ]);
47
+ const REGISTRY_RE = /^[pr]\/[A-Za-z0-9_.\-/]+$/;
48
+ function isObj(v) {
49
+ return typeof v === "object" && v !== null && !Array.isArray(v);
50
+ }
51
+ /** A repo-relative path that stays inside the repo (no absolute, no `..`). */
52
+ function isRepoRelative(p) {
53
+ // Reject URLs / drive prefixes ("https://...", "C:\..."), NUL bytes, absolute and `..` paths.
54
+ const hasScheme = /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(p);
55
+ return p.length > 0 && !hasScheme && isSafeGlob(p) && !p.includes("\0");
56
+ }
57
+ /** Paths the tool must never write: git internals and its own configuration. */
58
+ function isProtectedPath(p) {
59
+ // Normalize first, then compare case-insensitively: `.//.git/config`, `a/../.git/config` and `.GIT/config`
60
+ // (a case-insensitive file system) all resolve into git's own directory.
61
+ const parts = posix
62
+ .normalize(p.replace(/\\/g, "/"))
63
+ .split("/")
64
+ .filter((x) => x && x !== ".");
65
+ if (parts.some((x) => x.toLowerCase() === ".git"))
66
+ return true;
67
+ return parts.join("/").toLowerCase() === CONFIG_FILE.toLowerCase();
68
+ }
69
+ function strArray(v, path, issues) {
70
+ if (!Array.isArray(v) || v.some((x) => typeof x !== "string")) {
71
+ issues.push({ path, message: "must be an array of strings" });
72
+ return null;
73
+ }
74
+ return v;
75
+ }
76
+ export function validateConfig(raw) {
77
+ const issues = [];
78
+ const cfg = defaultConfig();
79
+ if (!isObj(raw)) {
80
+ return { config: cfg, issues: [{ path: "$", message: "config must be a JSON object" }] };
81
+ }
82
+ for (const k of Object.keys(raw)) {
83
+ if (!ROOT_KEYS.has(k))
84
+ issues.push({ path: k, message: "unknown key" });
85
+ }
86
+ if (raw.failOn !== undefined) {
87
+ if (isSeverity(raw.failOn))
88
+ cfg.failOn = raw.failOn;
89
+ else
90
+ issues.push({ path: "failOn", message: "must be one of info, low, medium, high, critical" });
91
+ }
92
+ if (raw.exclude !== undefined) {
93
+ const arr = strArray(raw.exclude, "exclude", issues);
94
+ if (arr) {
95
+ if (arr.length > MAX_PATTERNS) {
96
+ issues.push({ path: "exclude", message: `must have at most ${MAX_PATTERNS} entries` });
97
+ }
98
+ if (totalAlternatives(arr) > MAX_TOTAL_ALTERNATIVES) {
99
+ issues.push({
100
+ path: "exclude",
101
+ message: `patterns may expand to at most ${MAX_TOTAL_ALTERNATIVES} alternatives in total (brace groups multiply)`,
102
+ });
103
+ }
104
+ arr.forEach((g, i) => {
105
+ const problem = globProblem(g);
106
+ if (problem)
107
+ issues.push({ path: `exclude[${i}]`, message: problem });
108
+ });
109
+ cfg.exclude = [...DEFAULT_EXCLUDE, ...arr];
110
+ }
111
+ }
112
+ if (raw.allowMissingEngines !== undefined) {
113
+ if (typeof raw.allowMissingEngines === "boolean")
114
+ cfg.allowMissingEngines = raw.allowMissingEngines;
115
+ else
116
+ issues.push({ path: "allowMissingEngines", message: "must be a boolean" });
117
+ }
118
+ if (raw.lineFilter !== undefined) {
119
+ if (typeof raw.lineFilter === "boolean")
120
+ cfg.lineFilter = raw.lineFilter;
121
+ else
122
+ issues.push({ path: "lineFilter", message: "must be a boolean" });
123
+ }
124
+ if (raw.baseline !== undefined) {
125
+ if (typeof raw.baseline !== "string" || !isRepoRelative(raw.baseline)) {
126
+ issues.push({ path: "baseline", message: "must be a repo-relative path without '..'" });
127
+ }
128
+ else if (isProtectedPath(raw.baseline)) {
129
+ issues.push({
130
+ path: "baseline",
131
+ message: "must not point into .git or at the configuration file",
132
+ });
133
+ }
134
+ else
135
+ cfg.baseline = raw.baseline;
136
+ }
137
+ if (raw.profiles !== undefined) {
138
+ if (!isObj(raw.profiles)) {
139
+ issues.push({ path: "profiles", message: "must be an object" });
140
+ }
141
+ else {
142
+ for (const [name, p] of Object.entries(raw.profiles)) {
143
+ if (!PROFILES.includes(name)) {
144
+ issues.push({
145
+ path: `profiles.${name}`,
146
+ message: "unknown profile (expected quick or deep)",
147
+ });
148
+ continue;
149
+ }
150
+ if (!isObj(p)) {
151
+ issues.push({ path: `profiles.${name}`, message: "must be an object" });
152
+ continue;
153
+ }
154
+ const target = cfg.profiles[name];
155
+ for (const k of Object.keys(p)) {
156
+ if (k !== "engines" && k !== "timeoutMs") {
157
+ issues.push({ path: `profiles.${name}.${k}`, message: "unknown key" });
158
+ }
159
+ }
160
+ if (p.engines !== undefined) {
161
+ const arr = strArray(p.engines, `profiles.${name}.engines`, issues);
162
+ if (arr)
163
+ target.engines = arr;
164
+ }
165
+ if (p.timeoutMs !== undefined) {
166
+ if (Number.isInteger(p.timeoutMs) &&
167
+ p.timeoutMs >= 1000 &&
168
+ p.timeoutMs <= 3_600_000) {
169
+ target.timeoutMs = p.timeoutMs;
170
+ }
171
+ else {
172
+ issues.push({
173
+ path: `profiles.${name}.timeoutMs`,
174
+ message: "must be an integer between 1000 and 3600000",
175
+ });
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }
181
+ if (raw.engines !== undefined) {
182
+ if (!isObj(raw.engines)) {
183
+ issues.push({ path: "engines", message: "must be an object" });
184
+ }
185
+ else {
186
+ for (const [name, e] of Object.entries(raw.engines)) {
187
+ if (name !== "gitleaks" && name !== "semgrep") {
188
+ issues.push({ path: `engines.${name}`, message: "unknown engine config" });
189
+ continue;
190
+ }
191
+ if (!isObj(e)) {
192
+ issues.push({ path: `engines.${name}`, message: "must be an object" });
193
+ continue;
194
+ }
195
+ if (name === "gitleaks") {
196
+ for (const k of Object.keys(e)) {
197
+ if (k !== "configPath")
198
+ issues.push({ path: `engines.gitleaks.${k}`, message: "unknown key" });
199
+ }
200
+ if (e.configPath !== undefined) {
201
+ if (typeof e.configPath === "string" && isRepoRelative(e.configPath)) {
202
+ cfg.engines.gitleaks.configPath = e.configPath;
203
+ }
204
+ else {
205
+ issues.push({
206
+ path: "engines.gitleaks.configPath",
207
+ message: "must be a repo-relative path without '..'",
208
+ });
209
+ }
210
+ }
211
+ }
212
+ else {
213
+ for (const k of Object.keys(e)) {
214
+ if (k !== "configs")
215
+ issues.push({ path: `engines.semgrep.${k}`, message: "unknown key" });
216
+ }
217
+ if (e.configs !== undefined) {
218
+ const arr = strArray(e.configs, "engines.semgrep.configs", issues);
219
+ if (arr) {
220
+ arr.forEach((c, i) => {
221
+ if (!REGISTRY_RE.test(c) && !isRepoRelative(c)) {
222
+ issues.push({
223
+ path: `engines.semgrep.configs[${i}]`,
224
+ message: "must be a registry id like 'p/default' or a repo-relative path",
225
+ });
226
+ }
227
+ else if (c.startsWith("-")) {
228
+ issues.push({
229
+ path: `engines.semgrep.configs[${i}]`,
230
+ message: "must not start with '-'",
231
+ });
232
+ }
233
+ });
234
+ if (arr.length === 0) {
235
+ issues.push({ path: "engines.semgrep.configs", message: "must not be empty" });
236
+ }
237
+ else {
238
+ cfg.engines.semgrep.configs = arr;
239
+ }
240
+ }
241
+ }
242
+ }
243
+ }
244
+ }
245
+ }
246
+ return { config: cfg, issues };
247
+ }
248
+ /** Category or engine names accepted in profile.engines / --engines. */
249
+ export function isKnownSelector(s, engineNames) {
250
+ return CATEGORIES.includes(s) || engineNames.includes(s);
251
+ }
252
+ export function loadConfig(root, engineNames, opts = {}) {
253
+ if (opts.ignoreRepoConfig)
254
+ return { ...defaultConfig(), repoConfigIgnored: true };
255
+ const file = join(root, CONFIG_FILE);
256
+ // A committed symlink could point at any file on the machine and parse errors echo file contents.
257
+ if (isSymlink(file))
258
+ throw new UsageError(`Refusing to read ${CONFIG_FILE}: it is a symlink`);
259
+ if (!existsSync(file))
260
+ return defaultConfig();
261
+ let parsed;
262
+ let text = "";
263
+ try {
264
+ text = readFileSync(file, "utf8");
265
+ parsed = JSON.parse(text);
266
+ }
267
+ catch (e) {
268
+ // Report where, never what: the parser's message quotes the offending text.
269
+ const m = /position (\d+)/.exec(e.message);
270
+ const upTo = m ? text.slice(0, Number(m[1])).split("\n") : [];
271
+ const where = m ? ` (line ${upTo.length}, column ${(upTo.at(-1)?.length ?? 0) + 1})` : "";
272
+ throw new UsageError(`Invalid ${CONFIG_FILE}: not valid JSON${where}`);
273
+ }
274
+ const { config, issues } = validateConfig(parsed);
275
+ for (const [name, p] of Object.entries(config.profiles)) {
276
+ p.engines.forEach((s, i) => {
277
+ if (!isKnownSelector(s, engineNames)) {
278
+ issues.push({
279
+ path: `profiles.${name}.engines[${i}]`,
280
+ message: `unknown engine or category '${s}'`,
281
+ });
282
+ }
283
+ });
284
+ }
285
+ if (issues.length > 0) {
286
+ throw new UsageError(`Invalid ${CONFIG_FILE}`, issues.map((i) => `${i.path}: ${i.message}`));
287
+ }
288
+ return config;
289
+ }
290
+ /**
291
+ * What the repository's configuration changed compared to the defaults. A scan of a repository you do not
292
+ * control can be quietly neutralized by its own config (everything excluded, a catch-all allowlist, a high
293
+ * failing threshold), so the report always says when non-default settings are in effect.
294
+ */
295
+ export function describeNonDefaults(cfg) {
296
+ const d = defaultConfig();
297
+ const out = [];
298
+ if (cfg.failOn !== d.failOn)
299
+ out.push(`failOn=${cfg.failOn}`);
300
+ const extra = cfg.exclude.filter((e, i) => e !== d.exclude[i]);
301
+ if (extra.length > 0) {
302
+ out.push(`${extra.length} extra exclude pattern(s): ${extra.slice(0, 3).join(", ")}${extra.length > 3 ? ", ..." : ""}`);
303
+ }
304
+ if (cfg.allowMissingEngines)
305
+ out.push("allowMissingEngines=true");
306
+ if (!cfg.lineFilter)
307
+ out.push("lineFilter=false");
308
+ if (cfg.baseline !== d.baseline)
309
+ out.push(`baseline=${cfg.baseline}`);
310
+ for (const name of Object.keys(cfg.profiles)) {
311
+ if (JSON.stringify(cfg.profiles[name]) !== JSON.stringify(d.profiles[name]))
312
+ out.push(`profiles.${name} customized`);
313
+ }
314
+ if (JSON.stringify(cfg.engines) !== JSON.stringify(d.engines))
315
+ out.push("engine settings customized");
316
+ return out;
317
+ }
@@ -0,0 +1,18 @@
1
+ /** Bad CLI usage or invalid configuration. Maps to exit code 2. */
2
+ export class UsageError extends Error {
3
+ details;
4
+ constructor(message, details = []) {
5
+ super(message);
6
+ this.name = "UsageError";
7
+ this.details = details;
8
+ }
9
+ }
10
+ /** An engine failed to run or produced unusable output. */
11
+ export class EngineFailure extends Error {
12
+ kind;
13
+ constructor(kind, message) {
14
+ super(message);
15
+ this.name = "EngineFailure";
16
+ this.kind = kind;
17
+ }
18
+ }
@@ -0,0 +1,154 @@
1
+ import { spawn } from "node:child_process";
2
+ import { accessSync, constants, statSync } from "node:fs";
3
+ import { delimiter, join } from "node:path";
4
+ const DEFAULT_MAX_BYTES = 64 * 1024 * 1024;
5
+ function killTree(child) {
6
+ const pid = child.pid;
7
+ if (pid === undefined)
8
+ return;
9
+ try {
10
+ if (process.platform === "win32") {
11
+ spawn("taskkill", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
12
+ }
13
+ else {
14
+ process.kill(-pid, "SIGKILL"); // whole process group
15
+ }
16
+ }
17
+ catch {
18
+ try {
19
+ child.kill("SIGKILL");
20
+ }
21
+ catch {
22
+ /* already gone */
23
+ }
24
+ }
25
+ }
26
+ /**
27
+ * Run a binary with an argv array. Never uses a shell, so arguments cannot be
28
+ * interpreted as shell syntax. Kills the whole process group on timeout.
29
+ */
30
+ export function exec(cmd, args, opts) {
31
+ const started = performance.now();
32
+ const maxBytes = opts.maxBytes ?? DEFAULT_MAX_BYTES;
33
+ return new Promise((resolve) => {
34
+ let child;
35
+ try {
36
+ // spawn() changes the child's cwd but leaves an inherited $PWD pointing at the parent's directory;
37
+ // Go programs (osv-scanner) trust $PWD, then report paths that disagree with git's. Keep it in sync.
38
+ const env = { ...process.env, NO_COLOR: "1", PWD: opts.cwd, ...opts.env };
39
+ for (const k of opts.unsetEnv ?? [])
40
+ delete env[k];
41
+ child = spawn(cmd, [...args], {
42
+ cwd: opts.cwd,
43
+ env,
44
+ stdio: [opts.input === undefined ? "ignore" : "pipe", "pipe", "pipe"],
45
+ shell: false,
46
+ detached: process.platform !== "win32",
47
+ windowsHide: true,
48
+ });
49
+ }
50
+ catch (e) {
51
+ resolve({
52
+ code: null,
53
+ signal: null,
54
+ stdout: "",
55
+ stderr: "",
56
+ timedOut: false,
57
+ truncated: false,
58
+ durationMs: 0,
59
+ spawnError: e,
60
+ });
61
+ return;
62
+ }
63
+ const out = [];
64
+ const err = [];
65
+ let outBytes = 0;
66
+ let errBytes = 0;
67
+ let timedOut = false;
68
+ let truncated = false;
69
+ let spawnError = null;
70
+ let settled = false;
71
+ const timer = setTimeout(() => {
72
+ timedOut = true;
73
+ killTree(child);
74
+ }, opts.timeoutMs);
75
+ const collect = (sink, bytes, add) => (b) => {
76
+ if (bytes() >= maxBytes) {
77
+ truncated = true;
78
+ killTree(child);
79
+ return;
80
+ }
81
+ sink.push(b);
82
+ add(b.length);
83
+ };
84
+ child.stdout?.on("data", collect(out, () => outBytes, (n) => {
85
+ outBytes += n;
86
+ }));
87
+ child.stderr?.on("data", collect(err, () => errBytes, (n) => {
88
+ errBytes += n;
89
+ }));
90
+ child.on("error", (e) => {
91
+ spawnError = e;
92
+ });
93
+ child.on("close", (code, signal) => {
94
+ if (settled)
95
+ return;
96
+ settled = true;
97
+ clearTimeout(timer);
98
+ resolve({
99
+ code,
100
+ signal,
101
+ stdout: Buffer.concat(out).toString("utf8"),
102
+ stderr: Buffer.concat(err).toString("utf8"),
103
+ timedOut,
104
+ truncated,
105
+ durationMs: Math.round(performance.now() - started),
106
+ spawnError,
107
+ });
108
+ });
109
+ if (opts.input !== undefined && child.stdin) {
110
+ child.stdin.on("error", () => { });
111
+ child.stdin.end(opts.input);
112
+ }
113
+ });
114
+ }
115
+ /** Locate an executable on PATH (honors PATHEXT on Windows). Returns null if absent. */
116
+ export function findBinary(name, env = process.env) {
117
+ const exts = process.platform === "win32" ? (env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";") : [""];
118
+ for (const dir of (env.PATH ?? "").split(delimiter)) {
119
+ if (!dir)
120
+ continue;
121
+ for (const ext of exts) {
122
+ const candidate = join(dir, name + ext);
123
+ try {
124
+ if (!statSync(candidate).isFile())
125
+ continue;
126
+ accessSync(candidate, constants.X_OK);
127
+ return candidate;
128
+ }
129
+ catch {
130
+ /* keep looking */
131
+ }
132
+ }
133
+ }
134
+ return null;
135
+ }
136
+ /** Split a list into chunks whose joined length stays under a command-line budget. */
137
+ export function chunkByLength(items, maxChars) {
138
+ const chunks = [];
139
+ let cur = [];
140
+ let len = 0;
141
+ for (const it of items) {
142
+ const add = it.length + 1;
143
+ if (cur.length > 0 && len + add > maxChars) {
144
+ chunks.push(cur);
145
+ cur = [];
146
+ len = 0;
147
+ }
148
+ cur.push(it);
149
+ len += add;
150
+ }
151
+ if (cur.length > 0)
152
+ chunks.push(cur);
153
+ return chunks;
154
+ }