|
| 1 | +import type { AuditOutput, Issue, IssueSeverity } from '@code-pushup/models'; |
| 2 | +import { objectToEntries } from '@code-pushup/utils'; |
| 3 | +import { |
| 4 | + PackageAuditLevel, |
| 5 | + PackageDependency, |
| 6 | + packageAuditLevels, |
| 7 | +} from '../../config'; |
| 8 | +import { NpmAuditResultJson, Vulnerabilities } from './types'; |
| 9 | + |
| 10 | +export function auditResultToAuditOutput( |
| 11 | + result: NpmAuditResultJson, |
| 12 | + dependenciesType: PackageDependency, |
| 13 | + auditLevelMapping: Record<PackageAuditLevel, IssueSeverity>, |
| 14 | +): AuditOutput { |
| 15 | + const issues = vulnerabilitiesToIssues( |
| 16 | + result.vulnerabilities, |
| 17 | + auditLevelMapping, |
| 18 | + ); |
| 19 | + return { |
| 20 | + slug: `npm-audit-${dependenciesType}`, |
| 21 | + score: result.metadata.vulnerabilities.total === 0 ? 1 : 0, |
| 22 | + value: result.metadata.vulnerabilities.total, |
| 23 | + displayValue: vulnerabilitiesToDisplayValue( |
| 24 | + result.metadata.vulnerabilities, |
| 25 | + ), |
| 26 | + ...(issues.length > 0 && { details: { issues } }), |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +export function vulnerabilitiesToDisplayValue( |
| 31 | + vulnerabilities: Record<PackageAuditLevel | 'total', number>, |
| 32 | +): string { |
| 33 | + if (vulnerabilities.total === 0) { |
| 34 | + return 'passed'; |
| 35 | + } |
| 36 | + |
| 37 | + const displayValue = packageAuditLevels |
| 38 | + .map(level => |
| 39 | + vulnerabilities[level] > 0 ? `${vulnerabilities[level]} ${level}` : '', |
| 40 | + ) |
| 41 | + .filter(text => text !== '') |
| 42 | + .join(', '); |
| 43 | + return `${displayValue} ${ |
| 44 | + vulnerabilities.total === 1 ? 'vulnerability' : 'vulnerabilities' |
| 45 | + }`; |
| 46 | +} |
| 47 | + |
| 48 | +export function vulnerabilitiesToIssues( |
| 49 | + vulnerabilities: Vulnerabilities, |
| 50 | + auditLevelMapping: Record<PackageAuditLevel, IssueSeverity>, |
| 51 | +): Issue[] { |
| 52 | + if (Object.keys(vulnerabilities).length === 0) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + return objectToEntries(vulnerabilities).map<Issue>(([, detail]) => { |
| 57 | + // Advisory details via can refer to another vulnerability |
| 58 | + // For now, only direct context is supported |
| 59 | + if ( |
| 60 | + Array.isArray(detail.via) && |
| 61 | + detail.via.length > 0 && |
| 62 | + typeof detail.via[0] === 'object' |
| 63 | + ) { |
| 64 | + return { |
| 65 | + message: `${detail.name} dependency has a vulnerability "${ |
| 66 | + detail.via[0].title |
| 67 | + }" for versions ${detail.range}. Fix is ${ |
| 68 | + detail.fixAvailable ? '' : 'not ' |
| 69 | + }available. More information [here](${detail.via[0].url})`, |
| 70 | + severity: auditLevelMapping[detail.severity], |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + message: `${detail.name} dependency has a vulnerability for versions ${ |
| 76 | + detail.range |
| 77 | + }. Fix is ${detail.fixAvailable ? '' : 'not '}available.`, |
| 78 | + severity: auditLevelMapping[detail.severity], |
| 79 | + }; |
| 80 | + }); |
| 81 | +} |
0 commit comments