1+ import { addIPv4MappedAddresses } from "../helpers/addIPv4MappedAddresses" ;
12import { IPMatcher } from "../helpers/ip-matcher/IPMatcher" ;
23import { LimitedContext , matchEndpoints } from "../helpers/matchEndpoints" ;
34import { isPrivateIP } from "../vulnerabilities/ssrf/isPrivateIP" ;
@@ -50,12 +51,15 @@ export class ServiceConfig {
5051 this . graphqlFields = [ ] ;
5152
5253 for ( const endpoint of endpointConfigs ) {
53- let allowedIPAddresses = undefined ;
54+ let allowedIPAddresses : IPMatcher | undefined = undefined ;
5455 if (
5556 Array . isArray ( endpoint . allowedIPAddresses ) &&
5657 endpoint . allowedIPAddresses . length > 0
5758 ) {
58- allowedIPAddresses = new IPMatcher ( endpoint . allowedIPAddresses ) ;
59+ // Small list, frequently accessed: add IPv4-mapped versions at creation time for fast lookups
60+ allowedIPAddresses = new IPMatcher (
61+ addIPv4MappedAddresses ( endpoint . allowedIPAddresses )
62+ ) ;
5963 }
6064
6165 const endpointConfig = { ...endpoint , allowedIPAddresses } ;
@@ -98,7 +102,10 @@ export class ServiceConfig {
98102 this . bypassedIPAddresses = undefined ;
99103 return ;
100104 }
101- this . bypassedIPAddresses = new IPMatcher ( ipAddresses ) ;
105+ // Small list, frequently accessed: add IPv4-mapped versions at creation time for fast lookups
106+ this . bypassedIPAddresses = new IPMatcher (
107+ addIPv4MappedAddresses ( ipAddresses )
108+ ) ;
102109 }
103110
104111 isBypassedIP ( ip : string ) {
@@ -119,8 +126,8 @@ export class ServiceConfig {
119126 isIPAddressBlocked (
120127 ip : string
121128 ) : { blocked : true ; reason : string } | { blocked : false } {
122- const blocklist = this . blockedIPAddresses . find ( ( blocklist ) =>
123- blocklist . blocklist . has ( ip )
129+ const blocklist = this . blockedIPAddresses . find ( ( list ) =>
130+ list . blocklist . hasWithMappedCheck ( ip )
124131 ) ;
125132
126133 if ( blocklist ) {
@@ -136,6 +143,7 @@ export class ServiceConfig {
136143 for ( const source of blockedIPAddresses ) {
137144 this . blockedIPAddresses . push ( {
138145 key : source . key ,
146+ // Large list: IPv4-mapped checked at lookup time to save memory
139147 blocklist : new IPMatcher ( source . ips ) ,
140148 description : source . description ,
141149 } ) ;
@@ -152,6 +160,7 @@ export class ServiceConfig {
152160 for ( const source of monitoredIPAddresses ) {
153161 this . monitoredIPAddresses . push ( {
154162 key : source . key ,
163+ // Large list: IPv4-mapped checked at lookup time to save memory
155164 list : new IPMatcher ( source . ips ) ,
156165 } ) ;
157166 }
@@ -213,13 +222,13 @@ export class ServiceConfig {
213222
214223 getMatchingBlockedIPListKeys ( ip : string ) : string [ ] {
215224 return this . blockedIPAddresses
216- . filter ( ( list ) => list . blocklist . has ( ip ) )
225+ . filter ( ( list ) => list . blocklist . hasWithMappedCheck ( ip ) )
217226 . map ( ( list ) => list . key ) ;
218227 }
219228
220229 getMatchingMonitoredIPListKeys ( ip : string ) : string [ ] {
221230 return this . monitoredIPAddresses
222- . filter ( ( list ) => list . list . has ( ip ) )
231+ . filter ( ( list ) => list . list . hasWithMappedCheck ( ip ) )
223232 . map ( ( list ) => list . key ) ;
224233 }
225234
@@ -232,6 +241,7 @@ export class ServiceConfig {
232241 continue ;
233242 }
234243 this . allowedIPAddresses . push ( {
244+ // Large list: IPv4-mapped checked at lookup time to save memory
235245 allowlist : new IPMatcher ( source . ips ) ,
236246 description : source . description ,
237247 } ) ;
@@ -253,7 +263,7 @@ export class ServiceConfig {
253263 }
254264
255265 const allowlist = this . allowedIPAddresses . find ( ( list ) =>
256- list . allowlist . has ( ip )
266+ list . allowlist . hasWithMappedCheck ( ip )
257267 ) ;
258268
259269 return { allowed : ! ! allowlist } ;
0 commit comments