Skip to content

Commit f7b056f

Browse files
authored
fix: clean up audit-report code (#8400)
1 parent f163d01 commit f7b056f

File tree

1 file changed

+35
-48
lines changed

1 file changed

+35
-48
lines changed

workspaces/arborist/lib/audit-report.js

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// an object representing the set of vulnerabilities in a tree
2-
/* eslint camelcase: "off" */
32

43
const localeCompare = require('@isaacs/string-locale-compare')('en')
54
const npa = require('npm-package-arg')
@@ -8,16 +7,15 @@ const pickManifest = require('npm-pick-manifest')
87
const Vuln = require('./vuln.js')
98
const Calculator = require('@npmcli/metavuln-calculator')
109

11-
const _getReport = Symbol('getReport')
12-
const _fixAvailable = Symbol('fixAvailable')
13-
const _checkTopNode = Symbol('checkTopNode')
14-
const _init = Symbol('init')
15-
const _omit = Symbol('omit')
1610
const { log, time } = require('proc-log')
1711

1812
const npmFetch = require('npm-registry-fetch')
1913

2014
class AuditReport extends Map {
15+
#omit
16+
error = null
17+
topVulns = new Map()
18+
2119
static load (tree, opts) {
2220
return new AuditReport(tree, opts).run()
2321
}
@@ -91,22 +89,18 @@ class AuditReport extends Map {
9189

9290
constructor (tree, opts = {}) {
9391
super()
94-
const { omit } = opts
95-
this[_omit] = new Set(omit || [])
96-
this.topVulns = new Map()
97-
92+
this.#omit = new Set(opts.omit || [])
9893
this.calculator = new Calculator(opts)
99-
this.error = null
10094
this.options = opts
10195
this.tree = tree
10296
this.filterSet = opts.filterSet
10397
}
10498

10599
async run () {
106-
this.report = await this[_getReport]()
100+
this.report = await this.#getReport()
107101
log.silly('audit report', this.report)
108102
if (this.report) {
109-
await this[_init]()
103+
await this.#init()
110104
}
111105
return this
112106
}
@@ -116,7 +110,7 @@ class AuditReport extends Map {
116110
return !!(vuln && vuln.isVulnerable(node))
117111
}
118112

119-
async [_init] () {
113+
async #init () {
120114
const timeEnd = time.start('auditReport:init')
121115

122116
const promises = []
@@ -171,7 +165,15 @@ class AuditReport extends Map {
171165
vuln.nodes.add(node)
172166
for (const { from: dep, spec } of node.edgesIn) {
173167
if (dep.isTop && !vuln.topNodes.has(dep)) {
174-
this[_checkTopNode](dep, vuln, spec)
168+
vuln.fixAvailable = this.#fixAvailable(vuln, spec)
169+
if (vuln.fixAvailable !== true) {
170+
// now we know the top node is vulnerable, and cannot be
171+
// upgraded out of the bad place without --force. But, there's
172+
// no need to add it to the actual vulns list, because nothing
173+
// depends on root.
174+
this.topVulns.set(vuln.name, vuln)
175+
vuln.topNodes.add(dep)
176+
}
175177
} else {
176178
// calculate a metavuln, if necessary
177179
const calc = this.calculator.calculate(dep.packageName, advisory)
@@ -214,33 +216,14 @@ class AuditReport extends Map {
214216
timeEnd()
215217
}
216218

217-
[_checkTopNode] (topNode, vuln, spec) {
218-
vuln.fixAvailable = this[_fixAvailable](topNode, vuln, spec)
219-
220-
if (vuln.fixAvailable !== true) {
221-
// now we know the top node is vulnerable, and cannot be
222-
// upgraded out of the bad place without --force. But, there's
223-
// no need to add it to the actual vulns list, because nothing
224-
// depends on root.
225-
this.topVulns.set(vuln.name, vuln)
226-
vuln.topNodes.add(topNode)
227-
}
228-
}
229-
230-
// check whether the top node is vulnerable.
231-
// check whether we can get out of the bad place with --force, and if
232-
// so, whether that update is SemVer Major
233-
[_fixAvailable] (topNode, vuln, spec) {
234-
// this will always be set to at least {name, versions:{}}
235-
const paku = vuln.packument
236-
219+
// given the spec, see if there is a fix available at all, and note whether or not it's a semver major fix or not (i.e. will need --force)
220+
#fixAvailable (vuln, spec) {
221+
// TODO we return true, false, OR an object here. this is probably a bad pattern.
237222
if (!vuln.testSpec(spec)) {
238223
return true
239224
}
240225

241-
// similarly, even if we HAVE a packument, but we're looking for it
242-
// somewhere other than the registry, and we got something vulnerable,
243-
// then we're stuck with it.
226+
// even if we HAVE a packument, if we're looking for it somewhere other than the registry and we have something vulnerable then we're stuck with it.
244227
const specObj = npa(spec)
245228
if (!specObj.registry) {
246229
return false
@@ -250,15 +233,13 @@ class AuditReport extends Map {
250233
spec = specObj.subSpec.rawSpec
251234
}
252235

253-
// We don't provide fixes for top nodes other than root, but we
254-
// still check to see if the node is fixable with a different version,
255-
// and if that is a semver major bump.
236+
// we don't provide fixes for top nodes other than root, but we still check to see if the node is fixable with a different version, and note if that is a semver major bump.
256237
try {
257238
const {
258239
_isSemVerMajor: isSemVerMajor,
259240
version,
260241
name,
261-
} = pickManifest(paku, spec, {
242+
} = pickManifest(vuln.packument, spec, {
262243
...this.options,
263244
before: null,
264245
avoid: vuln.range,
@@ -274,7 +255,7 @@ class AuditReport extends Map {
274255
throw new Error('do not call AuditReport.set() directly')
275256
}
276257

277-
async [_getReport] () {
258+
async #getReport () {
278259
// if we're not auditing, just return false
279260
if (this.options.audit === false || this.options.offline === true || this.tree.inventory.size === 1) {
280261
return null
@@ -312,11 +293,17 @@ class AuditReport extends Map {
312293

313294
// return true if we should audit this one
314295
shouldAudit (node) {
315-
return !node.version ? false
316-
: node.isRoot ? false
317-
: this.filterSet && this.filterSet.size !== 0 && !this.filterSet.has(node) ? false
318-
: this[_omit].size === 0 ? true
319-
: !node.shouldOmit(this[_omit])
296+
if (
297+
!node.version ||
298+
node.isRoot ||
299+
(this.filterSet && this.filterSet?.size !== 0 && !this.filterSet?.has(node))
300+
) {
301+
return false
302+
}
303+
if (this.#omit.size === 0) {
304+
return true
305+
}
306+
return !node.shouldOmit(this.#omit)
320307
}
321308

322309
prepareBulkData () {

0 commit comments

Comments
 (0)