-
Notifications
You must be signed in to change notification settings - Fork 566
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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); | ||
if (Objects.nonNull(hostHeader)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use brackets for the if statement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. Thanks |
||
return Integer.parseInt(hostHeader); | ||
} | ||
return 0; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Thanks.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
andReturns the port number to which the request was sent
don't mean the same thing. Is ALB considered as a client in this case?There was a problem hiding this comment.
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.