@@ -23,26 +23,29 @@ function stringToArrayBuffer(str: string) {
23
23
24
24
describe ( "request()" , ( ) => {
25
25
it ( "Test ReDoS - attack string" , ( ) => {
26
- const originalFetch = globalThis . fetch ;
27
- globalThis . fetch = async ( url , options ) => {
28
- const response = await originalFetch ( url , options ) ;
26
+ const fakeFetch = async ( url , options ) => {
27
+ const response = await fetch ( url , options ) ;
29
28
const fakeHeaders = new Headers ( response . headers ) ;
30
29
fakeHeaders . set ( "link" , "<" . repeat ( 100000 ) + ">" ) ;
31
30
fakeHeaders . set ( "deprecation" , "true" ) ;
32
31
return new Response ( response . body , {
33
32
status : response . status ,
34
33
statusText : response . statusText ,
35
- headers : fakeHeaders
34
+ headers : fakeHeaders ,
36
35
} ) ;
37
36
} ;
38
37
const startTime = performance . now ( ) ;
39
- request ( "GET /repos/octocat/hello-world" ) ;
38
+ request ( "GET /repos/octocat/hello-world" , {
39
+ request : { fetch : fakeFetch } ,
40
+ } ) ;
40
41
const endTime = performance . now ( ) ;
41
42
const elapsedTime = endTime - startTime ;
42
- const reDosThreshold = 2000 ;
43
+ const reDosThreshold = 2000 ;
43
44
expect ( elapsedTime ) . toBeLessThanOrEqual ( reDosThreshold ) ;
44
45
if ( elapsedTime > reDosThreshold ) {
45
- console . warn ( `🚨 Potential ReDoS Attack! getDuration method took ${ elapsedTime . toFixed ( 2 ) } ms, exceeding threshold of ${ reDosThreshold } ms.` ) ;
46
+ console . warn (
47
+ `🚨 Potential ReDoS Attack! getDuration method took ${ elapsedTime . toFixed ( 2 ) } ms, exceeding threshold of ${ reDosThreshold } ms.` ,
48
+ ) ;
46
49
}
47
50
} ) ;
48
51
0 commit comments