4
4
import axios from '../../../index.js' ;
5
5
import http from 'http' ;
6
6
import assert from 'assert' ;
7
+ import utils from '../../../lib/utils.js' ;
8
+ import platform from '../../../lib/platform/index.js' ;
9
+
7
10
8
11
const GOOD_PORT = 4666 ;
9
12
const BAD_PORT = 4667 ;
@@ -27,7 +30,7 @@ describe('Server-Side Request Forgery (SSRF)', () => {
27
30
badServer . close ( ) ;
28
31
} ) ;
29
32
30
- it ( 'should not fetch bad server' , async ( ) => {
33
+ it ( 'should not fetch in server-side mode ' , async ( ) => {
31
34
const ssrfAxios = axios . create ( {
32
35
baseURL : 'http://localhost:' + String ( GOOD_PORT ) ,
33
36
} ) ;
@@ -36,10 +39,43 @@ describe('Server-Side Request Forgery (SSRF)', () => {
36
39
// Malicious payload is as below.
37
40
const userId = '/localhost:' + String ( BAD_PORT ) ;
38
41
39
- const response = await ssrfAxios . get ( `/${ userId } ` ) ;
40
- assert . strictEqual ( response . data , 'good' ) ;
41
- assert . strictEqual ( response . config . baseURL , 'http://localhost:' + String ( GOOD_PORT ) ) ;
42
- assert . strictEqual ( response . config . url , '//localhost:' + String ( BAD_PORT ) ) ;
43
- assert . strictEqual ( response . request . res . responseUrl , 'http://localhost:' + String ( GOOD_PORT ) + '/localhost:' + String ( BAD_PORT ) ) ;
42
+ try {
43
+ await ssrfAxios . get ( `/${ userId } ` ) ;
44
+ } catch ( error ) {
45
+ assert . ok ( error . message . startsWith ( 'Invalid URL' ) ) ;
46
+ return ;
47
+ }
48
+ assert . fail ( 'Expected an error to be thrown' ) ;
49
+ } ) ;
50
+
51
+ describe ( 'should fetch in client-side mode' , ( ) => {
52
+ let hasBrowserEnv , origin ;
53
+
54
+ before ( ( ) => {
55
+ hasBrowserEnv = utils . hasBrowserEnv ;
56
+ origin = platform . origin ;
57
+ utils . hasBrowserEnv = true ;
58
+ platform . origin = 'http://localhost:' + String ( GOOD_PORT ) ;
59
+ } ) ;
60
+ after ( ( ) => {
61
+ utils . hasBrowserEnv = hasBrowserEnv ;
62
+ platform . origin = origin ;
63
+ } ) ;
64
+ it ( 'should fetch in client-side mode' , async ( ) => {
65
+ utils . hasBrowserEnv = true ;
66
+ const ssrfAxios = axios . create ( {
67
+ baseURL : 'http://localhost:' + String ( GOOD_PORT ) ,
68
+ } ) ;
69
+
70
+ // Good payload would be `userId = '12345'`
71
+ // Malicious payload is as below.
72
+ const userId = '/localhost:' + String ( BAD_PORT ) ;
73
+
74
+ const response = await ssrfAxios . get ( `/${ userId } ` ) ;
75
+ assert . strictEqual ( response . data , 'bad' ) ;
76
+ assert . strictEqual ( response . config . baseURL , 'http://localhost:' + String ( GOOD_PORT ) ) ;
77
+ assert . strictEqual ( response . config . url , '//localhost:' + String ( BAD_PORT ) ) ;
78
+ assert . strictEqual ( response . request . res . responseUrl , 'http://localhost:' + String ( BAD_PORT ) + '/' ) ;
79
+ } ) ;
44
80
} ) ;
45
81
} ) ;
0 commit comments