cve-lite-cli 1.40.0 → 1.41.1
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/README.md +17 -11
- package/dist/advisory/local-advisory-source.js +9 -5
- package/dist/advisory/local-db.js +91 -13
- package/dist/advisory/osv-sync.js +187 -56
- package/dist/advisory/staging-directory.js +112 -0
- package/dist/cli/advisories-sync.js +16 -2
- package/dist/cli/args.js +46 -35
- package/dist/cli/config.js +4 -1
- package/dist/cli/help.js +4 -3
- package/dist/cli/validate.js +23 -1
- package/dist/constants.js +11 -25
- package/dist/index.js +8 -3
- package/dist/output/cyclonedx.js +23 -14
- package/dist/output/debug.js +68 -18
- package/dist/output/formatters.js +33 -5
- package/dist/output/html-reporter.js +55 -5
- package/dist/output/multi-folder-html-reporter.js +59 -3
- package/dist/output/output-dir.js +193 -7
- package/dist/output/override-fix-command.js +5 -7
- package/dist/output/printers.js +16 -5
- package/dist/output/sarif.js +131 -48
- package/dist/output/sbom-dependency-edges.js +16 -10
- package/dist/output/sbom-file.js +14 -6
- package/dist/output/scan-json.js +21 -3
- package/dist/output/spdx.js +61 -23
- package/dist/output/spinner.js +2 -1
- package/dist/output/write-outputs.js +142 -52
- package/dist/overrides/context-builder.js +23 -70
- package/dist/overrides/parsing/registry.js +19 -69
- package/dist/parsers/bun-lock.js +1 -2
- package/dist/parsers/index.js +22 -1
- package/dist/parsers/npm-lock-graph.js +39 -1
- package/dist/parsers/package-lock.js +25 -5
- package/dist/parsers/pnpm-lock.js +3 -2
- package/dist/parsers/utils.js +5 -3
- package/dist/parsers/yarn-lock-graph.js +36 -17
- package/dist/parsers/yarn-lock.js +92 -25
- package/dist/remediation/fix-commands.js +101 -45
- package/dist/remediation/npm-registry.js +10 -2
- package/dist/remediation/npm-transitive-graph.js +30 -72
- package/dist/remediation/npm-transitive-resolution.js +24 -77
- package/dist/remediation/parent-upgrade-blockers.js +51 -0
- package/dist/remediation/parent-upgrade.js +5 -79
- package/dist/remediation/registry-parent-ranges.js +21 -0
- package/dist/remediation/transitive-chain-resolver.js +3 -3
- package/dist/scan/completeness.js +10 -4
- package/dist/scan/multi-folder-scan.js +133 -58
- package/dist/scan/override-audit.js +6 -4
- package/dist/scan/single-scan.js +41 -25
- package/dist/scanner.js +263 -58
- package/dist/usage/scanner.js +19 -8
- package/dist/utils/array.js +11 -3
- package/dist/utils/checked-address-policy.js +44 -0
- package/dist/utils/debug-bundle.js +118 -0
- package/dist/utils/debug-project.js +102 -0
- package/dist/utils/debug-redaction.js +117 -0
- package/dist/utils/file.js +2 -1
- package/dist/utils/fix-runner.js +95 -13
- package/dist/utils/import-type-queries.js +322 -0
- package/dist/utils/network.js +14 -9
- package/dist/utils/npm-lockfile.js +12 -0
- package/dist/utils/osv-fetch.js +34 -0
- package/dist/utils/package-identity.js +43 -0
- package/dist/utils/package-manager-source.js +24 -0
- package/dist/utils/purl.js +37 -0
- package/dist/utils/registry-config.js +47 -15
- package/dist/utils/registry-fetch.js +42 -0
- package/dist/utils/shell-quote.js +12 -0
- package/dist/utils/string.js +8 -0
- package/dist/utils/validate-url.js +1 -1
- package/dist/utils/version.js +30 -7
- package/dist/utils/versions/osv-range.js +191 -0
- package/dist/utils/versions/pep440.js +246 -0
- package/dist/utils/versions/scheme.js +10 -0
- package/package.json +3 -2
package/dist/usage/scanner.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { EXCLUDED_DIRS } from "../constants.js";
|
|
4
|
-
|
|
4
|
+
import { importTypeQueryOffsets } from "../utils/import-type-queries.js";
|
|
5
|
+
const ALLOWED_EXTS = new Set([".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"]);
|
|
6
|
+
const TYPESCRIPT_DECLARATION_FILE = /\.d\.[cm]?ts$/;
|
|
5
7
|
export const MAX_FILES_TO_SCAN = 5000;
|
|
6
8
|
/**
|
|
7
9
|
* Single source of truth for the cap disclosure, so the three call sites cannot
|
|
@@ -10,12 +12,13 @@ export const MAX_FILES_TO_SCAN = 5000;
|
|
|
10
12
|
* reported. Otherwise it only warns that coverage is incomplete.
|
|
11
13
|
*/
|
|
12
14
|
export function usageScanTruncationWarning(opts) {
|
|
15
|
+
const remedy = "The only way to improve usage scan coverage is to scan a narrower path.";
|
|
13
16
|
const scope = opts?.subfolder ? ` in ${opts.subfolder}/` : "";
|
|
14
17
|
const head = `Usage scan stopped at ${MAX_FILES_TO_SCAN} files${scope}`;
|
|
15
18
|
if (opts?.onlyUsed) {
|
|
16
|
-
return `${head}; --only-used filtering was skipped so packages imported only in unscanned files are not dropped
|
|
19
|
+
return `${head}; --only-used filtering was skipped so packages imported only in unscanned files are not dropped. ${remedy}`;
|
|
17
20
|
}
|
|
18
|
-
return `${head}; packages imported only in unscanned files may not be detected
|
|
21
|
+
return `${head}; packages imported only in unscanned files may not be detected. ${remedy}`;
|
|
19
22
|
}
|
|
20
23
|
const ALL_IMPORTS_EXCLUDED_DIRS = new Set([
|
|
21
24
|
...EXCLUDED_DIRS,
|
|
@@ -35,7 +38,7 @@ const ALL_IMPORTS_EXCLUDED_DIRS = new Set([
|
|
|
35
38
|
// import 'pkg'
|
|
36
39
|
// require('pkg')
|
|
37
40
|
// await import('pkg')
|
|
38
|
-
const IMPORT_REQUIRE_REGEX = /(?:(?:import|export)\s+[\w
|
|
41
|
+
const IMPORT_REQUIRE_REGEX = /(?:(?:import|export)\s+[\w$\s{},*]+\s+from\s+['"]([^'"]+)['"]|import\s+['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\)|import\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
39
42
|
// A `/` opens a regex literal only where an expression may begin. After a value
|
|
40
43
|
// - an identifier, a number, `)`, `]` or a closing backtick - it is division.
|
|
41
44
|
// These keywords are the cases where a `/` directly after a word is still a
|
|
@@ -189,7 +192,7 @@ export function isTypeOnlyImportStatement(statement) {
|
|
|
189
192
|
const clause = /^(?:import|export)\s+([\s\S]*?)\s+from\s/.exec(statement)?.[1]?.trim();
|
|
190
193
|
if (!clause)
|
|
191
194
|
return false;
|
|
192
|
-
if (/^type\s+[\w{*]/.test(clause))
|
|
195
|
+
if (/^type\s+[\w${*]/.test(clause))
|
|
193
196
|
return true;
|
|
194
197
|
const braced = /^\{([\s\S]*)\}$/.exec(clause);
|
|
195
198
|
if (!braced)
|
|
@@ -202,7 +205,15 @@ export function isTypeOnlyImportStatement(statement) {
|
|
|
202
205
|
// `import()`, minus anything inside a comment or in a type-only import.
|
|
203
206
|
function collectRuntimeImportPaths(content) {
|
|
204
207
|
const paths = [];
|
|
205
|
-
|
|
208
|
+
const source = stripComments(content);
|
|
209
|
+
let typeQueries;
|
|
210
|
+
for (const match of source.matchAll(IMPORT_REQUIRE_REGEX)) {
|
|
211
|
+
if (match[4]) {
|
|
212
|
+
typeQueries ??= importTypeQueryOffsets(source);
|
|
213
|
+
if (typeQueries.has(match.index)) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
206
217
|
if (match[1] && isTypeOnlyImportStatement(match[0]))
|
|
207
218
|
continue;
|
|
208
219
|
const importPath = match[1] || match[2] || match[3] || match[4];
|
|
@@ -252,7 +263,7 @@ export function scanProjectForPackageUsage(projectPath, packagesToLookFor) {
|
|
|
252
263
|
}
|
|
253
264
|
else if (entry.isFile()) {
|
|
254
265
|
const ext = path.extname(entry.name);
|
|
255
|
-
if (ALLOWED_EXTS.has(ext)) {
|
|
266
|
+
if (ALLOWED_EXTS.has(ext) && !TYPESCRIPT_DECLARATION_FILE.test(entry.name)) {
|
|
256
267
|
// Cap at 5000 files to prevent performance issues on massive projects
|
|
257
268
|
if (scannedCount >= MAX_FILES_TO_SCAN) {
|
|
258
269
|
truncated = true;
|
|
@@ -323,7 +334,7 @@ export function scanAllImports(projectPath) {
|
|
|
323
334
|
}
|
|
324
335
|
else if (entry.isFile()) {
|
|
325
336
|
const ext = path.extname(entry.name);
|
|
326
|
-
if (ALLOWED_EXTS.has(ext)) {
|
|
337
|
+
if (ALLOWED_EXTS.has(ext) && !TYPESCRIPT_DECLARATION_FILE.test(entry.name)) {
|
|
327
338
|
if (scannedCount >= MAX_FILES_TO_SCAN) {
|
|
328
339
|
truncated = true;
|
|
329
340
|
return;
|
package/dist/utils/array.js
CHANGED
|
@@ -20,14 +20,22 @@ export function uniquePathArrays(paths) {
|
|
|
20
20
|
}
|
|
21
21
|
return output;
|
|
22
22
|
}
|
|
23
|
-
export async function runWithConcurrency(items, limit, fn) {
|
|
23
|
+
export async function runWithConcurrency(items, limit, fn, options) {
|
|
24
24
|
const results = new Array(items.length);
|
|
25
25
|
const queue = items.map((item, index) => ({ item, index }));
|
|
26
26
|
let next = 0;
|
|
27
|
+
let stopped = false;
|
|
27
28
|
async function worker() {
|
|
28
|
-
while (next < queue.length) {
|
|
29
|
+
while (!stopped && next < queue.length) {
|
|
29
30
|
const { item, index } = queue[next++];
|
|
30
|
-
|
|
31
|
+
try {
|
|
32
|
+
results[index] = await fn(item);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (options?.onError === "stop")
|
|
36
|
+
stopped = true;
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import dns from "node:dns";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
import { isPrivateOrReservedIp, stripIpv6Brackets } from "./validate-url.js";
|
|
4
|
+
export function createCheckedAddressPolicy(options) {
|
|
5
|
+
function checkAddresses(addresses) {
|
|
6
|
+
if (!addresses.length || addresses.some(({ address }) => !isIP(address))) {
|
|
7
|
+
throw new Error(options.invalidAddressMessage);
|
|
8
|
+
}
|
|
9
|
+
if (!options.allowPrivate && addresses.some(({ address }) => isPrivateOrReservedIp(address))) {
|
|
10
|
+
throw options.privateAddressError();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
// Validate all DNS answers and return only those same addresses to the socket.
|
|
14
|
+
const lookup = (hostname, requestOptions, callback) => {
|
|
15
|
+
dns.lookup(hostname, { all: true }, (error, addresses) => {
|
|
16
|
+
if (error) {
|
|
17
|
+
callback(error, "", 0);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
checkAddresses(addresses);
|
|
22
|
+
const family = requestOptions.family;
|
|
23
|
+
const candidates = family ? addresses.filter(address => address.family === family) : addresses;
|
|
24
|
+
if (!candidates.length)
|
|
25
|
+
throw new Error(options.missingFamilyMessage);
|
|
26
|
+
if (requestOptions.all)
|
|
27
|
+
callback(null, candidates);
|
|
28
|
+
else
|
|
29
|
+
callback(null, candidates[0].address, candidates[0].family);
|
|
30
|
+
}
|
|
31
|
+
catch (failure) {
|
|
32
|
+
callback(failure instanceof Error ? failure : new Error(String(failure)), "", 0);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
function checkLiteral(hostname) {
|
|
37
|
+
// Node bypasses lookup callbacks for IP literals.
|
|
38
|
+
const address = stripIpv6Brackets(hostname);
|
|
39
|
+
const family = isIP(address);
|
|
40
|
+
if (family)
|
|
41
|
+
checkAddresses([{ address, family }]);
|
|
42
|
+
}
|
|
43
|
+
return { lookup, checkLiteral };
|
|
44
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { getCliVersion } from "./version-info.js";
|
|
4
|
+
import { summarizeEffectiveOptions } from "./effective-options.js";
|
|
5
|
+
import { createBundleRedactor } from "./debug-redaction.js";
|
|
6
|
+
import { findNpmLockfile } from "./npm-lockfile.js";
|
|
7
|
+
import { buildProjectJson as projectMetadata } from "./debug-project.js";
|
|
8
|
+
export { redactDeep, redactPath } from "./debug-redaction.js";
|
|
9
|
+
function detectPackageManager(projectPath) {
|
|
10
|
+
if (fs.existsSync(path.join(projectPath, "pnpm-lock.yaml")))
|
|
11
|
+
return "pnpm";
|
|
12
|
+
if (fs.existsSync(path.join(projectPath, "bun.lockb")) || fs.existsSync(path.join(projectPath, "bun.lock")))
|
|
13
|
+
return "bun";
|
|
14
|
+
if (fs.existsSync(path.join(projectPath, "yarn.lock")))
|
|
15
|
+
return "yarn";
|
|
16
|
+
if (findNpmLockfile(projectPath))
|
|
17
|
+
return "npm";
|
|
18
|
+
return "unknown";
|
|
19
|
+
}
|
|
20
|
+
function readLockfileVersion(projectPath) {
|
|
21
|
+
const lockPath = findNpmLockfile(projectPath);
|
|
22
|
+
if (lockPath === null)
|
|
23
|
+
return null;
|
|
24
|
+
try {
|
|
25
|
+
const parsed = JSON.parse(fs.readFileSync(lockPath, "utf8"));
|
|
26
|
+
return typeof parsed.lockfileVersion === "number" ? parsed.lockfileVersion : null;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function buildEnvironmentJson(options, projectPath) {
|
|
33
|
+
const packageManager = detectPackageManager(projectPath);
|
|
34
|
+
return {
|
|
35
|
+
cliVersion: getCliVersion(),
|
|
36
|
+
nodeVersion: process.version,
|
|
37
|
+
platform: process.platform,
|
|
38
|
+
arch: process.arch,
|
|
39
|
+
packageManager,
|
|
40
|
+
lockfileVersion: packageManager === "npm" ? readLockfileVersion(projectPath) : null,
|
|
41
|
+
nodeModulesPresent: fs.existsSync(path.join(projectPath, "node_modules")),
|
|
42
|
+
effectiveFlags: summarizeEffectiveOptions(options),
|
|
43
|
+
osvEndpoint: {
|
|
44
|
+
configured: !!options.osvUrl,
|
|
45
|
+
kind: !options.osvUrl || options.osvUrl.replace(/\/+$/, "") === "https://api.osv.dev" ? "default" : "custom",
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function eventsJsonl(events, redact) {
|
|
50
|
+
return events.map(e => {
|
|
51
|
+
const obj = { timestamp: e.timestamp, message: e.message };
|
|
52
|
+
if (e.details !== undefined) {
|
|
53
|
+
// Keep the established flat event format, without letting details replace
|
|
54
|
+
// the event's message or timestamp.
|
|
55
|
+
if (e.details !== null && typeof e.details === "object" && !Array.isArray(e.details)) {
|
|
56
|
+
Object.assign(obj, e.details, { timestamp: e.timestamp, message: e.message });
|
|
57
|
+
if ("message" in e.details)
|
|
58
|
+
obj.detailsMessage = e.details.message;
|
|
59
|
+
}
|
|
60
|
+
else
|
|
61
|
+
obj.details = e.details;
|
|
62
|
+
}
|
|
63
|
+
return JSON.stringify(redact(obj));
|
|
64
|
+
}).join("\n") + (events.length ? "\n" : "");
|
|
65
|
+
}
|
|
66
|
+
export function buildEventsJsonl(events, projectPath) {
|
|
67
|
+
return eventsJsonl(events, createBundleRedactor(projectPath, [events]));
|
|
68
|
+
}
|
|
69
|
+
export function buildProjectJson(events, projectPath) {
|
|
70
|
+
const metadata = projectMetadata(events, projectPath);
|
|
71
|
+
return createBundleRedactor(projectPath, [events, metadata])(metadata);
|
|
72
|
+
}
|
|
73
|
+
/** Only the exact JSON path reported by this run's writer may be attached. */
|
|
74
|
+
export function createDebugBundle(events, options, projectPath, scanJsonPath) {
|
|
75
|
+
let scan;
|
|
76
|
+
if (options.json && scanJsonPath && !/\.(?:cdx|spdx)\.json$/i.test(scanJsonPath)) {
|
|
77
|
+
try {
|
|
78
|
+
scan = JSON.parse(fs.readFileSync(scanJsonPath, "utf8"));
|
|
79
|
+
}
|
|
80
|
+
catch { /* An unavailable scan artifact does not prevent a diagnostic bundle. */ }
|
|
81
|
+
}
|
|
82
|
+
const environment = buildEnvironmentJson(options, projectPath);
|
|
83
|
+
const project = projectMetadata(events, projectPath);
|
|
84
|
+
const redact = createBundleRedactor(projectPath, [events, options, environment, project, scan]);
|
|
85
|
+
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
86
|
+
// A unique directory prevents a same-second run from inheriting scan.json
|
|
87
|
+
// from a previous bundle, including when this run failed before writing JSON.
|
|
88
|
+
const prefix = path.join(process.cwd(), `cve-lite-debug-${ts}-`);
|
|
89
|
+
let bundleDir;
|
|
90
|
+
try {
|
|
91
|
+
bundleDir = fs.mkdtempSync(prefix);
|
|
92
|
+
fs.writeFileSync(path.join(bundleDir, "environment.json"), JSON.stringify(redact(environment), null, 2), "utf8");
|
|
93
|
+
fs.writeFileSync(path.join(bundleDir, "events.jsonl"), eventsJsonl(events, redact), "utf8");
|
|
94
|
+
fs.writeFileSync(path.join(bundleDir, "project.json"), JSON.stringify(redact(project), null, 2), "utf8");
|
|
95
|
+
if (scan !== undefined)
|
|
96
|
+
fs.writeFileSync(path.join(bundleDir, "scan.json"), JSON.stringify(redact(scan), null, 2), "utf8");
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
// Do not leave an apparently complete bundle after a partial write.
|
|
100
|
+
if (bundleDir !== undefined) {
|
|
101
|
+
try {
|
|
102
|
+
fs.rmSync(bundleDir, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
catch { /* best effort */ }
|
|
105
|
+
}
|
|
106
|
+
throw tagWriteTarget(error, bundleDir ?? prefix);
|
|
107
|
+
}
|
|
108
|
+
return bundleDir;
|
|
109
|
+
}
|
|
110
|
+
function tagWriteTarget(error, target) {
|
|
111
|
+
if (error && typeof error === "object") {
|
|
112
|
+
try {
|
|
113
|
+
Object.defineProperty(error, "writeTarget", { value: target });
|
|
114
|
+
}
|
|
115
|
+
catch { /* frozen */ }
|
|
116
|
+
}
|
|
117
|
+
return error;
|
|
118
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import YAML from "yaml";
|
|
4
|
+
import { findNpmLockfile } from "./npm-lockfile.js";
|
|
5
|
+
function isDetails(value) {
|
|
6
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function finiteCount(value) {
|
|
9
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : null;
|
|
10
|
+
}
|
|
11
|
+
function stringList(value) {
|
|
12
|
+
if (!Array.isArray(value))
|
|
13
|
+
return null;
|
|
14
|
+
const values = value.filter((entry) => typeof entry === "string");
|
|
15
|
+
return values.length === value.length ? values : null;
|
|
16
|
+
}
|
|
17
|
+
/** Read only workspace declarations; this deliberately does not resolve members. */
|
|
18
|
+
function readWorkspaceGlobs(projectPath) {
|
|
19
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
20
|
+
try {
|
|
21
|
+
const manifest = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
22
|
+
if (isDetails(manifest)) {
|
|
23
|
+
const workspaces = manifest.workspaces;
|
|
24
|
+
const patterns = Array.isArray(workspaces)
|
|
25
|
+
? stringList(workspaces)
|
|
26
|
+
: isDetails(workspaces) ? stringList(workspaces.packages) : null;
|
|
27
|
+
if (patterns !== null)
|
|
28
|
+
return patterns;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// A missing or invalid root manifest does not prevent other metadata.
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const parsed = YAML.parse(fs.readFileSync(path.join(projectPath, "pnpm-workspace.yaml"), "utf8"));
|
|
36
|
+
return isDetails(parsed) ? stringList(parsed.packages) : null;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Count package entries explicitly marked as links in npm's active lockfile. */
|
|
43
|
+
function readNpmLinkEntryCount(projectPath) {
|
|
44
|
+
const lockPath = findNpmLockfile(projectPath);
|
|
45
|
+
if (lockPath === null)
|
|
46
|
+
return null;
|
|
47
|
+
try {
|
|
48
|
+
const lockfile = JSON.parse(fs.readFileSync(lockPath, "utf8"));
|
|
49
|
+
if (!isDetails(lockfile) || !isDetails(lockfile.packages))
|
|
50
|
+
return null;
|
|
51
|
+
return Object.values(lockfile.packages).filter(entry => isDetails(entry) && entry.link === true).length;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Build project metadata from debug events and project declaration files.
|
|
59
|
+
* Workspace member names/counts are intentionally never inferred from lockfile maps.
|
|
60
|
+
*/
|
|
61
|
+
export function buildProjectJson(events, projectPath) {
|
|
62
|
+
const project = {};
|
|
63
|
+
const skippedRules = [];
|
|
64
|
+
const workspaceGlobs = readWorkspaceGlobs(projectPath);
|
|
65
|
+
if (workspaceGlobs !== null)
|
|
66
|
+
project.workspaceGlobs = workspaceGlobs;
|
|
67
|
+
const linkEntryCount = readNpmLinkEntryCount(projectPath);
|
|
68
|
+
if (linkEntryCount !== null)
|
|
69
|
+
project.linkEntryCount = linkEntryCount;
|
|
70
|
+
for (const event of events) {
|
|
71
|
+
if (!isDetails(event.details))
|
|
72
|
+
continue;
|
|
73
|
+
if (event.message === "Findings classified") {
|
|
74
|
+
const direct = finiteCount(event.details.direct);
|
|
75
|
+
const transitive = finiteCount(event.details.transitive);
|
|
76
|
+
if (direct !== null)
|
|
77
|
+
project.directFindingCount = direct;
|
|
78
|
+
if (transitive !== null)
|
|
79
|
+
project.transitiveFindingCount = transitive;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (event.message === "Scan finished") {
|
|
83
|
+
const packageCount = finiteCount(event.details.packages);
|
|
84
|
+
if (packageCount !== null)
|
|
85
|
+
project.packageCount = packageCount;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (event.message === "Rules skipped" && Array.isArray(event.details.rules)) {
|
|
89
|
+
const eventProjectPath = typeof event.details.projectPath === "string" ? event.details.projectPath : undefined;
|
|
90
|
+
for (const rule of event.details.rules) {
|
|
91
|
+
if (!isDetails(rule) || typeof rule.ruleId !== "string" || typeof rule.reason !== "string")
|
|
92
|
+
continue;
|
|
93
|
+
skippedRules.push(eventProjectPath === undefined
|
|
94
|
+
? { ruleId: rule.ruleId, reason: rule.reason }
|
|
95
|
+
: { ruleId: rule.ruleId, reason: rule.reason, projectPath: eventProjectPath });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (skippedRules.length > 0)
|
|
100
|
+
project.skippedRules = skippedRules;
|
|
101
|
+
return project;
|
|
102
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const URL_PATTERN = /[a-z][a-z\d+.-]*:\/\/[^\s<>"'`]+/gi;
|
|
4
|
+
const SECRET_KEY = /^(?:password|passwd|_authToken|_auth|authToken|token|authorization|credentials?|apiKey|secret|accessToken|refreshToken)$/i;
|
|
5
|
+
const PRIVATE_VALUE_KEY = /^(?:cacheDir|offlineDb|output|caCert|nodeExtraCaCerts|auditLog|report|osvUrl|proxy|httpsProxy|httpProxy)$/i;
|
|
6
|
+
/** One context for every file: hostnames and credentials also occur bare in errors. */
|
|
7
|
+
export function createBundleRedactor(projectPath, inputs) {
|
|
8
|
+
const sensitive = new Set();
|
|
9
|
+
const hosts = new Set();
|
|
10
|
+
const seen = new WeakSet();
|
|
11
|
+
function collect(value, key = "") {
|
|
12
|
+
if (typeof value === "string") {
|
|
13
|
+
if ((SECRET_KEY.test(key) || PRIVATE_VALUE_KEY.test(key)) && value)
|
|
14
|
+
sensitive.add(value);
|
|
15
|
+
for (const match of value.replace(/\\\//g, "/").matchAll(URL_PATTERN)) {
|
|
16
|
+
try {
|
|
17
|
+
const url = new URL(match[0]);
|
|
18
|
+
if (url.hostname)
|
|
19
|
+
hosts.add(url.hostname);
|
|
20
|
+
for (const [name, param] of url.searchParams) {
|
|
21
|
+
if (SECRET_KEY.test(name) && param)
|
|
22
|
+
sensitive.add(param);
|
|
23
|
+
}
|
|
24
|
+
for (const part of [url.username, url.password]) {
|
|
25
|
+
if (!part)
|
|
26
|
+
continue;
|
|
27
|
+
sensitive.add(part);
|
|
28
|
+
try {
|
|
29
|
+
sensitive.add(decodeURIComponent(part));
|
|
30
|
+
}
|
|
31
|
+
catch { /* malformed escape */ }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch { /* All URL-shaped strings are still removed below. */ }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (value && typeof value === "object" && !seen.has(value)) {
|
|
38
|
+
seen.add(value);
|
|
39
|
+
for (const [k, v] of Object.entries(value)) {
|
|
40
|
+
collect(k);
|
|
41
|
+
collect(v, k);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
inputs.forEach(input => collect(input));
|
|
46
|
+
const replacements = [...sensitive].filter(Boolean).sort((a, b) => b.length - a.length);
|
|
47
|
+
const home = os.homedir();
|
|
48
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
49
|
+
const homePattern = home ? new RegExp(escape(home) + "(?:[/\\\\][^\\n\\r\\\"'<>)]*)?", "g") : null;
|
|
50
|
+
function text(value) {
|
|
51
|
+
let result = value.replace(/\\\//g, "/").replace(URL_PATTERN, "<url>");
|
|
52
|
+
for (const host of hosts)
|
|
53
|
+
result = result.replace(new RegExp(escape(host), "gi"), "<host>");
|
|
54
|
+
for (const secret of replacements)
|
|
55
|
+
result = result.split(secret).join("<redacted>");
|
|
56
|
+
// Whole project paths retain useful relative context; other home paths lose
|
|
57
|
+
// the entire suffix, not just the username. Embedded paths are conservative.
|
|
58
|
+
if (path.isAbsolute(result))
|
|
59
|
+
return redactPath(result, projectPath);
|
|
60
|
+
if (homePattern)
|
|
61
|
+
result = result.replace(homePattern, "<home>");
|
|
62
|
+
result = result.replace(/(?:[A-Za-z]:\\|\/)(?:[^\s"'<>()[\],;]+[\/\\])+[^\s"'<>()[\],;]*/g, match => redactPath(match, projectPath));
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
function redact(value, ancestors = new WeakSet()) {
|
|
66
|
+
if (typeof value === "string")
|
|
67
|
+
return text(value);
|
|
68
|
+
if (!value || typeof value !== "object")
|
|
69
|
+
return value;
|
|
70
|
+
if (ancestors.has(value))
|
|
71
|
+
return "<circular>";
|
|
72
|
+
ancestors.add(value);
|
|
73
|
+
let output;
|
|
74
|
+
if (Array.isArray(value)) {
|
|
75
|
+
output = value.map(v => redact(v, ancestors));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
const out = Object.create(null);
|
|
79
|
+
for (const [key, v] of Object.entries(value)) {
|
|
80
|
+
const safeKey = text(key);
|
|
81
|
+
if (SECRET_KEY.test(key) || PRIVATE_VALUE_KEY.test(key)) {
|
|
82
|
+
out[safeKey] = v == null || v === false ? v : true;
|
|
83
|
+
}
|
|
84
|
+
else if (/^(?:registry|defaultRegistry)$/i.test(key) && typeof v === "string") {
|
|
85
|
+
out[safeKey] = { configured: true, kind: v === "https://registry.npmjs.org" || v === "https://registry.npmjs.org/" ? "default" : "custom" };
|
|
86
|
+
}
|
|
87
|
+
else if (key === "args" && Array.isArray(v)) {
|
|
88
|
+
// Never preserve positional or option values (including relative paths).
|
|
89
|
+
out[safeKey] = v.map(arg => typeof arg === "string" && /^--[a-z][a-z-]*(?:=.*)?$/i.test(arg)
|
|
90
|
+
? arg.split("=")[0] : "<set>");
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
out[safeKey] = redact(v, ancestors);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
output = out;
|
|
97
|
+
}
|
|
98
|
+
ancestors.delete(value);
|
|
99
|
+
return output;
|
|
100
|
+
}
|
|
101
|
+
return (value) => redact(value);
|
|
102
|
+
}
|
|
103
|
+
export function redactPath(p, projectPath) {
|
|
104
|
+
if (typeof p !== "string" || !path.isAbsolute(p))
|
|
105
|
+
return p;
|
|
106
|
+
const project = path.resolve(projectPath);
|
|
107
|
+
const abs = path.resolve(p);
|
|
108
|
+
if (abs === project || abs.startsWith(project + path.sep))
|
|
109
|
+
return path.relative(project, abs) || ".";
|
|
110
|
+
const home = os.homedir();
|
|
111
|
+
if (home && (abs === home || abs.startsWith(home + path.sep)))
|
|
112
|
+
return "<home>";
|
|
113
|
+
return "<outside-project>";
|
|
114
|
+
}
|
|
115
|
+
export function redactDeep(value, projectPath) {
|
|
116
|
+
return createBundleRedactor(projectPath, [value])(value);
|
|
117
|
+
}
|
package/dist/utils/file.js
CHANGED
|
@@ -64,7 +64,8 @@ export function chooseBestLockfile(candidates) {
|
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Position in LOCKFILE_PRIORITY_NESTED, with anything unrecognised sorting last.
|
|
67
|
-
*
|
|
67
|
+
* npm shrinkwrap precedence agrees with root detection; cross-manager
|
|
68
|
+
* differences are documented alongside the priority lists.
|
|
68
69
|
*/
|
|
69
70
|
function lockfileRank(filePath) {
|
|
70
71
|
const name = path.basename(filePath);
|
package/dist/utils/fix-runner.js
CHANGED
|
@@ -5,7 +5,7 @@ import { createSpinner } from "../output/spinner.js";
|
|
|
5
5
|
import { chalk } from "./chalk.js";
|
|
6
6
|
import { pluralize } from "./string.js";
|
|
7
7
|
import { isBreakingUpgrade } from "./version.js";
|
|
8
|
-
import {
|
|
8
|
+
import { installPackageSpec } from "./package-identity.js";
|
|
9
9
|
import { formatParentUpdateChildNote } from "../remediation/parent-update-child-note.js";
|
|
10
10
|
export async function runInstallCommand(command, args, cwd, debugLog) {
|
|
11
11
|
const commandLine = [command, ...args].join(" ");
|
|
@@ -61,19 +61,101 @@ const EMPTY_UNAPPLIED = {
|
|
|
61
61
|
breakingUpgradeCount: 0,
|
|
62
62
|
noFixCount: 0,
|
|
63
63
|
};
|
|
64
|
-
// Build
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
64
|
+
// Build argv lists for --fix without round-tripping through a shell string.
|
|
65
|
+
// Workspace paths and package tokens are passed as whole argv elements to spawn
|
|
66
|
+
// (no shell), so a directory name containing `&&` cannot become a second executable.
|
|
67
|
+
// Human-facing plan strings still come from buildTargetCommand.
|
|
68
68
|
function buildApplyCommands(packageManager, target) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
69
|
+
if (target.kind === "parent-update") {
|
|
70
|
+
return buildParentUpdateApplyArgv(packageManager, target);
|
|
71
|
+
}
|
|
72
|
+
return buildInstallApplyArgv(packageManager, target);
|
|
73
|
+
}
|
|
74
|
+
function buildInstallApplyArgv(packageManager, target) {
|
|
75
|
+
const workspaces = (target.workspaces ?? []).filter(w => w !== ".");
|
|
76
|
+
const pkgArg = installPackageSpec(target);
|
|
77
|
+
const isDev = Boolean(target.isDev);
|
|
78
|
+
if (packageManager === "npm") {
|
|
79
|
+
const args = ["install"];
|
|
80
|
+
if (isDev)
|
|
81
|
+
args.push("-D");
|
|
82
|
+
for (const ws of workspaces) {
|
|
83
|
+
args.push("-w", ws);
|
|
84
|
+
}
|
|
85
|
+
args.push(pkgArg);
|
|
86
|
+
return [["npm", ...args]];
|
|
87
|
+
}
|
|
88
|
+
if (packageManager === "pnpm") {
|
|
89
|
+
const args = [];
|
|
90
|
+
for (const ws of workspaces) {
|
|
91
|
+
args.push("--filter", `./${ws}`);
|
|
92
|
+
}
|
|
93
|
+
args.push("add");
|
|
94
|
+
if (isDev)
|
|
95
|
+
args.push("-D");
|
|
96
|
+
args.push(pkgArg);
|
|
97
|
+
return [["pnpm", ...args]];
|
|
98
|
+
}
|
|
99
|
+
if (packageManager === "yarn") {
|
|
100
|
+
if (workspaces.length === 0) {
|
|
101
|
+
const args = ["add"];
|
|
102
|
+
if (isDev)
|
|
103
|
+
args.push("-D");
|
|
104
|
+
args.push(pkgArg);
|
|
105
|
+
return [["yarn", ...args]];
|
|
106
|
+
}
|
|
107
|
+
return workspaces.map(ws => {
|
|
108
|
+
const args = ["workspace", ws, "add"];
|
|
109
|
+
if (isDev)
|
|
110
|
+
args.push("-D");
|
|
111
|
+
args.push(pkgArg);
|
|
112
|
+
return ["yarn", ...args];
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
// bun
|
|
116
|
+
const args = ["add"];
|
|
117
|
+
if (isDev)
|
|
118
|
+
args.push("--dev");
|
|
119
|
+
for (const ws of workspaces) {
|
|
120
|
+
args.push("--filter", ws);
|
|
121
|
+
}
|
|
122
|
+
args.push(pkgArg);
|
|
123
|
+
return [["bun", ...args]];
|
|
124
|
+
}
|
|
125
|
+
function buildParentUpdateApplyArgv(packageManager, target) {
|
|
126
|
+
// Always build from structured fields. Never tokenize target.command: plan
|
|
127
|
+
// strings may be shell-quoted for display, and must not be re-parsed into argv.
|
|
128
|
+
const updatePkg = target.childPackage ?? target.package;
|
|
129
|
+
const workspaces = target.workspaces ?? [];
|
|
130
|
+
const nonRoot = workspaces.filter(w => w !== ".");
|
|
131
|
+
if (packageManager === "npm") {
|
|
132
|
+
const args = ["update"];
|
|
133
|
+
if (nonRoot.length === 1) {
|
|
134
|
+
args.push(`--workspace=${nonRoot[0]}`);
|
|
135
|
+
}
|
|
136
|
+
else if (nonRoot.length > 1) {
|
|
137
|
+
args.push("--workspaces");
|
|
138
|
+
}
|
|
139
|
+
args.push(updatePkg);
|
|
140
|
+
return [["npm", ...args]];
|
|
141
|
+
}
|
|
142
|
+
if (packageManager === "pnpm") {
|
|
143
|
+
if (workspaces.length === 0 || workspaces.length > 1) {
|
|
144
|
+
return [["pnpm", "update", "--recursive", "--no-save", updatePkg]];
|
|
145
|
+
}
|
|
146
|
+
const workspace = workspaces[0];
|
|
147
|
+
if (workspace === ".") {
|
|
148
|
+
return [["pnpm", "update", "--no-save", updatePkg]];
|
|
149
|
+
}
|
|
150
|
+
return [["pnpm", "--filter", `./${workspace}`, "update", "--no-save", updatePkg]];
|
|
151
|
+
}
|
|
152
|
+
if (packageManager === "yarn") {
|
|
153
|
+
if (nonRoot.length === 0) {
|
|
154
|
+
return [["yarn", "upgrade", updatePkg]];
|
|
155
|
+
}
|
|
156
|
+
return nonRoot.map(ws => ["yarn", "workspace", ws, "upgrade", updatePkg]);
|
|
157
|
+
}
|
|
158
|
+
return [["bun", "update", updatePkg]];
|
|
77
159
|
}
|
|
78
160
|
export function writeFixResultJson(json, projectPath) {
|
|
79
161
|
const outPath = path.join(projectPath, "cve-lite-fix-result.json");
|