Skip to content

fix: getRemoteHost and getRemotePort for ALB #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import com.amazonaws.serverless.proxy.model.RequestSource;
import com.amazonaws.services.lambda.runtime.Context;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpUpgradeHandler;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -441,6 +443,13 @@ public String getRemoteAddr() {

@Override
public String getRemoteHost() {
if (Objects.nonNull(request.getRequestContext().getElb())) {
String hostHeader = request.getHeaders().get(HttpHeaders.HOST);

// the host header has the form host:port, so we split the string to get the host part
return Arrays.asList(hostHeader.split(":")).get(0);
}

return request.getMultiValueHeaders().getFirst(HttpHeaders.HOST);
}

Expand Down Expand Up @@ -471,6 +480,11 @@ public RequestDispatcher getRequestDispatcher(String s) {

@Override
public int getRemotePort() {
if (Objects.nonNull(request.getRequestContext().getElb())) {
String hostHeader = request.getHeaders().get(PORT_HEADER_NAME);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable naming is misleading as it is no longer the host header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks.

Copy link
Contributor Author

@mbfreder mbfreder Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right. We need to update getRemoteAddr() to take into account the x-forwarded-for header. For getServerPort(), I'm a bit confused. Looking at the javadoc for both getServerPort() and getRemotePort(), I'm wondering if Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request. and Returns the port number to which the request was sent don't mean the same thing. Is ALB considered as a client in this case?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it even more interesting there is also getLocalPort().

ALB is the last proxy so that should be fine.

For comparison Tomcat sets serverPort based on scheme https://github.com/apache/tomcat/blob/main/java/org/apache/catalina/connector/CoyoteAdapter.java#L579.

if (Objects.nonNull(hostHeader))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use brackets for the if statement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks

return Integer.parseInt(hostHeader);
}
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,18 @@ void serverName_albHostHeader_returnsHostHeader() {
assertEquals("testapi.us-east-1.elb.amazonaws.com", serverName);
}

@Test
void getRemoteHost_albHostHeader_returnsHostHeader() {
initAwsProxyHttpServletRequestTest("ALB");
AwsProxyRequest proxyReq = new AwsProxyRequestBuilder("/test", "GET")
.alb().build();
proxyReq.getHeaders().put(HttpHeaders.HOST, "testapi.us-east-1.elb.amazonaws.com");
HttpServletRequest servletRequest = new AwsProxyHttpServletRequest(proxyReq, null, null);

String host = servletRequest.getRemoteHost();
assertEquals("testapi.us-east-1.elb.amazonaws.com", host);
}

private AwsProxyRequestBuilder getRequestWithHeaders() {
return new AwsProxyRequestBuilder("/hello", "GET")
.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public AwsProxyRequestBuilder(AwsProxyRequest req) {

public AwsProxyRequestBuilder(String path, String httpMethod) {
this.request = new AwsProxyRequest();
this.request.setMultiValueHeaders(new Headers()); // avoid NPE
this.request.setMultiValueHeaders(new Headers());// avoid NPE
this.request.setHeaders(new SingleValueHeaders());
this.request.setHttpMethod(httpMethod);
this.request.setPath(path);
this.request.setMultiValueQueryStringParameters(new MultiValuedTreeMap<>());
Expand Down