-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for Trusted Proxy Detection on Forwarded Requests
This commit introduces a way to determine if a request behind a proxy has been forwarded by a trusted proxy. It implements a custom header (`X-Forwarded-Trusted-Proxy`) that allows request processing to verify the presence of this header, indicating the request originated from a trusted source. To prevent forgery, any incoming request containing this custom header has it removed before further processing.
- Loading branch information
1 parent
0e4dc19
commit 5069761
Showing
7 changed files
with
230 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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
95 changes: 95 additions & 0 deletions
95
...rtx-http/deployment/src/test/java/io/quarkus/vertx/http/proxy/TrustedProxyHeaderTest.java
This file contains 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,95 @@ | ||
package io.quarkus.vertx.http.proxy; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.ForwardedHandlerInitializer; | ||
import io.restassured.RestAssured; | ||
|
||
/** | ||
* Test the trusted-proxy header | ||
*/ | ||
public class TrustedProxyHeaderTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ForwardedHandlerInitializer.class) | ||
.addAsResource(new StringAsset(""" | ||
quarkus.http.proxy.proxy-address-forwarding=true | ||
quarkus.http.proxy.allow-forwarded=true | ||
quarkus.http.proxy.enable-forwarded-host=true | ||
quarkus.http.proxy.enable-forwarded-prefix=true | ||
quarkus.http.proxy.allow-forwarded=true | ||
quarkus.http.proxy.enable-trusted-proxy-header=true | ||
quarkus.http.proxy.trusted-proxies=localhost | ||
"""), | ||
"application.properties")); | ||
|
||
@Test | ||
public void testHeadersAreUsed() { | ||
RestAssured.given() | ||
.header("Forwarded", "proto=http;for=backend2:5555;host=somehost2") | ||
.get("/path-trusted-proxy") | ||
.then() | ||
.body(Matchers | ||
.equalTo("http|somehost2|backend2:5555|/path-trusted-proxy|http://somehost2/path-trusted-proxy|true")); | ||
} | ||
|
||
@Test | ||
public void testTrustedProxyHeader() { | ||
assertThat(RestAssured.get("/forward").asString()).startsWith("http|"); | ||
RestAssured.given() | ||
.header("Forwarded", "by=proxy;for=backend:4444;host=somehost;proto=https") | ||
.get("/trusted-proxy") | ||
.then() | ||
.body(Matchers.equalTo("https|somehost|backend:4444|true")); | ||
} | ||
|
||
@Test | ||
public void testThatTrustedProxyHeaderCannotBeForged() { | ||
assertThat(RestAssured.get("/forward").asString()).startsWith("http|"); | ||
RestAssured.given() | ||
.header("Forwarded", "by=proxy;for=backend:4444;host=somehost;proto=https") | ||
.header("X-Forwarded-Trusted-Proxy", "true") | ||
.get("/trusted-proxy") | ||
.then() | ||
.body(Matchers.equalTo("https|somehost|backend:4444|true")); | ||
|
||
RestAssured.given() | ||
.header("Forwarded", "by=proxy;for=backend:4444;host=somehost;proto=https") | ||
.header("X-Forwarded-Trusted-Proxy", "hello") | ||
.get("/trusted-proxy") | ||
.then() | ||
.body(Matchers.equalTo("https|somehost|backend:4444|true")); | ||
|
||
RestAssured.given() | ||
.header("Forwarded", "by=proxy;for=backend:4444;host=somehost;proto=https") | ||
.header("X-Forwarded-Trusted-Proxy", "false") | ||
.get("/trusted-proxy") | ||
.then() | ||
.body(Matchers.equalTo("https|somehost|backend:4444|true")); | ||
} | ||
|
||
/** | ||
* As described on <a href= | ||
* "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded">https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded</a>, | ||
* the syntax should be case-insensitive. | ||
* <p> | ||
* Kong, for example, uses `Proto` instead of `proto` and `For` instead of `for`. | ||
*/ | ||
@Test | ||
public void testHeadersAreUsedWhenUsingCasedCharacters() { | ||
RestAssured.given() | ||
.header("Forwarded", "Proto=http;For=backend2:5555;Host=somehost2") | ||
.get("/path-trusted-proxy") | ||
.then() | ||
.body(Matchers | ||
.equalTo("http|somehost2|backend2:5555|/path-trusted-proxy|http://somehost2/path-trusted-proxy|true")); | ||
} | ||
} |
This file contains 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 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 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