-
-
Notifications
You must be signed in to change notification settings - Fork 26
Honor proxy headers for Swagger server origin #66
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 all commits
Commits
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
74 changes: 74 additions & 0 deletions
74
...er/src/main/java/uk/co/compendiumdev/thingifier/adapter/httpserver/HttpRequestOrigin.java
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package uk.co.compendiumdev.thingifier.adapter.httpserver; | ||
|
|
||
| final class HttpRequestOrigin { | ||
|
|
||
| private HttpRequestOrigin() {} | ||
|
|
||
| static String from(final HttpServerRequest request) { | ||
| return "%s://%s".formatted(schemeFrom(request), hostFrom(request)); | ||
| } | ||
|
|
||
| private static String schemeFrom(final HttpServerRequest request) { | ||
| final String forwardedProto = forwardedHeaderValue(request.header("Forwarded"), "proto"); | ||
| if (hasText(forwardedProto)) { | ||
| return forwardedProto; | ||
| } | ||
|
|
||
| final String proxyProto = firstHeaderValue(request.header("X-Forwarded-Proto")); | ||
| if (hasText(proxyProto)) { | ||
| return proxyProto; | ||
| } | ||
|
|
||
| return request.scheme(); | ||
| } | ||
|
|
||
| private static String hostFrom(final HttpServerRequest request) { | ||
| final String forwardedHost = forwardedHeaderValue(request.header("Forwarded"), "host"); | ||
| if (hasText(forwardedHost)) { | ||
| return forwardedHost; | ||
| } | ||
|
|
||
| final String proxyHost = firstHeaderValue(request.header("X-Forwarded-Host")); | ||
| if (hasText(proxyHost)) { | ||
| return proxyHost; | ||
| } | ||
|
|
||
| return request.host(); | ||
| } | ||
|
|
||
| private static String forwardedHeaderValue(final String header, final String key) { | ||
| final String firstValue = firstHeaderValue(header); | ||
| if (!hasText(firstValue)) { | ||
| return ""; | ||
| } | ||
|
|
||
| final String prefix = "%s=".formatted(key); | ||
| final String[] parts = firstValue.split(";"); | ||
| for (final String part : parts) { | ||
| final String trimmed = part.trim(); | ||
| if (trimmed.toLowerCase().startsWith(prefix)) { | ||
| return unquote(trimmed.substring(prefix.length()).trim()); | ||
| } | ||
| } | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| private static String firstHeaderValue(final String header) { | ||
| if (!hasText(header)) { | ||
| return ""; | ||
| } | ||
| return header.split(",", 2)[0].trim(); | ||
| } | ||
|
|
||
| private static String unquote(final String value) { | ||
| if (value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) { | ||
| return value.substring(1, value.length() - 1); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| private static boolean hasText(final String value) { | ||
| return value != null && !value.trim().isEmpty(); | ||
| } | ||
| } | ||
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
87 changes: 87 additions & 0 deletions
87
...rc/test/java/uk/co/compendiumdev/thingifier/adapter/httpserver/HttpRequestOriginTest.java
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package uk.co.compendiumdev.thingifier.adapter.httpserver; | ||
|
|
||
| import java.lang.reflect.Proxy; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class HttpRequestOriginTest { | ||
|
|
||
| @Test | ||
| void usesDirectRequestOriginWhenProxyHeadersAreAbsent() { | ||
| final HttpServerRequest request = request("http", "localhost:4567"); | ||
|
|
||
| Assertions.assertEquals("http://localhost:4567", HttpRequestOrigin.from(request)); | ||
| } | ||
|
|
||
| @Test | ||
| void usesForwardedProtoAndHostWhenPresent() { | ||
| final HttpServerRequest request = | ||
| request( | ||
| "http", | ||
| "internal:4567", | ||
| "X-Forwarded-Proto", | ||
| "https", | ||
| "X-Forwarded-Host", | ||
| "apichallenges.eviltester.com"); | ||
|
|
||
| Assertions.assertEquals( | ||
| "https://apichallenges.eviltester.com", HttpRequestOrigin.from(request)); | ||
| } | ||
|
|
||
| @Test | ||
| void usesStandardForwardedHeaderWhenPresent() { | ||
| final HttpServerRequest request = | ||
| request( | ||
| "http", | ||
| "internal:4567", | ||
| "Forwarded", | ||
| "for=192.0.2.60;proto=https;host=\"apichallenges.eviltester.com\""); | ||
|
|
||
| Assertions.assertEquals( | ||
| "https://apichallenges.eviltester.com", HttpRequestOrigin.from(request)); | ||
| } | ||
|
|
||
| private HttpServerRequest request(final String scheme, final String host) { | ||
| return request(scheme, host, "", ""); | ||
| } | ||
|
|
||
| private HttpServerRequest request( | ||
| final String scheme, | ||
| final String host, | ||
| final String firstHeaderName, | ||
| final String firstHeaderValue) { | ||
| return request(scheme, host, firstHeaderName, firstHeaderValue, "", ""); | ||
| } | ||
|
|
||
| private HttpServerRequest request( | ||
| final String scheme, | ||
| final String host, | ||
| final String firstHeaderName, | ||
| final String firstHeaderValue, | ||
| final String secondHeaderName, | ||
| final String secondHeaderValue) { | ||
| return (HttpServerRequest) | ||
| Proxy.newProxyInstance( | ||
| getClass().getClassLoader(), | ||
| new Class<?>[] {HttpServerRequest.class}, | ||
| (proxy, method, args) -> { | ||
| if ("scheme".equals(method.getName())) { | ||
| return scheme; | ||
| } | ||
| if ("host".equals(method.getName())) { | ||
| return host; | ||
| } | ||
| if ("header".equals(method.getName())) { | ||
| final String requestedHeader = (String) args[0]; | ||
| if (requestedHeader.equals(firstHeaderName)) { | ||
| return firstHeaderValue; | ||
| } | ||
| if (requestedHeader.equals(secondHeaderName)) { | ||
| return secondHeaderValue; | ||
| } | ||
| return null; | ||
| } | ||
| throw new UnsupportedOperationException(method.getName()); | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
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.
When a reverse proxy exposes the service on a non-default port and sends it separately as
X-Forwarded-Port(for example,X-Forwarded-Proto: https,X-Forwarded-Host: api.example, andX-Forwarded-Port: 8443), this buildshttps://api.exampleinstead ofhttps://api.example:8443. The generated OpenAPI server then directs Swagger UI requests to port 443 rather than the externally reachable service port.Useful? React with 👍 / 👎.