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

Handle port and IPv6 in forwarded headers #3651

Merged
merged 3 commits into from
Jul 23, 2021
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 @@ -206,7 +206,7 @@ private String clientIp(CONNECTION connection, REQUEST request) {
// try Forwarded
String forwarded = requestHeader(request, "Forwarded");
if (forwarded != null) {
forwarded = extractForwardedFor(forwarded);
forwarded = extractForwarded(forwarded);
if (forwarded != null) {
return forwarded;
}
Expand All @@ -215,12 +215,8 @@ private String clientIp(CONNECTION connection, REQUEST request) {
// try X-Forwarded-For
forwarded = requestHeader(request, "X-Forwarded-For");
if (forwarded != null) {
// may be split by ,
int endIndex = forwarded.indexOf(',');
if (endIndex > 0) {
forwarded = forwarded.substring(0, endIndex);
}
if (!forwarded.isEmpty()) {
forwarded = extractForwardedFor(forwarded);
if (forwarded != null) {
return forwarded;
}
}
Expand All @@ -230,7 +226,7 @@ private String clientIp(CONNECTION connection, REQUEST request) {
}

// VisibleForTesting
static String extractForwardedFor(String forwarded) {
static String extractForwarded(String forwarded) {
int start = forwarded.toLowerCase().indexOf("for=");
if (start < 0) {
return null;
Expand All @@ -239,9 +235,31 @@ static String extractForwardedFor(String forwarded) {
if (start >= forwarded.length() - 1) { // the value after for= must not be empty
return null;
}
return extractIpAddress(forwarded, start);
}

// VisibleForTesting
static String extractForwardedFor(String forwarded) {
return extractIpAddress(forwarded, 0);
}

private static String extractIpAddress(String forwarded, int start) {
if (forwarded.length() == start) {
return null;
}
if (forwarded.charAt(start) == '[') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded and https://www.rfc-editor.org/rfc/rfc7239 it seems to me that ipv6 addresses need to be quoted, for example for="[2001:db8:cafe::17]". Similarly when ipv4 address includes a port it should be quoted "192.0.2.43:47011"

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch, fixed, and yet more tests added...

// from https://www.rfc-editor.org/rfc/rfc7239
// "note that an IPv6 address is always enclosed in square brackets"
int end = forwarded.indexOf(']', start);
if (end <= start) {
return null;
}
return forwarded.substring(start + 1, end);
}
for (int i = start; i < forwarded.length() - 1; i++) {
char c = forwarded.charAt(i);
if (c == ',' || c == ';') {
// ok to check ':' (for start of port) because ipv6 case is checked above
if (c == ',' || c == ';' || c == ':') {
if (i == start) { // empty string
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,166 @@

public class HttpServerTracerTest {
@Test
public void extractForwardedFor() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("for=1.1.1.1"));
public void extractForwarded() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwarded("for=1.1.1.1"));
}

@Test
public void extractForwardedForCaps() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("For=1.1.1.1"));
public void extractForwardedIpv6() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded("for=[1111:1111:1111:1111:1111:1111:1111:1111]"));
}

@Test
public void extractForwardedForMalformed() {
assertNull(HttpServerTracer.extractForwardedFor("for=;for=1.1.1.1"));
public void extractForwardedWithPort() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwarded("for=1.1.1.1:2222"));
}

@Test
public void extractForwardedForEmpty() {
assertNull(HttpServerTracer.extractForwardedFor(""));
public void extractForwardedIpv6WithPort() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded("for=[1111:1111:1111:1111:1111:1111:1111:1111]:2222"));
}

@Test
public void extractForwardedForEmptyValue() {
assertNull(HttpServerTracer.extractForwardedFor("for="));
public void extractForwardedCaps() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwarded("For=1.1.1.1"));
}

@Test
public void extractForwardedForEmptyValueWithSemicolon() {
assertNull(HttpServerTracer.extractForwardedFor("for=;"));
public void extractForwardedMalformed() {
assertNull(HttpServerTracer.extractForwarded("for=;for=1.1.1.1"));
}

@Test
public void extractForwardedForNoFor() {
assertNull(HttpServerTracer.extractForwardedFor("by=1.1.1.1;test=1.1.1.1"));
public void extractForwardedEmpty() {
assertNull(HttpServerTracer.extractForwarded(""));
}

@Test
public void extractForwardedForMultiple() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("for=1.1.1.1;for=1.2.3.4"));
public void extractForwardedEmptyValue() {
assertNull(HttpServerTracer.extractForwarded("for="));
}

@Test
public void extractForwardedForMixedSplitter() {
public void extractForwardedEmptyValueWithSemicolon() {
assertNull(HttpServerTracer.extractForwarded("for=;"));
}

@Test
public void extractForwardedNoFor() {
assertNull(HttpServerTracer.extractForwarded("by=1.1.1.1;test=1.1.1.1"));
}

@Test
public void extractForwardedMultiple() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwarded("for=1.1.1.1;for=1.2.3.4"));
}

@Test
public void extractForwardedMultipleIpV6() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded(
"for=[1111:1111:1111:1111:1111:1111:1111:1111];for=1.2.3.4"));
}

@Test
public void extractForwardedMultipleWithPort() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwarded("for=1.1.1.1:2222;for=1.2.3.4"));
}

