Skip to content

Commit 70f0ea5

Browse files
fix(approve-scripts): approve deps with no resolved URL by name (#9607)
Backport of #9606 to `release/v11`. Co-authored-by: Jamie Magee <[email protected]>
1 parent b2e6338 commit 70f0ea5

4 files changed

Lines changed: 180 additions & 4 deletions

File tree

‎docs/lib/content/commands/npm-approve-scripts.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ the command cannot infer. Existing `false` entries always win;
5353
`approve-scripts` will not silently re-allow a package you previously
5454
denied.
5555

56+
If a registry dependency has no `resolved` URL in your `package-lock.json`
57+
(for example, an older lockfile or one written with
58+
`omit-lockfile-registry-resolved`), npm cannot verify a trusted version for
59+
it and cannot pin it: a `[email protected]` entry never matches, so the package
60+
keeps appearing under `--allow-scripts-pending`. `approve-scripts` approves
61+
these by name (`pkg: true`) and warns when it does. To restore pinning,
62+
refresh the lockfile with `npm install`.
63+
5664
### Examples
5765

5866
```bash

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,41 @@ const applyApprovalForPackage = (existing, nodes, { pin = true } = {}) => {
250250
// package are removed. Per the RFC's pin-mismatch table, an existing
251251
// name-only entry (`pkg: true`) is replaced by `[email protected]: true` once
252252
// every installed version has a pin.
253-
const installedKeys = new Set(nodes.map(versionedKeyFor).filter(Boolean))
253+
const versionedKeys = nodes.map(versionedKeyFor)
254+
const installedKeys = new Set(versionedKeys.filter(Boolean))
255+
256+
// A registry dep with no `resolved` URL in the lockfile has no trustable
257+
// version (getTrustedRegistryIdentity won't trust the tarball's
258+
// node.version), so versionedKeyFor returns null and a `[email protected]` pin can
259+
// never match it (npm/cli#9558). When any installed version can't be
260+
// pinned, approve the whole package by name and drop now-redundant pins.
261+
if (name && versionedKeys.some(key => !key)) {
262+
for (const key of Object.keys(allowScripts)) {
263+
if (
264+
keyTargetsNode(key, sample) &&
265+
key !== name &&
266+
isSingleVersionPin(key) &&
267+
allowScripts[key] === true
268+
) {
269+
delete allowScripts[key]
270+
changes.push({ key, change: 'removed-pinned-allow' })
271+
}
272+
}
273+
if (allowScripts[name] !== true) {
274+
allowScripts[name] = true
275+
changes.push({ key: name, change: 'added' })
276+
}
277+
return {
278+
allowScripts,
279+
changes,
280+
warning: changes.length
281+
? `${name}: approved by name (all versions) because its ` +
282+
`package-lock.json entry has no "resolved" URL, so npm can't pin a ` +
283+
`specific version. Run \`npm install\` to refresh the lockfile and ` +
284+
`enable pinning.`
285+
: undefined,
286+
}
287+
}
254288

