1- /* eslint-disable max-lines-per-function */
21import { lookup } from "dns" ;
32import { Agent } from "../agent/Agent" ;
43import { getContext } from "../agent/Context" ;
@@ -16,13 +15,28 @@ export class Fetch implements Wrapper {
1615
1716 private inspectHostname (
1817 agent : Agent ,
19- hostname : string ,
18+ url : URL ,
2019 port : number | undefined
2120 ) : InterceptorResult {
21+ if ( agent . getConfig ( ) . shouldBlockOutgoingRequest ( url . hostname ) ) {
22+ if ( typeof port === "number" && port > 0 ) {
23+ agent . onConnectHostname ( url . hostname , port , true ) ;
24+ }
25+
26+ return {
27+ operation : "fetch" ,
28+ kind : "blocked_outgoing_request" ,
29+ source : "url" ,
30+ pathsToPayload : [ ] ,
31+ metadata : { } ,
32+ payload : url . href ,
33+ } ;
34+ }
35+
2236 // Let the agent know that we are connecting to this hostname
2337 // This is to build a list of all hostnames that the application is connecting to
2438 if ( typeof port === "number" && port > 0 ) {
25- agent . onConnectHostname ( hostname , port ) ;
39+ agent . onConnectHostname ( url . hostname , port ) ;
2640 }
2741 const context = getContext ( ) ;
2842
@@ -31,7 +45,7 @@ export class Fetch implements Wrapper {
3145 }
3246
3347 return checkContextForSSRF ( {
34- hostname : hostname ,
48+ hostname : url . hostname ,
3549 operation : "fetch" ,
3650 context : context ,
3751 port : port ,
@@ -44,11 +58,7 @@ export class Fetch implements Wrapper {
4458 if ( typeof args [ 0 ] === "string" && args [ 0 ] . length > 0 ) {
4559 const url = tryParseURL ( args [ 0 ] ) ;
4660 if ( url ) {
47- const attack = this . inspectHostname (
48- agent ,
49- url . hostname ,
50- getPortFromURL ( url )
51- ) ;
61+ const attack = this . inspectHostname ( agent , url , getPortFromURL ( url ) ) ;
5262 if ( attack ) {
5363 return attack ;
5464 }
@@ -62,11 +72,7 @@ export class Fetch implements Wrapper {
6272 if ( Array . isArray ( args [ 0 ] ) ) {
6373 const url = tryParseURL ( args [ 0 ] . toString ( ) ) ;
6474 if ( url ) {
65- const attack = this . inspectHostname (
66- agent ,
67- url . hostname ,
68- getPortFromURL ( url )
69- ) ;
75+ const attack = this . inspectHostname ( agent , url , getPortFromURL ( url ) ) ;
7076 if ( attack ) {
7177 return attack ;
7278 }
@@ -77,7 +83,7 @@ export class Fetch implements Wrapper {
7783 if ( args [ 0 ] instanceof URL && args [ 0 ] . hostname . length > 0 ) {
7884 const attack = this . inspectHostname (
7985 agent ,
80- args [ 0 ] . hostname ,
86+ args [ 0 ] ,
8187 getPortFromURL ( args [ 0 ] )
8288 ) ;
8389 if ( attack ) {
@@ -89,11 +95,7 @@ export class Fetch implements Wrapper {
8995 if ( args [ 0 ] instanceof Request ) {
9096 const url = tryParseURL ( args [ 0 ] . url ) ;
9197 if ( url ) {
92- const attack = this . inspectHostname (
93- agent ,
94- url . hostname ,
95- getPortFromURL ( url )
96- ) ;
98+ const attack = this . inspectHostname ( agent , url , getPortFromURL ( url ) ) ;
9799 if ( attack ) {
98100 return attack ;
99101 }
0 commit comments