@@ -6,6 +6,7 @@ import { HttpProxyAgent } from 'http-proxy-agent';
6
6
import { HttpsProxyAgent } from 'https-proxy-agent' ;
7
7
import config from '../config' ;
8
8
import { checkPrivateIp } from './check-private-ip' ;
9
+ import { checkAllowedUrl } from './check-allowed-url' ;
9
10
10
11
export async function getJson ( url : string , accept = 'application/json, */*' , timeout = 10000 , headers ?: Record < string , string > ) : Promise < any > {
11
12
const res = await getResponse ( {
@@ -42,9 +43,8 @@ const OPERATION_TIMEOUT = 60 * 1000;
42
43
const MAX_RESPONSE_SIZE = 10 * 1024 * 1024 ;
43
44
44
45
export async function getResponse ( args : { url : string , method : 'GET' | 'POST' , body ?: string , headers : Record < string , string > , timeout ?: number , size ?: number } ) {
45
- const u = new URL ( args . url ) ;
46
- if ( ! u . protocol . match ( / ^ h t t p s ? : $ / ) || u . hostname === 'unix' ) {
47
- throw new StatusError ( 'Invalid protocol' , 400 ) ;
46
+ if ( ! checkAllowedUrl ( args . url ) ) {
47
+ throw new StatusError ( 'Invalid URL' , 400 ) ;
48
48
}
49
49
50
50
const timeout = args . timeout || RESPONSE_TIMEOUT ;
@@ -71,6 +71,12 @@ export async function getResponse(args: { url: string, method: 'GET' | 'POST', b
71
71
retry : 0 ,
72
72
} ) ;
73
73
74
+ req . on ( 'redirect' , ( res , opts ) => {
75
+ if ( ! checkAllowedUrl ( opts . url ) ) {
76
+ req . cancel ( `Invalid url: ${ opts . url } ` ) ;
77
+ }
78
+ } ) ;
79
+
74
80
return await receiveResponce ( req , args . size || MAX_RESPONSE_SIZE ) ;
75
81
}
76
82
0 commit comments