Skip to content

Commit

Permalink
Merge pull request #1756 from gritGmbH/bugfix/fix-xforwarded-host-han…
Browse files Browse the repository at this point in the history
…dling-3.5

Fix URL creation when header X-Forwarded-Port and X-Forwarded-Host with port are used (3.5)
  • Loading branch information
copierrj authored Nov 6, 2024
2 parents 01c6030 + d87cbe3 commit 377a434
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1594,11 +1594,12 @@ private static String buildUrlFromForwardedHeader( RequestContext context, URL s
String xForwardedProto = context.getXForwardedProto();

String protocol = parseProtocol( xForwardedProto, serviceUrl );
String port = parsePort( xForwardedPort, serviceUrl );
String port = parsePort( xForwardedPort, xForwardedHost, serviceUrl );
String path = serviceUrl.getPath();
String host = parseHost( xForwardedHost );

StringBuffer urlBuilder = new StringBuffer();
urlBuilder.append( protocol ).append( "://" ).append( xForwardedHost );
urlBuilder.append( protocol ).append( "://" ).append( host );
if ( port != null )
urlBuilder.append( ":" ).append( port );
if ( path != null && !"".equals( path ) )
Expand All @@ -1613,12 +1614,19 @@ private static String parseProtocol( String xForwardedProto, URL serviceUrl ) {
return serviceUrl.getProtocol();
}

private static String parsePort( String xForwardedPort, URL serviceUrl ) {
private static String parsePort( String xForwardedPort, String xForwardedHost, URL serviceUrl ) {
if ( xForwardedPort != null && !"".equals( xForwardedPort ) )
return xForwardedPort;
else if ( xForwardedHost != null && xForwardedHost.contains( ":" ) && ( xForwardedHost.lastIndexOf( ":" ) + 1 ) < xForwardedHost.length() )
return xForwardedHost.substring( xForwardedHost.lastIndexOf( ":" ) + 1 );
else if ( serviceUrl.getPort() > -1 )
return Integer.toString( serviceUrl.getPort() );
return null;
}

private static String parseHost(String xForwardedHost) {
if ( xForwardedHost != null && xForwardedHost.contains(":") )
return xForwardedHost.substring( 0, xForwardedHost.indexOf(":") );
return xForwardedHost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ private void prepareOGCFrontController(RequestContext mockedContext) throws Exce
PowerMockito.when(OGCFrontController.class, "getHttpURL").thenCallRealMethod();
PowerMockito.when(OGCFrontController.class, "buildUrlFromForwardedHeader", eq(mockedContext), any(URL.class))
.thenCallRealMethod();
PowerMockito.when(OGCFrontController.class, "parseHost", anyString()).thenCallRealMethod();
PowerMockito.when(OGCFrontController.class, "parseProtocol", anyString(), any(URL.class)).thenCallRealMethod();
PowerMockito.when(OGCFrontController.class, "parsePort", anyString(), any(URL.class)).thenCallRealMethod();
PowerMockito.when(OGCFrontController.class, "parsePort", any(), anyString(), any(URL.class))
.thenCallRealMethod();
PowerMockito.when(OGCFrontController.getContext()).thenReturn(mockedContext);
}

Expand All @@ -163,4 +165,34 @@ private RequestContext mockContext(String serviceUrl, String xForwardedHost, Str
return context;
}

@Test
public void testGetHttpPostURLWithXForwardedHostWithPortAndXForwardedPort() throws Exception {
String serviceUrl = "http://myservice.de:9090/deegree-webservices/test";
String xForwardedHost = "xForwardedHost.de:8088";
String xForwardedPort = "8089";
String xForwardedProto = "https";
RequestContext mockedContext = mockContext(serviceUrl, xForwardedHost, xForwardedPort, xForwardedProto);

prepareOGCFrontController(mockedContext);

String httpPostURL = OGCFrontController.getHttpPostURL();

assertThat(httpPostURL, is("https://xForwardedHost.de:8089/deegree-webservices/test"));
}

@Test
public void testGetHttpPostURLWithXForwardedHostWithPortWithoutXForwardedPort() throws Exception {
String serviceUrl = "http://myservice.de:9090/deegree-webservices/test";
String xForwardedHost = "xForwardedHost.de:8089";
String xForwardedPort = null;
String xForwardedProto = "https";
RequestContext mockedContext = mockContext(serviceUrl, xForwardedHost, xForwardedPort, xForwardedProto);

prepareOGCFrontController(mockedContext);

String httpPostURL = OGCFrontController.getHttpPostURL();

assertThat(httpPostURL, is("https://xForwardedHost.de:8089/deegree-webservices/test"));
}

}

0 comments on commit 377a434

Please sign in to comment.