Skip to content

Commit

Permalink
Make the Forwarded Parser syntax parsing case-insensitive
Browse files Browse the repository at this point in the history
As described on https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded, the forwarded header syntax parsing should be case-insensitive. It's not a real spec, but Kong is using `Proto` instead of `proto` and `For` instead of `for`.
  • Loading branch information
cescoffier committed Feb 5, 2024
1 parent 7b617ae commit 17fb8e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ public void testHeadersAreUsed() {
.then()
.body(Matchers.equalTo("http|somehost2|backend2:5555|/path|http://somehost2/path"));
}

/**
* 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")
.then()
.body(Matchers.equalTo("http|somehost2|backend2:5555|/path|http://somehost2/path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class ForwardedParser {
private static final AsciiString X_FORWARDED_PORT = AsciiString.cached("X-Forwarded-Port");
private static final AsciiString X_FORWARDED_FOR = AsciiString.cached("X-Forwarded-For");

private static final Pattern FORWARDED_HOST_PATTERN = Pattern.compile("host=\"?([^;,\"]+)\"?");
private static final Pattern FORWARDED_PROTO_PATTERN = Pattern.compile("proto=\"?([^;,\"]+)\"?");
private static final Pattern FORWARDED_FOR_PATTERN = Pattern.compile("for=\"?([^;,\"]+)\"?");
private static final Pattern FORWARDED_HOST_PATTERN = Pattern.compile("host=\"?([^;,\"]+)\"?", Pattern.CASE_INSENSITIVE);
private static final Pattern FORWARDED_PROTO_PATTERN = Pattern.compile("proto=\"?([^;,\"]+)\"?", Pattern.CASE_INSENSITIVE);
private static final Pattern FORWARDED_FOR_PATTERN = Pattern.compile("for=\"?([^;,\"]+)\"?", Pattern.CASE_INSENSITIVE);

private final static int PORT_MIN_VALID_VALUE = 0;
private final static int PORT_MAX_VALID_VALUE = 65535;
Expand Down

0 comments on commit 17fb8e0

Please sign in to comment.