cve-lite-cli 1.17.2 → 1.18.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/README.md +2 -0
- package/dist/advisory/osv-advisory-source.js +3 -2
- package/dist/cli/args.js +6 -3
- package/dist/cli/help.js +2 -0
- package/dist/cli/validate.js +22 -0
- package/dist/constants.js +2 -0
- package/dist/index.js +26 -189
- package/dist/output/formatters.js +9 -2
- package/dist/output/html-reporter.js +7 -2
- package/dist/output/printers.js +8 -1
- package/dist/parsers/bun-lock.js +19 -0
- package/dist/parsers/package-lock.js +23 -0
- package/dist/parsers/pnpm-lock.js +20 -0
- package/dist/remediation/fix-commands.js +54 -12
- package/dist/remediation/npm-transitive-resolution.js +63 -0
- package/dist/scanner.js +52 -1
- package/dist/usage/scanner.js +3 -2
- package/dist/utils/network.js +81 -16
- package/dist/utils/package-json.js +124 -0
- package/dist/utils/severity.js +14 -0
- package/dist/utils/time.js +30 -0
- package/dist/utils/update-check.js +102 -0
- package/package.json +1 -1
|
@@ -112,7 +112,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
112
112
|
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
113
113
|
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
114
114
|
targetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
115
|
-
displayTargetVersion: "
|
|
115
|
+
displayTargetVersion: "lockfile refresh",
|
|
116
116
|
scannedVersions: null,
|
|
117
117
|
knownVulnerableVersions: null,
|
|
118
118
|
kind: "parent-update",
|
|
@@ -120,8 +120,8 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
120
120
|
severity: finding.severity,
|
|
121
121
|
adjusted: false,
|
|
122
122
|
adjustmentNote: null,
|
|
123
|
-
reason:
|
|
124
|
-
command:
|
|
123
|
+
reason: `${finding.recommendedNpmTransitiveRemediation.package}@${finding.recommendedNpmTransitiveRemediation.currentVersion} already permits ${finding.pkg.name}@${finding.recommendedNpmTransitiveRemediation.targetChildVersion} — refreshing the lockfile is enough.`,
|
|
124
|
+
command: buildNpmUpdateCommand(finding.recommendedNpmTransitiveRemediation.package, finding.recommendedNpmTransitiveRemediation.workspaces),
|
|
125
125
|
usage: finding.usage ?? null,
|
|
126
126
|
});
|
|
127
127
|
continue;
|
|
@@ -186,12 +186,26 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
186
186
|
if (packageManager !== "npm" &&
|
|
187
187
|
finding.relationship === "transitive" &&
|
|
188
188
|
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
189
|
-
const
|
|
190
|
-
|
|
189
|
+
const lockfileRefreshCommand = packageManager === "pnpm"
|
|
190
|
+
? buildPnpmLockfileRefreshCommand(finding.pkg.name, finding.recommendedNpmTransitiveRemediation.workspaces)
|
|
191
|
+
: packageManager === "yarn"
|
|
192
|
+
? `yarn upgrade ${finding.pkg.name}`
|
|
193
|
+
: `bun update ${finding.pkg.name}`;
|
|
194
|
+
upsertTarget(targetsByPackage, {
|
|
191
195
|
package: finding.pkg.name,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
196
|
+
currentVersion: finding.pkg.version,
|
|
197
|
+
targetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
198
|
+
displayTargetVersion: "lockfile refresh",
|
|
199
|
+
scannedVersions: null,
|
|
200
|
+
knownVulnerableVersions: null,
|
|
201
|
+
kind: "parent-update",
|
|
202
|
+
urgent,
|
|
203
|
+
severity: finding.severity,
|
|
204
|
+
adjusted: false,
|
|
205
|
+
adjustmentNote: null,
|
|
206
|
+
reason: `${finding.pkg.name}@${finding.pkg.version} can be refreshed to ${finding.recommendedNpmTransitiveRemediation.targetChildVersion}+ — no parent upgrade needed.`,
|
|
207
|
+
command: lockfileRefreshCommand,
|
|
208
|
+
usage: finding.usage ?? null,
|
|
195
209
|
});
|
|
196
210
|
continue;
|
|
197
211
|
}
|
|
@@ -235,14 +249,18 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
235
249
|
const command = targets.length > 0
|
|
236
250
|
? buildCommandForTargets(targets, packageManager)
|
|
237
251
|
: null;
|
|
238
|
-
|
|
252
|
+
const plan = {
|
|
239
253
|
packageManager,
|
|
240
254
|
sourceLabel: packageManagerSourceLabel(scanInput),
|
|
241
255
|
command,
|
|
242
256
|
sections,
|
|
243
257
|
targets,
|
|
244
258
|
skipped,
|
|
259
|
+
coveredFindingCount: 0,
|
|
260
|
+
totalFindingCount: findings.length,
|
|
245
261
|
};
|
|
262
|
+
plan.coveredFindingCount = findings.filter(f => findSuggestedCommandForFinding(plan, f) !== null).length;
|
|
263
|
+
return plan;
|
|
246
264
|
}
|
|
247
265
|
function inferPackageManager(scanInput) {
|
|
248
266
|
if (scanInput.source === "package-lock")
|
|
@@ -255,6 +273,26 @@ function inferPackageManager(scanInput) {
|
|
|
255
273
|
return "bun";
|
|
256
274
|
return null;
|
|
257
275
|
}
|
|
276
|
+
function buildNpmUpdateCommand(pkg, workspaces) {
|
|
277
|
+
const nonRoot = (workspaces ?? []).filter(w => w !== ".");
|
|
278
|
+
if (nonRoot.length === 1) {
|
|
279
|
+
return `npm update --workspace=${nonRoot[0]} ${pkg}`;
|
|
280
|
+
}
|
|
281
|
+
if (nonRoot.length > 1) {
|
|
282
|
+
return `npm update --workspaces ${pkg}`;
|
|
283
|
+
}
|
|
284
|
+
return `npm update ${pkg}`;
|
|
285
|
+
}
|
|
286
|
+
function buildPnpmLockfileRefreshCommand(pkg, workspaces) {
|
|
287
|
+
if (!workspaces || workspaces.length === 0 || workspaces.length > 1) {
|
|
288
|
+
return `pnpm update --recursive --no-save ${pkg}`;
|
|
289
|
+
}
|
|
290
|
+
const workspace = workspaces[0];
|
|
291
|
+
if (workspace === ".") {
|
|
292
|
+
return `pnpm update --no-save ${pkg}`;
|
|
293
|
+
}
|
|
294
|
+
return `pnpm -C ${workspace} update --no-save ${pkg}`;
|
|
295
|
+
}
|
|
258
296
|
function commandPrefix(packageManager) {
|
|
259
297
|
if (packageManager === "npm")
|
|
260
298
|
return "npm install";
|
|
@@ -268,8 +306,8 @@ export function findSuggestedCommandForFinding(plan, finding) {
|
|
|
268
306
|
const target = plan.targets.find(item => {
|
|
269
307
|
if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
270
308
|
return (item.kind === "parent-update" &&
|
|
271
|
-
item.
|
|
272
|
-
item.
|
|
309
|
+
item.targetVersion === finding.recommendedNpmTransitiveRemediation.targetChildVersion &&
|
|
310
|
+
(item.package === finding.recommendedNpmTransitiveRemediation.package || item.package === finding.pkg.name));
|
|
273
311
|
}
|
|
274
312
|
if (finding.recommendedParentUpgrade) {
|
|
275
313
|
return (item.package === finding.recommendedParentUpgrade.package &&
|
|
@@ -382,7 +420,9 @@ function buildSections(targets, packageManager) {
|
|
|
382
420
|
const groups = [];
|
|
383
421
|
const sectionOrder = [
|
|
384
422
|
{ kind: "urgent", severity: "critical" },
|
|
423
|
+
{ kind: "parent-update", severity: "critical" },
|
|
385
424
|
{ kind: "urgent", severity: "high" },
|
|
425
|
+
{ kind: "parent-update", severity: "high" },
|
|
386
426
|
{ kind: "direct", severity: "medium" },
|
|
387
427
|
{ kind: "direct", severity: "low" },
|
|
388
428
|
{ kind: "direct", severity: "unknown" },
|
|
@@ -398,7 +438,9 @@ function buildSections(targets, packageManager) {
|
|
|
398
438
|
];
|
|
399
439
|
for (const entry of sectionOrder) {
|
|
400
440
|
const sectionTargets = targets.filter(target => {
|
|
401
|
-
const targetKind = target.
|
|
441
|
+
const targetKind = target.kind === "parent-update"
|
|
442
|
+
? "parent-update"
|
|
443
|
+
: target.urgent ? "urgent" : target.adjusted ? "direct-adjusted" : target.kind;
|
|
402
444
|
return targetKind === entry.kind && target.severity === entry.severity;
|
|
403
445
|
});
|
|
404
446
|
if (sectionTargets.length === 0)
|
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
|
|
2
2
|
import { fetchPackument } from "./npm-registry.js";
|
|
3
3
|
import { findSafeVersionWithinParentRange } from "./npm-transitive-graph.js";
|
|
4
|
+
export async function resolveTransitiveRemediationViaRegistry(args) {
|
|
5
|
+
if (args.finding.relationship !== "transitive")
|
|
6
|
+
return null;
|
|
7
|
+
const viaPath = getBestPath(args.finding);
|
|
8
|
+
if (!viaPath || viaPath.length < 3)
|
|
9
|
+
return null;
|
|
10
|
+
const directParentContext = resolveDirectParentContext(viaPath, args.packages, args.directDependencyNames);
|
|
11
|
+
if (!directParentContext)
|
|
12
|
+
return null;
|
|
13
|
+
const { directParentName, immediateParentName, directParent } = directParentContext;
|
|
14
|
+
if (directParentName !== immediateParentName)
|
|
15
|
+
return null;
|
|
16
|
+
if (!looksLikeVersion(directParent.version))
|
|
17
|
+
return null;
|
|
18
|
+
const vulnerableName = args.finding.pkg.name;
|
|
19
|
+
const fixHint = args.finding.validatedFirstFixedVersion ?? args.finding.firstFixedVersion;
|
|
20
|
+
const safeCandidates = await findSafeChildCandidates(vulnerableName, args.finding.pkg.version, fixHint);
|
|
21
|
+
if (safeCandidates.length === 0)
|
|
22
|
+
return null;
|
|
23
|
+
const packument = await fetchPackument(directParentName);
|
|
24
|
+
const parentManifest = packument?.versions?.[directParent.version];
|
|
25
|
+
const depRange = parentManifest?.dependencies?.[vulnerableName] ??
|
|
26
|
+
parentManifest?.optionalDependencies?.[vulnerableName];
|
|
27
|
+
if (!depRange)
|
|
28
|
+
return null;
|
|
29
|
+
const inRangeTarget = [...safeCandidates]
|
|
30
|
+
.sort(compareVersions)
|
|
31
|
+
.filter(v => versionSatisfiesRange(v, depRange))
|
|
32
|
+
.at(-1);
|
|
33
|
+
if (!inRangeTarget)
|
|
34
|
+
return null;
|
|
35
|
+
const workspaces = collectWorkspacesFromAllPaths(args.finding.dependencyPaths ?? [], args.workspaceMap);
|
|
36
|
+
return {
|
|
37
|
+
kind: "update-parent-within-range",
|
|
38
|
+
package: directParentName,
|
|
39
|
+
currentVersion: directParent.version,
|
|
40
|
+
targetChildVersion: inRangeTarget,
|
|
41
|
+
viaPath,
|
|
42
|
+
reason: `${directParentName}@${directParent.version} already allows ${vulnerableName}@${inRangeTarget} within the current dependency range`,
|
|
43
|
+
workspaces,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
4
46
|
export async function resolveNpmTransitiveRemediation(args) {
|
|
5
47
|
if (args.finding.relationship !== "transitive")
|
|
6
48
|
return null;
|
|
@@ -38,6 +80,7 @@ export async function resolveNpmTransitiveRemediation(args) {
|
|
|
38
80
|
candidates: safeCandidates,
|
|
39
81
|
});
|
|
40
82
|
if (inRangeTarget) {
|
|
83
|
+
const workspaces = collectWorkspacesFromAllPaths(args.finding.dependencyPaths ?? [], args.workspaceMap);
|
|
41
84
|
return {
|
|
42
85
|
kind: "update-parent-within-range",
|
|
43
86
|
package: directParentName,
|
|
@@ -45,6 +88,7 @@ export async function resolveNpmTransitiveRemediation(args) {
|
|
|
45
88
|
targetChildVersion: inRangeTarget,
|
|
46
89
|
viaPath,
|
|
47
90
|
reason: `${directParentName}@${directParent.version} already allows ${vulnerableName}@${inRangeTarget} within the current dependency range`,
|
|
91
|
+
workspaces,
|
|
48
92
|
};
|
|
49
93
|
}
|
|
50
94
|
// The upgrade-parent-to-version path needs the parent's published manifests
|
|
@@ -236,3 +280,22 @@ function parseCoreVersion(version) {
|
|
|
236
280
|
const [major = "0", minor = "0", patch = "0"] = version.split(/[+-]/)[0].split(".");
|
|
237
281
|
return [Number(major), Number(minor), Number(patch)];
|
|
238
282
|
}
|
|
283
|
+
function collectWorkspacesFromAllPaths(dependencyPaths, workspaceMap) {
|
|
284
|
+
if (!workspaceMap)
|
|
285
|
+
return undefined;
|
|
286
|
+
const workspaceSet = new Set();
|
|
287
|
+
for (const path of dependencyPaths) {
|
|
288
|
+
if (path.length !== 3)
|
|
289
|
+
continue;
|
|
290
|
+
const parentName = path[1];
|
|
291
|
+
if (!parentName)
|
|
292
|
+
continue;
|
|
293
|
+
const wsForParent = workspaceMap.get(parentName);
|
|
294
|
+
if (wsForParent) {
|
|
295
|
+
for (const ws of wsForParent) {
|
|
296
|
+
workspaceSet.add(ws);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return workspaceSet.size > 0 ? [...workspaceSet] : undefined;
|
|
301
|
+
}
|
package/dist/scanner.js
CHANGED
|
@@ -9,8 +9,11 @@ import { LocalAdvisoryDatabase } from "./advisory/local-db.js";
|
|
|
9
9
|
import { ADVISORY_DB_STALE_AFTER_MS, getDefaultAdvisoryDbPath } from "./advisory/osv-sync.js";
|
|
10
10
|
import { resolveRecommendedParentUpgrade } from "./remediation/parent-upgrade.js";
|
|
11
11
|
import { resolveLowestKnownNonVulnerableVersion, resolvePublishedFixVersion } from "./remediation/npm-registry.js";
|
|
12
|
-
import { resolveNpmTransitiveRemediation } from "./remediation/npm-transitive-resolution.js";
|
|
12
|
+
import { resolveNpmTransitiveRemediation, resolveTransitiveRemediationViaRegistry } from "./remediation/npm-transitive-resolution.js";
|
|
13
13
|
import { loadNpmLockGraph } from "./parsers/npm-lock-graph.js";
|
|
14
|
+
import { buildPnpmWorkspaceMap } from "./parsers/pnpm-lock.js";
|
|
15
|
+
import { buildNpmWorkspaceMap } from "./parsers/package-lock.js";
|
|
16
|
+
import { buildBunWorkspaceMap } from "./parsers/bun-lock.js";
|
|
14
17
|
export function createAdvisorySource(options) {
|
|
15
18
|
const offline = !!options?.offline || !!options?.offlineDb;
|
|
16
19
|
if (offline) {
|
|
@@ -191,6 +194,34 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
191
194
|
const npmTransitiveGraph = context?.scanSource === "package-lock" && context.scanFilePath
|
|
192
195
|
? createNpmTransitiveGraphFromLockfile(context.scanFilePath)
|
|
193
196
|
: null;
|
|
197
|
+
const npmWorkspaceMap = (() => {
|
|
198
|
+
try {
|
|
199
|
+
return context?.scanSource === "package-lock" && context.scanFilePath
|
|
200
|
+
? buildNpmWorkspaceMap(context.scanFilePath) : null;
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
})();
|
|
206
|
+
const pnpmWorkspaceMap = (() => {
|
|
207
|
+
try {
|
|
208
|
+
return context?.scanSource === "pnpm-lock" && context.scanFilePath
|
|
209
|
+
? buildPnpmWorkspaceMap(context.scanFilePath) : null;
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
})();
|
|
215
|
+
const bunWorkspaceMap = (() => {
|
|
216
|
+
try {
|
|
217
|
+
return context?.scanSource === "bun-lock" && context.scanFilePath
|
|
218
|
+
? buildBunWorkspaceMap(context.scanFilePath) : null;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
})();
|
|
224
|
+
const lockfileWorkspaceMap = pnpmWorkspaceMap ?? bunWorkspaceMap ?? null;
|
|
194
225
|
const npmRemediationCache = new Map();
|
|
195
226
|
const parentUpgradeCache = new Map();
|
|
196
227
|
const directValidationCount = offline ? 0 : findings.length;
|
|
@@ -233,6 +264,7 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
233
264
|
packages,
|
|
234
265
|
directDependencyNames: context?.directDependencyNames,
|
|
235
266
|
offline,
|
|
267
|
+
workspaceMap: npmWorkspaceMap,
|
|
236
268
|
});
|
|
237
269
|
npmRemediationCache.set(remediationCacheKey, finding.recommendedNpmTransitiveRemediation ?? null);
|
|
238
270
|
}
|
|
@@ -253,6 +285,25 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
253
285
|
continue;
|
|
254
286
|
}
|
|
255
287
|
}
|
|
288
|
+
if (!offline && !npmTransitiveGraph) {
|
|
289
|
+
if (npmRemediationCache.has(remediationCacheKey)) {
|
|
290
|
+
finding.recommendedNpmTransitiveRemediation =
|
|
291
|
+
npmRemediationCache.get(remediationCacheKey) ?? undefined;
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
finding.recommendedNpmTransitiveRemediation = await resolveTransitiveRemediationViaRegistry({
|
|
295
|
+
finding,
|
|
296
|
+
packages,
|
|
297
|
+
directDependencyNames: context?.directDependencyNames,
|
|
298
|
+
workspaceMap: lockfileWorkspaceMap,
|
|
299
|
+
});
|
|
300
|
+
npmRemediationCache.set(remediationCacheKey, finding.recommendedNpmTransitiveRemediation ?? null);
|
|
301
|
+
}
|
|
302
|
+
if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
303
|
+
finding.recommendedParentUpgrade = undefined;
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
256
307
|
if (parentUpgradeCache.has(remediationCacheKey)) {
|
|
257
308
|
finding.recommendedParentUpgrade = parentUpgradeCache.get(remediationCacheKey) ?? undefined;
|
|
258
309
|
}
|
package/dist/usage/scanner.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
|
|
3
|
+
// Shared with lockfile scanning in utils/file.ts; additions affect both code paths.
|
|
4
|
+
import { EXCLUDED_DIRS } from "../constants.js";
|
|
4
5
|
const ALLOWED_EXTS = new Set([".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs"]);
|
|
5
6
|
// Matches:
|
|
6
7
|
// import { foo } from 'pkg'
|
|
@@ -46,7 +47,7 @@ export function scanProjectForPackageUsage(projectPath, packagesToLookFor) {
|
|
|
46
47
|
return;
|
|
47
48
|
const fullPath = path.join(dir, entry.name);
|
|
48
49
|
if (entry.isDirectory()) {
|
|
49
|
-
if (!
|
|
50
|
+
if (!EXCLUDED_DIRS.has(entry.name) && !entry.name.startsWith(".")) {
|
|
50
51
|
walk(fullPath);
|
|
51
52
|
}
|
|
52
53
|
}
|
package/dist/utils/network.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export function extractErrorMessage(error) {
|
|
2
|
+
if (!(error instanceof Error))
|
|
3
|
+
return String(error);
|
|
4
|
+
const cause = error.cause;
|
|
5
|
+
if (cause instanceof Error)
|
|
6
|
+
return `${error.message}: ${extractErrorMessage(cause)}`;
|
|
7
|
+
return error.message;
|
|
8
|
+
}
|
|
1
9
|
export function sslCertificateErrorHint() {
|
|
2
10
|
return [
|
|
3
11
|
"Hint: SSL certificate error — your network may be using a corporate proxy that intercepts HTTPS traffic.",
|
|
@@ -14,26 +22,83 @@ export function blockedAdvisoryRequestHint() {
|
|
|
14
22
|
"Command to build the DB on a network-allowed machine: `cve-lite advisories sync --output /path/to/advisories.db`",
|
|
15
23
|
];
|
|
16
24
|
}
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
const OFFLINE_FALLBACK_LINES = [
|
|
26
|
+
" cve-lite . --offline",
|
|
27
|
+
" (requires a local advisory DB - run `cve-lite advisories sync` to build one)",
|
|
28
|
+
];
|
|
29
|
+
export function rateLimitAdvisoryRequestHint() {
|
|
19
30
|
return [
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
"Hint: OSV API rate limit reached. Wait a moment and retry, or scan offline:",
|
|
32
|
+
...OFFLINE_FALLBACK_LINES,
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
export function serverAdvisoryRequestHint() {
|
|
36
|
+
return [
|
|
37
|
+
"Hint: OSV API may be temporarily unavailable. Wait a moment and retry, or scan offline:",
|
|
38
|
+
...OFFLINE_FALLBACK_LINES,
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
const SSL_ERROR_CODES = new Set([
|
|
42
|
+
"SELF_SIGNED_CERT_IN_CHAIN",
|
|
43
|
+
"CERT_UNTRUSTED",
|
|
44
|
+
"UNABLE_TO_VERIFY_LEAF_SIGNATURE",
|
|
45
|
+
"DEPTH_ZERO_SELF_SIGNED_CERT",
|
|
46
|
+
"CERT_HAS_EXPIRED",
|
|
47
|
+
"UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
|
|
48
|
+
"ERR_TLS_CERT_ALTNAME_INVALID",
|
|
49
|
+
]);
|
|
50
|
+
const SSL_ERROR_MESSAGE_FRAGMENTS = [
|
|
51
|
+
"self signed certificate",
|
|
52
|
+
"self-signed certificate",
|
|
53
|
+
"certificate chain",
|
|
54
|
+
"certificate has expired",
|
|
55
|
+
"unable to verify the first certificate",
|
|
56
|
+
];
|
|
57
|
+
function finalErrorCause(message) {
|
|
58
|
+
const normalized = message.toLowerCase();
|
|
59
|
+
return normalized.split(":").pop()?.trim() ?? normalized;
|
|
60
|
+
}
|
|
61
|
+
function isOsvError(message) {
|
|
62
|
+
return message.toLowerCase().includes("osv");
|
|
63
|
+
}
|
|
64
|
+
function isRateLimitCause(finalCause) {
|
|
65
|
+
return /^429\b/.test(finalCause);
|
|
66
|
+
}
|
|
67
|
+
function isServerCause(finalCause) {
|
|
68
|
+
return /^5\d\d\b/.test(finalCause);
|
|
69
|
+
}
|
|
70
|
+
export function isSslCertificateError(error) {
|
|
71
|
+
if (!(error instanceof Error))
|
|
72
|
+
return false;
|
|
73
|
+
const code = error.code;
|
|
74
|
+
if (code && SSL_ERROR_CODES.has(code))
|
|
75
|
+
return true;
|
|
76
|
+
const cause = error.cause;
|
|
77
|
+
if (cause)
|
|
78
|
+
return isSslCertificateError(cause);
|
|
79
|
+
const normalized = error.message.toLowerCase();
|
|
80
|
+
return SSL_ERROR_MESSAGE_FRAGMENTS.some(f => normalized.includes(f));
|
|
81
|
+
}
|
|
82
|
+
export function isRateLimitError(message) {
|
|
83
|
+
if (!isOsvError(message)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return isRateLimitCause(finalErrorCause(message));
|
|
87
|
+
}
|
|
88
|
+
export function isServerError(message) {
|
|
89
|
+
if (!isOsvError(message)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return isServerCause(finalErrorCause(message));
|
|
30
93
|
}
|
|
31
94
|
export function isLikelyBlockedAdvisoryRequestError(message) {
|
|
32
|
-
if (!message
|
|
95
|
+
if (!isOsvError(message)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const finalCause = finalErrorCause(message);
|
|
99
|
+
if (isRateLimitCause(finalCause) || isServerCause(finalCause)) {
|
|
33
100
|
return false;
|
|
34
101
|
}
|
|
35
|
-
const normalized = message.toLowerCase();
|
|
36
|
-
const finalCause = normalized.split(":").pop()?.trim() ?? normalized;
|
|
37
102
|
const blockedIndicators = [
|
|
38
103
|
"access denied",
|
|
39
104
|
"blocked",
|
|
@@ -59,5 +124,5 @@ export function isLikelyBlockedAdvisoryRequestError(message) {
|
|
|
59
124
|
if (blockedIndicators.some(indicator => finalCause.includes(indicator))) {
|
|
60
125
|
return true;
|
|
61
126
|
}
|
|
62
|
-
return /^(401|403|407|408|
|
|
127
|
+
return /^(401|403|407|408|451)\b/.test(finalCause);
|
|
63
128
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import YAML from "yaml";
|
|
4
|
+
export function readDirectDependencyNames(projectPath, prodOnly) {
|
|
5
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
6
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const directNames = new Set();
|
|
11
|
+
const rootManifest = readPackageJsonObject(packageJsonPath);
|
|
12
|
+
if (!rootManifest) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
addDependencyNamesFromManifest(directNames, rootManifest, prodOnly);
|
|
16
|
+
const workspacePatterns = readWorkspacePatterns(rootManifest, projectPath);
|
|
17
|
+
for (const workspacePackageJsonPath of resolveWorkspacePackageJsonPaths(projectPath, workspacePatterns)) {
|
|
18
|
+
const workspaceManifest = readPackageJsonObject(workspacePackageJsonPath);
|
|
19
|
+
if (!workspaceManifest)
|
|
20
|
+
continue;
|
|
21
|
+
addDependencyNamesFromManifest(directNames, workspaceManifest, prodOnly);
|
|
22
|
+
}
|
|
23
|
+
return directNames;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function readPackageJsonObject(filePath) {
|
|
30
|
+
try {
|
|
31
|
+
const raw = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
32
|
+
return raw && typeof raw === "object" ? raw : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function addDependencyNamesFromManifest(target, manifest, prodOnly) {
|
|
39
|
+
const sections = [
|
|
40
|
+
isRecord(manifest.dependencies) ? manifest.dependencies : undefined,
|
|
41
|
+
isRecord(manifest.optionalDependencies) ? manifest.optionalDependencies : undefined,
|
|
42
|
+
];
|
|
43
|
+
if (!prodOnly) {
|
|
44
|
+
sections.push(isRecord(manifest.devDependencies) ? manifest.devDependencies : undefined);
|
|
45
|
+
}
|
|
46
|
+
for (const section of sections) {
|
|
47
|
+
if (!section)
|
|
48
|
+
continue;
|
|
49
|
+
for (const name of Object.keys(section)) {
|
|
50
|
+
target.add(name);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function readWorkspacePatterns(manifest, projectPath) {
|
|
55
|
+
const workspaces = manifest.workspaces;
|
|
56
|
+
if (Array.isArray(workspaces)) {
|
|
57
|
+
return workspaces.filter((value) => typeof value === "string");
|
|
58
|
+
}
|
|
59
|
+
if (isRecord(workspaces) && Array.isArray(workspaces.packages)) {
|
|
60
|
+
return workspaces.packages.filter((value) => typeof value === "string");
|
|
61
|
+
}
|
|
62
|
+
const pnpmWorkspaceYamlPath = path.join(projectPath, "pnpm-workspace.yaml");
|
|
63
|
+
if (fs.existsSync(pnpmWorkspaceYamlPath)) {
|
|
64
|
+
try {
|
|
65
|
+
const parsed = YAML.parse(fs.readFileSync(pnpmWorkspaceYamlPath, "utf8"));
|
|
66
|
+
const packages = parsed?.packages;
|
|
67
|
+
if (Array.isArray(packages)) {
|
|
68
|
+
return packages.filter((value) => typeof value === "string");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// ignore
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
function resolveWorkspacePackageJsonPaths(projectPath, patterns) {
|
|
78
|
+
const matches = new Set();
|
|
79
|
+
for (const pattern of patterns) {
|
|
80
|
+
const normalized = pattern.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
81
|
+
if (!normalized)
|
|
82
|
+
continue;
|
|
83
|
+
for (const relativeDir of expandWorkspacePattern(projectPath, normalized.split("/").filter(Boolean), "")) {
|
|
84
|
+
const packageJsonPath = path.join(projectPath, relativeDir, "package.json");
|
|
85
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
86
|
+
matches.add(packageJsonPath);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return [...matches];
|
|
91
|
+
}
|
|
92
|
+
function expandWorkspacePattern(projectPath, segments, currentRelativePath) {
|
|
93
|
+
if (segments.length === 0) {
|
|
94
|
+
return currentRelativePath ? [currentRelativePath] : [];
|
|
95
|
+
}
|
|
96
|
+
const [segment, ...rest] = segments;
|
|
97
|
+
if (segment === "*") {
|
|
98
|
+
const baseDir = currentRelativePath ? path.join(projectPath, currentRelativePath) : projectPath;
|
|
99
|
+
let entries = [];
|
|
100
|
+
try {
|
|
101
|
+
entries = fs.readdirSync(baseDir, { withFileTypes: true });
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
return entries
|
|
107
|
+
.filter(entry => entry.isDirectory())
|
|
108
|
+
.flatMap(entry => {
|
|
109
|
+
const nextRelativePath = currentRelativePath
|
|
110
|
+
? path.join(currentRelativePath, entry.name)
|
|
111
|
+
: entry.name;
|
|
112
|
+
return expandWorkspacePattern(projectPath, rest, nextRelativePath);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const nextRelativePath = currentRelativePath ? path.join(currentRelativePath, segment) : segment;
|
|
116
|
+
const nextAbsolutePath = path.join(projectPath, nextRelativePath);
|
|
117
|
+
if (!fs.existsSync(nextAbsolutePath) || !fs.statSync(nextAbsolutePath).isDirectory()) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
return expandWorkspacePattern(projectPath, rest, nextRelativePath);
|
|
121
|
+
}
|
|
122
|
+
export function isRecord(value) {
|
|
123
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
124
|
+
}
|
package/dist/utils/severity.js
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
export function countBySeverity(findings) {
|
|
2
|
+
const counts = {
|
|
3
|
+
none: 0,
|
|
4
|
+
low: 0,
|
|
5
|
+
medium: 0,
|
|
6
|
+
high: 0,
|
|
7
|
+
critical: 0,
|
|
8
|
+
unknown: 0,
|
|
9
|
+
};
|
|
10
|
+
for (const finding of findings) {
|
|
11
|
+
counts[finding.severity] += 1;
|
|
12
|
+
}
|
|
13
|
+
return counts;
|
|
14
|
+
}
|
|
1
15
|
export function severityToSarifLevel(severity) {
|
|
2
16
|
if (severity === "critical" || severity === "high")
|
|
3
17
|
return "error";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { chalk } from "./chalk.js";
|
|
2
|
+
export function formatAdvisoryDbFreshness(lastSyncAt) {
|
|
3
|
+
if (!lastSyncAt) {
|
|
4
|
+
return chalk.yellow("unknown");
|
|
5
|
+
}
|
|
6
|
+
const timestamp = Date.parse(lastSyncAt);
|
|
7
|
+
if (Number.isNaN(timestamp)) {
|
|
8
|
+
return chalk.yellow("unknown");
|
|
9
|
+
}
|
|
10
|
+
return `${relativeAge(timestamp)} ${chalk.gray(`(${lastSyncAt})`)}`;
|
|
11
|
+
}
|
|
12
|
+
export function relativeAge(timestamp) {
|
|
13
|
+
const deltaMs = Math.max(0, Date.now() - timestamp);
|
|
14
|
+
const minute = 60 * 1000;
|
|
15
|
+
const hour = 60 * minute;
|
|
16
|
+
const day = 24 * hour;
|
|
17
|
+
if (deltaMs < minute) {
|
|
18
|
+
return "just synced";
|
|
19
|
+
}
|
|
20
|
+
if (deltaMs < hour) {
|
|
21
|
+
const minutes = Math.floor(deltaMs / minute);
|
|
22
|
+
return `synced ${minutes} minute${minutes === 1 ? "" : "s"} ago`;
|
|
23
|
+
}
|
|
24
|
+
if (deltaMs < day) {
|
|
25
|
+
const hours = Math.floor(deltaMs / hour);
|
|
26
|
+
return `synced ${hours} hour${hours === 1 ? "" : "s"} ago`;
|
|
27
|
+
}
|
|
28
|
+
const days = Math.floor(deltaMs / day);
|
|
29
|
+
return `synced ${days} day${days === 1 ? "" : "s"} ago`;
|
|
30
|
+
}
|