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,67 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
let curated = null;
|
|
4
|
+
/** registry/alternatives.json: reviewed successors for packages outside npm (and npm overrides). */
|
|
5
|
+
function loadCurated() {
|
|
6
|
+
if (curated)
|
|
7
|
+
return curated;
|
|
8
|
+
curated = new Map();
|
|
9
|
+
try {
|
|
10
|
+
const file = JSON.parse(readFileSync(new URL("../../registry/alternatives.json", import.meta.url), "utf8"));
|
|
11
|
+
for (const a of file.alternatives)
|
|
12
|
+
curated.set(`${a.ecosystem}:${a.name}`, { options: a.options, docs: a.docs, source: "Foster registry" });
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// optional data
|
|
16
|
+
}
|
|
17
|
+
return curated;
|
|
18
|
+
}
|
|
19
|
+
let cache = null;
|
|
20
|
+
function load() {
|
|
21
|
+
if (cache)
|
|
22
|
+
return cache;
|
|
23
|
+
cache = new Map();
|
|
24
|
+
const require = createRequire(import.meta.url);
|
|
25
|
+
for (const file of ["preferred", "native", "micro-utilities"]) {
|
|
26
|
+
let manifest;
|
|
27
|
+
try {
|
|
28
|
+
manifest = JSON.parse(readFileSync(require.resolve(`module-replacements/manifests/${file}.json`), "utf8"));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
continue; // dataset missing: suggestions are optional
|
|
32
|
+
}
|
|
33
|
+
for (const [name, mapping] of Object.entries(manifest.mappings)) {
|
|
34
|
+
if (cache.has(name))
|
|
35
|
+
continue;
|
|
36
|
+
const ids = (mapping.replacements ?? []).filter((id) => id !== name); // "safe-buffer → safe-buffer" carries no advice
|
|
37
|
+
if (ids.length === 0)
|
|
38
|
+
continue;
|
|
39
|
+
const options = ids.map((id) => {
|
|
40
|
+
const r = manifest.replacements[id];
|
|
41
|
+
if (id.startsWith("snippet::"))
|
|
42
|
+
return { name: id.slice("snippet::".length), kind: "snippet", description: r?.description };
|
|
43
|
+
if (r?.type === "removal")
|
|
44
|
+
return { name: id, kind: "remove", description: r.description };
|
|
45
|
+
if (r?.type === "native")
|
|
46
|
+
return { name: id, kind: "native", description: r.description };
|
|
47
|
+
return { name: id, kind: "package", description: r?.description };
|
|
48
|
+
});
|
|
49
|
+
const docs = mapping.url?.type === "e18e" && mapping.url.id ? `https://e18e.dev/docs/replacements/${mapping.url.id}.html` : undefined;
|
|
50
|
+
cache.set(name, { options, docs, source: "e18e module-replacements" });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return cache;
|
|
54
|
+
}
|
|
55
|
+
export function alternativesFor(ecosystem, name) {
|
|
56
|
+
const own = loadCurated().get(`${ecosystem}:${ecosystem === "pypi" ? name.toLowerCase() : name}`);
|
|
57
|
+
if (own)
|
|
58
|
+
return own;
|
|
59
|
+
if (ecosystem !== "npm")
|
|
60
|
+
return undefined;
|
|
61
|
+
return load().get(name);
|
|
62
|
+
}
|
|
63
|
+
/** One-line human summary, e.g. "fetch (built in), ofetch, ky — https://e18e.dev/docs/replacements/fetch.html" */
|
|
64
|
+
export function describeAlternatives(set) {
|
|
65
|
+
const names = set.options.slice(0, 4).map((o) => o.kind === "native" ? `${o.name} (built in)` : o.kind === "snippet" ? `a one-line snippet (${o.name})` : o.kind === "remove" ? "removing it" : o.name);
|
|
66
|
+
return `${names.join(", ")}${set.options.length > 4 ? ", …" : ""}${set.docs ? ` (${set.docs})` : ""}`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FixResult } from "./fix.js";
|
|
2
|
+
import type { OrphanRecord, ScanResult } from "./types.js";
|
|
3
|
+
export declare function formatText(result: ScanResult, color?: boolean, showAll?: boolean): string;
|
|
4
|
+
/** Markdown for PR comments and GitHub job summaries. */
|
|
5
|
+
export declare function formatMarkdown(result: ScanResult): string;
|
|
6
|
+
export declare function formatFix(result: FixResult, installCmd: string): string;
|
|
7
|
+
export declare function formatRecord(r: OrphanRecord): string;
|
|
8
|
+
/**
|
|
9
|
+
* SARIF 2.1.0 for GitHub code scanning and other security dashboards.
|
|
10
|
+
* Locations point at the lockfile line that declares the package, relative to `baseDir`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function formatSarif(result: ScanResult, version: string, baseDir?: string, includeQuiet?: boolean): string;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import { describeAlternatives } from "./replacements.js";
|
|
4
|
+
const KIND_LABEL = {
|
|
5
|
+
"patch-available": "PATCH READY",
|
|
6
|
+
"orphaned-vulnerable": "ORPHANED+VULN",
|
|
7
|
+
"unpatched-vulnerable": "NO FIX YET",
|
|
8
|
+
"upgrade-available": "UPGRADE",
|
|
9
|
+
orphaned: "ORPHANED",
|
|
10
|
+
};
|
|
11
|
+
const KIND_TITLE = {
|
|
12
|
+
"patch-available": "Drop-in security patch available",
|
|
13
|
+
"orphaned-vulnerable": "Vulnerable, no fix exists, and nobody maintains it",
|
|
14
|
+
"unpatched-vulnerable": "Vulnerable, no fix released yet",
|
|
15
|
+
"upgrade-available": "Vulnerable, fixed upstream: just upgrade",
|
|
16
|
+
orphaned: "Widely used but unmaintained",
|
|
17
|
+
};
|
|
18
|
+
function counts(findings) {
|
|
19
|
+
const c = { "patch-available": 0, "orphaned-vulnerable": 0, "unpatched-vulnerable": 0, "upgrade-available": 0, orphaned: 0 };
|
|
20
|
+
for (const f of findings)
|
|
21
|
+
c[f.kind]++;
|
|
22
|
+
return c;
|
|
23
|
+
}
|
|
24
|
+
export function formatText(result, color = false, showAll = false) {
|
|
25
|
+
const c = (code, s) => (color ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
26
|
+
const lines = [];
|
|
27
|
+
lines.push(c(1, `Foster scan: ${result.dir}`));
|
|
28
|
+
lines.push(`Lockfiles: ${result.lockfiles.join(", ") || "none"} · ${result.dependencyCount} packages checked`);
|
|
29
|
+
for (const w of result.warnings)
|
|
30
|
+
lines.push(c(33, `warning: ${w}`));
|
|
31
|
+
lines.push("");
|
|
32
|
+
if (result.findings.length === 0) {
|
|
33
|
+
lines.push(c(32, "No orphaned or unpatched dependencies found."));
|
|
34
|
+
return lines.join("\n");
|
|
35
|
+
}
|
|
36
|
+
const quiet = result.findings.filter((f) => f.kind === "orphaned");
|
|
37
|
+
const shown = showAll ? result.findings : result.findings.filter((f) => f.kind !== "orphaned");
|
|
38
|
+
for (const f of shown) {
|
|
39
|
+
const tagColor = f.kind === "patch-available" ? 36 : f.kind === "orphaned-vulnerable" ? 31 : f.kind === "unpatched-vulnerable" ? 33 : 90;
|
|
40
|
+
const tag = c(tagColor, KIND_LABEL[f.kind].padEnd(14));
|
|
41
|
+
const sev = f.severity === "UNKNOWN" ? "" : ` [${f.severity}]`;
|
|
42
|
+
lines.push(`${tag} ${f.dependency.name}@${f.dependency.version}${sev}${f.dependency.direct ? "" : c(90, " (transitive)")}`);
|
|
43
|
+
lines.push(` ${f.message.replace(/^[^:]+: /, "")}`);
|
|
44
|
+
for (const a of f.advisories.slice(0, 3))
|
|
45
|
+
lines.push(c(90, ` - ${a.id}: ${a.title}`));
|
|
46
|
+
if (f.advisories.length > 3)
|
|
47
|
+
lines.push(c(90, ` - ...and ${f.advisories.length - 3} more`));
|
|
48
|
+
if (f.alternatives)
|
|
49
|
+
lines.push(c(32, ` consider: ${describeAlternatives(f.alternatives)}`));
|
|
50
|
+
}
|
|
51
|
+
if (!showAll && quiet.length > 0) {
|
|
52
|
+
const names = quiet.slice(0, 6).map((f) => f.dependency.name).join(", ");
|
|
53
|
+
lines.push(c(90, `ORPHANED ${quiet.length} widely used dependencies nobody maintains, with no known vulnerability today: ${names}${quiet.length > 6 ? ", …" : ""}`), c(90, ` Run with --all to list them.`));
|
|
54
|
+
}
|
|
55
|
+
lines.push("");
|
|
56
|
+
lines.push(summaryLine(result.findings));
|
|
57
|
+
if (result.findings.some((f) => f.kind === "patch-available"))
|
|
58
|
+
lines.push(c(36, "Run `npx foster-oss fix` to switch to the patched packages."));
|
|
59
|
+
if (result.findings.some((f) => f.kind === "orphaned-vulnerable"))
|
|
60
|
+
lines.push("Orphaned + vulnerable packages can be nominated for adoption: https://github.com/foster-oss/foster/issues/new?template=adopt.yml");
|
|
61
|
+
return lines.join("\n");
|
|
62
|
+
}
|
|
63
|
+
function summaryLine(findings) {
|
|
64
|
+
const n = counts(findings);
|
|
65
|
+
return `Summary: ${n["patch-available"]} patch available · ${n["orphaned-vulnerable"]} orphaned+vulnerable · ${n["unpatched-vulnerable"]} no fix yet · ${n["upgrade-available"]} upgrade · ${n.orphaned} orphaned`;
|
|
66
|
+
}
|
|
67
|
+
/** Markdown for PR comments and GitHub job summaries. */
|
|
68
|
+
export function formatMarkdown(result) {
|
|
69
|
+
const out = ["## 🏠 Foster dependency check", ""];
|
|
70
|
+
if (result.findings.length === 0) {
|
|
71
|
+
out.push(`No orphaned or unpatched dependencies among ${result.dependencyCount} packages. ✅`);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
for (const kind of Object.keys(KIND_TITLE)) {
|
|
75
|
+
const group = result.findings.filter((f) => f.kind === kind);
|
|
76
|
+
if (group.length === 0)
|
|
77
|
+
continue;
|
|
78
|
+
const collapse = kind === "orphaned";
|
|
79
|
+
if (collapse)
|
|
80
|
+
out.push(`<details><summary><b>${KIND_TITLE[kind]} (${group.length})</b>: no known vulnerability today</summary>`, "");
|
|
81
|
+
else
|
|
82
|
+
out.push(`### ${KIND_TITLE[kind]} (${group.length})`, "");
|
|
83
|
+
out.push("| Package | Severity | Maintenance | Advisories | Action |", "|---|---|---|---|---|");
|
|
84
|
+
for (const f of group) {
|
|
85
|
+
const adv = f.advisories.slice(0, 3).map((a) => `[${a.id}](${a.url})`).join(", ") || "-";
|
|
86
|
+
const action = f.kind === "patch-available"
|
|
87
|
+
? `\`npx foster-oss fix\` → \`${f.patch.replacement.name}@${f.patch.replacement.version}\``
|
|
88
|
+
: f.kind === "upgrade-available"
|
|
89
|
+
? `upgrade${f.fixedIn ? ` to ${f.fixedIn}` : ""}`
|
|
90
|
+
: f.alternatives
|
|
91
|
+
? `replace with ${f.alternatives.options.slice(0, 3).map((o) => (o.kind === "snippet" ? "a snippet" : `\`${o.name}\``)).join(", ")}${f.alternatives.docs ? ` ([guide](${f.alternatives.docs}))` : ""}`
|
|
92
|
+
: f.kind === "orphaned-vulnerable"
|
|
93
|
+
? "[nominate for adoption](https://github.com/foster-oss/foster/issues/new?template=adopt.yml)"
|
|
94
|
+
: "watch";
|
|
95
|
+
out.push(`| \`${f.dependency.name}@${f.dependency.version}\`${f.dependency.direct ? "" : " (transitive)"} | ${f.severity} | ${f.status} | ${adv} | ${action} |`);
|
|
96
|
+
}
|
|
97
|
+
out.push("");
|
|
98
|
+
if (collapse)
|
|
99
|
+
out.push("</details>", "");
|
|
100
|
+
}
|
|
101
|
+
out.push(summaryLine(result.findings));
|
|
102
|
+
}
|
|
103
|
+
for (const w of result.warnings)
|
|
104
|
+
out.push("", `> ⚠️ ${w}`);
|
|
105
|
+
out.push("", `<sub>Checked ${result.dependencyCount} packages from ${result.lockfiles.join(", ") || "no lockfile"} with [Foster](https://github.com/foster-oss/foster).</sub>`);
|
|
106
|
+
return out.join("\n");
|
|
107
|
+
}
|
|
108
|
+
export function formatFix(result, installCmd) {
|
|
109
|
+
const lines = [];
|
|
110
|
+
if (result.changes.length === 0 && result.skipped.length === 0)
|
|
111
|
+
return "Nothing to fix: no released drop-in patch matches your dependencies.";
|
|
112
|
+
for (const ch of result.changes) {
|
|
113
|
+
const verb = ch.applied ? (result.written ? "updated" : "would update") : "kept";
|
|
114
|
+
lines.push(`${verb} ${ch.field}["${ch.key}"]: ${ch.from ?? "(none)"} → ${ch.to}${ch.note ? ` (${ch.note})` : ""}`);
|
|
115
|
+
}
|
|
116
|
+
for (const s of result.skipped)
|
|
117
|
+
lines.push(`skipped ${s.package}: ${s.reason}`);
|
|
118
|
+
if (result.written)
|
|
119
|
+
lines.push("", `package.json updated. Now run \`${installCmd}\` and your tests.`);
|
|
120
|
+
return lines.join("\n");
|
|
121
|
+
}
|
|
122
|
+
export function formatRecord(r) {
|
|
123
|
+
const lines = [
|
|
124
|
+
`${r.name} (${r.ecosystem}): ${r.status.toUpperCase()}`,
|
|
125
|
+
...(r.reasons.length ? [` why: ${r.reasons.join("; ")}`] : []),
|
|
126
|
+
` latest: ${r.latestVersion ?? "?"}${r.latestReleaseAt ? ` (${r.latestReleaseAt.slice(0, 10)})` : ""}`,
|
|
127
|
+
` reach: ${fmt(r.downloadsLastMonth)} downloads/month · ${fmt(r.dependentRepos)} dependent repos · ${fmt(r.dependentPackages)} dependent packages`,
|
|
128
|
+
` repo: ${r.repoUrl ?? "none"}${r.archived ? " (archived)" : ""}`,
|
|
129
|
+
];
|
|
130
|
+
if (r.unpatchedAdvisories.length) {
|
|
131
|
+
lines.push(` advisories with no fix:`);
|
|
132
|
+
for (const a of r.unpatchedAdvisories)
|
|
133
|
+
lines.push(` - ${a.id} [${a.severity}] ${a.title}${a.range ? ` (${a.range})` : ""}`);
|
|
134
|
+
}
|
|
135
|
+
return lines.join("\n");
|
|
136
|
+
}
|
|
137
|
+
function fmt(n) {
|
|
138
|
+
if (n === null)
|
|
139
|
+
return "?";
|
|
140
|
+
return Intl.NumberFormat("en", { notation: "compact" }).format(n);
|
|
141
|
+
}
|
|
142
|
+
const SARIF_LEVEL = {
|
|
143
|
+
"orphaned-vulnerable": "error",
|
|
144
|
+
"patch-available": "warning",
|
|
145
|
+
"unpatched-vulnerable": "warning",
|
|
146
|
+
"upgrade-available": "warning",
|
|
147
|
+
orphaned: "note",
|
|
148
|
+
};
|
|
149
|
+
const SECURITY_SEVERITY = { CRITICAL: "9.5", HIGH: "7.5", MODERATE: "5.5", LOW: "3.0" };
|
|
150
|
+
const RULE_HELP = {
|
|
151
|
+
"patch-available": "A published drop-in security patch exists for this exact version. Run `npx foster-oss fix` to switch to it through an npm alias; your imports do not change.",
|
|
152
|
+
"orphaned-vulnerable": "This dependency has a known vulnerability that no release fixes, and nobody appears to maintain it. Replace it, or nominate it for adoption.",
|
|
153
|
+
"unpatched-vulnerable": "This dependency has a known vulnerability with no fixed release yet. The package still looks maintained; watch for a release.",
|
|
154
|
+
"upgrade-available": "This dependency has a known vulnerability that a newer upstream release fixes. Upgrade it.",
|
|
155
|
+
orphaned: "This widely used dependency is no longer maintained. There is no known vulnerability today, but nobody will fix one. Prefer a maintained alternative for new code.",
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* SARIF 2.1.0 for GitHub code scanning and other security dashboards.
|
|
159
|
+
* Locations point at the lockfile line that declares the package, relative to `baseDir`.
|
|
160
|
+
*/
|
|
161
|
+
export function formatSarif(result, version, baseDir = process.cwd(), includeQuiet = false) {
|
|
162
|
+
const lockText = new Map();
|
|
163
|
+
const lineOf = (source, name) => {
|
|
164
|
+
let lines = lockText.get(source);
|
|
165
|
+
if (!lines) {
|
|
166
|
+
try {
|
|
167
|
+
lines = readFileSync(join(result.dir, source), "utf8").split(/\r?\n/);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
lines = [];
|
|
171
|
+
}
|
|
172
|
+
lockText.set(source, lines);
|
|
173
|
+
}
|
|
174
|
+
const needles = [`node_modules/${name}"`, `"${name}"`, `/${name}@`, `${name}@`, ` ${name} `, name];
|
|
175
|
+
for (const n of needles) {
|
|
176
|
+
const i = lines.findIndex((l) => l.includes(n));
|
|
177
|
+
if (i >= 0)
|
|
178
|
+
return i + 1;
|
|
179
|
+
}
|
|
180
|
+
return 1;
|
|
181
|
+
};
|
|
182
|
+
const kinds = Object.keys(KIND_TITLE);
|
|
183
|
+
const sarif = {
|
|
184
|
+
$schema: "https://json.schemastore.org/sarif-2.1.0.json",
|
|
185
|
+
version: "2.1.0",
|
|
186
|
+
runs: [
|
|
187
|
+
{
|
|
188
|
+
tool: {
|
|
189
|
+
driver: {
|
|
190
|
+
name: "Foster",
|
|
191
|
+
informationUri: "https://github.com/foster-oss/foster",
|
|
192
|
+
version,
|
|
193
|
+
rules: kinds.map((k) => ({
|
|
194
|
+
id: `foster/${k}`,
|
|
195
|
+
name: KIND_TITLE[k].replace(/[^A-Za-z]+/g, ""),
|
|
196
|
+
shortDescription: { text: KIND_TITLE[k] },
|
|
197
|
+
fullDescription: { text: RULE_HELP[k] },
|
|
198
|
+
help: { text: RULE_HELP[k], markdown: RULE_HELP[k] },
|
|
199
|
+
helpUri: "https://github.com/foster-oss/foster#what-the-findings-mean",
|
|
200
|
+
defaultConfiguration: { level: SARIF_LEVEL[k] },
|
|
201
|
+
properties: { tags: ["security", "supply-chain", "dependencies"] },
|
|
202
|
+
})),
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
// Quiet orphans (no known vulnerability) only with --all, like the text report: alerts must be actionable.
|
|
206
|
+
results: result.findings.filter((f) => includeQuiet || f.kind !== "orphaned").map((f) => {
|
|
207
|
+
const rel = relative(baseDir, join(result.dir, f.dependency.source)).replace(/\\/g, "/");
|
|
208
|
+
// Another drive (Windows) or outside the checkout: fall back to an absolute file URI.
|
|
209
|
+
const uri = /^[A-Za-z]:\//.test(rel) || rel.startsWith("/") ? `file:///${rel.replace(/^\//, "")}` : rel;
|
|
210
|
+
const extra = f.alternatives ? ` Consider: ${describeAlternatives(f.alternatives)}.` : "";
|
|
211
|
+
return {
|
|
212
|
+
ruleId: `foster/${f.kind}`,
|
|
213
|
+
level: SARIF_LEVEL[f.kind],
|
|
214
|
+
message: { text: `${f.message}${extra}` },
|
|
215
|
+
locations: [{ physicalLocation: { artifactLocation: { uri }, region: { startLine: lineOf(f.dependency.source, f.dependency.name) } } }],
|
|
216
|
+
partialFingerprints: { "fosterDependency/v1": `${f.dependency.ecosystem}:${f.dependency.name}@${f.dependency.version}:${f.kind}` },
|
|
217
|
+
properties: {
|
|
218
|
+
...(SECURITY_SEVERITY[f.severity] ? { "security-severity": SECURITY_SEVERITY[f.severity] } : {}),
|
|
219
|
+
ecosystem: f.dependency.ecosystem,
|
|
220
|
+
package: f.dependency.name,
|
|
221
|
+
version: f.dependency.version,
|
|
222
|
+
maintenance: f.status,
|
|
223
|
+
advisories: f.advisories.map((a) => a.id),
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}),
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
};
|
|
230
|
+
return JSON.stringify(sarif, null, 2);
|
|
231
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Dependency } from "./types.js";
|
|
2
|
+
export interface SbomRead {
|
|
3
|
+
format: "cyclonedx" | "spdx";
|
|
4
|
+
dependencies: Dependency[];
|
|
5
|
+
/** purl types Foster does not cover yet (e.g. maven, nuget), with counts. */
|
|
6
|
+
skipped: Record<string, number>;
|
|
7
|
+
}
|
|
8
|
+
/** Parse a purl such as pkg:npm/%40scope/name@1.2.3 or pkg:golang/github.com/a/b@v1.0.0. */
|
|
9
|
+
export declare function parsePurl(purl: string): {
|
|
10
|
+
type: string;
|
|
11
|
+
name: string;
|
|
12
|
+
version?: string;
|
|
13
|
+
} | null;
|
|
14
|
+
export declare function readSbom(path: string): SbomRead;
|
package/dist/lib/sbom.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { normalizePyName } from "./osv.js";
|
|
3
|
+
/**
|
|
4
|
+
* Read dependencies from a Software Bill of Materials: CycloneDX JSON (components[].purl)
|
|
5
|
+
* or SPDX JSON (packages[].externalRefs[] of type purl). Package URLs (purl) carry the
|
|
6
|
+
* ecosystem, name and version, so any tool that emits an SBOM can feed Foster.
|
|
7
|
+
*/
|
|
8
|
+
const PURL_TYPE = {
|
|
9
|
+
npm: "npm",
|
|
10
|
+
pypi: "pypi",
|
|
11
|
+
cargo: "cargo",
|
|
12
|
+
golang: "go",
|
|
13
|
+
gem: "rubygems",
|
|
14
|
+
composer: "packagist",
|
|
15
|
+
};
|
|
16
|
+
/** Parse a purl such as pkg:npm/%40scope/name@1.2.3 or pkg:golang/github.com/a/b@v1.0.0. */
|
|
17
|
+
export function parsePurl(purl) {
|
|
18
|
+
// The name may start with an unencoded npm scope ("pkg:npm/@babel/core@7.22.5").
|
|
19
|
+
const m = purl.match(/^pkg:([a-zA-Z0-9.+-]+)\/(@?[^@?#]+)(?:@([^?#]+))?/);
|
|
20
|
+
if (!m?.[1] || !m[2])
|
|
21
|
+
return null;
|
|
22
|
+
try {
|
|
23
|
+
const type = m[1].toLowerCase();
|
|
24
|
+
const name = m[2].split("/").map(decodeURIComponent).join("/");
|
|
25
|
+
const version = m[3] ? decodeURIComponent(m[3]) : undefined;
|
|
26
|
+
return { type, name, version };
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null; // malformed percent-encoding
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function readSbom(path) {
|
|
33
|
+
const doc = JSON.parse(readFileSync(path, "utf8"));
|
|
34
|
+
const purls = [];
|
|
35
|
+
let format;
|
|
36
|
+
if (doc.bomFormat === "CycloneDX" || Array.isArray(doc.components)) {
|
|
37
|
+
format = "cyclonedx";
|
|
38
|
+
const walk = (components) => {
|
|
39
|
+
for (const c of components ?? []) {
|
|
40
|
+
if (c.purl)
|
|
41
|
+
purls.push(c.purl);
|
|
42
|
+
if (c.components)
|
|
43
|
+
walk(c.components);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
walk(doc.components);
|
|
47
|
+
}
|
|
48
|
+
else if (typeof doc.spdxVersion === "string" || Array.isArray(doc.packages)) {
|
|
49
|
+
format = "spdx";
|
|
50
|
+
for (const p of doc.packages ?? []) {
|
|
51
|
+
for (const ref of p.externalRefs ?? []) {
|
|
52
|
+
if (ref.referenceType === "purl" && ref.referenceLocator)
|
|
53
|
+
purls.push(ref.referenceLocator);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
throw new Error(`${path} is not a CycloneDX or SPDX JSON document`);
|
|
59
|
+
}
|
|
60
|
+
const dependencies = [];
|
|
61
|
+
const skipped = {};
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
for (const purl of purls) {
|
|
64
|
+
const p = parsePurl(purl);
|
|
65
|
+
if (!p) {
|
|
66
|
+
skipped["unreadable purl"] = (skipped["unreadable purl"] ?? 0) + 1;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (!p.version)
|
|
70
|
+
continue;
|
|
71
|
+
const ecosystem = PURL_TYPE[p.type];
|
|
72
|
+
if (!ecosystem) {
|
|
73
|
+
skipped[p.type] = (skipped[p.type] ?? 0) + 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const name = ecosystem === "pypi" ? normalizePyName(p.name) : p.name;
|
|
77
|
+
const key = `${ecosystem}:${name}@${p.version}`;
|
|
78
|
+
if (seen.has(key))
|
|
79
|
+
continue;
|
|
80
|
+
seen.add(key);
|
|
81
|
+
dependencies.push({ ecosystem, name, version: p.version, direct: true, source: path.replace(/^.*[\\/]/, "") });
|
|
82
|
+
}
|
|
83
|
+
return { format, dependencies, skipped };
|
|
84
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Dependency, Finding, OrphanRecord, ScanResult } from "./types.js";
|
|
2
|
+
export interface ScanOptions {
|
|
3
|
+
/** Use only data bundled with this release: no OSV, no ecosyste.ms, no remote registry. */
|
|
4
|
+
offline?: boolean;
|
|
5
|
+
includeStaged?: boolean;
|
|
6
|
+
/** Look up maintenance status live for vulnerable packages missing from the index. Default true. */
|
|
7
|
+
deep?: boolean;
|
|
8
|
+
/** Maximum live ecosyste.ms lookups per scan. */
|
|
9
|
+
deepLimit?: number;
|
|
10
|
+
registryFile?: string;
|
|
11
|
+
indexFile?: string;
|
|
12
|
+
/** Maintenance records already known to the caller (key: "ecosystem:name"); they take precedence over the index. */
|
|
13
|
+
records?: Map<string, OrphanRecord>;
|
|
14
|
+
/** Also scan lockfiles in subfolders (monorepos), skipping node_modules, vendor, target, etc. */
|
|
15
|
+
recursive?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Directories under `root` (including root) that contain at least one supported lockfile. */
|
|
18
|
+
export declare function findProjectDirs(root: string, maxDepth?: number): string[];
|
|
19
|
+
export declare function scanProject(dirInput: string, opts?: ScanOptions): Promise<ScanResult>;
|
|
20
|
+
/** Scan an SBOM (CycloneDX or SPDX JSON) instead of lockfiles. */
|
|
21
|
+
export declare function scanSbom(file: string, opts?: ScanOptions): Promise<ScanResult>;
|
|
22
|
+
export declare function analyzeDependencies(deps: Dependency[], opts?: ScanOptions, warnings?: string[]): Promise<Finding[]>;
|
|
23
|
+
export declare function sortFindings(findings: Finding[]): Finding[];
|
|
24
|
+
export type FailOn = "none" | "patch-available" | "orphaned-vulnerable" | "unpatched" | "any";
|
|
25
|
+
/** Whether the scan should fail CI, given the --fail-on threshold. */
|
|
26
|
+
export declare function shouldFail(findings: Finding[], failOn: FailOn): boolean;
|