Skip to content
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

fix: Error with version 2.0.2 when multi-value header is active in alb #910

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -437,23 +437,27 @@ public String getRemoteAddr() {
if (request.getRequestContext() == null || request.getRequestContext().getIdentity() == null) {
return "127.0.0.1";
}
if (request.getRequestContext().getElb() != null) {
return request.getHeaders().get(CLIENT_IP_HEADER);
if (request.getRequestSource().equals(RequestSource.ALB)) {
return Objects.nonNull(request.getHeaders()) ?
request.getHeaders().get(CLIENT_IP_HEADER) :
request.getMultiValueHeaders().getFirst(CLIENT_IP_HEADER);
}
return request.getRequestContext().getIdentity().getSourceIp();
}


@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);
String hostHeader;
if (request.getRequestSource().equals(RequestSource.ALB)) {
hostHeader = Objects.nonNull(request.getHeaders()) ?
request.getHeaders().get(HttpHeaders.HOST) :
request.getMultiValueHeaders().getFirst(HttpHeaders.HOST);
} else {
hostHeader = request.getMultiValueHeaders().getFirst(HttpHeaders.HOST);
}

return request.getMultiValueHeaders().getFirst(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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you please remove the duplication, so we end up with a single return 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.

}


Expand Down Expand Up @@ -483,8 +487,11 @@ public RequestDispatcher getRequestDispatcher(String s) {

@Override
public int getRemotePort() {
if (Objects.nonNull(request.getRequestContext().getElb())) {
String portHeader = request.getHeaders().get(PORT_HEADER_NAME);
if (request.getRequestSource().equals(RequestSource.ALB)) {
String portHeader;
portHeader = Objects.nonNull(request.getHeaders()) ?
request.getHeaders().get(PORT_HEADER_NAME) :
request.getMultiValueHeaders().getFirst(PORT_HEADER_NAME);
if (Objects.nonNull(portHeader)) {
return Integer.parseInt(portHeader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,17 +652,31 @@ void serverName_albHostHeader_returnsHostHeader() {
}

@Test
void getRemoteHost_albHostHeader_returnsHostHeader() {
void getRemoteHost_albHostHeader_singleValue_returnsHostHeader() {
initAwsProxyHttpServletRequestTest("ALB");
AwsProxyRequest proxyReq = new AwsProxyRequestBuilder("/test", "GET")
.alb().build();
proxyReq.setMultiValueHeaders(null);
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);
}

@Test
void getRemoteHost_albHostHeader_multiValue_returnsHostHeader() {
initAwsProxyHttpServletRequestTest("ALB");
AwsProxyRequest proxyReq = new AwsProxyRequestBuilder("/test", "GET")
.header(HttpHeaders.HOST, "testapi.us-east-1.elb.amazonaws.com")
.alb().build();
proxyReq.setHeaders(null);
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
Loading