Skip to content

Commit 146f0f8

Browse files
committed
feat: refuse to set deprecated/invalid config
BREAKING CHANGE: `npm config set` will no longer accept deprecated or invalid config options.
1 parent 6a27a7b commit 146f0f8

File tree

2 files changed

+122
-66
lines changed

2 files changed

+122
-66
lines changed

lib/commands/config.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ const localeCompare = require('@isaacs/string-locale-compare')('en')
1010
const rpj = require('read-package-json-fast')
1111
const log = require('../utils/log-shim.js')
1212

13+
// These are the configs that we can nerf dart. Not al of them currently even
14+
// *have* config definitions so we have to explicitly validate them here
15+
const nerfDarts = [
16+
'_auth',
17+
'_authToken',
18+
'username',
19+
'_password',
20+
'email',
21+
'certfile',
22+
'keyfile'
23+
]
24+
1325
// take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into
1426
// { key: value, k2: v2, k3: v3 }
1527
const keyValues = args => {
@@ -146,6 +158,16 @@ class Config extends BaseCommand {
146158
const where = this.npm.flatOptions.location
147159
for (const [key, val] of Object.entries(keyValues(args))) {
148160
log.info('config', 'set %j %j', key, val)
161+
const baseKey = key.split(':').pop()
162+
if (!this.npm.config.definitions[baseKey] && !nerfDarts.includes(baseKey)) {
163+
throw new Error(`\`${baseKey}\` is not a valid npm option`)
164+
}
165+
const deprecated = this.npm.config.definitions[baseKey]?.deprecated
166+
if (deprecated) {
167+
throw new Error(
168+
`The \`${baseKey}\` option is deprecated, and can not be set in this way${deprecated}`
169+
)
170+
}
149171
this.npm.config.set(key, val || '', where)
150172
if (!this.npm.config.validate(where)) {
151173
log.warn('config', 'omitting invalid config values')
@@ -163,7 +185,7 @@ class Config extends BaseCommand {
163185
const out = []
164186
for (const key of keys) {
165187
if (!publicVar(key)) {
166-
throw new Error(`The ${key} option is protected, and cannot be retrieved in this way`)
188+
throw new Error(`The ${key} option is protected, and can not be retrieved in this way`)
167189
}
168190

169191
const pref = keys.length > 1 ? `${key}=` : ''

test/lib/commands/config.js

Lines changed: 99 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const { join } = require('path')
2-
const { promisify } = require('util')
3-
const fs = require('fs')
2+
const fs = require('fs/promises')
3+
const ini = require('ini')
44
const tspawk = require('../../fixtures/tspawk')
55
const t = require('tap')
66

77
const spawk = tspawk(t)
88

9-
const readFile = promisify(fs.readFile)
10-
119
const Sandbox = require('../../fixtures/sandbox.js')
1210

1311
t.test('config no args', async t => {
@@ -142,60 +140,92 @@ t.test('config delete no args', async t => {
142140
t.test('config delete single key', async t => {
143141
// location defaults to user, so we work with a userconfig
144142
const home = t.testdir({
145-
'.npmrc': 'foo=bar\nbar=baz',
143+
'.npmrc': 'access=public\nall=true',
146144
})
147145

148146
const sandbox = new Sandbox(t)
149-
await sandbox.run('config', ['delete', 'foo'], { home })
147+
await sandbox.run('config', ['delete', 'access'], { home })
150148

151-
t.equal(sandbox.config.get('foo'), undefined, 'foo should no longer be set')
149+
t.equal(sandbox.config.get('access'), null, 'acces should be defaulted')
152150

153-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
154-
t.not(contents.includes('foo='), 'foo was removed on disk')
151+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
152+
const rc = ini.parse(contents)
153+
t.not(rc.access, 'access is not set')
155154
})
156155

157156
t.test('config delete multiple keys', async t => {
158157
const home = t.testdir({
159-
'.npmrc': 'foo=bar\nbar=baz\nbaz=buz',
158+
'.npmrc': 'access=public\nall=true\naudit=false',
160159
})
161160

162161
const sandbox = new Sandbox(t)
163-
await sandbox.run('config', ['delete', 'foo', 'bar'], { home })
162+
await sandbox.run('config', ['delete', 'access', 'all'], { home })
164163

165-
t.equal(sandbox.config.get('foo'), undefined, 'foo should no longer be set')
166-
t.equal(sandbox.config.get('bar'), undefined, 'bar should no longer be set')
164+
t.equal(sandbox.config.get('access'), null, 'access should be defaulted')
165+
t.equal(sandbox.config.get('all'), false, 'all should be defaulted')
167166

168-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
169-
t.not(contents.includes('foo='), 'foo was removed on disk')
170-
t.not(contents.includes('bar='), 'bar was removed on disk')
167+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
168+
const rc = ini.parse(contents)
169+
t.not(rc.access, 'access is not set')
170+
t.not(rc.all, 'all is not set')
171171
})
172172

173173
t.test('config delete key --location=global', async t => {
174174
const global = t.testdir({
175-
npmrc: 'foo=bar\nbar=baz',
175+
npmrc: 'access=public\nall=true',
176176
})
177177

178178
const sandbox = new Sandbox(t)
179-
await sandbox.run('config', ['delete', 'foo', '--location=global'], { global })
179+
await sandbox.run('config', ['delete', 'access', '--location=global'], { global })
180180

181-
t.equal(sandbox.config.get('foo', 'global'), undefined, 'foo should no longer be set')
181+
t.equal(sandbox.config.get('access', 'global'), undefined, 'access should be defaulted')
182182

183-
const contents = await readFile(join(global, 'npmrc'), { encoding: 'utf8' })
184-
t.not(contents.includes('foo='), 'foo was removed on disk')
183+
const contents = await fs.readFile(join(global, 'npmrc'), { encoding: 'utf8' })
184+
const rc = ini.parse(contents)
185+
t.not(rc.access, 'access is not set')
185186
})
186187

187188
t.test('config delete key --global', async t => {
188189
const global = t.testdir({
189-
npmrc: 'foo=bar\nbar=baz',
190+
npmrc: 'access=public\nall=true',
190191
})
191192

192193
const sandbox = new Sandbox(t)
193-
await sandbox.run('config', ['delete', 'foo', '--global'], { global })
194+
await sandbox.run('config', ['delete', 'access', '--global'], { global })
195+
196+
t.equal(sandbox.config.get('access', 'global'), undefined, 'access should no longer be set')
197+
198+
const contents = await fs.readFile(join(global, 'npmrc'), { encoding: 'utf8' })
199+
const rc = ini.parse(contents)
200+
t.not(rc.access, 'access is not set')
201+
})
202+
203+
t.test('config set invalid option', async t => {
204+
const sandbox = new Sandbox(t)
205+
await t.rejects(
206+
sandbox.run('config', ['set', 'nonexistantconfigoption', 'something']),
207+
/not a valid npm option/
208+
)
209+
})
210+
211+
t.test('config set deprecated option', async t => {
212+
const sandbox = new Sandbox(t)
213+
await t.rejects(
214+
sandbox.run('config', ['set', 'shrinkwrap', 'true']),
215+
/deprecated/
216+
)
217+
})
194218

195-
t.equal(sandbox.config.get('foo', 'global'), undefined, 'foo should no longer be set')
219+
t.test('config set nerf-darted option', async t => {
220+
const sandbox = new Sandbox(t)
221+
await sandbox.run('config', ['set', '//npm.pkg.github.com/:_authToken', '0xdeadbeef'])
222+
t.equal(sandbox.config.get('//npm.pkg.github.com/:_authToken'), '0xdeadbeef', 'nerf-darted config is set')
223+
})
196224

197-
const contents = await readFile(join(global, 'npmrc'), { encoding: 'utf8' })
198-
t.not(contents.includes('foo='), 'foo was removed on disk')
225+
t.test('config set scoped optoin', async t => {
226+
const sandbox = new Sandbox(t)
227+
await sandbox.run('config', ['set', '@npm:registry', 'https://registry.npmjs.org'])
228+
t.equal(sandbox.config.get('@npm:registry'), 'https://registry.npmjs.org', 'scoped config is set')
199229
})
200230

201231
t.test('config set no args', async t => {
@@ -212,65 +242,67 @@ t.test('config set no args', async t => {
212242

213243
t.test('config set key', async t => {
214244
const home = t.testdir({
215-
'.npmrc': 'foo=bar',
245+
'.npmrc': 'access=public',
216246
})
217247

218248
const sandbox = new Sandbox(t, { home })
219249

220-
await sandbox.run('config', ['set', 'foo'])
250+
await sandbox.run('config', ['set', 'access'])
221251

222-
t.equal(sandbox.config.get('foo'), '', 'set the value for foo')
252+
t.equal(sandbox.config.get('access'), null, 'set the value for access')
223253

224-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
225-
t.ok(contents.includes('foo='), 'wrote foo to disk')
254+
await t.rejects(fs.stat(join(home, '.npmrc'), { encoding: 'utf8' }), 'removed empty config')
226255
})
227256

228257
t.test('config set key value', async t => {
229258
const home = t.testdir({
230-
'.npmrc': 'foo=bar',
259+
'.npmrc': 'access=public',
231260
})
232261

233262
const sandbox = new Sandbox(t, { home })
234263

235-
await sandbox.run('config', ['set', 'foo', 'baz'])
264+
await sandbox.run('config', ['set', 'access', 'restricted'])
236265

237-
t.equal(sandbox.config.get('foo'), 'baz', 'set the value for foo')
266+
t.equal(sandbox.config.get('access'), 'restricted', 'set the value for access')
238267

239-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
240-
t.ok(contents.includes('foo=baz'), 'wrote foo to disk')
268+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
269+
const rc = ini.parse(contents)
270+
t.equal(rc.access, 'restricted', 'access is set to restricted')
241271
})
242272

243273
t.test('config set key=value', async t => {
244274
const home = t.testdir({
245-
'.npmrc': 'foo=bar',
275+
'.npmrc': 'access=public',
246276
})
247277

248278
const sandbox = new Sandbox(t, { home })
249279

250-
await sandbox.run('config', ['set', 'foo=baz'])
280+
await sandbox.run('config', ['set', 'access=restricted'])
251281

252-
t.equal(sandbox.config.get('foo'), 'baz', 'set the value for foo')
282+
t.equal(sandbox.config.get('access'), 'restricted', 'set the value for access')
253283

254-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
255-
t.ok(contents.includes('foo=baz'), 'wrote foo to disk')
284+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
285+
const rc = ini.parse(contents)
286+
t.equal(rc.access, 'restricted', 'access is set to restricted')
256287
})
257288

258289
t.test('config set key1 value1 key2=value2 key3', async t => {
259290
const home = t.testdir({
260-
'.npmrc': 'foo=bar\nbar=baz\nbaz=foo',
291+
'.npmrc': 'access=public\nall=true\naudit=true',
261292
})
262293

263294
const sandbox = new Sandbox(t, { home })
264-
await sandbox.run('config', ['set', 'foo', 'oof', 'bar=rab', 'baz'])
295+
await sandbox.run('config', ['set', 'access', 'restricted', 'all=false', 'audit'])
265296

266-
t.equal(sandbox.config.get('foo'), 'oof', 'foo was set')
267-
t.equal(sandbox.config.get('bar'), 'rab', 'bar was set')
268-
t.equal(sandbox.config.get('baz'), '', 'baz was set')
297+
t.equal(sandbox.config.get('access'), 'restricted', 'access was set')
298+
t.equal(sandbox.config.get('all'), false, 'all was set')
299+
t.equal(sandbox.config.get('audit'), false, 'audit was set')
269300

270-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
271-
t.ok(contents.includes('foo=oof'), 'foo was written to disk')
272-
t.ok(contents.includes('bar=rab'), 'bar was written to disk')
273-
t.ok(contents.includes('baz='), 'baz was written to disk')
301+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
302+
const rc = ini.parse(contents)
303+
t.equal(rc.access, 'restricted', 'access is set to restricted')
304+
t.equal(rc.all, false, 'all is set to false')
305+
t.equal(rc.audit, false, 'audit is set to false')
274306
})
275307

276308
t.test('config set invalid key logs warning', async t => {
@@ -287,30 +319,32 @@ t.test('config set invalid key logs warning', async t => {
287319

288320
t.test('config set key=value --location=global', async t => {
289321
const global = t.testdir({
290-
npmrc: 'foo=bar\nbar=baz',
322+
npmrc: 'access=public\nall=true',
291323
})
292324

293325
const sandbox = new Sandbox(t, { global })
294-
await sandbox.run('config', ['set', 'foo=buzz', '--location=global'])
326+
await sandbox.run('config', ['set', 'access=restricted', '--location=global'])
295327

296-
t.equal(sandbox.config.get('foo', 'global'), 'buzz', 'foo should be set')
328+
t.equal(sandbox.config.get('access', 'global'), 'restricted', 'foo should be set')
297329

298-
const contents = await readFile(join(global, 'npmrc'), { encoding: 'utf8' })
299-
t.not(contents.includes('foo=buzz'), 'foo was saved on disk')
330+
const contents = await fs.readFile(join(global, 'npmrc'), { encoding: 'utf8' })
331+
const rc = ini.parse(contents)
332+
t.equal(rc.access, 'restricted', 'access is set to restricted')
300333
})
301334

302335
t.test('config set key=value --global', async t => {
303336
const global = t.testdir({
304-
npmrc: 'foo=bar\nbar=baz',
337+
npmrc: 'access=public\nall=true',
305338
})
306339

307340
const sandbox = new Sandbox(t, { global })
308-
await sandbox.run('config', ['set', 'foo=buzz', '--global'])
341+
await sandbox.run('config', ['set', 'access=restricted', '--global'])
309342

310-
t.equal(sandbox.config.get('foo', 'global'), 'buzz', 'foo should be set')
343+
t.equal(sandbox.config.get('access', 'global'), 'restricted', 'access should be set')
311344

312-
const contents = await readFile(join(global, 'npmrc'), { encoding: 'utf8' })
313-
t.not(contents.includes('foo=buzz'), 'foo was saved on disk')
345+
const contents = await fs.readFile(join(global, 'npmrc'), { encoding: 'utf8' })
346+
const rc = ini.parse(contents)
347+
t.equal(rc.access, 'restricted', 'access is set to restricted')
314348
})
315349

316350
t.test('config get no args', async t => {
@@ -383,7 +417,7 @@ t.test('config edit', async t => {
383417
'editor opened the user config file'
384418
)
385419

386-
const contents = await readFile(join(home, '.npmrc'), { encoding: 'utf8' })
420+
const contents = await fs.readFile(join(home, '.npmrc'), { encoding: 'utf8' })
387421
t.ok(contents.includes('foo=bar'), 'kept foo')
388422
t.ok(contents.includes('bar=baz'), 'kept bar')
389423
t.ok(contents.includes('shown below with default values'), 'appends defaults to file')
@@ -448,7 +482,7 @@ t.test('config fix', (t) => {
448482
t.not(sandbox.config.get('_authToken', 'global'), '_authToken is not set globally')
449483
t.equal(sandbox.config.get(`${registry}:_authToken`, 'global'), 'afaketoken',
450484
'global _authToken was scoped')
451-
const globalConfig = await readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' })
485+
const globalConfig = await fs.readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' })
452486
t.equal(globalConfig, `${registry}:_authToken=afaketoken\n`, 'global config was written')
453487

454488
// user config fixes
@@ -459,7 +493,7 @@ t.test('config fix', (t) => {
459493
t.not(sandbox.config.get('_authtoken', 'user'), '_authtoken is not set in user config')
460494
t.not(sandbox.config.get('_auth'), '_auth is not set in user config')
461495
t.equal(sandbox.config.get(`${registry}:_auth`, 'user'), 'beef', 'user _auth was scoped')
462-
const userConfig = await readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' })
496+
const userConfig = await fs.readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' })
463497
t.equal(userConfig, `${registry}:_auth=beef\n`, 'user config was written')
464498
})
465499

@@ -488,7 +522,7 @@ t.test('config fix', (t) => {
488522
t.equal(sandbox.config.get('_authtoken', 'global'), 'notatoken', 'global _authtoken untouched')
489523
t.equal(sandbox.config.get('_authToken', 'global'), 'afaketoken', 'global _authToken untouched')
490524
t.not(sandbox.config.get(`${registry}:_authToken`, 'global'), 'global _authToken not scoped')
491-
const globalConfig = await readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' })
525+
const globalConfig = await fs.readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' })
492526
t.equal(globalConfig, '_authtoken=notatoken\n_authToken=afaketoken',
493527
'global config was not written')
494528

@@ -500,7 +534,7 @@ t.test('config fix', (t) => {
500534
t.not(sandbox.config.get('_authtoken', 'user'), '_authtoken is not set in user config')
501535
t.not(sandbox.config.get('_auth', 'user'), '_auth is not set in user config')
502536
t.equal(sandbox.config.get(`${registry}:_auth`, 'user'), 'beef', 'user _auth was scoped')
503-
const userConfig = await readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' })
537+
const userConfig = await fs.readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' })
504538
t.equal(userConfig, `${registry}:_auth=beef\n`, 'user config was written')
505539
})
506540

0 commit comments

Comments
 (0)