Skip to content

Commit c84ccaf

Browse files
committed
fix: fail closed on versioned denies when pruning unverifiable versions
1 parent 1d0de8a commit c84ccaf

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const classifyUnusedEntries = (allowScripts, nodes) => {
2929
continue
3030
}
3131

32-
const matching = nodes.filter(({ node }) => matches(node, key))
32+
// Pass deny intent so an unverifiable version fails closed: an active versioned deny must not be classified as unused while its broad allow survives.
33+
const matching = nodes.filter(({ node }) => matches(node, key, value === false))
3334
if (matching.length === 0) {
3435
removed.push({ key, value, reason: 'not-installed' })
3536
continue

‎test/lib/commands/approve-scripts.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,62 @@ t.test('approve-scripts <pkg> under linked strategy without resolved URLs approv
10471047
const pkg = JSON.parse(fs.readFileSync(resolve(prefix, 'package.json'), 'utf8'))
10481048
t.strictSame(pkg.allowScripts, { canvas: true })
10491049
})
1050+
1051+
for (const state of ['missing', 'stale']) {
1052+
t.test(`prune keeps denies with a ${state} hidden lockfile`, async t => {
1053+
// With no trusted version, the versioned deny still blocks at runtime, so prune must fail closed and keep it instead of flipping the effective policy to allowed (npm/cli#9941 review).
1054+
const Arborist = require('@npmcli/arborist')
1055+
const isScriptAllowed = require('@npmcli/arborist/lib/script-allowed.js')
1056+
const allowScripts = {
1057+
canvas: true,
1058+
1059+
}
1060+
1061+
const { npm, prefix } = await mockNpm(t, {
1062+
prefixDir: setupLinkedProject(t, { allowScripts }),
1063+
config: { 'install-strategy': 'linked' },
1064+
})
1065+
1066+
const hidden = resolve(prefix, 'node_modules', '.package-lock.json')
1067+
if (state === 'missing') {
1068+
fs.unlinkSync(hidden)
1069+
} else {
1070+
fs.utimesSync(hidden, new Date(0), new Date(0))
1071+
}
1072+
1073+
t.equal(npm.config.get('omit-lockfile-registry-resolved'), false)
1074+
1075+
const arb = new Arborist({ ...npm.flatOptions, path: prefix })
1076+
const tree = await arb.loadActual()
1077+
const target = [...tree.inventory.values()]
1078+
.find(node => !node.isLink && node.name === 'canvas')
1079+
1080+
t.ok(target, 'finds the installed target')
1081+
t.strictSame(
1082+
isScriptAllowed.getTrustedRegistryIdentity(target),
1083+
{ name: 'canvas', version: null },
1084+
'recovers the registry name without inventing a trusted version'
1085+
)
1086+
t.equal(
1087+
isScriptAllowed(target, allowScripts),
1088+
false,
1089+
'the versioned deny blocks the package before pruning'
1090+
)
1091+
1092+
await npm.exec('install-scripts', ['prune'])
1093+
1094+
const pkg = JSON.parse(
1095+
fs.readFileSync(resolve(prefix, 'package.json'), 'utf8')
1096+
)
1097+
t.strictSame(
1098+
pkg.allowScripts,
1099+
allowScripts,
1100+
'pruning preserves both the allow and its active deny exception'
1101+
)
1102+
t.equal(
1103+
isScriptAllowed(target, pkg.allowScripts),
1104+
false,
1105+
'the package remains denied after pruning'
1106+
)
1107+
})
1108+
}

0 commit comments

Comments
 (0)