@@ -2,9 +2,9 @@ const t = require('tap')
22
33const  {  load : loadMockNpm  }  =  require ( '../../fixtures/mock-npm' ) 
44const  MockRegistry  =  require ( '../../fixtures/mock-registry.js' ) 
5- const  util  =  require ( 'util' ) 
65const  zlib  =  require ( 'zlib' ) 
7- const  gzip  =  util . promisify ( zlib . gzip ) 
6+ const  gzip  =  zlib . gzipSync 
7+ const  gunzip  =  zlib . gunzipSync 
88const  path  =  require ( 'path' ) 
99const  fs  =  require ( 'fs' ) 
1010
@@ -43,7 +43,14 @@ const tree = {
4343      } , 
4444    } , 
4545  } ) , 
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' : { 
4754    'package.json' : JSON . stringify ( { 
4855      name : 'test-dep-a' , 
4956      version : '1.0.1' , 
@@ -66,8 +73,11 @@ t.test('normal audit', async t => {
6673    packuments : [ {  version : '1.0.0'  } ,  {  version : '1.0.1'  } ] , 
6774  } ) 
6875  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' ]  } ) ) 
7181  registry . nock . post ( '/-/npm/v1/security/advisories/bulk' ,  bulkBody ) 
7282    . reply ( 200 ,  { 
7383      'test-dep-a' : [ advisory ] , 
@@ -79,6 +89,55 @@ t.test('normal audit', async t => {
7989  t . matchSnapshot ( joinedOutput ( ) ) 
8090} ) 
8191
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+ 
82141t . test ( 'json audit' ,  async  t  =>  { 
83142  const  {  npm,  joinedOutput }  =  await  loadMockNpm ( t ,  { 
84143    prefixDir : tree , 
@@ -97,7 +156,7 @@ t.test('json audit', async t => {
97156  } ) 
98157  await  registry . package ( {  manifest } ) 
99158  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' ]  } ) ) 
101160  registry . nock . post ( '/-/npm/v1/security/advisories/bulk' ,  bulkBody ) 
102161    . reply ( 200 ,  { 
103162      'test-dep-a' : [ advisory ] , 
@@ -109,7 +168,7 @@ t.test('json audit', async t => {
109168  t . matchSnapshot ( joinedOutput ( ) ) 
110169} ) 
111170
112- t . test ( 'audit fix' ,  async  t  =>  { 
171+ t . test ( 'audit fix - bulk endpoint ' ,  async  t  =>  { 
113172  const  {  npm,  joinedOutput }  =  await  loadMockNpm ( t ,  { 
114173    prefixDir : tree , 
115174  } ) 
@@ -124,20 +183,23 @@ t.test('audit fix', async t => {
124183  await  registry . package ( { 
125184    manifest, 
126185    tarballs : { 
127-       '1.0.1' : path . join ( npm . prefix ,  'test-dep-a' ) , 
186+       '1.0.1' : path . join ( npm . prefix ,  'test-dep-a-fixed ' ) , 
128187    } , 
129188  } ) 
130189  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+   } ) 
135194    . reply ( 200 ,  {  // first audit 
136195      'test-dep-a' : [ advisory ] , 
137196    } ) 
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+     } ) 
139201    . reply ( 200 ,  {  // after fix 
140-       'test-dep-a' : [ advisory ] , 
202+       'test-dep-a' : [ ] , 
141203    } ) 
142204  await  npm . exec ( 'audit' ,  [ 'fix' ] ) 
143205  t . matchSnapshot ( joinedOutput ( ) ) 
0 commit comments