@Test
public void extractForwardedMultipleIpV6WithPort() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded(
"for=[1111:1111:1111:1111:1111:1111:1111:1111]:2222;for=1.2.3.4"));
}

@Test
public void extractForwardedMixedSplitter() {
assertEquals(
"1.1.1.1",
HttpServerTracer.extractForwarded("test=abcd; by=1.2.3.4, for=1.1.1.1;for=1.2.3.4"));
}

@Test
public void extractForwardedMixedSplitterIpv6() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded(
"test=abcd; by=1.2.3.4, for=[1111:1111:1111:1111:1111:1111:1111:1111];for=1.2.3.4"));
}

@Test
public void extractForwardedMixedSplitterWithPort() {
assertEquals(
"1.1.1.1",
HttpServerTracer.extractForwardedFor("test=abcd; by=1.2.3.4, for=1.1.1.1;for=1.2.3.4"));
HttpServerTracer.extractForwarded("test=abcd; by=1.2.3.4, for=1.1.1.1:2222;for=1.2.3.4"));
}

@Test
public void extractForwardedMixedSplitterIpv6WithPort() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwarded(
"test=abcd; by=1.2.3.4, for=[1111:1111:1111:1111:1111:1111:1111:1111]:2222;for=1.2.3.4"));
}

@Test
public void extractForwardedFor() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("1.1.1.1"));
}

@Test
public void extractForwardedForIpv6() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwardedFor("[1111:1111:1111:1111:1111:1111:1111:1111]]"));
}

@Test
public void extractForwardedForWithPort() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("1.1.1.1:2222"));
}

@Test
public void extractForwardedForIpv6WithPort() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwardedFor("[1111:1111:1111:1111:1111:1111:1111:1111]:2222"));
}

@Test
public void extractForwardedForEmpty() {
assertNull(HttpServerTracer.extractForwardedFor(""));
}

@Test
public void extractForwardedForMultiple() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("1.1.1.1,1.2.3.4"));
}

@Test
public void extractForwardedForMultipleIpv6() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwardedFor("[1111:1111:1111:1111:1111:1111:1111:1111],1.2.3.4"));
}

@Test
public void extractForwardedForMultipleWithPort() {
assertEquals("1.1.1.1", HttpServerTracer.extractForwardedFor("1.1.1.1:2222,1.2.3.4"));
}

@Test
public void extractForwardedForMultipleIpv6WithPort() {
assertEquals(
"1111:1111:1111:1111:1111:1111:1111:1111",
HttpServerTracer.extractForwardedFor(
"[1111:1111:1111:1111:1111:1111:1111:1111]:2222,1.2.3.4"));
}
}