Skip to content

Commit

Permalink
HostAndPort fails to parse a nip host (eclipse-vertx#4953)
Browse files Browse the repository at this point in the history
See eclipse-vertx#4947

If parseIPv4Address finds an IP address, and the string still has characters which do not begin with `:`, this is not an IP address.

In other words, 10.0.0.1.nip.io should not be parsed as an IP address.

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont authored Nov 15, 2023
1 parent 3820048 commit f1a280c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/net/impl/HostAndPortImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static int parseIPv4Address(String s, int from, int to) {
return -1;
}
}
return from;
return from < to && (from + 1 == s.length() || s.charAt(from + 1) != ':') ? -1 : from;
}

static int parseDecOctet(String s, int from, int to) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/vertx/core/net/impl/HostAndPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void testParseIPV4Address() {
assertEquals(7, HostAndPortImpl.parseIPv4Address("0.0.0.0", 0, 7));
assertEquals(11, HostAndPortImpl.parseIPv4Address("192.168.0.0", 0, 11));
assertEquals(-1, HostAndPortImpl.parseIPv4Address("011.168.0.0", 0, 11));
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 15));
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 9));
assertEquals(8, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 8));
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1:", 0, 9));
}

@Test
Expand All @@ -42,24 +46,30 @@ public void testParseRegName() {
assertEquals(5, HostAndPortImpl.parseRegName("abcdef:1234", 0, 5));
assertEquals(11, HostAndPortImpl.parseRegName("example.com", 0, 11));
assertEquals(14, HostAndPortImpl.parseRegName("example-fr.com", 0, 14));
assertEquals(15, HostAndPortImpl.parseRegName("10.0.0.1.nip.io", 0, 15));
}

@Test
public void testParseHost() {
assertEquals(14, HostAndPortImpl.parseHost("example-fr.com", 0, 14));
assertEquals(5, HostAndPortImpl.parseHost("[0::]", 0, 5));
assertEquals(7, HostAndPortImpl.parseHost("0.0.0.0", 0, 7));
assertEquals(8, HostAndPortImpl.parseHost("10.0.0.1.nip.io", 0, 8));
assertEquals(15, HostAndPortImpl.parseHost("10.0.0.1.nip.io", 0, 15));
}

@Test
public void testParseHostAndPort() {
assertHostAndPort("10.0.0.1.nip.io", -1, "10.0.0.1.nip.io");
assertHostAndPort("10.0.0.1.nip.io", 8443, "10.0.0.1.nip.io:8443");
assertHostAndPort("example.com", 8080, "example.com:8080");
assertHostAndPort("example.com", -1, "example.com");
assertHostAndPort("0.1.2.3", -1, "0.1.2.3");
assertHostAndPort("[0::]", -1, "[0::]");
assertHostAndPort("", -1, "");
assertHostAndPort("", 8080, ":8080");
assertNull(HostAndPortImpl.parseHostAndPort("/", -1));
assertNull(HostAndPortImpl.parseHostAndPort("10.0.0.1:x", -1));
}

private void assertHostAndPort(String expectedHost, int expectedPort, String actual) {
Expand Down

0 comments on commit f1a280c

Please sign in to comment.