@@ -25,7 +25,6 @@ class VerifySignatures {
25
25
this . checkedPackages = new Set ( )
26
26
this . auditedWithKeysCount = 0
27
27
this . verifiedCount = 0
28
- this . output = [ ]
29
28
this . exitCode = 0
30
29
}
31
30
@@ -60,13 +59,13 @@ class VerifySignatures {
60
59
const hasNoInvalidOrMissing = invalid . length === 0 && missing . length === 0
61
60
62
61
if ( ! hasNoInvalidOrMissing ) {
63
- this . exitCode = 1
62
+ process . exitCode = 1
64
63
}
65
64
66
65
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,
70
69
} , null , 2 ) )
71
70
return
72
71
}
@@ -76,54 +75,62 @@ class VerifySignatures {
76
75
const auditedPlural = this . auditedWithKeysCount > 1 ? 's' : ''
77
76
const timing = `audited ${ this . auditedWithKeysCount } package${ auditedPlural } in ` +
78
77
`${ Math . floor ( Number ( elapsed ) / 1e9 ) } s`
79
- this . appendOutput ( `${ timing } \n` )
78
+ this . npm . output ( timing )
79
+ this . npm . output ( '' )
80
80
81
81
if ( this . verifiedCount ) {
82
82
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 ( '' )
87
89
}
88
90
89
91
if ( missing . length ) {
90
92
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 } )` )
97
103
)
98
- this . appendOutput ( this . humanOutput ( missing ) )
99
104
}
100
105
101
106
if ( invalid . length ) {
107
+ if ( missing . length ) {
108
+ this . npm . output ( '' )
109
+ }
102
110
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
+ } else if ( invalidSignatures . length > 1 ) {
116
+ this . npm . output ( `${ invalidSignatures . length } packages have ${ invalidClr } registry signatures:` )
117
+ }
118
+ this . npm . output ( '' )
119
+ invalidSignatures . map ( i =>
120
+ this . npm . output ( `${ this . npm . chalk . red ( `${ i . name } @${ i . version } ` ) } (${ i . registry } )` )
108
121
)
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 )
122
+ this . npm . output ( '' )
123
+ if ( invalid . length === 1 ) {
124
+ /* eslint-disable-next-line max-len */
125
+ this . npm . output ( `Someone might have tampered with this package since it was published on the registry!` )
126
+ } else {
127
+ /* eslint-disable-next-line max-len */
128
+ this . npm . output ( `Someone might have tampered with these packages since they were published on the registry!` )
129
+ }
130
+ this . npm . output ( '' )
116
131
}
117
132
}
118
133
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
-
127
134
getEdgesOut ( nodes , filterSet ) {
128
135
const edges = new Set ( )
129
136
const registries = new Set ( )
@@ -249,11 +256,12 @@ class VerifySignatures {
249
256
...this . npm . flatOptions ,
250
257
} )
251
258
const signatures = _signatures || [ ]
252
- return {
259
+ const result = {
253
260
integrity,
254
261
signatures,
255
262
resolved,
256
263
}
264
+ return result
257
265
}
258
266
259
267
async getVerifiedInfo ( edge ) {
@@ -286,51 +294,33 @@ class VerifySignatures {
286
294
this . verifiedCount += 1
287
295
} else if ( keys . length ) {
288
296
this . missing . push ( {
289
- name,
290
- version,
291
- location,
292
- resolved,
293
297
integrity,
298
+ location,
299
+ name,
294
300
registry,
301
+ resolved,
302
+ version,
295
303
} )
296
304
}
297
305
} catch ( e ) {
298
306
if ( e . code === 'EINTEGRITYSIGNATURE' ) {
299
- const { signature, keyid, integrity, resolved } = e
300
307
this . invalid . push ( {
308
+ code : e . code ,
309
+ integrity : e . integrity ,
310
+ keyid : e . keyid ,
311
+ location,
301
312
name,
313
+ registry,
314
+ resolved : e . resolved ,
315
+ signature : e . signature ,
302
316
type,
303
317
version,
304
- resolved,
305
- location,
306
- integrity,
307
- registry,
308
- signature,
309
- keyid,
310
318
} )
311
319
} else {
312
320
throw e
313
321
}
314
322
}
315
323
}
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
- }
334
324
}
335
325
336
326
class Audit extends ArboristWorkspaceCmd {
@@ -432,9 +422,6 @@ class Audit extends ArboristWorkspaceCmd {
432
422
433
423
const verify = new VerifySignatures ( tree , filterSet , this . npm , { ...opts } )
434
424
await verify . run ( )
435
- const result = verify . report ( )
436
- process . exitCode = process . exitCode || result . exitCode
437
- this . npm . output ( result . report )
438
425
}
439
426
}
440
427
0 commit comments