255289
for (const key of Object.keys(allowScripts)) {
256290
if (

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const mockNpm = async (t, opts = {}) => {
77
return _mockNpm(t, opts)
88
}
99

10-
const setupProject = ({ allowScripts, withScripts = ['canvas'] } = {}) => {
10+
const setupProject = ({ allowScripts, withScripts = ['canvas'], noResolved = [] } = {}) => {
1111
const pkg = {
1212
name: 'host',
1313
version: '1.0.0',
@@ -28,11 +28,16 @@ const setupProject = ({ allowScripts, withScripts = ['canvas'] } = {}) => {
2828
scripts: { install: 'echo install' },
2929
}),
3030
}
31-
lockPackages[`node_modules/${name}`] = {
31+
const lockEntry = {
3232
version: '1.0.0',
33-
resolved: tarUrl,
3433
hasInstallScript: true,
3534
}
35+
// Some lockfiles omit `resolved` for registry deps. Those nodes have no
36+
// trustable version, so they can only be approved by name.
37+
if (!noResolved.includes(name)) {
38+
lockEntry.resolved = tarUrl
39+
}
40+
lockPackages[`node_modules/${name}`] = lockEntry
3641
}
3742

3843
return {
@@ -119,6 +124,51 @@ t.test('approve-scripts --all approves every unreviewed package', async t => {
119124
})
120125
})
121126

127+
t.test('approve-scripts --all approves a dep without a resolved URL by name', async t => {
128+
// Regression for npm/cli#9558: a dep with no `resolved` URL can't be
129+
// pinned, but must still be approved by name, not silently skipped.
130+
const { npm, prefix, logs } = await mockNpm(t, {
131+
prefixDir: setupProject({ withScripts: ['canvas'], noResolved: ['canvas'] }),
132+
config: { all: true },
133+
})
134+
await npm.exec('approve-scripts', [])
135+
136+
const pkg = JSON.parse(fs.readFileSync(resolve(prefix, 'package.json'), 'utf8'))
137+
t.strictSame(pkg.allowScripts, { canvas: true }, 'approved by name, not skipped')
138+
t.match(
139+
logs.warn.byTitle('approve-scripts').join('\n'),
140+
/no "resolved" URL/,
141+
'warns that a version pin could not be written'
142+
)
143+
})
144+
145+
t.test('approve-scripts <pkg> approves a dep without a resolved URL by name', async t => {
146+
const { npm, prefix } = await mockNpm(t, {
147+
prefixDir: setupProject({ withScripts: ['canvas'], noResolved: ['canvas'] }),
148+
})
149+
await npm.exec('approve-scripts', ['canvas'])
150+
151+
const pkg = JSON.parse(fs.readFileSync(resolve(prefix, 'package.json'), 'utf8'))
152+
t.strictSame(pkg.allowScripts, { canvas: true })
153+
})
154+
155+
t.test('approve-scripts --pending is empty after a no-resolved dep is approved by name', async t => {
156+
const { npm, joinedOutput } = await mockNpm(t, {
157+
prefixDir: setupProject({
158+
allowScripts: { canvas: true },
159+
withScripts: ['canvas'],
160+
noResolved: ['canvas'],
161+
}),
162+
config: { 'allow-scripts-pending': true },
163+
})
164+
await npm.exec('approve-scripts', [])
165+
t.match(
166+
joinedOutput(),
167+
/No packages with unreviewed install scripts/,
168+
'name-only entry covers the dep even without a resolved URL'
169+
)
170+
})
171+
122172
t.test('approve-scripts errors on unknown package', async t => {
123173
const { npm } = await mockNpm(t, {
124174
prefixDir: setupProject({ withScripts: ['canvas'] }),

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ const node = (overrides = {}) => {
2424
}
2525
}
2626

27+
// A registry node with no `resolved` URL in the lockfile. Its trusted name
28+
// comes from a dependency edge, but its version isn't trustable, so
29+
// versionedKeyFor returns null (npm/cli#9558).
30+
const noResolvedNode = (overrides = {}) => {
31+
const name = overrides.name ?? 'pkg'
32+
const version = overrides.version ?? '1.0.0'
33+
return {
34+
name,
35+
packageName: overrides.packageName ?? name,
36+
version,
37+
resolved: undefined,
38+
location: `node_modules/${name}`,
39+
isRegistryDependency: true,
40+
edgesIn: new Set([{ name, spec: overrides.spec ?? `^${version}` }]),
41+
}
42+
}
43+
2744
t.test('nameKeyFor / versionedKeyFor — registry', async t => {
2845
const n = node({ name: 'canvas', version: '2.11.0' })
2946
t.equal(nameKeyFor(n), 'canvas')
@@ -214,6 +231,73 @@ t.test('applyApprovalForPackage — keeps existing pin matching one installed, a
214231
t.strictSame(allowScripts, { '[email protected]': true, '[email protected]': true })
215232
})
216233

234+
t.test('versionedKeyFor — registry dep without a resolved URL has no trustable version', async t => {
235+
const n = noResolvedNode({ name: 'esbuild', version: '0.25.0' })
236+
t.equal(nameKeyFor(n), 'esbuild', 'name still recoverable from the dependency edge')
237+
t.equal(versionedKeyFor(n), null, 'version cannot be trusted without a resolved URL')
238+
})
239+
240+
t.test('applyApprovalForPackage — pin mode falls back to name-only when version is not trustable', async t => {
241+
const { allowScripts, changes, warning } = applyApprovalForPackage(
242+
{},
243+
[noResolvedNode({ name: 'esbuild', version: '0.25.0' })],
244+
{ pin: true }
245+
)
246+
t.strictSame(allowScripts, { esbuild: true }, 'approved by name instead of silently skipped')
247+
t.strictSame(changes, [{ key: 'esbuild', change: 'added' }])
248+
t.match(warning, /no "resolved" URL/, 'explains why a pin was not written')
249+
})
250+
251+
t.test('applyApprovalForPackage — mixed resolved/no-resolved collapses to one name-only entry', async t => {
252+
const { allowScripts, warning } = applyApprovalForPackage(
253+
{},
254+
[
255+
node({ name: 'esbuild', version: '0.21.5' }),
256+
noResolvedNode({ name: 'esbuild', version: '0.25.0' }),
257+
],
258+
{ pin: true }
259+
)
260+
t.strictSame(allowScripts, { esbuild: true }, 'no redundant pin alongside the name-only entry')
261+
t.match(warning, /no "resolved" URL/)
262+
})
263+
264+
t.test('applyApprovalForPackage — existing pin collapses into name-only when a sibling cannot be pinned', async t => {
265+
const { allowScripts, changes } = applyApprovalForPackage(
266+
{ '[email protected]': true },
267+
[
268+
node({ name: 'esbuild', version: '0.21.5' }),
269+
noResolvedNode({ name: 'esbuild', version: '0.25.0' }),
270+
],
271+
{ pin: true }
272+
)
273+
t.strictSame(allowScripts, { esbuild: true })
274+
t.match(changes, [
275+
{ key: '[email protected]', change: 'removed-pinned-allow' },
276+
{ key: 'esbuild', change: 'added' },
277+
])
278+
})
279+
280+
t.test('applyApprovalForPackage — no-resolved dep already approved by name is a no-op', async t => {
281+
const { allowScripts, changes } = applyApprovalForPackage(
282+
{ esbuild: true },
283+
[noResolvedNode({ name: 'esbuild', version: '0.25.0' })],
284+
{ pin: true }
285+
)
286+
t.strictSame(allowScripts, { esbuild: true })
287+
t.strictSame(changes, [])
288+
})
289+
290+
t.test('applyApprovalForPackage — no-resolved dep with name-only deny still loses to the deny', async t => {
291+
const { allowScripts, changes, warning } = applyApprovalForPackage(
292+
{ esbuild: false },
293+
[noResolvedNode({ name: 'esbuild', version: '0.25.0' })],
294+
{ pin: true }
295+
)
296+
t.strictSame(allowScripts, { esbuild: false }, 'deny wins; nothing approved')
297+
t.strictSame(changes, [])
298+
t.match(warning, /esbuild is denied/)
299+
})
300+
217301
t.test('applyDenyForPackage — empty allowScripts adds name-only false', async t => {
218302
const { allowScripts, changes } = applyDenyForPackage(
219303
{},

0 commit comments

Comments
 (0)