Skip to content

Commit 887ca97

Browse files
fix(arborist): audit the non-isolated tree under the linked strategy (#9631)
Backport of #9625 to `release/v11`. Co-authored-by: Manzoor Wani <[email protected]>
1 parent 70f0ea5 commit 887ca97

3 files changed

Lines changed: 40 additions & 20 deletions

File tree

‎workspaces/arborist/lib/arborist/reify.js‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ module.exports = cls => class Reifier extends cls {
7777
#sparseTreeDirs = new Set()
7878
#sparseTreeRoots = new Set()
7979
#linkedActualForDiff = null
80+
// Under the linked strategy the audit runs against this non-isolated ideal tree.
81+
// The isolated tree's inventory has no queryable indexes and its edges route through symlinks, so auditing it reports no vulnerabilities.
82+
#linkedIdealForAudit = null
8083

8184
constructor (options) {
8285
super(options)
@@ -123,21 +126,29 @@ module.exports = cls => class Reifier extends cls {
123126
this.idealTree, this.actualTree
124127
)
125128
}
129+
// Keep the non-isolated tree so the quick audit can run against it.
130+
this.#linkedIdealForAudit = oldTree
126131
}
127-
await this[_diffTrees]()
128-
await this.#reifyPackages()
129-
if (linked) {
130-
// The sweep mutates node_modules on disk, so skip it for dry runs and lockfile-only installs (those modes also short-circuit #reifyPackages).
131-
// The sweep itself scopes to in-filter workspaces when a filter is active, so it's safe to run for filtered installs too.
132-
if (!this.options.dryRun && !this.options.packageLockOnly) {
133-
await this.#cleanOrphanedStoreEntries()
132+
try {
133+
await this[_diffTrees]()
134+
await this.#reifyPackages()
135+
if (linked) {
136+
// The sweep mutates node_modules on disk, so skip it for dry runs and lockfile-only installs (those modes also short-circuit #reifyPackages).
137+
// The sweep itself scopes to in-filter workspaces when a filter is active, so it's safe to run for filtered installs too.
138+
if (!this.options.dryRun && !this.options.packageLockOnly) {
139+
await this.#cleanOrphanedStoreEntries()
140+
}
141+
}
142+
} finally {
143+
// Restore the non-isolated tree so the lockfile is preserved and a reused Arborist never sees the isolated tree, even if reify throws.
144+
if (linked) {
145+
this.idealTree = oldTree
134146
}
135-
// swap back in the idealTree
136-
// so that the lockfile is preserved
137-
this.idealTree = oldTree
147+
// The quick audit has captured its tree synchronously by now, so drop the stashed references even on throw.
148+
this.#linkedIdealForAudit = null
149+
this.#linkedActualForDiff = null
138150
}
139151
await this[_saveIdealTree](options)
140-
this.#linkedActualForDiff = null
141152
// clean inert
142153
for (const node of this.idealTree.inventory.values()) {
143154
if (node.inert) {
@@ -1152,7 +1163,8 @@ module.exports = cls => class Reifier extends cls {
11521163
// with the reification, and be resolved at a later time.
11531164
const timeEnd = time.start('reify:audit')
11541165
const options = { ...this.options }
1155-
const tree = this.idealTree
1166+
// Under the linked strategy idealTree is the isolated tree, which the audit cannot traverse; audit the non-isolated tree instead.
1167+
const tree = this.#linkedIdealForAudit || this.idealTree
11561168

11571169
// if we're operating on a workspace, only audit the workspace deps
11581170
if (this.options.workspaces.length) {

‎workspaces/arborist/lib/isolated-classes.js‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
const CaseInsensitiveMap = require('./case-insensitive-map.js')
33
const { resolve } = require('node:path')
44

5-
// fake lib/inventory.js
6-
class IsolatedInventory extends Map {
7-
query () {
8-
return []
9-
}
10-
}
11-
125
// fake lib/node.js
136
class IsolatedNode {
147
binPaths = []
@@ -18,7 +11,7 @@ class IsolatedNode {
1811
fsChildren = new Set()
1912
hasShrinkwrap = false
2013
integrity = null
21-
inventory = new IsolatedInventory()
14+
inventory = new Map()
2215
isInStore = false
2316
inBundle = false
2417
isRegistryDependency = false

‎workspaces/arborist/test/arborist/reify.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,21 @@ t.test('packageLockOnly with linked strategy in workspaces', async t => {
221221
t.throws(() => fs.statSync(path + '/node_modules'), { code: 'ENOENT' })
222222
})
223223

224+
t.test('linked strategy audits the non-isolated tree', async t => {
225+
// The isolated tree has no queryable inventory, so auditing it reports nothing. The install-time audit must run against the non-isolated tree, matching standalone npm audit. https://github.com/npm/cli/issues/9609
226+
const src = resolve(fixtures, 'audit-one-vuln')
227+
// Copy into a throwaway dir since packageLockOnly rewrites the lockfile.
228+
const path = t.testdir({
229+
'package.json': fs.readFileSync(join(src, 'package.json'), 'utf8'),
230+
'package-lock.json': fs.readFileSync(join(src, 'package-lock.json'), 'utf8'),
231+
})
232+
const registry = createRegistry(t, true)
233+
registry.audit({ convert: true, results: require(join(src, 'audit.json')) })
234+
const arb = newArb({ path, audit: true, packageLockOnly: true, installStrategy: 'linked' })
235+
await arb.reify()
236+
t.ok(arb.auditReport.has('minimist'), 'vulnerable package reported under linked strategy')
237+
})
238+
224239
t.test('malformed package.json should not be overwritten', async t => {
225240
t.plan(2)
226241

0 commit comments

Comments
 (0)