| 1 |
'use strict' |
| 2 |
|
| 3 |
const colors = require('../colors.js') |
| 4 |
const install = require('./install.js') |
| 5 |
|
| 6 |
module.exports = (data, { chalk }) => { |
| 7 |
const summary = install.summary(data, { chalk }) |
| 8 |
const none = data.metadata.vulnerabilities.total === 0 |
| 9 |
return none ? summary : fullReport(data, { chalk, summary }) |
| 10 |
} |
| 11 |
|
| 12 |
const fullReport = (data, { chalk, summary }) => { |
| 13 |
const c = colors(chalk) |
| 14 |
const output = [c.white('# npm audit report'), ''] |
| 15 |
|
| 16 |
const printed = new Set() |
| 17 |
for (const [, vuln] of Object.entries(data.vulnerabilities)) { |
| 18 |
// only print starting from the top-level advisories |
| 19 |
if (vuln.via.filter(v => typeof v !== 'string').length !== 0) { |
| 20 |
output.push(printVuln(vuln, c, data.vulnerabilities, printed)) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
output.push(summary) |
| 25 |
|
| 26 |
return output.join('\n') |
| 27 |
} |
| 28 |
|
| 29 |
const printVuln = (vuln, c, vulnerabilities, printed, indent = '') => { |
| 30 |
if (printed.has(vuln)) { |
| 31 |
return null |
| 32 |
} |
| 33 |
|
| 34 |
printed.add(vuln) |
| 35 |
const output = [] |
| 36 |
|
| 37 |
output.push(c.white(vuln.name) + ' ' + vuln.range) |
| 38 |
|
| 39 |
if (indent === '' && (vuln.severity !== 'low' || vuln.severity === 'info')) { |
| 40 |
output.push(`Severity: ${c.severity(vuln.severity)}`) |
| 41 |
} |
| 42 |
|
| 43 |
for (const via of vuln.via) { |
| 44 |
if (typeof via === 'string') { |
| 45 |
output.push(`Depends on vulnerable versions of ${c.white(via)}`) |
| 46 |
} else if (indent === '') { |
| 47 |
output.push(`${c.white(via.title)} - ${via.url}`) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if (indent === '') { |
| 52 |
const { fixAvailable: fa } = vuln |
| 53 |
if (fa === false) { |
| 54 |
output.push(c.red('No fix available')) |
| 55 |
} else if (fa === true) { |
| 56 |
output.push(c.green('fix available') + ' via `npm audit fix`') |
| 57 |
} else { |
| 58 |
/* istanbul ignore else - should be impossible, just being cautious */ |
| 59 |
if (typeof fa === 'object' && indent === '') { |
| 60 |
output.push( |
| 61 |
`${c.yellow('fix available')} via \`npm audit fix --force\``, |
| 62 |
`Will install ${fa.name}@${fa.version}` + |
| 63 |
`, which is ${fa.isSemVerMajor ? 'a breaking change' : |
| 64 |
'outside the stated dependency range'}` |
| 65 |
) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
for (const path of vuln.nodes) { |
| 71 |
output.push(c.dim(path)) |
| 72 |
} |
| 73 |
|
| 74 |
for (const effect of vuln.effects) { |
| 75 |
const e = printVuln(vulnerabilities[effect], c, vulnerabilities, printed, ' ') |
| 76 |
if (e) { |
| 77 |
output.push(...e.split('\n')) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (indent === '') { |
| 82 |
output.push('') |
| 83 |
} |
| 84 |
|
| 85 |
return output.map(l => `${indent}${l}`).join('\n') |
| 86 |
} |