@@ -66,7 +66,7 @@ t.test("it blocks in blocking mode", (t) => {
6666 } ) ;
6767
6868 server . on ( "error" , ( err ) => {
69- t . fail ( err . message ) ;
69+ t . fail ( err ) ;
7070 } ) ;
7171
7272 let stdout = "" ;
@@ -92,14 +92,35 @@ t.test("it blocks in blocking mode", (t) => {
9292 signal : AbortSignal . timeout ( 5000 ) ,
9393 }
9494 ) ,
95+ fetch (
96+ `http://local.aikido.io:4000/images/${ encodeURIComponent ( "http://local.aikido.io:4000" ) } ` ,
97+ {
98+ signal : AbortSignal . timeout ( 5000 ) ,
99+ headers : {
100+ Origin : "http://local.aikido.io:4000" ,
101+ Referer : "http://local.aikido.io:4000" ,
102+ } ,
103+ }
104+ ) ,
105+ fetch (
106+ `http://local.aikido.io:4000/images/${ encodeURIComponent ( "http://local.aikido.io:5875" ) } ` ,
107+ {
108+ signal : AbortSignal . timeout ( 5000 ) ,
109+ }
110+ ) ,
95111 ] ) ;
96112 } )
97- . then ( ( [ safeRequest , ssrfRequest ] ) => {
113+ . then ( ( [ safeRequest , ssrfRequest , requestToItself , differentPort ] ) => {
98114 t . equal ( safeRequest . status , 200 ) ;
99115 t . equal ( ssrfRequest . status , 500 ) ;
100116 t . match ( stdout , / S t a r t i n g a g e n t / ) ;
101117 t . match ( stderr , / Z e n h a s b l o c k e d a s e r v e r - s i d e r e q u e s t f o r g e r y / ) ;
102118
119+ // Requests to same hostname as the server should be allowed
120+ t . equal ( requestToItself . status , 200 ) ;
121+ // If the port is different, it should be blocked
122+ t . equal ( differentPort . status , 500 ) ;
123+
103124 return fetch ( `${ testServerUrl } /api/runtime/events` , {
104125 method : "GET" ,
105126 headers : {
@@ -114,14 +135,14 @@ t.test("it blocks in blocking mode", (t) => {
114135 const attacks = events . filter (
115136 ( event ) => event . type === "detected_attack"
116137 ) ;
117- t . same ( attacks . length , 1 ) ;
138+ t . same ( attacks . length , 2 ) ;
118139 const [ attack ] = attacks ;
119140 t . match ( attack . attack . stack , / a p p \. j s / ) ;
120141 t . match ( attack . attack . stack , / f e t c h I m a g e \. j s / ) ;
121142 t . match ( attack . attack . stack , / e x p r e s s - a s y n c - h a n d l e r / ) ;
122143 } )
123144 . catch ( ( error ) => {
124- t . fail ( error . message ) ;
145+ t . fail ( error ) ;
125146 } )
126147 . finally ( ( ) => {
127148 server . kill ( ) ;
@@ -142,6 +163,10 @@ t.test("it does not block in dry mode", (t) => {
142163 t . end ( ) ;
143164 } ) ;
144165
166+ server . on ( "error" , ( err ) => {
167+ t . fail ( err ) ;
168+ } ) ;
169+
145170 let stdout = "" ;
146171 server . stdout . on ( "data" , ( data ) => {
147172 stdout += data . toString ( ) ;
@@ -174,7 +199,58 @@ t.test("it does not block in dry mode", (t) => {
174199 t . notMatch ( stderr , / Z e n h a s b l o c k e d a s e r v e r - s i d e r e q u e s t f o r g e r y / ) ;
175200 } )
176201 . catch ( ( error ) => {
177- t . fail ( error . message ) ;
202+ t . fail ( error ) ;
203+ } )
204+ . finally ( ( ) => {
205+ server . kill ( ) ;
206+ } ) ;
207+ } ) ;
208+
209+ t . test ( "it blocks request to base URL if proxy is not trusted" , ( t ) => {
210+ const server = spawn ( `node` , [ pathToApp , "4002" ] , {
211+ env : {
212+ ...process . env ,
213+ AIKIDO_DEBUG : "true" ,
214+ AIKIDO_BLOCKING : "true" ,
215+ AIKIDO_TOKEN : token ,
216+ AIKIDO_URL : testServerUrl ,
217+ AIKIDO_TRUST_PROXY : "false" ,
218+ } ,
219+ } ) ;
220+
221+ server . on ( "close" , ( ) => {
222+ t . end ( ) ;
223+ } ) ;
224+
225+ server . on ( "error" , ( err ) => {
226+ t . fail ( err ) ;
227+ } ) ;
228+
229+ let stdout = "" ;
230+ server . stdout . on ( "data" , ( data ) => {
231+ stdout += data . toString ( ) ;
232+ } ) ;
233+
234+ let stderr = "" ;
235+ server . stderr . on ( "data" , ( data ) => {
236+ stderr += data . toString ( ) ;
237+ } ) ;
238+
239+ // Wait for the server to start
240+ timeout ( 2000 )
241+ . then ( ( ) => {
242+ return fetch (
243+ `http://local.aikido.io:4002/images/${ encodeURIComponent ( "http://local.aikido.io:4002" ) } ` ,
244+ {
245+ signal : AbortSignal . timeout ( 5000 ) ,
246+ }
247+ ) ;
248+ } )
249+ . then ( ( requestToItself ) => {
250+ t . equal ( requestToItself . status , 500 ) ;
251+ } )
252+ . catch ( ( error ) => {
253+ t . fail ( error ) ;
178254 } )
179255 . finally ( ( ) => {
180256 server . kill ( ) ;
0 commit comments