|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | +const { promisify } = require('util') |
| 5 | +const writeFile = promisify(require('fs').writeFile) |
| 6 | +const { |
| 7 | + summaryInfo, |
| 8 | + filterVulns, |
| 9 | + SEVERITY_RMAP |
| 10 | +} = require('./util') |
| 11 | +const { |
| 12 | + COLORS, |
| 13 | + success, |
| 14 | + formatError |
| 15 | +} = require('../ncm-style') |
| 16 | +const L = console.log |
| 17 | + |
| 18 | +module.exports = htmlReport |
| 19 | + |
| 20 | +async function htmlReport (report, whitelist, dir, output) { |
| 21 | + if (output === true) output = path.join(process.cwd(), `ncm-report-${Date.now()}.html`) |
| 22 | + |
| 23 | + const { riskCount, insecureModules, complianceCount, securityCount } = summaryInfo(report) |
| 24 | + |
| 25 | + /* Define embedded CSS report styles */ |
| 26 | + const reportStyles = ` |
| 27 | + body { |
| 28 | + background: ${COLORS.base.match(/(#[a-zA-Z0-9]*)/)[0]}; |
| 29 | + padding: 10px; |
| 30 | + } |
| 31 | + p { |
| 32 | + color: white; |
| 33 | + } |
| 34 | + .summary { |
| 35 | + text-align: left; |
| 36 | + font-size: 14pt; |
| 37 | + color: #ffffff; |
| 38 | + } |
| 39 | + .module-list { |
| 40 | +
|
| 41 | + } |
| 42 | + .module-element { |
| 43 | + padding: 0px; |
| 44 | + margin: 0px; |
| 45 | + font-size: 10pt; |
| 46 | + } |
| 47 | + ` |
| 48 | + |
| 49 | + /* Begin body with report summary */ |
| 50 | + let reportBody = ` |
| 51 | + <h1 class="">${path.basename(dir) || 'NCM'} Report</h1> |
| 52 | + <h6>Powered by <a href="https://docs.nodesource.com/ncmv2/docs">Node Certified Modules v2</a></h6> |
| 53 | + <div class="summary"> |
| 54 | + <div style=""><b>${report.length}</b> packages checked</div> |
| 55 | + <br> |
| 56 | + <div style="color:${COLORS.red.match(/(#[a-zA-Z0-9]*)/)[0]}"><b>${riskCount[4]}</b> Critical Risk</div> |
| 57 | + <div style="color:${COLORS.orange.match(/(#[a-zA-Z0-9]*)/)[0]}"><b>${riskCount[3]}</b> High Risk</div> |
| 58 | + <div style="color:${COLORS.yellow.match(/(#[a-zA-Z0-9]*)/)[0]}"><b>${riskCount[2]}</b> Medium Risk</div> |
| 59 | + <div style="color:${COLORS.light1.match(/(#[a-zA-Z0-9]*)/)[0]}"><b>${riskCount[1]}</b> Low Risk</div> |
| 60 | + <br> |
| 61 | + <div><b>${securityCount}</b> security vulnerabilities found across <b>${insecureModules}</b> modules</div> |
| 62 | + <div><b>${complianceCount}</b> noncompliant modules found</div> |
| 63 | + ${(whitelist.length > 0 ? `<div><b>${whitelist.length}</b> used modules whitelisted </div>` : '')} |
| 64 | + </div> |
| 65 | + ` |
| 66 | + |
| 67 | + /* Whitelisted Modules */ |
| 68 | + reportBody += ` |
| 69 | + <a data-toggle="collapse" href="#collapseWL" data-target="#collapseWL"> |
| 70 | + <h2>Whitelisted Modules</h2> |
| 71 | + </a> |
| 72 | + <div class="module-list collapse" id="collapseWL">` |
| 73 | + for (const pkg of whitelist) { |
| 74 | + reportBody += formatSegment(pkg) |
| 75 | + } |
| 76 | + reportBody += '</div>' |
| 77 | + |
| 78 | + /* Non-whitelisted Modules */ |
| 79 | + reportBody += ` |
| 80 | + <h2>Non-whitelisted Modules</h2> |
| 81 | + <div class="module-list">` |
| 82 | + |
| 83 | + /* Critical risk */ |
| 84 | + reportBody += ` |
| 85 | + <a data-toggle="collapse" href="#collapseCritRisk" data-target="#collapseCritRisk"> |
| 86 | + <h4>Critical Risk Modules</h4> |
| 87 | + </a> |
| 88 | + <div class="module-list collapse" id="collapseCritRisk">` |
| 89 | + for (const pkg of report.filter(pkg => pkg.maxSeverity === 4)) { |
| 90 | + reportBody += formatSegment(pkg) |
| 91 | + } |
| 92 | + reportBody += '</div>' |
| 93 | + |
| 94 | + /* High risk */ |
| 95 | + reportBody += ` |
| 96 | + <a data-toggle="collapse" href="#collapseHighRisk" data-target="#collapseHighRisk"> |
| 97 | + <h4>High Risk Modules</h4> |
| 98 | + </a> |
| 99 | + <div class="module-list collapse" id="collapseHighRisk">` |
| 100 | + for (const pkg of report.filter(pkg => pkg.maxSeverity === 3)) { |
| 101 | + reportBody += formatSegment(pkg) |
| 102 | + } |
| 103 | + reportBody += '</div>' |
| 104 | + |
| 105 | + /* Medium risk */ |
| 106 | + reportBody += ` |
| 107 | + <a data-toggle="collapse" href="#collapseMedRisk" data-target="#collapseMedRisk"> |
| 108 | + <h4>Medium Risk Modules</h4> |
| 109 | + </a> |
| 110 | + <div class="module-list collapse" id="collapseMedRisk">` |
| 111 | + for (const pkg of report.filter(pkg => pkg.maxSeverity === 2)) { |
| 112 | + reportBody += formatSegment(pkg) |
| 113 | + } |
| 114 | + reportBody += '</div>' |
| 115 | + |
| 116 | + /* Low risk */ |
| 117 | + reportBody += ` |
| 118 | + <a data-toggle="collapse" href="#collapseLowRisk" data-target="#collapseLowRisk"> |
| 119 | + <h4>Low Risk Modules</h4> |
| 120 | + </a> |
| 121 | + <div class="module-list collapse" id="collapseLowRisk">` |
| 122 | + for (const pkg of report.filter(pkg => pkg.maxSeverity === 1)) { |
| 123 | + reportBody += formatSegment(pkg) |
| 124 | + } |
| 125 | + reportBody += '</div>' |
| 126 | + |
| 127 | + /* None risk */ |
| 128 | + reportBody += ` |
| 129 | + <a data-toggle="collapse" href="#collapseNoneRisk" data-target="#collapseNoneRisk"> |
| 130 | + <h4>No Risk Modules</h4> |
| 131 | + </a> |
| 132 | + <div class="module-list collapse" id="collapseNoneRisk">` |
| 133 | + for (const pkg of report.filter(pkg => pkg.maxSeverity === 0)) { |
| 134 | + reportBody += formatSegment(pkg) |
| 135 | + } |
| 136 | + reportBody += '</div>' |
| 137 | + |
| 138 | + reportBody += '</div>' |
| 139 | + |
| 140 | + /* Format final HTML report */ |
| 141 | + const html = ` |
| 142 | + <!DOCTYPE html> |
| 143 | + <html> |
| 144 | + <head> |
| 145 | + <title>${path.basename(dir) || 'NCM'} Report</title> |
| 146 | + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> |
| 147 | + <style type="text/css"> |
| 148 | + ${reportStyles} |
| 149 | + </style> |
| 150 | + </head> |
| 151 | + <body> |
| 152 | + ${reportBody} |
| 153 | + <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> |
| 154 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> |
| 155 | + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> |
| 156 | + </body> |
| 157 | + </html> |
| 158 | + ` |
| 159 | + |
| 160 | + /* Write report to file */ |
| 161 | + try { |
| 162 | + await writeFile(output, html) |
| 163 | + L() |
| 164 | + L(success(`Wrote HTML report to: ${output}`)) |
| 165 | + L() |
| 166 | + } catch (error) { |
| 167 | + L() |
| 168 | + L(formatError(`Unable to write HTML report to: ${output}`, error)) |
| 169 | + L() |
| 170 | + process.exitCode = 1 |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function formatSegment (pkg) { |
| 175 | + const { name, version, maxSeverity, failures, license } = pkg |
| 176 | + const pkgVulns = filterVulns(failures).map(v => v === 0 ? '' : `${v} ${['Critical', 'High', 'Medium', 'Low'][v]}`) |
| 177 | + const pkgLicense = license && license.data && license.data.spdx ? license.data.spdx : 'UNKNOWN' |
| 178 | + const pkgSeverity = SEVERITY_RMAP[maxSeverity] |
| 179 | + |
| 180 | + const segment = ` |
| 181 | + <div class="row module-element"> |
| 182 | + <div class="col-4" style="display:inline-block;">${name}@${version}</div> |
| 183 | + <div class="col-2" style="display:inline-block;">Risk: ${pkgSeverity}</div> |
| 184 | + <div class="col-3" style="display:inline-block;">License: ${pkgLicense}</div> |
| 185 | + <div class="col-3" style="display:inline-block;"> |
| 186 | + Vulnerabilities: ${pkgVulns.join('').length === 0 ? 'None' : pkgVulns.join(' ')} |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + ` |
| 190 | + |
| 191 | + return segment |
| 192 | +} |
0 commit comments