@@ -390,7 +390,7 @@ function isUrlAllowed(parsedUrl: URL, allowList: string[], allowSubdomains: bool
390390
391391 const allowedScheme = hasExplicitScheme ? parsedAllowed . protocol . replace ( / : $ / , '' ) . toLowerCase ( ) : '' ;
392392 const allowedPort = safeGetPort ( parsedAllowed , allowedScheme ) ;
393- const allowIndicatesPort = parsedAllowed . host . includes ( ':' ) && ! parsedAllowed . host . startsWith ( '[' ) ;
393+ const allowIndicatesPort = Boolean ( parsedAllowed . host ) && parsedAllowed . host . includes ( ':' ) && ! parsedAllowed . host . startsWith ( '[' ) ;
394394 if ( allowedPort === null && allowIndicatesPort ) {
395395 continue ;
396396 }
@@ -410,10 +410,17 @@ function isUrlAllowed(parsedUrl: URL, allowList: string[], allowSubdomains: bool
410410 continue ;
411411 }
412412
413- // Port matching: only enforce when allow list entry explicitly specifies a port
414- // Check parsedAllowed.port (empty string when no port specified) not allowedPort (always has default)
415- if ( parsedAllowed . port ) {
416- if ( urlPort === null || allowedPort !== urlPort ) {
413+ // Port matching: only enforce when allow list entry explicitly specifies a non-default port
414+ // Explicit default ports (e.g., :443 for https) should be treated as no port specified
415+ const allowedHasNonDefaultPort = parsedAllowed . port &&
416+ ( allowedPort !== DEFAULT_PORTS [ allowedScheme as keyof typeof DEFAULT_PORTS ] ) ;
417+
418+ if ( allowedHasNonDefaultPort ) {
419+ // Allow list has explicit non-default port, so URL must match exactly
420+ const urlHasNonDefaultPort = parsedUrl . port &&
421+ ( urlPort !== DEFAULT_PORTS [ schemeLower as keyof typeof DEFAULT_PORTS ] ) ;
422+
423+ if ( ! urlHasNonDefaultPort || allowedPort !== urlPort ) {
417424 continue ;
418425 }
419426 }
@@ -448,10 +455,17 @@ function isUrlAllowed(parsedUrl: URL, allowList: string[], allowSubdomains: bool
448455
449456 const allowedDomain = allowedHost . replace ( / ^ w w w \. / , '' ) ;
450457
451- // Port matching: only enforce when allow list entry explicitly specifies a port
452- // Check parsedAllowed.port (empty string when no port specified) not allowedPort (always has default)
453- if ( parsedAllowed . port ) {
454- if ( urlPort === null || allowedPort !== urlPort ) {
458+ // Port matching: only enforce when allow list entry explicitly specifies a non-default port
459+ // Explicit default ports (e.g., :443 for https) should be treated as no port specified
460+ const allowedHasNonDefaultPort = parsedAllowed . port &&
461+ ( allowedPort !== DEFAULT_PORTS [ allowedScheme as keyof typeof DEFAULT_PORTS ] ) ;
462+
463+ if ( allowedHasNonDefaultPort ) {
464+ // Allow list has explicit non-default port, so URL must match exactly
465+ const urlHasNonDefaultPort = parsedUrl . port &&
466+ ( urlPort !== DEFAULT_PORTS [ schemeLower as keyof typeof DEFAULT_PORTS ] ) ;
467+
468+ if ( ! urlHasNonDefaultPort || allowedPort !== urlPort ) {
455469 continue ;
456470 }
457471 }
0 commit comments