Skip to content

Commit b407a30

Browse files
committed
HostAndPort fails to parse a nip host (#4953)
See #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 <tsegismont@gmail.com>
1 parent a33a8f6 commit b407a30

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/main/java/io/vertx/core/net/impl/HostAndPortImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int parseIPv4Address(String s, int from, int to) {
3030
return -1;
3131
}
3232
}
33-
return from;
33+
return from < to && (from + 1 == s.length() || s.charAt(from + 1) != ':') ? -1 : from;
3434
}
3535

3636
static int parseDecOctet(String s, int from, int to) {

src/test/java/io/vertx/core/net/impl/HostAndPortTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void testParseIPV4Address() {
3434
assertEquals(7, HostAndPortImpl.parseIPv4Address("0.0.0.0", 0, 7));
3535
assertEquals(11, HostAndPortImpl.parseIPv4Address("192.168.0.0", 0, 11));
3636
assertEquals(-1, HostAndPortImpl.parseIPv4Address("011.168.0.0", 0, 11));
37+
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 15));
38+
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 9));
39+
assertEquals(8, HostAndPortImpl.parseIPv4Address("10.0.0.1.nip.io", 0, 8));
40+
assertEquals(-1, HostAndPortImpl.parseIPv4Address("10.0.0.1:", 0, 9));
3741
}
3842

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

4752
@Test
4853
public void testParseHost() {
4954
assertEquals(14, HostAndPortImpl.parseHost("example-fr.com", 0, 14));
5055
assertEquals(5, HostAndPortImpl.parseHost("[0::]", 0, 5));
5156
assertEquals(7, HostAndPortImpl.parseHost("0.0.0.0", 0, 7));
57+
assertEquals(8, HostAndPortImpl.parseHost("10.0.0.1.nip.io", 0, 8));
58+
assertEquals(15, HostAndPortImpl.parseHost("10.0.0.1.nip.io", 0, 15));
5259
}
5360

5461
@Test
5562
public void testParseHostAndPort() {
63+
assertHostAndPort("10.0.0.1.nip.io", -1, "10.0.0.1.nip.io");
64+
assertHostAndPort("10.0.0.1.nip.io", 8443, "10.0.0.1.nip.io:8443");
5665
assertHostAndPort("example.com", 8080, "example.com:8080");
5766
assertHostAndPort("example.com", -1, "example.com");
5867
assertHostAndPort("0.1.2.3", -1, "0.1.2.3");
5968
assertHostAndPort("[0::]", -1, "[0::]");
6069
assertHostAndPort("", -1, "");
6170
assertHostAndPort("", 8080, ":8080");
6271
assertNull(HostAndPortImpl.parseHostAndPort("/", -1));
72+
assertNull(HostAndPortImpl.parseHostAndPort("10.0.0.1:x", -1));
6373
}
6474

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

0 commit comments

Comments
 (0)