Skip to content

Commit

Permalink
Issue #11776 - NPE proteection in Request.getServerPort
Browse files Browse the repository at this point in the history
If the HttpURI is scheme-less, the attempt to get the port can result in a NPE
  • Loading branch information
joakime authored and lorban committed May 14, 2024
1 parent c54141a commit 9160108
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,8 @@ public static Stream<Arguments> concatNormalizedURICases()
Arguments.of("hTTps", "example.org", 443, "/", null, null, "https://example.org/"),
Arguments.of("WS", "example.org", 8282, "/", null, null, "ws://example.org:8282/"),
Arguments.of("wsS", "example.org", 8383, "/", null, null, "wss://example.org:8383/"),
// Undefined scheme
Arguments.of(null, "example.org", 8181, "/", null, null, "//example.org:8181/"),
// Undefined Ports
Arguments.of("http", "example.org", 0, "/", null, null, "http://example.org/"),
Arguments.of("https", "example.org", -1, "/", null, null, "https://example.org/"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,13 @@ static int getServerPort(Request request)
return authority.getPort();

// Is there a scheme with a default port?
HttpScheme scheme = HttpScheme.CACHE.get(request.getHttpURI().getScheme());
if (scheme != null && scheme.getDefaultPort() > 0)
return scheme.getDefaultPort();
String rawScheme = uri.getScheme();
if (StringUtil.isNotBlank(rawScheme))
{
HttpScheme scheme = HttpScheme.CACHE.get(rawScheme);
if (scheme != null && scheme.getDefaultPort() > 0)
return scheme.getDefaultPort();
}

// Is there a local port?
SocketAddress local = request.getConnectionMetaData().getLocalSocketAddress();
Expand Down

0 comments on commit 9160108

Please sign in to comment.