@@ -249,6 +249,72 @@ describe('SyncFetch', () => {
249
249
expect ( response . body . toString ( ) ) . toBe ( responseText ) ;
250
250
} ) ;
251
251
252
+ it ( 'Should not allow to inject code into scripts executed using child_process.execFileSync().' , ( ) => {
253
+ browserFrame . url = 'https://localhost:8080/' ;
254
+
255
+ const url =
256
+ "https://localhost:8080/`+require('child_process').execSync('id')+`/'+require('child_process').execSync('id')+'" ;
257
+ const responseText = 'test' ;
258
+
259
+ mockModule ( 'child_process' , {
260
+ execFileSync : (
261
+ command : string ,
262
+ args : string [ ] ,
263
+ options : { encoding : string ; maxBuffer : number }
264
+ ) => {
265
+ expect ( command ) . toEqual ( process . argv [ 0 ] ) ;
266
+ expect ( args [ 0 ] ) . toBe ( '-e' ) ;
267
+ expect ( args [ 1 ] ) . toBe (
268
+ SyncFetchScriptBuilder . getScript ( {
269
+ url : new URL (
270
+ "https://localhost:8080/%60+require('child_process').execSync('id')+%60/'+require('child_process').execSync('id')+'"
271
+ ) ,
272
+ method : 'GET' ,
273
+ headers : {
274
+ Accept : '*/*' ,
275
+ Connection : 'close' ,
276
+ Referer : 'https://localhost:8080/' ,
277
+ 'User-Agent' : window . navigator . userAgent ,
278
+ 'Accept-Encoding' : 'gzip, deflate, br'
279
+ } ,
280
+ body : null
281
+ } )
282
+ ) ;
283
+ // new URL() will convert ` into %60
284
+ // By using ` for the URL string within the script, we can prevent the script from being injected
285
+ expect (
286
+ args [ 1 ] . includes (
287
+ `\`https://localhost:8080/%60+require('child_process').execSync('id')+%60/'+require('child_process').execSync('id')+'\``
288
+ )
289
+ ) . toBe ( true ) ;
290
+ expect ( options ) . toEqual ( {
291
+ encoding : 'buffer' ,
292
+ maxBuffer : 1024 * 1024 * 1024
293
+ } ) ;
294
+ return JSON . stringify ( {
295
+ error : null ,
296
+ incomingMessage : {
297
+ statusCode : 200 ,
298
+ statusMessage : 'OK' ,
299
+ rawHeaders : [ ] ,
300
+ data : Buffer . from ( responseText ) . toString ( 'base64' )
301
+ }
302
+ } ) ;
303
+ }
304
+ } ) ;
305
+
306
+ const response = new SyncFetch ( {
307
+ browserFrame,
308
+ window,
309
+ url,
310
+ init : {
311
+ method : 'GET'
312
+ }
313
+ } ) . send ( ) ;
314
+
315
+ expect ( response . body . toString ( ) ) . toBe ( responseText ) ;
316
+ } ) ;
317
+
252
318
it ( 'Should send custom key/value object request headers.' , ( ) => {
253
319
browserFrame . url = 'https://localhost:8080/' ;
254
320
0 commit comments