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: getRemoteHost and getRemotePort for ALB #827

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 host_header = request.getHeaders().get(HttpHeaders.HOST);
Copy link
Collaborator

Choose a reason for hiding this comment

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

please avoid underscores in variable names, use hostHeader instead.


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

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

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

@Override
public int getRemotePort() {
if (Objects.nonNull(request.getRequestContext().getElb())) {
String hostHeader = request.getHeaders().get(HttpHeaders.HOST);
String port = Arrays.asList(hostHeader.split(":")).get(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm currently seeing java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 when calling getRemotePort().

For a http Request on 80 the payload looks like this:

{
    "requestContext": {
        "elb": {
            "targetGroupArn": "arn:aws:elasticloadbalancing:eu-central-1:5952169XXXXX:targetgroup/sam-ap-MyTar-ZEEXXXXX/77425e11d8XXXX"
        }
    },
    "httpMethod": "GET",
    "path": "/",
    "queryStringParameters": {},
    "headers": {
        "accept": "*/*",
        "host": "sam-ap-MyLoa-jNFZXXXXX-000000000.eu-central-1.elb.amazonaws.com",
        "user-agent": "curl/8.4.0",
        "x-amzn-trace-id": "Root=1-661944f2-348171d959c3a5cb69d00000",
        "x-forwarded-for": "1.2.3.4",
        "x-forwarded-port": "80",
        "x-forwarded-proto": "http"
    },
    "body": "",
    "isBase64Encoded": false
}

if (Objects.nonNull(port))
return Integer.parseInt(port);
}
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
Loading