Skip to content

Commit

Permalink
chore: Fix audit-deps script on the new npm audit format introduced i…
Browse files Browse the repository at this point in the history
…n npm v8 (#2471)
  • Loading branch information
rpl authored Jul 26, 2022
1 parent 014b669 commit 5e3b214
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions scripts/audit-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,63 @@ if (auditReport) {
}
}

for (const advId of Object.keys(auditReport.advisories)) {
const adv = auditReport.advisories[advId];

if (exceptions.includes(adv.url)) {
ignoredIssues.push(adv);
continue;
if (auditReport.auditReportVersion > 2) {
// Throw a more clear error when a new format that this script does not expect
// has been introduced.
console.error(
'ERROR: npm audit JSON is using a new format not yet supported.',
'\nPlease file a bug in the github repository and attach the following JSON data sample to it:',
`\n\n${JSON.stringify(auditReport, null, 2)}`
);
} else if (auditReport.auditReportVersion === 2) {
// New npm audit json format introduced in npm v8.
for (const vulnerablePackage of Object.keys(auditReport.vulnerabilities)) {
const item = auditReport.vulnerabilities[vulnerablePackage];

if (item.via.every((via) => exceptions.includes(via.url))) {
ignoredIssues.push(item);
continue;
}
blockingIssues.push(item);
}
} else {
// Old npm audit json format for npm versions < npm v8
for (const advId of Object.keys(auditReport.advisories)) {
const adv = auditReport.advisories[advId];

if (exceptions.includes(adv.url)) {
ignoredIssues.push(adv);
continue;
}
blockingIssues.push(adv);
}
blockingIssues.push(adv);
}
}

// Reporting.

function formatFinding(desc) {
const details = `(dev: ${desc.dev}, optional: ${desc.optional}, bundled: ${desc.bundled})`;
return `${desc.version} ${details}\n ${desc.paths.join('\n ')}`;
function formatAdvisoryV1(adv) {
function formatFinding(desc) {
return `${desc.version}, paths: ${desc.paths.join(', ')}`;
}
const findings = adv.findings.map(formatFinding).map((msg) => ` ${msg}`).join('\n');
return `${adv.module_name} (${adv.url}):\n${findings}`;
}

function formatAdvisoryV2(adv) {
function formatVia(via) {
return `${via.url}\n ${via.dependency} ${via.range}\n ${via.title}`;
}
const entryVia = adv.via.map(formatVia).map((msg) => ` ${msg}`).join('\n');
const fixAvailable = Boolean(adv.fixAvailable);
const entryDetails = `isDirect: ${adv.isDirect}, severity: ${adv.severity}, fixAvailable: ${fixAvailable}`;
return `${adv.name} (${entryDetails}):\n${entryVia}`;
}

function formatAdvisory(adv) {
const findings = adv.findings.map(formatFinding).map((msg) => ` ${msg}`).join('\n');
return `${adv.module_name} (${adv.url}):\n${findings}`;
return auditReport.auditReportVersion === 2
? formatAdvisoryV2(adv)
: formatAdvisoryV1(adv);
}

if (ignoredIssues.length > 0) {
Expand Down

0 comments on commit 5e3b214

Please sign in to comment.