@@ -25,7 +25,6 @@ class VerifySignatures {
2525 this . checkedPackages = new Set ( )
2626 this . auditedWithKeysCount = 0
2727 this . verifiedCount = 0
28- this . output = [ ]
2928 this . exitCode = 0
3029 }
3130
@@ -60,13 +59,13 @@ class VerifySignatures {
6059 const hasNoInvalidOrMissing = invalid . length === 0 && missing . length === 0
6160
6261 if ( ! hasNoInvalidOrMissing ) {
63- this . exitCode = 1
62+ process . exitCode = 1
6463 }
6564
6665 if ( this . npm . config . get ( 'json' ) ) {
67- this . appendOutput ( JSON . stringify ( {
68- invalid : this . makeJSON ( invalid ) ,
69- missing : this . makeJSON ( missing ) ,
66+ this . npm . output ( JSON . stringify ( {
67+ invalid,
68+ missing,
7069 } , null , 2 ) )
7170 return
7271 }
@@ -76,54 +75,66 @@ class VerifySignatures {
7675 const auditedPlural = this . auditedWithKeysCount > 1 ? 's' : ''
7776 const timing = `audited ${ this . auditedWithKeysCount } package${ auditedPlural } in ` +
7877 `${ Math . floor ( Number ( elapsed ) / 1e9 ) } s`
79- this . appendOutput ( `${ timing } \n` )
78+ this . npm . output ( timing )
79+ this . npm . output ( '' )
8080
8181 if ( this . verifiedCount ) {
8282 const verifiedBold = this . npm . chalk . bold ( 'verified' )
83- const msg = this . verifiedCount === 1 ?
84- `${ this . verifiedCount } package has a ${ verifiedBold } registry signature\n` :
85- `${ this . verifiedCount } packages have ${ verifiedBold } registry signatures\n`
86- this . appendOutput ( msg )
83+ if ( this . verifiedCount === 1 ) {
84+ this . npm . output ( `${ this . verifiedCount } package has a ${ verifiedBold } registry signature` )
85+ } else {
86+ this . npm . output ( `${ this . verifiedCount } packages have ${ verifiedBold } registry signatures` )
87+ }
88+ this . npm . output ( '' )
8789 }
8890
8991 if ( missing . length ) {
9092 const missingClr = this . npm . chalk . bold ( this . npm . chalk . red ( 'missing' ) )
91- const msg = missing . length === 1 ?
92- `package has a ${ missingClr } registry signature` :
93- `packages have ${ missingClr } registry signatures`
94- this . appendOutput (
95- `${ missing . length } ${ msg } but the registry is ` +
96- `providing signing keys:\n`
93+ if ( missing . length === 1 ) {
94+ /* eslint-disable-next-line max-len */
95+ this . npm . output ( `1 package has a ${ missingClr } registry signature but the registry is providing signing keys:` )
96+ } else {
97+ /* eslint-disable-next-line max-len */
98+ this . npm . output ( `${ missing . length } packages have ${ missingClr } registry signatures but the registry is providing signing keys:` )
99+ }
100+ this . npm . output ( '' )
101+ missing . map ( m =>
102+ this . npm . output ( `${ this . npm . chalk . red ( `${ m . name } @${ m . version } ` ) } (${ m . registry } )` )
97103 )
98- this . appendOutput ( this . humanOutput ( missing ) )
99104 }
100105
101106 if ( invalid . length ) {
107+ if ( missing . length ) {
108+ this . npm . output ( '' )
109+ }
102110 const invalidClr = this . npm . chalk . bold ( this . npm . chalk . red ( 'invalid' ) )
103- const msg = invalid . length === 1 ?
104- `${ invalid . length } package has an ${ invalidClr } registry signature:\n` :
105- `${ invalid . length } packages have ${ invalidClr } registry signatures:\n`
106- this . appendOutput (
107- `${ missing . length ? '\n' : '' } ${ msg } `
111+ // We can have either invalid signatures or invalid provenance
112+ const invalidSignatures = this . invalid . filter ( i => i . code === 'EINTEGRITYSIGNATURE' )
113+ if ( invalidSignatures . length === 1 ) {
114+ this . npm . output ( `1 package has an ${ invalidClr } registry signature:` )
115+ // Ignoring coverage because this error state can't happen until we have
116+ // other kinds of invalid responses, i.e. provenance attestation
117+ /* istanbul ignore next */
118+ } else if ( invalidSignatures . length > 1 ) {
119+ /* eslint-disable-next-line max-len */
120+ this . npm . output ( `${ invalidSignatures . length } packages have ${ invalidClr } registry signatures:` )
121+ }
122+ this . npm . output ( '' )
123+ invalidSignatures . map ( i =>
124+ this . npm . output ( `${ this . npm . chalk . red ( `${ i . name } @${ i . version } ` ) } (${ i . registry } )` )
108125 )
109- this . appendOutput ( this . humanOutput ( invalid ) )
110- const tamperMsg = invalid . length === 1 ?
111- `\nSomeone might have tampered with this package since it was ` +
112- `published on the registry!\n` :
113- `\nSomeone might have tampered with these packages since they where ` +
114- `published on the registry!\n`
115- this . appendOutput ( tamperMsg )
126+ this . npm . output ( '' )
127+ if ( invalid . length === 1 ) {
128+ /* eslint-disable-next-line max-len */
129+ this . npm . output ( `Someone might have tampered with this package since it was published on the registry!` )
130+ } else {
131+ /* eslint-disable-next-line max-len */
132+ this . npm . output ( `Someone might have tampered with these packages since they were published on the registry!` )
133+ }
134+ this . npm . output ( '' )
116135 }
117136 }
118137
119- appendOutput ( ...args ) {
120- this . output . push ( ...args . flat ( ) )
121- }
122-
123- report ( ) {
124- return { report : this . output . join ( '\n' ) , exitCode : this . exitCode }
125- }
126-
127138 getEdgesOut ( nodes , filterSet ) {
128139 const edges = new Set ( )
129140 const registries = new Set ( )
@@ -249,11 +260,12 @@ class VerifySignatures {
249260 ...this . npm . flatOptions ,
250261 } )
251262 const signatures = _signatures || [ ]
252- return {
263+ const result = {
253264 integrity,
254265 signatures,
255266 resolved,
256267 }
268+ return result
257269 }
258270
259271 async getVerifiedInfo ( edge ) {
@@ -286,51 +298,33 @@ class VerifySignatures {
286298 this . verifiedCount += 1
287299 } else if ( keys . length ) {
288300 this . missing . push ( {
289- name,
290- version,
291- location,
292- resolved,
293301 integrity,
302+ location,
303+ name,
294304 registry,
305+ resolved,
306+ version,
295307 } )
296308 }
297309 } catch ( e ) {
298310 if ( e . code === 'EINTEGRITYSIGNATURE' ) {
299- const { signature, keyid, integrity, resolved } = e
300311 this . invalid . push ( {
312+ code : e . code ,
313+ integrity : e . integrity ,
314+ keyid : e . keyid ,
315+ location,
301316 name,
317+ registry,
318+ resolved : e . resolved ,
319+ signature : e . signature ,
302320 type,
303321 version,
304- resolved,
305- location,
306- integrity,
307- registry,
308- signature,
309- keyid,
310322 } )
311323 } else {
312324 throw e
313325 }
314326 }
315327 }
316-
317- humanOutput ( list ) {
318- return list . map ( v =>
319- `${ this . npm . chalk . red ( `${ v . name } @${ v . version } ` ) } (${ v . registry } )`
320- ) . join ( '\n' )
321- }
322-
323- makeJSON ( deps ) {
324- return deps . map ( d => ( {
325- name : d . name ,
326- version : d . version ,
327- location : d . location ,
328- resolved : d . resolved ,
329- integrity : d . integrity ,
330- signature : d . signature ,
331- keyid : d . keyid ,
332- } ) )
333- }
334328}
335329
336330class Audit extends ArboristWorkspaceCmd {
@@ -432,9 +426,6 @@ class Audit extends ArboristWorkspaceCmd {
432426
433427 const verify = new VerifySignatures ( tree , filterSet , this . npm , { ...opts } )
434428 await verify . run ( )
435- const result = verify . report ( )
436- process . exitCode = process . exitCode || result . exitCode
437- this . npm . output ( result . report )
438429 }
439430}
440431
0 commit comments