44import axios from '../../../index.js' ;
55import http from 'http' ;
66import assert from 'assert' ;
7+ import utils from '../../../lib/utils.js' ;
8+ import platform from '../../../lib/platform/index.js' ;
9+
710
811const GOOD_PORT = 4666 ;
912const BAD_PORT = 4667 ;
@@ -27,7 +30,7 @@ describe('Server-Side Request Forgery (SSRF)', () => {
2730 badServer . close ( ) ;
2831 } ) ;
2932
30- it ( 'should not fetch bad server' , async ( ) => {
33+ it ( 'should not fetch in server-side mode ' , async ( ) => {
3134 const ssrfAxios = axios . create ( {
3235 baseURL : 'http://localhost:' + String ( GOOD_PORT ) ,
3336 } ) ;
@@ -36,10 +39,43 @@ describe('Server-Side Request Forgery (SSRF)', () => {
3639 // Malicious payload is as below.
3740 const userId = '/localhost:' + String ( BAD_PORT ) ;
3841
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+ } ) ;
4480 } ) ;
4581} ) ;
0 commit comments