Skip to content

Commit 70317da

Browse files
fix(arborist): reject uninstall args that carry a version (#9930)
Backport of #9881 to `release/v11`. Co-authored-by: Lazizbek Ergashev <[email protected]>
1 parent 55ae484 commit 70317da

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎test/lib/commands/uninstall.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ t.test('remove multiple installed libs', async t => {
143143
t.throws(() => fs.statSync(b), 'should have removed b package from nm')
144144
})
145145

146+
t.test('rejects an arg with a version spec', async t => {
147+
const { uninstall } = await mockNpm(t, {
148+
prefixDir: {
149+
'package.json': JSON.stringify({
150+
name: 'test-rm-version-spec',
151+
version: '1.0.0',
152+
dependencies: {
153+
foo: '*',
154+
},
155+
}),
156+
node_modules: {
157+
foo: {
158+
'package.json': JSON.stringify({
159+
name: 'foo',
160+
version: '1.0.0',
161+
}),
162+
},
163+
},
164+
},
165+
})
166+
167+
await t.rejects(
168+
uninstall(['foo@1']),
169+
{ code: 'ERMARGS', message: /npm rm foo/ },
170+
'should throw ERMARGS instead of silently no-oping'
171+
)
172+
})
173+
146174
t.test('no args local', async t => {
147175
const { uninstall } = await mockNpm(t)
148176

‎workspaces/arborist/lib/arborist/build-ideal-tree.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ module.exports = cls => class IdealTreeBuilder extends cls {
257257
this[_updateNames] = update.names
258258

259259
this[_updateAll] = update.all
260+
261+
// validates list of rm names, they must
262+
// be dep names only, no semver ranges are supported
263+
for (const name of options.rm || []) {
264+
const spec = npa(name)
265+
const validationError =
266+
new TypeError(`Remove arguments must only contain package names, eg:
267+
npm rm ${spec.name || '<pkg>'}`)
268+
validationError.code = 'ERMARGS'
269+
270+
// If they gave us anything other than a bare package name
271+
if (spec.raw !== spec.name) {
272+
throw validationError
273+
}
274+
}
275+
260276
// we prune by default unless explicitly set to boolean false
261277
this.#prune = options.prune !== false
262278

‎workspaces/arborist/test/arborist/build-ideal-tree.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,47 @@ t.test('remove deps when initializing tree from actual tree', async t => {
23892389
t.equal(tree.children.get('foo'), undefined, 'removed foo child')
23902390
})
23912391

2392+
t.test('remove deps with a version spec', async t => {
2393+
const path = t.testdir({
2394+
node_modules: {
2395+
foo: {
2396+
'package.json': JSON.stringify({
2397+
name: 'foo',
2398+
version: '1.2.3',
2399+
}),
2400+
},
2401+
},
2402+
})
2403+
2404+
createRegistry(t, false)
2405+
const invalidArgs = [
2406+
2407+
'foo@next',
2408+
'foo@^1.0.0',
2409+
'foo@>=2.0.0',
2410+
'foo@2',
2411+
]
2412+
for (const rmName of invalidArgs) {
2413+
await t.rejects(
2414+
buildIdeal(path, { rm: [rmName] }),
2415+
{ code: 'ERMARGS', message: /npm rm foo/ },
2416+
'should throw an error when the package name has a version'
2417+
)
2418+
}
2419+
2420+
await t.rejects(
2421+
buildIdeal(path, { rm: ['@scope/[email protected]'] }),
2422+
{ code: 'ERMARGS', message: /npm rm @scope\/foo/ },
2423+
'should throw an error when a scoped package name has a version'
2424+
)
2425+
2426+
await t.rejects(
2427+
buildIdeal(path, { rm: ['./foo'] }),
2428+
{ code: 'ERMARGS', message: /npm rm <pkg>/ },
2429+
'should throw an error when the package is a path'
2430+
)
2431+
})
2432+
23922433
t.test('detect conflicts in transitive peerOptional deps', async t => {
23932434
const base = resolve(fixtures, 'test-conflicted-optional-peer-dep')
23942435

0 commit comments

Comments
 (0)