From 60ee40cdfbfac5eae324abd781cd9b64ed792526 Mon Sep 17 00:00:00 2001 From: sosoba Date: Tue, 22 Nov 2022 07:07:15 +0100 Subject: [PATCH 1/4] Update get-identity.js Fix for #5867 (prevent undefined username) --- lib/utils/get-identity.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils/get-identity.js b/lib/utils/get-identity.js index 41d882473ab7b..a2a70eeb455b4 100644 --- a/lib/utils/get-identity.js +++ b/lib/utils/get-identity.js @@ -12,7 +12,9 @@ module.exports = async (npm, opts) => { // No username, but we have other credentials; fetch the username from registry if (creds.token || creds.certfile && creds.keyfile) { const registryData = await npmFetch.json('/-/whoami', { ...opts }) - return registryData.username + if (registryData?.username) + return registryData.username + ) } // At this point, even if they have a credentials object, it doesn't have a From 2571d9505923d7dabfaf6f387dad53a77b525d75 Mon Sep 17 00:00:00 2001 From: sosoba Date: Tue, 22 Nov 2022 07:15:08 +0100 Subject: [PATCH 2/4] Update get-identity.js --- lib/utils/get-identity.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/get-identity.js b/lib/utils/get-identity.js index a2a70eeb455b4..1f51d67ab3bb6 100644 --- a/lib/utils/get-identity.js +++ b/lib/utils/get-identity.js @@ -12,7 +12,7 @@ module.exports = async (npm, opts) => { // No username, but we have other credentials; fetch the username from registry if (creds.token || creds.certfile && creds.keyfile) { const registryData = await npmFetch.json('/-/whoami', { ...opts }) - if (registryData?.username) + if (typeof registryData?.username === 'string') return registryData.username ) } From 373676bbad049054874ab210b022ac84f2f72213 Mon Sep 17 00:00:00 2001 From: sosoba Date: Fri, 25 Nov 2022 09:52:54 +0100 Subject: [PATCH 3/4] Update lib/utils/get-identity.js Co-authored-by: nlf --- lib/utils/get-identity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/get-identity.js b/lib/utils/get-identity.js index 1f51d67ab3bb6..d8f59da14247a 100644 --- a/lib/utils/get-identity.js +++ b/lib/utils/get-identity.js @@ -12,9 +12,9 @@ module.exports = async (npm, opts) => { // No username, but we have other credentials; fetch the username from registry if (creds.token || creds.certfile && creds.keyfile) { const registryData = await npmFetch.json('/-/whoami', { ...opts }) - if (typeof registryData?.username === 'string') + if (typeof registryData?.username === 'string') { return registryData.username - ) + } } // At this point, even if they have a credentials object, it doesn't have a From 18f0121ad6d22f74ab46767bf67dbed265489d2f Mon Sep 17 00:00:00 2001 From: nlf Date: Wed, 30 Nov 2022 13:38:05 -0800 Subject: [PATCH 4/4] fixup! Update lib/utils/get-identity.js --- test/lib/commands/whoami.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/lib/commands/whoami.js b/test/lib/commands/whoami.js index a4532390bc66b..8291b092dc167 100644 --- a/test/lib/commands/whoami.js +++ b/test/lib/commands/whoami.js @@ -1,6 +1,7 @@ const t = require('tap') const { load: loadMockNpm } = require('../../fixtures/mock-npm') const MockRegistry = require('@npmcli/mock-registry') +const nock = require('nock') const username = 'foo' const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' } @@ -67,3 +68,27 @@ t.test('not logged in', async t => { }) await t.rejects(npm.exec('whoami', []), { code: 'ENEEDAUTH' }) }) + +t.test('non-string username in response', async t => { + nock.disableNetConnect() + t.teardown(() => { + nock.enableNetConnect() + }) + + const server = nock('https://registry.npmjs.org', { + reqheaders: { + authorization: 'Bearer abcd1234', + }, + }) + .get('/-/whoami') + .reply(200, { username: null }) + + const { npm } = await loadMockNpm(t, { + config: { + '//registry.npmjs.org/:_authToken': 'abcd1234', + }, + }) + + await t.rejects(npm.exec('whoami', []), { code: 'ENEEDAUTH' }) + t.ok(server.isDone()) +})