|
1 | 1 | import assert from "node:assert"; |
2 | | -import { exec } from "node:child_process"; |
3 | 2 | import { existsSync } from "node:fs"; |
4 | 3 | import { join } from "node:path"; |
5 | 4 | import { widgetRoot } from "../widget/paths"; |
| 5 | +import { ensure, partition } from "../common"; |
| 6 | +import { exec } from "node:child_process"; |
6 | 7 |
|
7 | 8 | export type Report = { |
8 | 9 | auditReportVersion: 2; |
@@ -59,15 +60,28 @@ export type Vulnerability = { |
59 | 60 | range: string; |
60 | 61 | }; |
61 | 62 |
|
62 | | -export function collectVulnerabilities(report: Report, dependency: Dependency): Vulnerability[] { |
63 | | - const vulnerabilities = dependency.via.filter(v => typeof v !== "string"); |
64 | | - if (vulnerabilities.length > 0) { |
65 | | - return vulnerabilities; |
| 63 | +export function collectVulnerabilities(report: Report, rootDependency: PackageName): Vulnerability[] { |
| 64 | + const dependencies: PackageName[] = [rootDependency]; |
| 65 | + const dependenciesSeen: PackageName[] = []; |
| 66 | + const allVulnerabilities: Vulnerability[] = []; |
| 67 | + |
| 68 | + while (dependencies.length > 0) { |
| 69 | + const dependencyName = ensure(dependencies.shift()); |
| 70 | + dependenciesSeen.push(dependencyName); |
| 71 | + |
| 72 | + const [transients, vulnerabilities] = partition( |
| 73 | + report.vulnerabilities[dependencyName].via, |
| 74 | + v => typeof v === "string" |
| 75 | + ); |
| 76 | + |
| 77 | + if (vulnerabilities.length > 0) { |
| 78 | + allVulnerabilities.push(...vulnerabilities); |
| 79 | + continue; |
| 80 | + } |
| 81 | + dependencies.push(...transients.filter(d => !dependenciesSeen.includes(d))); |
66 | 82 | } |
67 | 83 |
|
68 | | - return dependency.via |
69 | | - .filter(v => typeof v === "string") |
70 | | - .flatMap(v => collectVulnerabilities(report, report.vulnerabilities[v])); |
| 84 | + return allVulnerabilities; |
71 | 85 | } |
72 | 86 |
|
73 | 87 | export async function run(): Promise<Report> { |
|
0 commit comments