foster-oss 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +190 -2
- package/data/orphans.min.json +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +148 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/lib/check.d.ts +23 -0
- package/dist/lib/check.js +36 -0
- package/dist/lib/classify.d.ts +27 -0
- package/dist/lib/classify.js +183 -0
- package/dist/lib/data.d.ts +26 -0
- package/dist/lib/data.js +68 -0
- package/dist/lib/ecosystems.d.ts +52 -0
- package/dist/lib/ecosystems.js +19 -0
- package/dist/lib/fix.d.ts +38 -0
- package/dist/lib/fix.js +148 -0
- package/dist/lib/http.d.ts +17 -0
- package/dist/lib/http.js +84 -0
- package/dist/lib/lockfile.d.ts +51 -0
- package/dist/lib/lockfile.js +439 -0
- package/dist/lib/osv.d.ts +69 -0
- package/dist/lib/osv.js +157 -0
- package/dist/lib/replacements.d.ts +16 -0
- package/dist/lib/replacements.js +67 -0
- package/dist/lib/report.d.ts +12 -0
- package/dist/lib/report.js +231 -0
- package/dist/lib/sbom.d.ts +14 -0
- package/dist/lib/sbom.js +84 -0
- package/dist/lib/scan.d.ts +26 -0
- package/dist/lib/scan.js +274 -0
- package/dist/lib/types.d.ts +101 -0
- package/dist/lib/types.js +10 -0
- package/dist/lib/versions.d.ts +10 -0
- package/dist/lib/versions.js +81 -0
- package/dist/mcp.d.ts +3 -0
- package/dist/mcp.js +74 -0
- package/package.json +72 -4
- package/registry/alternatives.json +90 -0
- package/registry/patches.json +243 -0
- package/skills/foster/SKILL.md +58 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import YAML from "yaml";
|
|
4
|
+
import { normalizePyName } from "./osv.js";
|
|
5
|
+
const CANDIDATES = [
|
|
6
|
+
{ file: "package-lock.json", kind: "package-lock", ecosystem: "npm", manager: "npm" },
|
|
7
|
+
{ file: "npm-shrinkwrap.json", kind: "package-lock", ecosystem: "npm", manager: "npm" },
|
|
8
|
+
{ file: "pnpm-lock.yaml", kind: "pnpm-lock", ecosystem: "npm", manager: "pnpm" },
|
|
9
|
+
{ file: "yarn.lock", kind: "yarn-lock", ecosystem: "npm", manager: "yarn" },
|
|
10
|
+
{ file: "uv.lock", kind: "uv-lock", ecosystem: "pypi" },
|
|
11
|
+
{ file: "poetry.lock", kind: "poetry-lock", ecosystem: "pypi" },
|
|
12
|
+
{ file: "Pipfile.lock", kind: "pipfile-lock", ecosystem: "pypi" },
|
|
13
|
+
{ file: "requirements.txt", kind: "requirements", ecosystem: "pypi" },
|
|
14
|
+
{ file: "Cargo.lock", kind: "cargo-lock", ecosystem: "cargo" },
|
|
15
|
+
{ file: "go.mod", kind: "go-mod", ecosystem: "go" },
|
|
16
|
+
{ file: "Gemfile.lock", kind: "gemfile-lock", ecosystem: "rubygems" },
|
|
17
|
+
{ file: "composer.lock", kind: "composer-lock", ecosystem: "packagist" },
|
|
18
|
+
];
|
|
19
|
+
export function detectLockfiles(dir) {
|
|
20
|
+
return CANDIDATES.filter((c) => existsSync(join(dir, c.file)));
|
|
21
|
+
}
|
|
22
|
+
/** Read a text file, dropping a UTF-8 byte order mark (common on Windows) so JSON.parse accepts it. */
|
|
23
|
+
export function readText(path) {
|
|
24
|
+
const text = readFileSync(path, "utf8");
|
|
25
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
26
|
+
}
|
|
27
|
+
/** Names listed directly in package.json, used to mark direct dependencies. */
|
|
28
|
+
export function directNpmNames(dir) {
|
|
29
|
+
const path = join(dir, "package.json");
|
|
30
|
+
if (!existsSync(path))
|
|
31
|
+
return new Set();
|
|
32
|
+
try {
|
|
33
|
+
const pkg = JSON.parse(readText(path));
|
|
34
|
+
const names = new Set();
|
|
35
|
+
for (const field of ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"]) {
|
|
36
|
+
for (const n of Object.keys(pkg[field] ?? {}))
|
|
37
|
+
names.add(n);
|
|
38
|
+
}
|
|
39
|
+
return names;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return new Set();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function readDependencies(dir, lock) {
|
|
46
|
+
const text = readText(join(dir, lock.file));
|
|
47
|
+
// null = the lockfile format does not tell direct from transitive; report all as direct.
|
|
48
|
+
let direct = null;
|
|
49
|
+
let pairs;
|
|
50
|
+
switch (lock.kind) {
|
|
51
|
+
case "package-lock":
|
|
52
|
+
pairs = parsePackageLock(text);
|
|
53
|
+
direct = directNpmNames(dir);
|
|
54
|
+
break;
|
|
55
|
+
case "pnpm-lock":
|
|
56
|
+
pairs = parsePnpmLock(text);
|
|
57
|
+
direct = directNpmNames(dir);
|
|
58
|
+
break;
|
|
59
|
+
case "yarn-lock":
|
|
60
|
+
pairs = parseYarnLock(text);
|
|
61
|
+
direct = directNpmNames(dir);
|
|
62
|
+
break;
|
|
63
|
+
case "requirements":
|
|
64
|
+
pairs = parseRequirements(text);
|
|
65
|
+
break;
|
|
66
|
+
case "uv-lock":
|
|
67
|
+
pairs = parseUvLock(text);
|
|
68
|
+
break;
|
|
69
|
+
case "poetry-lock":
|
|
70
|
+
pairs = parsePoetryLock(text);
|
|
71
|
+
break;
|
|
72
|
+
case "pipfile-lock":
|
|
73
|
+
pairs = parsePipfileLock(text);
|
|
74
|
+
break;
|
|
75
|
+
case "cargo-lock":
|
|
76
|
+
pairs = parseCargoLock(text);
|
|
77
|
+
direct = directCargoNames(dir);
|
|
78
|
+
break;
|
|
79
|
+
case "go-mod": {
|
|
80
|
+
const mod = parseGoMod(text);
|
|
81
|
+
pairs = mod.pairs;
|
|
82
|
+
direct = mod.direct;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case "gemfile-lock": {
|
|
86
|
+
const gems = parseGemfileLock(text);
|
|
87
|
+
pairs = gems.pairs;
|
|
88
|
+
direct = gems.direct;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case "composer-lock":
|
|
92
|
+
pairs = parseComposerLock(text);
|
|
93
|
+
direct = directComposerNames(dir);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
const ecosystem = lock.ecosystem;
|
|
97
|
+
return dedupe(pairs.map(([name, version]) => ({
|
|
98
|
+
ecosystem,
|
|
99
|
+
name: ecosystem === "pypi" ? normalizePyName(name) : name,
|
|
100
|
+
version,
|
|
101
|
+
direct: direct === null || direct.size === 0 ? true : direct.has(name),
|
|
102
|
+
source: lock.file,
|
|
103
|
+
})));
|
|
104
|
+
}
|
|
105
|
+
/** Poetry's poetry.lock (TOML): [[package]] tables with name and version. */
|
|
106
|
+
export function parsePoetryLock(text) {
|
|
107
|
+
return tomlPackages(text);
|
|
108
|
+
}
|
|
109
|
+
/** Pipenv's Pipfile.lock (JSON): "default" and "develop" maps of name → { version: "==1.2.3" }. */
|
|
110
|
+
export function parsePipfileLock(text) {
|
|
111
|
+
const lock = JSON.parse(text);
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const section of ["default", "develop"]) {
|
|
114
|
+
for (const [name, spec] of Object.entries(lock[section] ?? {})) {
|
|
115
|
+
const v = spec?.version?.replace(/^===?/, "");
|
|
116
|
+
if (v)
|
|
117
|
+
out.push([name, v]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
/** Cargo.lock: registry packages only (workspace members and git/path deps have no registry source). */
|
|
123
|
+
export function parseCargoLock(text) {
|
|
124
|
+
const out = [];
|
|
125
|
+
for (const block of text.split(/^\[\[package\]\]\s*$/m).slice(1)) {
|
|
126
|
+
const name = block.match(/^name\s*=\s*"([^"]+)"/m)?.[1];
|
|
127
|
+
const version = block.match(/^version\s*=\s*"([^"]+)"/m)?.[1];
|
|
128
|
+
const source = block.match(/^source\s*=\s*"([^"]+)"/m)?.[1];
|
|
129
|
+
if (name && version && source?.startsWith("registry+"))
|
|
130
|
+
out.push([name, version]);
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/** Dependency names declared in Cargo.toml ([dependencies], [dev-dependencies], [build-dependencies], target tables). */
|
|
135
|
+
export function directCargoNames(dir) {
|
|
136
|
+
const path = join(dir, "Cargo.toml");
|
|
137
|
+
const names = new Set();
|
|
138
|
+
if (!existsSync(path))
|
|
139
|
+
return names;
|
|
140
|
+
let inDeps = false;
|
|
141
|
+
for (const raw of readFileSync(path, "utf8").split(/\r?\n/)) {
|
|
142
|
+
const line = raw.trim();
|
|
143
|
+
const header = line.match(/^\[(.+)\]$/);
|
|
144
|
+
if (header?.[1]) {
|
|
145
|
+
const h = header[1];
|
|
146
|
+
const sub = h.match(/(?:^|\.)(?:dependencies|dev-dependencies|build-dependencies)\.([A-Za-z0-9_-]+)$/);
|
|
147
|
+
if (sub?.[1]) {
|
|
148
|
+
names.add(sub[1]);
|
|
149
|
+
inDeps = false;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
inDeps = /(?:^|\.)(?:dependencies|dev-dependencies|build-dependencies)$/.test(h);
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (!inDeps)
|
|
157
|
+
continue;
|
|
158
|
+
const m = line.match(/^([A-Za-z0-9_-]+)\s*=/);
|
|
159
|
+
if (!m?.[1])
|
|
160
|
+
continue;
|
|
161
|
+
const pkg = line.match(/package\s*=\s*"([^"]+)"/)?.[1];
|
|
162
|
+
names.add(pkg ?? m[1]);
|
|
163
|
+
}
|
|
164
|
+
return names;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* go.mod: the require directives list the full module graph needed for the build since Go 1.17;
|
|
168
|
+
* `// indirect` marks transitive ones. `replace` directives are honoured for module path and version.
|
|
169
|
+
*/
|
|
170
|
+
export function parseGoMod(text) {
|
|
171
|
+
const pairs = [];
|
|
172
|
+
const direct = new Set();
|
|
173
|
+
// replace directives: "from [fromVersion] => to [toVersion]"; a version on the left limits the replace to it.
|
|
174
|
+
const replaces = [];
|
|
175
|
+
let block = null;
|
|
176
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
177
|
+
const indirect = /\/\/\s*indirect\b/.test(raw);
|
|
178
|
+
const line = raw.replace(/\/\/.*$/, "").trim();
|
|
179
|
+
if (!line)
|
|
180
|
+
continue;
|
|
181
|
+
const opener = line.match(/^(\w+)\s*\($/);
|
|
182
|
+
if (opener?.[1]) {
|
|
183
|
+
block = opener[1] === "require" ? "require" : opener[1] === "replace" ? "replace" : "other";
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (line === ")") {
|
|
187
|
+
block = null;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
let kind = null;
|
|
191
|
+
let body = line;
|
|
192
|
+
const single = line.match(/^(require|replace)\s+(.+)$/);
|
|
193
|
+
if (single?.[1] && single[2]) {
|
|
194
|
+
kind = single[1];
|
|
195
|
+
body = single[2];
|
|
196
|
+
}
|
|
197
|
+
else if (block === "require" || block === "replace") {
|
|
198
|
+
kind = block;
|
|
199
|
+
}
|
|
200
|
+
if (!kind)
|
|
201
|
+
continue; // module, go, toolchain, exclude, retract…
|
|
202
|
+
if (kind === "require") {
|
|
203
|
+
const [mod, ver] = body.split(/\s+/);
|
|
204
|
+
if (mod && ver) {
|
|
205
|
+
pairs.push([mod, ver]);
|
|
206
|
+
if (!indirect)
|
|
207
|
+
direct.add(mod);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const [left, right] = body.split("=>").map((x) => x.trim());
|
|
212
|
+
const [from, fromVersion] = (left ?? "").split(/\s+/);
|
|
213
|
+
const [to, toVer] = (right ?? "").split(/\s+/);
|
|
214
|
+
if (!from)
|
|
215
|
+
continue;
|
|
216
|
+
// Local path replacements (./x, ../x, /abs) are not registry packages.
|
|
217
|
+
replaces.push({ from, fromVersion: fromVersion || undefined, to: to && toVer && !/^[./]/.test(to) ? [to, toVer] : null });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const out = [];
|
|
221
|
+
for (const [mod, ver] of pairs) {
|
|
222
|
+
// A version-specific replace wins over a module-wide one, as in the go command.
|
|
223
|
+
const r = replaces.find((x) => x.from === mod && x.fromVersion === ver) ?? replaces.find((x) => x.from === mod && !x.fromVersion);
|
|
224
|
+
if (!r) {
|
|
225
|
+
out.push([mod, ver]);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (r.to) {
|
|
229
|
+
out.push(r.to);
|
|
230
|
+
if (direct.has(mod))
|
|
231
|
+
direct.add(r.to[0]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return { pairs: out, direct };
|
|
235
|
+
}
|
|
236
|
+
/** Gemfile.lock: gems under the GEM specs section; DEPENDENCIES lists the direct ones. */
|
|
237
|
+
export function parseGemfileLock(text) {
|
|
238
|
+
const pairs = [];
|
|
239
|
+
const direct = new Set();
|
|
240
|
+
let section = "";
|
|
241
|
+
let inSpecs = false;
|
|
242
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
243
|
+
if (/^[A-Z][A-Z ]*$/.test(raw)) {
|
|
244
|
+
section = raw.trim();
|
|
245
|
+
inSpecs = false;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (section === "GEM" && raw.trim() === "specs:") {
|
|
249
|
+
inSpecs = true;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (section === "GEM" && inSpecs) {
|
|
253
|
+
const m = raw.match(/^ {4}([^\s(]+) \(([^)]+)\)$/);
|
|
254
|
+
if (m?.[1] && m[2]) {
|
|
255
|
+
// strip platform suffixes like "1.15.4-x86_64-linux" or "-java"
|
|
256
|
+
const version = m[2].replace(/-(x86|x64|i386|i686|arm|aarch|universal|java|mingw|mswin|darwin|linux|ruby|wasm)[\w.-]*$/i, "");
|
|
257
|
+
pairs.push([m[1], version]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (section === "DEPENDENCIES") {
|
|
261
|
+
const m = raw.match(/^ {2}([^\s!(]+)/);
|
|
262
|
+
if (m?.[1])
|
|
263
|
+
direct.add(m[1]);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { pairs, direct };
|
|
267
|
+
}
|
|
268
|
+
/** composer.lock: "packages" and "packages-dev"; versions may carry a leading "v". */
|
|
269
|
+
export function parseComposerLock(text) {
|
|
270
|
+
const lock = JSON.parse(text);
|
|
271
|
+
const out = [];
|
|
272
|
+
for (const section of ["packages", "packages-dev"]) {
|
|
273
|
+
for (const p of lock[section] ?? []) {
|
|
274
|
+
if (!p.name || !p.version || p.version.startsWith("dev-") || p.version.endsWith("-dev"))
|
|
275
|
+
continue; // branches, not releases
|
|
276
|
+
out.push([p.name, p.version.replace(/^v(?=\d)/, "")]);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
export function directComposerNames(dir) {
|
|
282
|
+
const path = join(dir, "composer.json");
|
|
283
|
+
if (!existsSync(path))
|
|
284
|
+
return new Set();
|
|
285
|
+
try {
|
|
286
|
+
const pkg = JSON.parse(readText(path));
|
|
287
|
+
return new Set([...Object.keys(pkg.require ?? {}), ...Object.keys(pkg["require-dev"] ?? {})]);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return new Set();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/** Shared reader for TOML lockfiles made of [[package]] tables (poetry.lock). */
|
|
294
|
+
function tomlPackages(text) {
|
|
295
|
+
const out = [];
|
|
296
|
+
for (const block of text.split(/^\[\[package\]\]\s*$/m).slice(1)) {
|
|
297
|
+
const name = block.match(/^name\s*=\s*"([^"]+)"/m)?.[1];
|
|
298
|
+
const version = block.match(/^version\s*=\s*"([^"]+)"/m)?.[1];
|
|
299
|
+
if (name && version)
|
|
300
|
+
out.push([name, version]);
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
function dedupe(deps) {
|
|
305
|
+
const map = new Map();
|
|
306
|
+
for (const d of deps) {
|
|
307
|
+
const key = `${d.ecosystem}:${d.name}@${d.version}`;
|
|
308
|
+
const prev = map.get(key);
|
|
309
|
+
if (!prev)
|
|
310
|
+
map.set(key, d);
|
|
311
|
+
else if (d.direct && !prev.direct)
|
|
312
|
+
prev.direct = true;
|
|
313
|
+
}
|
|
314
|
+
return [...map.values()];
|
|
315
|
+
}
|
|
316
|
+
/** package-lock.json / npm-shrinkwrap.json, lockfileVersion 1, 2 and 3. */
|
|
317
|
+
export function parsePackageLock(text) {
|
|
318
|
+
const lock = JSON.parse(text);
|
|
319
|
+
const out = [];
|
|
320
|
+
if (lock.packages) {
|
|
321
|
+
for (const [path, entry] of Object.entries(lock.packages)) {
|
|
322
|
+
if (!path || entry.link || !entry.version)
|
|
323
|
+
continue;
|
|
324
|
+
const idx = path.lastIndexOf("node_modules/");
|
|
325
|
+
if (idx === -1)
|
|
326
|
+
continue; // workspace folder, not an installed package
|
|
327
|
+
const name = entry.name ?? path.slice(idx + "node_modules/".length);
|
|
328
|
+
if (entry.version.startsWith("file:") || entry.version.startsWith("link:"))
|
|
329
|
+
continue;
|
|
330
|
+
out.push([name, entry.version]);
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
const walk = (deps) => {
|
|
335
|
+
for (const [name, d] of Object.entries(deps ?? {})) {
|
|
336
|
+
if (d.version && !d.version.startsWith("file:")) {
|
|
337
|
+
const alias = d.version.match(/^npm:(.+)@([^@]+)$/);
|
|
338
|
+
if (alias?.[1] && alias[2])
|
|
339
|
+
out.push([alias[1], alias[2]]);
|
|
340
|
+
else
|
|
341
|
+
out.push([name, d.version]);
|
|
342
|
+
}
|
|
343
|
+
walk(d.dependencies);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
walk(lock.dependencies);
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
/** Split "name@version" where name may be scoped ("@scope/name@1.0.0"). */
|
|
350
|
+
export function splitNameVersion(spec) {
|
|
351
|
+
const at = spec.lastIndexOf("@");
|
|
352
|
+
if (at <= 0)
|
|
353
|
+
return null;
|
|
354
|
+
return [spec.slice(0, at), spec.slice(at + 1)];
|
|
355
|
+
}
|
|
356
|
+
/** pnpm-lock.yaml, lockfile versions 5.x, 6.x and 9.x. */
|
|
357
|
+
export function parsePnpmLock(text) {
|
|
358
|
+
const doc = YAML.parse(text);
|
|
359
|
+
const out = [];
|
|
360
|
+
for (const [rawKey, entry] of Object.entries(doc?.packages ?? {})) {
|
|
361
|
+
let key = rawKey.replace(/^\//, "");
|
|
362
|
+
key = key.replace(/\(.*$/, ""); // peer suffix "(react@18.2.0)"
|
|
363
|
+
key = key.replace(/(\/\d[^/_]*)_.*$/, "$1"); // v5 peer suffix after the version ("/foo/1.0.0_react@18"), never in a name
|
|
364
|
+
if (entry?.name && entry.version) {
|
|
365
|
+
out.push([entry.name, entry.version]);
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
// v5 uses "/name/1.2.3", v6+ uses "/name@1.2.3"
|
|
369
|
+
let pair = splitNameVersion(key);
|
|
370
|
+
if (!pair || pair[1].includes("/")) {
|
|
371
|
+
const slash = key.lastIndexOf("/");
|
|
372
|
+
if (slash > 0)
|
|
373
|
+
pair = [key.slice(0, slash), key.slice(slash + 1)];
|
|
374
|
+
}
|
|
375
|
+
if (pair && /^\d/.test(pair[1]))
|
|
376
|
+
out.push(pair);
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
/** yarn.lock, both classic (v1) and berry (v2+). */
|
|
381
|
+
export function parseYarnLock(text) {
|
|
382
|
+
const out = [];
|
|
383
|
+
let currentName = null;
|
|
384
|
+
for (const line of text.split(/\r?\n/)) {
|
|
385
|
+
if (!line.trim() || line.startsWith("#"))
|
|
386
|
+
continue;
|
|
387
|
+
if (!line.startsWith(" ") && line.endsWith(":")) {
|
|
388
|
+
currentName = null;
|
|
389
|
+
const first = line.slice(0, -1).split(",")[0]?.trim().replace(/^"|"$/g, "");
|
|
390
|
+
if (!first || first === "__metadata")
|
|
391
|
+
continue;
|
|
392
|
+
if (first.includes("@workspace:") || first.includes("@patch:") || first.includes("@link:") || first.includes("@file:"))
|
|
393
|
+
continue;
|
|
394
|
+
const npmProto = first.indexOf("@npm:");
|
|
395
|
+
if (npmProto > 0) {
|
|
396
|
+
// berry: "name@npm:^1.0.0" or alias "alias@npm:real@^1.0.0"
|
|
397
|
+
const target = first.slice(npmProto + "@npm:".length);
|
|
398
|
+
const aliased = splitNameVersion(target);
|
|
399
|
+
currentName = aliased && !/^[\^~<>=*\d]/.test(target) ? aliased[0] : first.slice(0, npmProto);
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
const pair = splitNameVersion(first);
|
|
403
|
+
currentName = pair ? pair[0] : null;
|
|
404
|
+
}
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
const m = line.match(/^\s+version:?\s+"?([^"\s]+)"?/);
|
|
408
|
+
if (m?.[1] && currentName) {
|
|
409
|
+
out.push([currentName, m[1]]);
|
|
410
|
+
currentName = null;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
/** requirements.txt: only exact pins (name==version) are resolvable without running pip. */
|
|
416
|
+
export function parseRequirements(text) {
|
|
417
|
+
const out = [];
|
|
418
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
419
|
+
const line = raw.replace(/#.*$/, "").trim();
|
|
420
|
+
if (!line || line.startsWith("-"))
|
|
421
|
+
continue;
|
|
422
|
+
const m = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)(\[[^\]]*\])?\s*===?\s*([^\s;,]+)/);
|
|
423
|
+
if (m?.[1] && m[3])
|
|
424
|
+
out.push([m[1], m[3]]);
|
|
425
|
+
}
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
/** uv.lock (TOML): read name/version pairs from [[package]] tables. */
|
|
429
|
+
export function parseUvLock(text) {
|
|
430
|
+
const out = [];
|
|
431
|
+
for (const block of text.split(/^\[\[package\]\]\s*$/m).slice(1)) {
|
|
432
|
+
const name = block.match(/^name\s*=\s*"([^"]+)"/m)?.[1];
|
|
433
|
+
const version = block.match(/^version\s*=\s*"([^"]+)"/m)?.[1];
|
|
434
|
+
const isLocal = /^source\s*=\s*\{\s*(editable|virtual|directory)\s*=/m.test(block);
|
|
435
|
+
if (name && version && !isLocal)
|
|
436
|
+
out.push([name, version]);
|
|
437
|
+
}
|
|
438
|
+
return out;
|
|
439
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { AdvisorySummary, Dependency } from "./types.js";
|
|
2
|
+
interface OsvRangeEvent {
|
|
3
|
+
introduced?: string;
|
|
4
|
+
fixed?: string;
|
|
5
|
+
last_affected?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface OsvVuln {
|
|
8
|
+
id: string;
|
|
9
|
+
summary?: string;
|
|
10
|
+
details?: string;
|
|
11
|
+
aliases?: string[];
|
|
12
|
+
withdrawn?: string;
|
|
13
|
+
database_specific?: {
|
|
14
|
+
severity?: string;
|
|
15
|
+
};
|
|
16
|
+
affected?: {
|
|
17
|
+
package?: {
|
|
18
|
+
ecosystem?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
};
|
|
21
|
+
ranges?: {
|
|
22
|
+
type?: string;
|
|
23
|
+
events?: OsvRangeEvent[];
|
|
24
|
+
}[];
|
|
25
|
+
versions?: string[];
|
|
26
|
+
database_specific?: {
|
|
27
|
+
severity?: string;
|
|
28
|
+
};
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
export interface VulnForDependency {
|
|
32
|
+
advisory: AdvisorySummary;
|
|
33
|
+
/** Versions that fix this vulnerability for this package, from OSV "fixed" events. Empty when no fix exists. */
|
|
34
|
+
fixedVersions: string[];
|
|
35
|
+
/** Highest version known to be affected ("last_affected" events). A newer release is then not affected. */
|
|
36
|
+
lastAffected: string[];
|
|
37
|
+
isMalware: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** Query OSV for every dependency in batches of up to 1000. Returns vuln ids per dependency index. */
|
|
40
|
+
export declare function queryBatch(deps: readonly Dependency[]): Promise<string[][]>;
|
|
41
|
+
export declare function getVuln(id: string): Promise<OsvVuln>;
|
|
42
|
+
/** Fetch full records for a set of ids (deduplicated, limited concurrency). */
|
|
43
|
+
export declare function getVulns(ids: Iterable<string>): Promise<Map<string, OsvVuln>>;
|
|
44
|
+
/** Summarise one OSV vulnerability from the point of view of a single dependency. */
|
|
45
|
+
export declare function describeForDependency(v: OsvVuln, dep: Dependency): VulnForDependency;
|
|
46
|
+
/**
|
|
47
|
+
* OSV ranges are lists of intervals: introduced → fixed / last_affected, possibly several per
|
|
48
|
+
* range (one per release line). Return the end of the interval that contains the installed
|
|
49
|
+
* version: { fixed } or { last_affected }, null when that interval is open-ended (no fix), or
|
|
50
|
+
* "unrelated" when no interval contains it.
|
|
51
|
+
*/
|
|
52
|
+
export declare function intervalEndFor(events: OsvRangeEvent[], installed: string): {
|
|
53
|
+
fixed?: string;
|
|
54
|
+
last_affected?: string;
|
|
55
|
+
} | null | "unrelated";
|
|
56
|
+
/** Advisories about hijacked or sabotaged releases: the answer is "avoid that version", not a patch. */
|
|
57
|
+
export declare const MALWARE_TITLE: RegExp;
|
|
58
|
+
export { compareVersions } from "./versions.js";
|
|
59
|
+
/**
|
|
60
|
+
* Is there a release newer than every "last_affected" bound? Then upgrading fixes the advisory,
|
|
61
|
+
* even though OSV records no explicit "fixed" event.
|
|
62
|
+
*/
|
|
63
|
+
export declare function newerReleaseEscapes(v: VulnForDependency, latest: string | null | undefined): boolean;
|
|
64
|
+
export declare function normalizePyName(name: string): string;
|
|
65
|
+
/**
|
|
66
|
+
* The lowest fixed version above the installed one. OSV lists one "fixed" event per release line
|
|
67
|
+
* (e.g. 8.5.52 and 9.6.33), so the nearest fix above the installed version is the relevant one.
|
|
68
|
+
*/
|
|
69
|
+
export declare function lowestFixAbove(dep: Dependency, fixedVersions: string[]): string | undefined;
|
package/dist/lib/osv.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { normalizeSeverity } from "./classify.js";
|
|
2
|
+
import { fetchJson, mapLimit } from "./http.js";
|
|
3
|
+
const OSV = "https://api.osv.dev/v1";
|
|
4
|
+
const OSV_ECOSYSTEM = {
|
|
5
|
+
npm: "npm",
|
|
6
|
+
pypi: "PyPI",
|
|
7
|
+
cargo: "crates.io",
|
|
8
|
+
go: "Go",
|
|
9
|
+
rubygems: "RubyGems",
|
|
10
|
+
packagist: "Packagist",
|
|
11
|
+
};
|
|
12
|
+
/** Query OSV for every dependency in batches of up to 1000. Returns vuln ids per dependency index. */
|
|
13
|
+
export async function queryBatch(deps) {
|
|
14
|
+
const out = deps.map(() => []);
|
|
15
|
+
for (let start = 0; start < deps.length; start += 1000) {
|
|
16
|
+
const chunk = deps.slice(start, start + 1000);
|
|
17
|
+
const body = {
|
|
18
|
+
queries: chunk.map((d) => ({ package: { name: d.name, ecosystem: OSV_ECOSYSTEM[d.ecosystem] }, version: d.version })),
|
|
19
|
+
};
|
|
20
|
+
const res = await fetchJson(`${OSV}/querybatch`, { method: "POST", body, timeoutMs: 60_000 });
|
|
21
|
+
res.results.forEach((r, i) => {
|
|
22
|
+
out[start + i] = (r.vulns ?? []).map((v) => v.id);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
const vulnCache = new Map();
|
|
28
|
+
export function getVuln(id) {
|
|
29
|
+
let p = vulnCache.get(id);
|
|
30
|
+
if (!p) {
|
|
31
|
+
p = fetchJson(`${OSV}/vulns/${encodeURIComponent(id)}`);
|
|
32
|
+
vulnCache.set(id, p);
|
|
33
|
+
}
|
|
34
|
+
return p;
|
|
35
|
+
}
|
|
36
|
+
/** Fetch full records for a set of ids (deduplicated, limited concurrency). */
|
|
37
|
+
export async function getVulns(ids) {
|
|
38
|
+
const unique = [...new Set(ids)];
|
|
39
|
+
const vulns = await mapLimit(unique, 8, (id) => getVuln(id));
|
|
40
|
+
return new Map(unique.map((id, i) => [id, vulns[i]]));
|
|
41
|
+
}
|
|
42
|
+
function displayId(v) {
|
|
43
|
+
if (v.id.startsWith("GHSA-"))
|
|
44
|
+
return v.id;
|
|
45
|
+
return v.aliases?.find((a) => a.startsWith("GHSA-")) ?? v.id;
|
|
46
|
+
}
|
|
47
|
+
/** Summarise one OSV vulnerability from the point of view of a single dependency. */
|
|
48
|
+
export function describeForDependency(v, dep) {
|
|
49
|
+
const fixed = [];
|
|
50
|
+
const lastAffected = [];
|
|
51
|
+
let severity = normalizeSeverity(v.database_specific?.severity);
|
|
52
|
+
const installed = stripV(dep.version);
|
|
53
|
+
for (const aff of v.affected ?? []) {
|
|
54
|
+
if (!sameName(aff.package?.name, dep))
|
|
55
|
+
continue;
|
|
56
|
+
if (severity === "UNKNOWN")
|
|
57
|
+
severity = normalizeSeverity(aff.database_specific?.severity);
|
|
58
|
+
for (const range of aff.ranges ?? []) {
|
|
59
|
+
// GIT ranges hold commit hashes, not release versions.
|
|
60
|
+
if (range.type && range.type !== "SEMVER" && range.type !== "ECOSYSTEM")
|
|
61
|
+
continue;
|
|
62
|
+
const end = intervalEndFor(range.events ?? [], installed);
|
|
63
|
+
if (end === "unrelated")
|
|
64
|
+
continue;
|
|
65
|
+
if (end?.fixed)
|
|
66
|
+
fixed.push(end.fixed);
|
|
67
|
+
if (end?.last_affected)
|
|
68
|
+
lastAffected.push(end.last_affected);
|
|
69
|
+
// end === null: the installed version is in an open-ended interval, nothing fixes it on this line.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const id = displayId(v);
|
|
73
|
+
const isMalware = v.id.startsWith("MAL-") || MALWARE_TITLE.test(v.summary ?? "");
|
|
74
|
+
return {
|
|
75
|
+
advisory: {
|
|
76
|
+
id,
|
|
77
|
+
url: id.startsWith("GHSA-") ? `https://github.com/advisories/${id}` : `https://osv.dev/vulnerability/${v.id}`,
|
|
78
|
+
severity,
|
|
79
|
+
title: v.summary ?? v.details?.split("\n")[0]?.slice(0, 160) ?? id,
|
|
80
|
+
},
|
|
81
|
+
fixedVersions: [...new Set(fixed)],
|
|
82
|
+
lastAffected: [...new Set(lastAffected)],
|
|
83
|
+
isMalware,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function stripV(version) {
|
|
87
|
+
return version.replace(/^v(?=\d)/, "");
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* OSV ranges are lists of intervals: introduced → fixed / last_affected, possibly several per
|
|
91
|
+
* range (one per release line). Return the end of the interval that contains the installed
|
|
92
|
+
* version: { fixed } or { last_affected }, null when that interval is open-ended (no fix), or
|
|
93
|
+
* "unrelated" when no interval contains it.
|
|
94
|
+
*/
|
|
95
|
+
export function intervalEndFor(events, installed) {
|
|
96
|
+
let start = null;
|
|
97
|
+
const cmp = (a, b) => compareVersions(stripV(a), stripV(b));
|
|
98
|
+
for (const ev of events) {
|
|
99
|
+
if (ev.introduced !== undefined) {
|
|
100
|
+
start = ev.introduced;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (start === null)
|
|
104
|
+
continue;
|
|
105
|
+
const afterStart = start === "0" || cmp(installed, start) >= 0;
|
|
106
|
+
if (ev.fixed !== undefined) {
|
|
107
|
+
if (afterStart && cmp(installed, ev.fixed) < 0)
|
|
108
|
+
return { fixed: ev.fixed };
|
|
109
|
+
start = null;
|
|
110
|
+
}
|
|
111
|
+
else if (ev.last_affected !== undefined) {
|
|
112
|
+
if (afterStart && cmp(installed, ev.last_affected) <= 0)
|
|
113
|
+
return { last_affected: ev.last_affected };
|
|
114
|
+
start = null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (start !== null && (start === "0" || cmp(installed, start) >= 0))
|
|
118
|
+
return null;
|
|
119
|
+
return "unrelated";
|
|
120
|
+
}
|
|
121
|
+
/** Advisories about hijacked or sabotaged releases: the answer is "avoid that version", not a patch. */
|
|
122
|
+
export const MALWARE_TITLE = /malicious|malware|compromised|hijack|embedded .*(code|payload)|protestware|sabotage/i;
|
|
123
|
+
export { compareVersions } from "./versions.js";
|
|
124
|
+
import { compareVersions } from "./versions.js";
|
|
125
|
+
/**
|
|
126
|
+
* Is there a release newer than every "last_affected" bound? Then upgrading fixes the advisory,
|
|
127
|
+
* even though OSV records no explicit "fixed" event.
|
|
128
|
+
*/
|
|
129
|
+
export function newerReleaseEscapes(v, latest) {
|
|
130
|
+
if (!latest || v.lastAffected.length === 0 || v.fixedVersions.length > 0)
|
|
131
|
+
return false;
|
|
132
|
+
return v.lastAffected.every((la) => compareVersions(latest, la) > 0);
|
|
133
|
+
}
|
|
134
|
+
function sameName(name, dep) {
|
|
135
|
+
if (!name)
|
|
136
|
+
return false;
|
|
137
|
+
if (dep.ecosystem === "pypi")
|
|
138
|
+
return normalizePyName(name) === normalizePyName(dep.name);
|
|
139
|
+
return name === dep.name;
|
|
140
|
+
}
|
|
141
|
+
export function normalizePyName(name) {
|
|
142
|
+
return name.toLowerCase().replace(/[-_.]+/g, "-");
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The lowest fixed version above the installed one. OSV lists one "fixed" event per release line
|
|
146
|
+
* (e.g. 8.5.52 and 9.6.33), so the nearest fix above the installed version is the relevant one.
|
|
147
|
+
*/
|
|
148
|
+
export function lowestFixAbove(dep, fixedVersions) {
|
|
149
|
+
if (fixedVersions.length === 0)
|
|
150
|
+
return undefined;
|
|
151
|
+
const installed = dep.version.replace(/^v(?=\d)/, "");
|
|
152
|
+
const above = fixedVersions
|
|
153
|
+
.filter((f) => compareVersions(f.replace(/^v(?=\d)/, ""), installed) > 0)
|
|
154
|
+
.sort((a, b) => compareVersions(a.replace(/^v(?=\d)/, ""), b.replace(/^v(?=\d)/, "")));
|
|
155
|
+
// Never suggest a fix below the installed version: that would be a downgrade.
|
|
156
|
+
return above[0];
|
|
157
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Ecosystem } from "./types.js";
|
|
2
|
+
export interface Alternative {
|
|
3
|
+
/** e.g. "fetch", "Object.assign", "day.js" */
|
|
4
|
+
name: string;
|
|
5
|
+
kind: "native" | "package" | "snippet" | "remove";
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AlternativeSet {
|
|
9
|
+
options: Alternative[];
|
|
10
|
+
/** Guide explaining the migration, when the dataset links one. */
|
|
11
|
+
docs?: string;
|
|
12
|
+
source: "e18e module-replacements" | "Foster registry";
|
|
13
|
+
}
|
|
14
|
+
export declare function alternativesFor(ecosystem: Ecosystem, name: string): AlternativeSet | undefined;
|
|
15
|
+
/** One-line human summary, e.g. "fetch (built in), ofetch, ky — https://e18e.dev/docs/replacements/fetch.html" */
|
|
16
|
+
export declare function describeAlternatives(set: AlternativeSet): string;
|