Skip to content

Commit a087892

Browse files
author
dmitrii
committed
Reject trailing content after ']' in bracketed-IPv6 parsing
Addresses review feedback: indexOf(']') accepted malformed values such as "[::1]foo" (returning the inner IP) because it ignored anything after the bracket. Require the char after ']' to be end-of-string or ':' (a port), matching the pre-existing strictness ("[ipv6]" or "[ipv6]:port" only).
1 parent d1aaf41 commit a087892

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

agent_api/src/main/java/dev/aikido/agent_api/helpers/net/ProxyForwardedParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ private static String normalizeIp(String ip) {
5959
// Handle first so we never run the (expensive) full isIP on the bracketed form.
6060
if (ip.startsWith("[")) {
6161
int closingBracket = ip.indexOf(']');
62-
if (closingBracket > 0) {
62+
// Only accept "[ipv6]" (bracket at the end) or "[ipv6]:port" (':' right
63+
// after the bracket) — reject any other trailing content, e.g. "[::1]foo".
64+
boolean validSuffix = closingBracket > 0
65+
&& (closingBracket == ip.length() - 1 || ip.startsWith(":", closingBracket + 1));
66+
if (validSuffix) {
6367
String unwrappedIp = ip.substring(1, closingBracket);
6468
if (IPValidator.isIP(unwrappedIp)) {
6569
return unwrappedIp;

agent_api/src/test/java/helpers/ProxyForwardedParserTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,11 @@ void testGetIpFromRequest_BracketedIPv4Resolves() {
236236
String result = getIpFromRequest("[1.2.3.4]", new HashMap<>());
237237
assertEquals("1.2.3.4", result);
238238
}
239+
240+
@Test
241+
void testGetIpFromRequest_BracketedIpWithTrailingGarbageFallsBackToRawIp() {
242+
// "[::1]foo" is malformed (trailing content after ']') and must not resolve.
243+
String result = getIpFromRequest("[::1]foo", new HashMap<>());
244+
assertEquals("[::1]foo", result);
245+
}
239246
}

0 commit comments

Comments
 (0)