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,126 @@
|
|
|
1
|
+
import { availableParallelism } from "node:os";
|
|
2
|
+
import { readVersion } from "../engines/util.js";
|
|
3
|
+
import { ResultCache } from "./cache.js";
|
|
4
|
+
import { EngineFailure } from "./errors.js";
|
|
5
|
+
async function pool(items, limit, fn) {
|
|
6
|
+
const out = new Array(items.length);
|
|
7
|
+
let next = 0;
|
|
8
|
+
const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => {
|
|
9
|
+
while (next < items.length) {
|
|
10
|
+
const i = next++;
|
|
11
|
+
out[i] = await fn(items[i]);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
await Promise.all(workers);
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
const emptyResult = (e, status, reason) => ({
|
|
18
|
+
engine: e.name,
|
|
19
|
+
category: e.category,
|
|
20
|
+
version: e.detection?.version ?? null,
|
|
21
|
+
status,
|
|
22
|
+
reason,
|
|
23
|
+
warnings: [],
|
|
24
|
+
durationMs: 0,
|
|
25
|
+
findingCount: 0,
|
|
26
|
+
cached: false,
|
|
27
|
+
filesScanned: null,
|
|
28
|
+
});
|
|
29
|
+
async function runOne(entry, s) {
|
|
30
|
+
if (entry.action === "skip")
|
|
31
|
+
return { result: emptyResult(entry, "skipped", entry.reason), findings: [] };
|
|
32
|
+
if (entry.action === "unavailable") {
|
|
33
|
+
return { result: emptyResult(entry, "unavailable", entry.reason), findings: [] };
|
|
34
|
+
}
|
|
35
|
+
const engine = entry.engine; // action === "run" always carries an engine
|
|
36
|
+
// Version lookup runs alongside the engine instead of before it: it adds no wall time.
|
|
37
|
+
const versionP = entry.detection?.version
|
|
38
|
+
? Promise.resolve(entry.detection.version)
|
|
39
|
+
: entry.detection?.binary
|
|
40
|
+
? readVersion(entry.detection.binary, engine.versionArgs).catch(() => null)
|
|
41
|
+
: Promise.resolve(null);
|
|
42
|
+
const binary = entry.detection?.binary ?? engine.binaryName;
|
|
43
|
+
const started = performance.now();
|
|
44
|
+
const base = emptyResult(entry, "ok", null);
|
|
45
|
+
base.filesScanned = entry.files.length;
|
|
46
|
+
const useCache = engine.cacheable(s.config) && s.cache.enabled;
|
|
47
|
+
const key = useCache
|
|
48
|
+
? ResultCache.key(engine.name, ResultCache.binaryIdentity(binary), engine.cacheInputs(s.config, s.root), s.root, entry.files)
|
|
49
|
+
: "";
|
|
50
|
+
if (useCache) {
|
|
51
|
+
const hit = s.cache.get(key);
|
|
52
|
+
if (hit) {
|
|
53
|
+
s.log.debug("cache hit", { engine: engine.name });
|
|
54
|
+
return {
|
|
55
|
+
result: {
|
|
56
|
+
...base,
|
|
57
|
+
// A cache hit must stay fast, so it reports the version stored with the entry, never spawning the tool.
|
|
58
|
+
version: hit.version ?? null,
|
|
59
|
+
status: hit.findings.length ? "findings" : "ok",
|
|
60
|
+
warnings: hit.warnings,
|
|
61
|
+
findingCount: hit.findings.length,
|
|
62
|
+
cached: true,
|
|
63
|
+
durationMs: Math.round(performance.now() - started),
|
|
64
|
+
},
|
|
65
|
+
findings: hit.findings,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const ctx = {
|
|
70
|
+
root: s.root,
|
|
71
|
+
files: entry.files,
|
|
72
|
+
mode: s.mode,
|
|
73
|
+
exclude: s.config.exclude,
|
|
74
|
+
config: s.config,
|
|
75
|
+
timeoutMs: s.timeoutMs,
|
|
76
|
+
binary,
|
|
77
|
+
log: s.log,
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
const out = await engine.run(ctx);
|
|
81
|
+
const version = out.version ?? (await versionP);
|
|
82
|
+
if (useCache) {
|
|
83
|
+
s.cache.put(key, {
|
|
84
|
+
findings: out.findings,
|
|
85
|
+
warnings: out.warnings,
|
|
86
|
+
...(version ? { version } : {}),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
result: {
|
|
91
|
+
...base,
|
|
92
|
+
version,
|
|
93
|
+
status: out.findings.length ? "findings" : "ok",
|
|
94
|
+
warnings: out.warnings,
|
|
95
|
+
findingCount: out.findings.length,
|
|
96
|
+
durationMs: Math.round(performance.now() - started),
|
|
97
|
+
},
|
|
98
|
+
findings: out.findings,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
const failure = e instanceof EngineFailure ? e : null;
|
|
103
|
+
s.log.debug("engine failed", {
|
|
104
|
+
engine: engine.name,
|
|
105
|
+
error: e instanceof Error ? (e.stack ?? e.message) : String(e),
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
result: {
|
|
109
|
+
...base,
|
|
110
|
+
version: await versionP,
|
|
111
|
+
status: failure?.kind ?? "error",
|
|
112
|
+
reason: failure
|
|
113
|
+
? failure.message
|
|
114
|
+
: `${engine.name}: unexpected error (${e instanceof Error ? e.message : String(e)})`,
|
|
115
|
+
durationMs: Math.round(performance.now() - started),
|
|
116
|
+
},
|
|
117
|
+
findings: [],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** Run planned engines concurrently. One engine failing never aborts or hides the others. */
|
|
122
|
+
export async function runPlan(entries, s) {
|
|
123
|
+
const parallel = s.concurrency ?? availableParallelism();
|
|
124
|
+
const settled = await pool(entries, parallel, (e) => runOne(e, s));
|
|
125
|
+
return { results: settled.map((x) => x.result), findings: settled.flatMap((x) => x.findings) };
|
|
126
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { lstatSync, mkdirSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, sep } from "node:path";
|
|
3
|
+
import { isSafeGlob } from "./glob.js";
|
|
4
|
+
/** Thrown when a write would go through a symlink or outside the repository. */
|
|
5
|
+
export class UnsafePathError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "UnsafePathError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** lstat-based existence: true for a dangling symlink too (existsSync follows links and says false). */
|
|
12
|
+
export function lexists(path) {
|
|
13
|
+
try {
|
|
14
|
+
lstatSync(path);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function isSymlink(path) {
|
|
22
|
+
try {
|
|
23
|
+
return lstatSync(path).isSymbolicLink();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Refuse a repo-relative path if it is not a plain relative path, or if any component that already
|
|
31
|
+
* exists is a symlink. A hostile repository can commit `out -> /somewhere/else` and a lexical check
|
|
32
|
+
* alone would happily write through it.
|
|
33
|
+
*/
|
|
34
|
+
export function assertPlainPath(root, rel) {
|
|
35
|
+
if (!rel || rel.includes("\0") || !isSafeGlob(rel)) {
|
|
36
|
+
throw new UnsafePathError(`'${rel}' is not a plain repository-relative path`);
|
|
37
|
+
}
|
|
38
|
+
let cur = root;
|
|
39
|
+
for (const part of rel.split("/").filter(Boolean)) {
|
|
40
|
+
cur = join(cur, part);
|
|
41
|
+
if (isSymlink(cur))
|
|
42
|
+
throw new UnsafePathError(`refusing to write through a symlink: ${rel}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Write a file inside the repository without ever following a link: components are checked, the data is
|
|
47
|
+
* written to a fresh temporary file in the same directory, and renamed over the target. A rename replaces
|
|
48
|
+
* a symlink at the target instead of writing through it.
|
|
49
|
+
*/
|
|
50
|
+
export function safeWrite(root, rel, data) {
|
|
51
|
+
assertPlainPath(root, rel);
|
|
52
|
+
const target = join(root, rel);
|
|
53
|
+
const dir = dirname(target);
|
|
54
|
+
mkdirSync(dir, { recursive: true });
|
|
55
|
+
assertPlainPath(root, rel); // re-check after mkdir: nothing may have turned into a link in between
|
|
56
|
+
try {
|
|
57
|
+
if (lstatSync(target).isDirectory())
|
|
58
|
+
throw new UnsafePathError(`${rel} is a directory`);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
if (e instanceof UnsafePathError)
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
const tmp = join(dir, `.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
65
|
+
try {
|
|
66
|
+
writeFileSync(tmp, data, { flag: "wx" });
|
|
67
|
+
renameSync(tmp, target);
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
rmSync(tmp, { force: true });
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** True only for a regular file (not a link) whose real location is inside the repository. */
|
|
75
|
+
export function isPlainFileInside(root, rel) {
|
|
76
|
+
try {
|
|
77
|
+
const abs = join(root, rel);
|
|
78
|
+
if (!lstatSync(abs).isFile())
|
|
79
|
+
return false;
|
|
80
|
+
const realRoot = realpathSync.native(root);
|
|
81
|
+
const real = realpathSync.native(abs);
|
|
82
|
+
return real === realRoot || real.startsWith(realRoot + sep);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { lstatSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const MAX_FILES = 5000;
|
|
4
|
+
const MAX_BYTES = 1_000_000;
|
|
5
|
+
/** Engine-native inline suppressions: a line carrying one is skipped by the engine, whatever the config says. */
|
|
6
|
+
const INLINE = /gitleaks:allow|nosemgrep/g;
|
|
7
|
+
/**
|
|
8
|
+
* Count inline suppression comments (`# gitleaks:allow`, `# nosemgrep`) in the files being scanned. They are
|
|
9
|
+
* honored by the engines and cannot be turned off by `--ignore-repo-config`, so a hostile repository can use
|
|
10
|
+
* them to hide a finding; the report states how many exist instead of leaving them invisible.
|
|
11
|
+
*/
|
|
12
|
+
export function countInlineSuppressions(root, files) {
|
|
13
|
+
let n = 0;
|
|
14
|
+
for (const f of files.slice(0, MAX_FILES)) {
|
|
15
|
+
try {
|
|
16
|
+
const abs = join(root, f);
|
|
17
|
+
const st = lstatSync(abs);
|
|
18
|
+
if (!st.isFile() || st.size > MAX_BYTES)
|
|
19
|
+
continue;
|
|
20
|
+
n += (readFileSync(abs, "utf8").match(INLINE) ?? []).length;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
/* unreadable: nothing to count */
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { lstatSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { join, sep } from "node:path";
|
|
3
|
+
import { UsageError } from "./errors.js";
|
|
4
|
+
import { exec } from "./exec.js";
|
|
5
|
+
import { compileGlobs } from "./glob.js";
|
|
6
|
+
const GIT_TIMEOUT = 30_000;
|
|
7
|
+
// A repository can carry its own git configuration (a copied or extracted .git directory). Neutralize the
|
|
8
|
+
// settings that make `git diff`/`ls-files` run external programs: fsmonitor hooks and textconv/external diff.
|
|
9
|
+
const GIT = [
|
|
10
|
+
"-c",
|
|
11
|
+
"core.quotepath=off",
|
|
12
|
+
"-c",
|
|
13
|
+
"core.fsmonitor=false",
|
|
14
|
+
"-c",
|
|
15
|
+
"diff.external=",
|
|
16
|
+
// `git diff` spawns `git status` inside submodules, which reads the submodule's own .git/config:
|
|
17
|
+
// a second configuration we cannot pre-scan. Never descend into nested repositories.
|
|
18
|
+
"-c",
|
|
19
|
+
"diff.ignoreSubmodules=all",
|
|
20
|
+
];
|
|
21
|
+
const hardeningCache = new Map();
|
|
22
|
+
/** git >= 2.31 understands GIT_CONFIG_COUNT/KEY_n/VALUE_n. */
|
|
23
|
+
async function supportsEnvConfig(cwd) {
|
|
24
|
+
const r = await exec("git", ["--version"], { cwd, timeoutMs: GIT_TIMEOUT });
|
|
25
|
+
const m = /(\d+)\.(\d+)/.exec(r.stdout);
|
|
26
|
+
return m ? Number(m[1]) > 2 || (Number(m[1]) === 2 && Number(m[2]) >= 31) : false;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `git diff` against the working tree runs a repository's configured clean filters
|
|
30
|
+
* (`filter.<name>.clean` in .git/config) to hash the files it compares, and --no-textconv does not stop that.
|
|
31
|
+
* A copied or extracted .git directory can therefore run commands. Override every configured filter with an
|
|
32
|
+
* empty command (verified to run nothing while the diff still computes). The overrides go through
|
|
33
|
+
* GIT_CONFIG_COUNT/KEY/VALUE in the environment: `-c filter.a=b.clean=` cannot express a filter *named*
|
|
34
|
+
* `a=b` (git splits at the first `=`), which an attacker can choose. Users of git-lfs see raw content instead
|
|
35
|
+
* of pointers for LFS-tracked files, which only changes line ranges for binaries.
|
|
36
|
+
*/
|
|
37
|
+
async function hardening(cwd) {
|
|
38
|
+
const hit = hardeningCache.get(cwd);
|
|
39
|
+
if (hit)
|
|
40
|
+
return hit;
|
|
41
|
+
const r = await exec("git", [...GIT, "config", "--get-regexp", "^filter\\."], {
|
|
42
|
+
cwd,
|
|
43
|
+
timeoutMs: GIT_TIMEOUT,
|
|
44
|
+
});
|
|
45
|
+
const names = new Set();
|
|
46
|
+
for (const line of r.stdout.split("\n")) {
|
|
47
|
+
const m = /^filter\.(.+)\.(?:clean|smudge|process|required)(?:\s|$)/.exec(line);
|
|
48
|
+
if (m?.[1])
|
|
49
|
+
names.add(m[1]);
|
|
50
|
+
}
|
|
51
|
+
const keys = [...names].flatMap((n) => [
|
|
52
|
+
`filter.${n}.clean`,
|
|
53
|
+
`filter.${n}.smudge`,
|
|
54
|
+
`filter.${n}.process`,
|
|
55
|
+
]);
|
|
56
|
+
const env = {};
|
|
57
|
+
if (keys.length > 0) {
|
|
58
|
+
if ([...names].some((n) => n.includes("=")) && !(await supportsEnvConfig(cwd))) {
|
|
59
|
+
throw new UsageError("This repository defines git filters that this git version cannot neutralize safely; upgrade git to 2.31 or newer");
|
|
60
|
+
}
|
|
61
|
+
env.GIT_CONFIG_COUNT = String(keys.length);
|
|
62
|
+
keys.forEach((k, i) => {
|
|
63
|
+
env[`GIT_CONFIG_KEY_${i}`] = k;
|
|
64
|
+
env[`GIT_CONFIG_VALUE_${i}`] = "";
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Old git ignores the environment form, so names without '=' are also passed with -c (belt and braces).
|
|
68
|
+
const args = keys.filter((k) => !k.slice(7).includes("=")).flatMap((k) => ["-c", `${k}=`]);
|
|
69
|
+
const result = { args, env };
|
|
70
|
+
hardeningCache.set(cwd, result);
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
async function git(cwd, args) {
|
|
74
|
+
const h = await hardening(cwd);
|
|
75
|
+
const r = await exec("git", [...GIT, ...h.args, ...args], {
|
|
76
|
+
cwd,
|
|
77
|
+
env: h.env,
|
|
78
|
+
timeoutMs: GIT_TIMEOUT,
|
|
79
|
+
});
|
|
80
|
+
if (r.spawnError)
|
|
81
|
+
throw new UsageError("git is required for changed-file scans but was not found on PATH");
|
|
82
|
+
return { ok: r.code === 0, out: r.stdout, err: r.stderr };
|
|
83
|
+
}
|
|
84
|
+
const splitZ = (s) => s.split("\0").filter(Boolean);
|
|
85
|
+
export async function findGitRoot(cwd) {
|
|
86
|
+
const r = await git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
87
|
+
return r.ok ? r.out.trim() : null;
|
|
88
|
+
}
|
|
89
|
+
async function hasCommits(root) {
|
|
90
|
+
return (await git(root, ["rev-parse", "--verify", "--quiet", "HEAD"])).ok;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Parse `git diff -U0` output into added-line ranges per file.
|
|
94
|
+
*
|
|
95
|
+
* File headers (`+++ path`) are only recognized in a file's header block, and hunk bodies are consumed
|
|
96
|
+
* by the exact line counts their `@@` header declares. Without that, an added source line such as
|
|
97
|
+
* `++ /dev/null` renders as `+++ /dev/null` and would be mistaken for a header, silently dropping the
|
|
98
|
+
* rest of the file's hunks and hiding findings from the changed-line filter.
|
|
99
|
+
*/
|
|
100
|
+
export function parseChangedLines(diff) {
|
|
101
|
+
const result = new Map();
|
|
102
|
+
let current = null;
|
|
103
|
+
let inHeader = false;
|
|
104
|
+
let oldLeft = 0;
|
|
105
|
+
let newLeft = 0;
|
|
106
|
+
for (const line of diff.split("\n")) {
|
|
107
|
+
if (oldLeft > 0 || newLeft > 0) {
|
|
108
|
+
// Inside a hunk body: whatever the content looks like, it is content.
|
|
109
|
+
if (line.startsWith("+"))
|
|
110
|
+
newLeft -= 1;
|
|
111
|
+
else if (line.startsWith("-"))
|
|
112
|
+
oldLeft -= 1;
|
|
113
|
+
else if (!line.startsWith("\\")) {
|
|
114
|
+
oldLeft -= 1;
|
|
115
|
+
newLeft -= 1;
|
|
116
|
+
}
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (line.startsWith("diff --git ")) {
|
|
120
|
+
inHeader = true;
|
|
121
|
+
current = null;
|
|
122
|
+
}
|
|
123
|
+
else if (inHeader && line.startsWith("+++ ")) {
|
|
124
|
+
let p = line.slice(4).split("\t")[0];
|
|
125
|
+
if (p === "/dev/null") {
|
|
126
|
+
current = null;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (p.startsWith('"') && p.endsWith('"'))
|
|
130
|
+
p = p.slice(1, -1);
|
|
131
|
+
current = p.startsWith("b/") ? p.slice(2) : p;
|
|
132
|
+
if (!result.has(current))
|
|
133
|
+
result.set(current, []);
|
|
134
|
+
}
|
|
135
|
+
else if (line.startsWith("@@")) {
|
|
136
|
+
const m = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
137
|
+
if (!m)
|
|
138
|
+
continue;
|
|
139
|
+
inHeader = false;
|
|
140
|
+
oldLeft = m[1] === undefined ? 1 : Number(m[1]);
|
|
141
|
+
newLeft = m[3] === undefined ? 1 : Number(m[3]);
|
|
142
|
+
const start = Number(m[2]);
|
|
143
|
+
if (current && newLeft > 0)
|
|
144
|
+
result.get(current)?.push({ start, end: start + newLeft - 1 });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
function walk(root, rel = "", out = []) {
|
|
150
|
+
for (const ent of readdirSync(join(root, rel), { withFileTypes: true })) {
|
|
151
|
+
const r = rel ? `${rel}/${ent.name}` : ent.name;
|
|
152
|
+
if (ent.name === ".git" || ent.name === "node_modules")
|
|
153
|
+
continue;
|
|
154
|
+
if (ent.isDirectory())
|
|
155
|
+
walk(root, r, out);
|
|
156
|
+
else if (ent.isFile())
|
|
157
|
+
out.push(r);
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
export async function resolveTarget(opts) {
|
|
162
|
+
const gitRoot = await findGitRoot(opts.cwd);
|
|
163
|
+
const isGit = gitRoot !== null;
|
|
164
|
+
const mode = opts.mode ?? (isGit ? "changed" : "all");
|
|
165
|
+
const root = gitRoot ?? opts.cwd;
|
|
166
|
+
if (!isGit && mode !== "all") {
|
|
167
|
+
throw new UsageError(`--${mode === "since" ? "since" : mode} needs a git repository; use --all outside git`);
|
|
168
|
+
}
|
|
169
|
+
let rels = [];
|
|
170
|
+
let diffArgs = null;
|
|
171
|
+
let untracked = [];
|
|
172
|
+
if (!isGit) {
|
|
173
|
+
rels = walk(root);
|
|
174
|
+
}
|
|
175
|
+
else if (mode === "all") {
|
|
176
|
+
rels = splitZ((await git(root, ["ls-files", "-z", "--cached", "--others", "--exclude-standard"])).out);
|
|
177
|
+
}
|
|
178
|
+
else if (mode === "staged") {
|
|
179
|
+
rels = splitZ((await git(root, [
|
|
180
|
+
"diff",
|
|
181
|
+
"--cached",
|
|
182
|
+
"--name-only",
|
|
183
|
+
"--ignore-submodules=all",
|
|
184
|
+
"-z",
|
|
185
|
+
"--diff-filter=ACMR",
|
|
186
|
+
])).out);
|
|
187
|
+
diffArgs = [
|
|
188
|
+
"diff",
|
|
189
|
+
"--cached",
|
|
190
|
+
"-U0",
|
|
191
|
+
"--no-color",
|
|
192
|
+
"--no-ext-diff",
|
|
193
|
+
"--ignore-submodules=all",
|
|
194
|
+
"--no-textconv",
|
|
195
|
+
"--diff-filter=ACMR",
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
let base = "HEAD";
|
|
200
|
+
if (mode === "since") {
|
|
201
|
+
const ref = opts.ref ?? "";
|
|
202
|
+
if (!ref || ref.startsWith("-"))
|
|
203
|
+
throw new UsageError("--since needs a git ref (e.g. main or HEAD~3)");
|
|
204
|
+
const v = await git(root, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
|
|
205
|
+
if (!v.ok)
|
|
206
|
+
throw new UsageError(`--since: '${ref}' is not a valid commit in this repository`);
|
|
207
|
+
base = v.out.trim();
|
|
208
|
+
}
|
|
209
|
+
untracked = splitZ((await git(root, ["ls-files", "-z", "--others", "--exclude-standard"])).out);
|
|
210
|
+
if (mode === "changed" && !(await hasCommits(root))) {
|
|
211
|
+
// Fresh repo without commits: everything is new.
|
|
212
|
+
rels = splitZ((await git(root, ["ls-files", "-z", "--cached", "--others", "--exclude-standard"])).out);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const tracked = await git(root, [
|
|
216
|
+
"diff",
|
|
217
|
+
"--name-only",
|
|
218
|
+
"--ignore-submodules=all",
|
|
219
|
+
"-z",
|
|
220
|
+
"--diff-filter=ACMR",
|
|
221
|
+
base,
|
|
222
|
+
]);
|
|
223
|
+
if (!tracked.ok)
|
|
224
|
+
throw new UsageError(`git diff failed: ${tracked.err.trim().split("\n")[0]}`);
|
|
225
|
+
rels = [...splitZ(tracked.out), ...untracked];
|
|
226
|
+
diffArgs = [
|
|
227
|
+
"diff",
|
|
228
|
+
"-U0",
|
|
229
|
+
"--no-color",
|
|
230
|
+
"--no-ext-diff",
|
|
231
|
+
"--ignore-submodules=all",
|
|
232
|
+
"--no-textconv",
|
|
233
|
+
"--diff-filter=ACMR",
|
|
234
|
+
base,
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const isExcluded = compileGlobs(opts.exclude);
|
|
239
|
+
let realRoot = root;
|
|
240
|
+
try {
|
|
241
|
+
realRoot = realpathSync.native(root);
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
/* keep the logical root */
|
|
245
|
+
}
|
|
246
|
+
const seen = new Set();
|
|
247
|
+
const files = [];
|
|
248
|
+
let excludedCount = 0;
|
|
249
|
+
for (const raw of rels) {
|
|
250
|
+
const rel = raw.replace(/\\/g, "/");
|
|
251
|
+
if (seen.has(rel))
|
|
252
|
+
continue;
|
|
253
|
+
seen.add(rel);
|
|
254
|
+
if (isExcluded(rel)) {
|
|
255
|
+
excludedCount += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
const abs = join(root, rel);
|
|
260
|
+
if (!lstatSync(abs).isFile())
|
|
261
|
+
continue; // drops deleted files, symlinks, submodules
|
|
262
|
+
// The last component is not a link, but a parent directory might be (a worktree that differs from the
|
|
263
|
+
// index). Never read a file whose real location is outside the repository.
|
|
264
|
+
const real = realpathSync.native(abs);
|
|
265
|
+
if (real !== realRoot && !real.startsWith(realRoot + sep)) {
|
|
266
|
+
excludedCount += 1;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
files.push(rel);
|
|
274
|
+
}
|
|
275
|
+
files.sort();
|
|
276
|
+
let changedLines = new Map();
|
|
277
|
+
if (diffArgs) {
|
|
278
|
+
const d = await git(root, diffArgs);
|
|
279
|
+
if (d.ok)
|
|
280
|
+
changedLines = parseChangedLines(d.out);
|
|
281
|
+
// Untracked files have no diff: absence of an entry means "whole file".
|
|
282
|
+
for (const u of untracked)
|
|
283
|
+
changedLines.delete(u);
|
|
284
|
+
}
|
|
285
|
+
return { root, mode, ref: opts.ref, isGit, files, changedLines, excludedCount };
|
|
286
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { chunkByLength, exec } from "../core/exec.js";
|
|
4
|
+
import { isLockfile } from "./engine.js";
|
|
5
|
+
import { detectBinary, parseJson, processFailure } from "./util.js";
|
|
6
|
+
const SEVERITY = {
|
|
7
|
+
error: "high",
|
|
8
|
+
warning: "medium",
|
|
9
|
+
info: "low",
|
|
10
|
+
hint: "info",
|
|
11
|
+
};
|
|
12
|
+
const CONFIGS = ["sgconfig.yml", "sgconfig.yaml"];
|
|
13
|
+
const ARG_BUDGET = process.platform === "win32" ? 6000 : 60_000;
|
|
14
|
+
/**
|
|
15
|
+
* Architecture boundaries as code: runs the rules a team keeps in sgconfig.yml
|
|
16
|
+
* (e.g. "src/domain must not import src/infra"). Runs only when that config exists;
|
|
17
|
+
* we ship no rules of our own, so we never invent opinions about your architecture.
|
|
18
|
+
*/
|
|
19
|
+
export const astGrep = {
|
|
20
|
+
name: "ast-grep",
|
|
21
|
+
category: "architecture",
|
|
22
|
+
priority: 0,
|
|
23
|
+
binaryName: "ast-grep",
|
|
24
|
+
installHint: "Install: brew install ast-grep (or npm i -g @ast-grep/cli)",
|
|
25
|
+
versionArgs: ["--version"],
|
|
26
|
+
detect: (opts) => detectBinary("ast-grep", astGrep.versionArgs, astGrep.installHint, opts?.withVersion ?? true),
|
|
27
|
+
select(target) {
|
|
28
|
+
if (!CONFIGS.some((c) => existsSync(join(target.root, c)))) {
|
|
29
|
+
return {
|
|
30
|
+
files: null,
|
|
31
|
+
skipReason: "no sgconfig.yml: add ast-grep rules to enforce architecture boundaries",
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const files = target.files.filter((f) => !isLockfile(f));
|
|
35
|
+
return files.length > 0
|
|
36
|
+
? { files, skipReason: null }
|
|
37
|
+
: { files: null, skipReason: "no scannable files in scope" };
|
|
38
|
+
},
|
|
39
|
+
// Rules live in project files that are not part of the cache key, so never reuse results.
|
|
40
|
+
cacheable: () => false,
|
|
41
|
+
cacheInputs: () => [],
|
|
42
|
+
async run(ctx) {
|
|
43
|
+
const findings = [];
|
|
44
|
+
for (const chunk of chunkByLength(ctx.files.map((f) => (f.startsWith("-") ? `./${f}` : f)), ARG_BUDGET)) {
|
|
45
|
+
const r = await exec(ctx.binary, ["scan", "--json=compact", ...chunk], {
|
|
46
|
+
cwd: ctx.root,
|
|
47
|
+
timeoutMs: ctx.timeoutMs,
|
|
48
|
+
});
|
|
49
|
+
// 0 = clean, 1 = error-level diagnostics found (a successful scan); anything else is a failure.
|
|
50
|
+
const fail = processFailure("ast-grep", r, [0, 1]);
|
|
51
|
+
if (fail)
|
|
52
|
+
throw fail;
|
|
53
|
+
const matches = r.stdout.trim() ? parseJson("ast-grep", r.stdout) : [];
|
|
54
|
+
for (const m of matches) {
|
|
55
|
+
findings.push({
|
|
56
|
+
engine: "ast-grep",
|
|
57
|
+
ruleId: m.ruleId,
|
|
58
|
+
category: "architecture",
|
|
59
|
+
severity: SEVERITY[m.severity] ?? "medium",
|
|
60
|
+
file: m.file.replace(/\\/g, "/").replace(/^\.\//, ""),
|
|
61
|
+
line: m.range.start.line + 1,
|
|
62
|
+
endLine: m.range.end.line + 1,
|
|
63
|
+
message: m.message ?? m.ruleId,
|
|
64
|
+
remediation: m.note ?? null,
|
|
65
|
+
references: [],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { findings, warnings: [] };
|
|
70
|
+
},
|
|
71
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const LOCKFILES = new Set([
|
|
2
|
+
"package-lock.json",
|
|
3
|
+
"npm-shrinkwrap.json",
|
|
4
|
+
"yarn.lock",
|
|
5
|
+
"pnpm-lock.yaml",
|
|
6
|
+
"bun.lock",
|
|
7
|
+
"requirements.txt",
|
|
8
|
+
"poetry.lock",
|
|
9
|
+
"Pipfile.lock",
|
|
10
|
+
"uv.lock",
|
|
11
|
+
"Cargo.lock",
|
|
12
|
+
"go.mod",
|
|
13
|
+
"composer.lock",
|
|
14
|
+
"Gemfile.lock",
|
|
15
|
+
"pom.xml",
|
|
16
|
+
"gradle.lockfile",
|
|
17
|
+
"pubspec.lock",
|
|
18
|
+
"mix.lock",
|
|
19
|
+
]);
|
|
20
|
+
export function basename(p) {
|
|
21
|
+
return p.slice(p.lastIndexOf("/") + 1);
|
|
22
|
+
}
|
|
23
|
+
export function isLockfile(p) {
|
|
24
|
+
return LOCKFILES.has(basename(p));
|
|
25
|
+
}
|