Skip to content

Commit d6c6122

Browse files
martinrrmCopilot
andauthored
fix(arborist): match allowScripts keys for local paths (#9914)
## Summary Fixes `allowScripts` matching for local file and tarball dependencies, particularly on Windows. Arborist stores a resolved Windows file source in this form: ```text file:C:\project\vendor\pkg.tgz ``` However,  `npm-package-arg` parses an equivalent policy key into: `saveSpec: file:C:/project/vendor/pkg.tgz` `fetchSpec: C:\project\vendor\pkg.tgz` The existing matcher compared `node.resolved` only with `saveSpec` and `fetchSpec`. Neither comparison could match because one uses different separators and the other lacks the `file:`  prefix. ## Changes • Match local file and directory sources against `file:${parsed.fetchSpec}`, which is the same representation produced by Arborist's `consistentResolve()`. • Resolve relative `allowScripts` file keys from the Arborist project root rather than the process working directory. • Reuse the Arborist file/directory matcher in `allow-scripts-writer`, keeping runtime enforcement and policy updates consistent. • Preserve the existing exact matching behavior for remote URLs. • Preserve deny-wins behavior when an existing local-file policy entry is `false`. The implementation intentionally does not globally normalize path separators. On POSIX systems, a backslash can be a literal filename character, so replacing every  `\`  with  `/`  could cause two different sources to share an approval. The change also does not modify dependency resolution, lockfile generation, fetching, linking, or `consistentResolve()`. ## Security considerations Local file and tarball dependencies continue to require an exact source identity. This change does not: • Match local packages by their self-reported name or version. • Case-fold paths. • Match by basename. • Resolve paths through `realpath()`. • Treat remote URLs as local file sources. • Change registry or Git identity matching. This preserves the manifest-confusion protections in `allowScripts` while recognizing the exact Windows representation npm already generates. ## Testing Added regression coverage for: • Backslash and forward-slash absolute Windows keys matching the same local tarball. • Relative keys resolving from the project root. • `versionedKeyFor()` producing a key accepted by `isScriptAllowed()`. • Different local paths remaining unmatched. • Existing `false` file entries continuing to block approval. • Windows UNC paths matching through the same `file:${fetchSpec}` representation. • POSIX filenames containing literal backslashes remaining distinct. • Remote URL policies not matching `file:  sources`. • `npm install-scripts prune` retaining valid local-file entries. The focused Arborist matcher, policy-writer, and prune tests pass locally. Native Windows path and UNC behavior is covered by platform-specific tests and will run in the repository's Windows CI matrix. Fixes #9900 --------- Co-authored-by: Copilot <[email protected]>
1 parent 6e40f73 commit d6c6122

5 files changed

Lines changed: 171 additions & 4 deletions

File tree

‎lib/utils/allow-scripts-writer.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const npa = require('npm-package-arg')
22
const { log } = require('proc-log')
33
const {
44
getTrustedRegistryIdentity,
5+
matchFileOrDir,
56
resolvedSourceSpecs,
67
} = require('@npmcli/arborist/lib/script-allowed.js')
78

@@ -150,7 +151,7 @@ const isNameOnlyKey = (key) => {
150151
const keyTargetsNode = (key, node) => {
151152
let parsed
152153
try {
153-
parsed = npa(key)
154+
parsed = npa(key, node?.root?.path)
154155
} catch {
155156
return false
156157
}
@@ -179,6 +180,7 @@ const keyTargetsNode = (key, node) => {
179180
}
180181
case 'file':
181182
case 'directory':
183+
return matchFileOrDir(node, parsed)
182184
case 'remote':
183185
return resolvedSourceSpecs(node)
184186
.some(resolved => resolved === parsed.saveSpec || resolved === parsed.fetchSpec)

‎test/lib/utils/allow-scripts-prune.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const t = require('tap')
2+
const path = require('node:path')
23
const { classifyUnusedEntries } = require('../../../lib/utils/allow-scripts-prune.js')
34

45
// Minimal registry node: `matches` derives name/version from the resolved URL.
@@ -29,6 +30,26 @@ t.test('keeps entries that match an installed package with scripts', t => {
2930
t.end()
3031
})
3132

33+
t.test('keeps a local file key matching its absolute resolved source', t => {
34+
const rootPath = path.resolve('project')
35+
const key = `file:${path.resolve(rootPath, 'local.tgz')}`
36+
const local = {
37+
name: 'local',
38+
version: '1.0.0',
39+
resolved: key,
40+
root: { path: rootPath },
41+
isRegistryDependency: false,
42+
}
43+
const { remaining, removed } = classifyUnusedEntries(
44+
{ [key]: true },
45+
[{ node: local, hasScripts: true }]
46+
)
47+
48+
t.same(remaining, { [key]: true })
49+
t.same(removed, [])
50+
t.end()
51+
})
52+
3253
t.test('removes entries for packages no longer installed', t => {
3354
const { remaining, removed } = classifyUnusedEntries(
3455
{ canvas: true, gone: true },

‎test/lib/utils/allow-scripts-writer.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const t = require('tap')
22
const path = require('node:path')
3+
const isScriptAllowed = require('../../../workspaces/arborist/lib/script-allowed.js')
34
const {
45
applyApprovalForPackage,
56
applyDenyForPackage,
7+
keyTargetsNode,
68
nameKeyFor,
79
versionedKeyFor,
810
isSingleVersionPin,
@@ -379,6 +381,21 @@ t.test('applyApprovalForPackage — file dep uses resolved as both keys', async
379381
t.strictSame(allowScripts, { 'file:../local': true })
380382
})
381383

384+
t.test('versionedKeyFor — local file key round-trips through policy matching', async t => {
385+
const rootPath = path.resolve('project')
386+
const local = {
387+
name: 'local',
388+
packageName: 'local',
389+
version: '1.0.0',
390+
resolved: `file:${path.resolve(rootPath, 'local.tgz')}`,
391+
root: { path: rootPath },
392+
isRegistryDependency: false,
393+
}
394+
const key = versionedKeyFor(local)
395+
396+
t.equal(isScriptAllowed(local, { [key]: true }), true)
397+
})
398+
382399
t.test('applyApprovalForPackage — empty nodes returns unchanged', async t => {
383400
const { allowScripts, changes } = applyApprovalForPackage({ x: true }, [], { pin: true })
384401
t.strictSame(allowScripts, { x: true })
@@ -493,6 +510,29 @@ t.test('applyApprovalForPackage — file dep with deny entry blocks approval', a
493510
t.match(warning, /denied|versioned deny/)
494511
})
495512

513+
t.test('applyApprovalForPackage — relative file deny matches absolute resolved', async t => {
514+
const rootPath = path.resolve('project')
515+
const resolved = `file:${path.resolve(rootPath, 'local.tgz')}`
516+
const local = {
517+
name: 'local',
518+
packageName: 'local',
519+
version: '1.0.0',
520+
resolved,
521+
root: { path: rootPath },
522+
isRegistryDependency: false,
523+
}
524+
const existing = { 'file:local.tgz': false }
525+
const { allowScripts, changes, warning } = applyApprovalForPackage(
526+
existing,
527+
[local],
528+
{ pin: true }
529+
)
530+
531+
t.strictSame(allowScripts, existing)
532+
t.strictSame(changes, [])
533+
t.match(warning, /denied|versioned deny/)
534+
})
535+
496536
t.test('applyApprovalForPackage — remote tarball deny blocks approval', async t => {
497537
const remote = { name: 'pkg', packageName: 'pkg', version: '1.0.0', resolved: 'https://example.com/pkg.tgz' }
498538
const { warning } = applyApprovalForPackage(
@@ -501,6 +541,7 @@ t.test('applyApprovalForPackage — remote tarball deny blocks approval', async
501541
{ pin: true }
502542
)
503543
t.match(warning, /denied|versioned deny/)
544+
t.equal(keyTargetsNode('https://example.com/other.tgz', remote), false)
504545
})
505546

506547
t.test('applyApprovalForPackage — no-pin with no name produces no-op', async t => {

‎workspaces/arborist/lib/script-allowed.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const isScriptAllowed = (node, policy) => {
7171
const matches = (node, key, failClosed) => {
7272
let parsed
7373
try {
74-
parsed = npa(key)
74+
parsed = npa(key, node?.root?.path)
7575
} catch {
7676
return false
7777
}
@@ -328,8 +328,15 @@ const matchGit = (node, parsed) => {
328328
}
329329

330330
const matchFileOrDir = (node, parsed) => {
331+
// consistentResolve stores local sources as `file:` plus npa's absolute,
332+
// platform-native fetchSpec.
333+
const absoluteFileSpec = parsed.fetchSpec && `file:${parsed.fetchSpec}`
331334
return resolvedSourceSpecs(node)
332-
.some(resolved => resolved === parsed.saveSpec || resolved === parsed.fetchSpec)
335+
.some(resolved =>
336+
resolved === parsed.saveSpec ||
337+
resolved === parsed.fetchSpec ||
338+
resolved === absoluteFileSpec
339+
)
333340
}
334341

335342
const matchRemote = (node, parsed) => {
@@ -381,4 +388,5 @@ module.exports.matches = matches
381388
module.exports.isExactVersionDisjunction = isExactVersionDisjunction
382389
module.exports.getTrustedRegistryIdentity = getTrustedRegistryIdentity
383390
module.exports.resolvedSourceSpecs = resolvedSourceSpecs
391+
module.exports.matchFileOrDir = matchFileOrDir
384392
module.exports.trustedDisplay = trustedDisplay

‎workspaces/arborist/test/script-allowed.js‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const t = require('tap')
22
const isScriptAllowed = require('../lib/script-allowed.js')
3-
const { trustedDisplay } = isScriptAllowed
3+
const { matchFileOrDir, trustedDisplay } = isScriptAllowed
44

55
// Test nodes default to a consistent registry-tarball shape: the resolved
66
// URL's name+version match the supplied name+version. Tests that need to
@@ -196,6 +196,90 @@ t.test('local tarball key — npa parses *.tgz paths as type=file', t => {
196196
t.end()
197197
})
198198

199+
t.test('local tarball key — relative key resolves from the project root', t => {
200+
const path = require('node:path')
201+
const rootPath = path.resolve('project')
202+
const tgzPath = path.resolve(rootPath, 'local-pkg.tgz')
203+
const tgzNode = node({
204+
name: 'local-pkg',
205+
packageName: 'local-pkg',
206+
version: '1.0.0',
207+
resolved: `file:${tgzPath}`,
208+
root: { path: rootPath },
209+
})
210+
211+
t.equal(isScriptAllowed(tgzNode, { 'file:local-pkg.tgz': true }), true)
212+
t.equal(isScriptAllowed(tgzNode, { 'file:other-pkg.tgz': true }), null)
213+
t.end()
214+
})
215+
216+
t.test('local tarball key — matches consistentResolve Windows representation', t => {
217+
const resolved = String.raw`file:C:\absolute\path\local-pkg.tgz`
218+
const parsed = {
219+
saveSpec: 'file:C:/absolute/path/local-pkg.tgz',
220+
fetchSpec: String.raw`C:\absolute\path\local-pkg.tgz`,
221+
}
222+
223+
t.equal(matchFileOrDir({ resolved }, parsed), true)
224+
t.equal(matchFileOrDir({ resolved }, {
225+
saveSpec: 'file:C:/absolute/path/other-pkg.tgz',
226+
fetchSpec: String.raw`C:\absolute\path\other-pkg.tgz`,
227+
}), false)
228+
t.end()
229+
})
230+
231+
t.test('local tarball key — Windows absolute key forms match', {
232+
skip: process.platform !== 'win32',
233+
}, t => {
234+
const resolved = String.raw`file:C:\project\local-pkg.tgz`
235+
const tgzNode = node({
236+
name: 'local-pkg',
237+
packageName: 'local-pkg',
238+
version: '1.0.0',
239+
resolved,
240+
root: { path: String.raw`C:\project` },
241+
})
242+
243+
t.equal(isScriptAllowed(tgzNode, {
244+
[String.raw`file:C:\project\local-pkg.tgz`]: true,
245+
}), true)
246+
t.equal(isScriptAllowed(tgzNode, {
247+
'file:C:/project/local-pkg.tgz': true,
248+
}), true)
249+
t.equal(isScriptAllowed(tgzNode, {
250+
'file:C:/project/other-pkg.tgz': true,
251+
}), null)
252+
t.end()
253+
})
254+
255+
t.test('local tarball key — Windows UNC key matches', {
256+
skip: process.platform !== 'win32',
257+
}, t => {
258+
const consistentResolve = require('../lib/consistent-resolve.js')
259+
const key = 'file:////server/share/local-pkg.tgz'
260+
const tgzNode = node({
261+
name: 'local-pkg',
262+
packageName: 'local-pkg',
263+
version: '1.0.0',
264+
resolved: consistentResolve(key),
265+
root: { path: String.raw`C:\project` },
266+
})
267+
268+
t.equal(isScriptAllowed(tgzNode, { [key]: true }), true)
269+
t.end()
270+
})
271+
272+
t.test('file source matching keeps POSIX backslashes distinct', t => {
273+
const resolved = String.raw`file:/tmp/local\pkg.tgz`
274+
const parsed = {
275+
saveSpec: 'file:/tmp/local/pkg.tgz',
276+
fetchSpec: '/tmp/local/pkg.tgz',
277+
}
278+
279+
t.equal(matchFileOrDir({ resolved }, parsed), false)
280+
t.end()
281+
})
282+
199283
t.test('remote tarball — exact resolved match', t => {
200284
const remoteNode = node({
201285
name: 'pkg',
@@ -208,6 +292,17 @@ t.test('remote tarball — exact resolved match', t => {
208292
t.end()
209293
})
210294

295+
t.test('remote tarball key does not match a file source', t => {
296+
const fileNode = node({
297+
name: 'pkg',
298+
packageName: 'pkg',
299+
version: '1.0.0',
300+
resolved: 'file:https://example.com/pkg.tgz',
301+
})
302+
t.equal(isScriptAllowed(fileNode, { 'https://example.com/pkg.tgz': true }), null)
303+
t.end()
304+
})
305+
211306
t.test('omitLockfileRegistryResolved: name-only match via edges; version-pinned does not', t => {
212307
// Without a resolved URL, the trusted name comes from an incoming
213308
// dependency edge (consumer-written), not from node.location (which

0 commit comments

Comments
 (0)