@@ -2,9 +2,9 @@ const t = require('tap')
2
2
3
3
const { load : loadMockNpm } = require ( '../../fixtures/mock-npm' )
4
4
const MockRegistry = require ( '../../fixtures/mock-registry.js' )
5
- const util = require ( 'util' )
6
5
const zlib = require ( 'zlib' )
7
- const gzip = util . promisify ( zlib . gzip )
6
+ const gzip = zlib . gzipSync
7
+ const gunzip = zlib . gunzipSync
8
8
const path = require ( 'path' )
9
9
const fs = require ( 'fs' )
10
10
@@ -43,7 +43,14 @@ const tree = {
43
43
} ,
44
44
} ,
45
45
} ) ,
46
- 'test-dep-a' : {
46
+ 'test-dep-a-vuln' : {
47
+ 'package.json' : JSON . stringify ( {
48
+ name : 'test-dep-a' ,
49
+ version : '1.0.0' ,
50
+ } ) ,
51
+ 'vulnerable.txt' : 'vulnerable test-dep-a' ,
52
+ } ,
53
+ 'test-dep-a-fixed' : {
47
54
'package.json' : JSON . stringify ( {
48
55
name : 'test-dep-a' ,
49
56
version : '1.0.1' ,
@@ -66,8 +73,11 @@ t.test('normal audit', async t => {
66
73
packuments : [ { version : '1.0.0' } , { version : '1.0.1' } ] ,
67
74
} )
68
75
await registry . package ( { manifest } )
69
- const advisory = registry . advisory ( { id : 100 } )
70
- const bulkBody = await gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
76
+ const advisory = registry . advisory ( {
77
+ id : 100 ,
78
+ vulnerable_versions : '<1.0.1' ,
79
+ } )
80
+ const bulkBody = gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
71
81
registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , bulkBody )
72
82
. reply ( 200 , {
73
83
'test-dep-a' : [ advisory ] ,
@@ -79,6 +89,55 @@ t.test('normal audit', async t => {
79
89
t . matchSnapshot ( joinedOutput ( ) )
80
90
} )
81
91
92
+ t . test ( 'fallback audit ' , async t => {
93
+ const { npm, joinedOutput } = await loadMockNpm ( t , {
94
+ prefixDir : tree ,
95
+ } )
96
+ const registry = new MockRegistry ( {
97
+ tap : t ,
98
+ registry : npm . config . get ( 'registry' ) ,
99
+ } )
100
+ const manifest = registry . manifest ( {
101
+ name : 'test-dep-a' ,
102
+ packuments : [ { version : '1.0.0' } , { version : '1.0.1' } ] ,
103
+ } )
104
+ await registry . package ( { manifest } )
105
+ const advisory = registry . advisory ( {
106
+ id : 100 ,
107
+ module_name : 'test-dep-a' ,
108
+ vulnerable_versions : '<1.0.1' ,
109
+ findings : [ { version : '1.0.0' , paths : [ 'test-dep-a' ] } ] ,
110
+ } )
111
+ registry . nock
112
+ . post ( '/-/npm/v1/security/advisories/bulk' ) . reply ( 404 )
113
+ . post ( '/-/npm/v1/security/audits/quick' , body => {
114
+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
115
+ return t . match ( unzipped , {
116
+ name : 'test-dep' ,
117
+ version : '1.0.0' ,
118
+ requires : { 'test-dep-a' : '*' } ,
119
+ dependencies : { 'test-dep-a' : { version : '1.0.0' } } ,
120
+ } )
121
+ } ) . reply ( 200 , {
122
+ actions : [ ] ,
123
+ muted : [ ] ,
124
+ advisories : {
125
+ 100 : advisory ,
126
+ } ,
127
+ metadata : {
128
+ vulnerabilities : { info : 0 , low : 0 , moderate : 0 , high : 1 , critical : 0 } ,
129
+ dependencies : 1 ,
130
+ devDependencies : 0 ,
131
+ optionalDependencies : 0 ,
132
+ totalDependencies : 1 ,
133
+ } ,
134
+ } )
135
+ await npm . exec ( 'audit' , [ ] )
136
+ t . ok ( process . exitCode , 'would have exited uncleanly' )
137
+ process . exitCode = 0
138
+ t . matchSnapshot ( joinedOutput ( ) )
139
+ } )
140
+
82
141
t . test ( 'json audit' , async t => {
83
142
const { npm, joinedOutput } = await loadMockNpm ( t , {
84
143
prefixDir : tree ,
@@ -97,7 +156,7 @@ t.test('json audit', async t => {
97
156
} )
98
157
await registry . package ( { manifest } )
99
158
const advisory = registry . advisory ( { id : 100 } )
100
- const bulkBody = await gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
159
+ const bulkBody = gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
101
160
registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , bulkBody )
102
161
. reply ( 200 , {
103
162
'test-dep-a' : [ advisory ] ,
@@ -109,7 +168,7 @@ t.test('json audit', async t => {
109
168
t . matchSnapshot ( joinedOutput ( ) )
110
169
} )
111
170
112
- t . test ( 'audit fix' , async t => {
171
+ t . test ( 'audit fix - bulk endpoint ' , async t => {
113
172
const { npm, joinedOutput } = await loadMockNpm ( t , {
114
173
prefixDir : tree ,
115
174
} )
@@ -124,20 +183,23 @@ t.test('audit fix', async t => {
124
183
await registry . package ( {
125
184
manifest,
126
185
tarballs : {
127
- '1.0.1' : path . join ( npm . prefix , 'test-dep-a' ) ,
186
+ '1.0.1' : path . join ( npm . prefix , 'test-dep-a-fixed ' ) ,
128
187
} ,
129
188
} )
130
189
const advisory = registry . advisory ( { id : 100 , vulnerable_versions : '1.0.0' } )
131
- // Can't validate this request body because it changes with each node
132
- // version/npm version and nock's body validation is not async, while
133
- // zlib.gunzip is
134
- registry . nock . post ( '/-/npm/v1/security/advisories/bulk' )
190
+ registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , body => {
191
+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
192
+ return t . same ( unzipped , { 'test-dep-a' : [ '1.0.0' ] } )
193
+ } )
135
194
. reply ( 200 , { // first audit
136
195
'test-dep-a' : [ advisory ] ,
137
196
} )
138
- . post ( '/-/npm/v1/security/advisories/bulk' )
197
+ . post ( '/-/npm/v1/security/advisories/bulk' , body => {
198
+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
199
+ return t . same ( unzipped , { 'test-dep-a' : [ '1.0.1' ] } )
200
+ } )
139
201
. reply ( 200 , { // after fix
140
- 'test-dep-a' : [ advisory ] ,
202
+ 'test-dep-a' : [ ] ,
141
203
} )
142
204
await npm . exec ( 'audit' , [ 'fix' ] )
143
205
t . matchSnapshot ( joinedOutput ( ) )
0 commit comments