Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -55,8 +55,12 @@ private static String normalizeIp(String ip) {

ip = ip.trim();

if (IPValidator.isIP(ip)) {
return ip;
// Some proxies pass along port numbers with IPv4 addresses: ip:port
Comment thread
Mishenevd marked this conversation as resolved.
if (ip.contains(":")) {
String[] ipParts = ip.split(":");
if (ipParts.length == 2 && IPValidator.isIP(ipParts[0], "4")) {
return ipParts[0];
}
}

if (ip.startsWith("[") && ip.endsWith("]")) {
Expand All @@ -76,12 +80,8 @@ private static String normalizeIp(String ip) {
}
}

// Some proxies pass along port numbers with IP addresses :
if (ip.contains(":")) {
String[] ipParts = ip.split(":");
if (ipParts.length == 2 && IPValidator.isIP(ipParts[0], "4")) {
return ipParts[0];
}
if (IPValidator.isIP(ip)) {
return ip;
}

return null;
Expand Down
26 changes: 26 additions & 0 deletions agent_api/src/test/java/helpers/ProxyForwardedParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,30 @@ void testGetIpFromRequest_RawIpWithEmptyStringFallsBackToRawIp() {
String result = getIpFromRequest("", new HashMap<>());
assertEquals("", result);
}

@Test
void testGetIpFromRequest_IPv4WithMultiplePortColonsFallsBackToRawIp() {
headers.put("X-Forwarded-For", List.of("1.2.3.4:80:90"));
String result = getIpFromRequest("10.0.0.1", headers);
assertEquals("10.0.0.1", result);
}

@Test
void testGetIpFromRequest_IPv4PortResolvesWithoutTouchingLaterEntries() {
headers.put("X-Forwarded-For", List.of("203.0.113.7:54321, 8.8.8.8"));
String result = getIpFromRequest("10.0.0.1", headers);
assertEquals("203.0.113.7", result);
}

@Test
void testGetIpFromRequest_BracketedIPv4Resolves() {
String result = getIpFromRequest("[1.2.3.4]", new HashMap<>());
assertEquals("1.2.3.4", result);
}

@Test
void testGetIpFromRequest_BracketedIpWithTrailingGarbageFallsBackToRawIp() {
String result = getIpFromRequest("[::1]foo", new HashMap<>());
assertEquals("[::1]foo", result);
}
}
Loading