@@ -4,7 +4,8 @@ const { load: loadMockNpm } = require('../../fixtures/mock-npm')
44const MockRegistry = require ( '../../fixtures/mock-registry.js' )
55const util = require ( 'util' )
66const zlib = require ( 'zlib' )
7- const gzip = util . promisify ( zlib . gzip )
7+ const gzip = zlib . gzipSync
8+ const gunzip = zlib . gunzipSync
89const path = require ( 'path' )
910const fs = require ( 'fs' )
1011
@@ -43,7 +44,14 @@ const tree = {
4344 } ,
4445 } ,
4546 } ) ,
46- 'test-dep-a' : {
47+ 'test-dep-a-vuln' : {
48+ 'package.json' : JSON . stringify ( {
49+ name : 'test-dep-a' ,
50+ version : '1.0.0' ,
51+ } ) ,
52+ 'vulnerable.txt' : 'vulnerable test-dep-a' ,
53+ } ,
54+ 'test-dep-a-fixed' : {
4755 'package.json' : JSON . stringify ( {
4856 name : 'test-dep-a' ,
4957 version : '1.0.1' ,
@@ -66,8 +74,11 @@ t.test('normal audit', async t => {
6674 packuments : [ { version : '1.0.0' } , { version : '1.0.1' } ] ,
6775 } )
6876 await registry . package ( { manifest } )
69- const advisory = registry . advisory ( { id : 100 } )
70- const bulkBody = await gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
77+ const advisory = registry . advisory ( {
78+ id : 100 ,
79+ vulnerable_versions : '<1.0.1' ,
80+ } )
81+ const bulkBody = gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
7182 registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , bulkBody )
7283 . reply ( 200 , {
7384 'test-dep-a' : [ advisory ] ,
@@ -79,6 +90,55 @@ t.test('normal audit', async t => {
7990 t . matchSnapshot ( joinedOutput ( ) )
8091} )
8192
93+ t . test ( 'fallback audit ' , async t => {
94+ const { npm, joinedOutput } = await loadMockNpm ( t , {
95+ prefixDir : tree ,
96+ } )
97+ const registry = new MockRegistry ( {
98+ tap : t ,
99+ registry : npm . config . get ( 'registry' ) ,
100+ } )
101+ const manifest = registry . manifest ( {
102+ name : 'test-dep-a' ,
103+ packuments : [ { version : '1.0.0' } , { version : '1.0.1' } ] ,
104+ } )
105+ await registry . package ( { manifest } )
106+ const advisory = registry . advisory ( {
107+ id : 100 ,
108+ module_name : 'test-dep-a' ,
109+ vulnerable_versions : '<1.0.1' ,
110+ findings : [ { version : '1.0.0' , paths : [ 'test-dep-a' ] } ] ,
111+ } )
112+ registry . nock
113+ . post ( '/-/npm/v1/security/advisories/bulk' ) . reply ( 404 )
114+ . post ( '/-/npm/v1/security/audits/quick' , body => {
115+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
116+ return t . match ( unzipped , {
117+ name : 'test-dep' ,
118+ version : '1.0.0' ,
119+ requires : { 'test-dep-a' : '*' } ,
120+ dependencies : { 'test-dep-a' : { version : '1.0.0' } } ,
121+ } )
122+ } ) . reply ( 200 , {
123+ actions : [ ] ,
124+ muted : [ ] ,
125+ advisories : {
126+ '100' : advisory
127+ } ,
128+ metadata : {
129+ vulnerabilities : { info : 0 , low : 0 , moderate : 0 , high : 1 , critical : 0 } ,
130+ dependencies : 1 ,
131+ devDependencies : 0 ,
132+ optionalDependencies : 0 ,
133+ totalDependencies : 1
134+ }
135+ } )
136+ await npm . exec ( 'audit' , [ ] )
137+ t . ok ( process . exitCode , 'would have exited uncleanly' )
138+ process . exitCode = 0
139+ t . matchSnapshot ( joinedOutput ( ) )
140+ } )
141+
82142t . test ( 'json audit' , async t => {
83143 const { npm, joinedOutput } = await loadMockNpm ( t , {
84144 prefixDir : tree ,
@@ -97,7 +157,7 @@ t.test('json audit', async t => {
97157 } )
98158 await registry . package ( { manifest } )
99159 const advisory = registry . advisory ( { id : 100 } )
100- const bulkBody = await gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
160+ const bulkBody = gzip ( JSON . stringify ( { 'test-dep-a' : [ '1.0.0' ] } ) )
101161 registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , bulkBody )
102162 . reply ( 200 , {
103163 'test-dep-a' : [ advisory ] ,
@@ -109,7 +169,7 @@ t.test('json audit', async t => {
109169 t . matchSnapshot ( joinedOutput ( ) )
110170} )
111171
112- t . test ( 'audit fix' , async t => {
172+ t . test ( 'audit fix - bulk endpoint ' , async t => {
113173 const { npm, joinedOutput } = await loadMockNpm ( t , {
114174 prefixDir : tree ,
115175 } )
@@ -124,20 +184,23 @@ t.test('audit fix', async t => {
124184 await registry . package ( {
125185 manifest,
126186 tarballs : {
127- '1.0.1' : path . join ( npm . prefix , 'test-dep-a' ) ,
187+ '1.0.1' : path . join ( npm . prefix , 'test-dep-a-fixed ' ) ,
128188 } ,
129189 } )
130190 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' )
191+ registry . nock . post ( '/-/npm/v1/security/advisories/bulk' , body => {
192+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
193+ return t . same ( unzipped , { 'test-dep-a' : [ '1.0.0' ] } )
194+ } )
135195 . reply ( 200 , { // first audit
136196 'test-dep-a' : [ advisory ] ,
137197 } )
138- . post ( '/-/npm/v1/security/advisories/bulk' )
198+ . post ( '/-/npm/v1/security/advisories/bulk' , body => {
199+ const unzipped = JSON . parse ( gunzip ( Buffer . from ( body , 'hex' ) ) )
200+ return t . same ( unzipped , { 'test-dep-a' : [ '1.0.1' ] } )
201+ } )
139202 . reply ( 200 , { // after fix
140- 'test-dep-a' : [ advisory ] ,
203+ 'test-dep-a' : [ ] ,
141204 } )
142205 await npm . exec ( 'audit' , [ 'fix' ] )
143206 t . matchSnapshot ( joinedOutput ( ) )
0 commit comments