File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,9 @@ module.exports = async (npm, opts) => {
12
12
// No username, but we have other credentials; fetch the username from registry
13
13
if ( creds . token || creds . certfile && creds . keyfile ) {
14
14
const registryData = await npmFetch . json ( '/-/whoami' , { ...opts } )
15
- return registryData . username
15
+ if ( typeof registryData ?. username === 'string' ) {
16
+ return registryData . username
17
+ }
16
18
}
17
19
18
20
// At this point, even if they have a credentials object, it doesn't have a
Original file line number Diff line number Diff line change 1
1
const t = require ( 'tap' )
2
2
const { load : loadMockNpm } = require ( '../../fixtures/mock-npm' )
3
3
const MockRegistry = require ( '@npmcli/mock-registry' )
4
+ const nock = require ( 'nock' )
4
5
5
6
const username = 'foo'
6
7
const auth = { '//registry.npmjs.org/:_authToken' : 'test-auth-token' }
@@ -67,3 +68,27 @@ t.test('not logged in', async t => {
67
68
} )
68
69
await t . rejects ( npm . exec ( 'whoami' , [ ] ) , { code : 'ENEEDAUTH' } )
69
70
} )
71
+
72
+ t . test ( 'non-string username in response' , async t => {
73
+ nock . disableNetConnect ( )
74
+ t . teardown ( ( ) => {
75
+ nock . enableNetConnect ( )
76
+ } )
77
+
78
+ const server = nock ( 'https://registry.npmjs.org' , {
79
+ reqheaders : {
80
+ authorization : 'Bearer abcd1234' ,
81
+ } ,
82
+ } )
83
+ . get ( '/-/whoami' )
84
+ . reply ( 200 , { username : null } )
85
+
86
+ const { npm } = await loadMockNpm ( t , {
87
+ config : {
88
+ '//registry.npmjs.org/:_authToken' : 'abcd1234' ,
89
+ } ,
90
+ } )
91
+
92
+ await t . rejects ( npm . exec ( 'whoami' , [ ] ) , { code : 'ENEEDAUTH' } )
93
+ t . ok ( server . isDone ( ) )
94
+ } )
You can’t perform that action at this time.
0 commit comments