Skip to content

Commit 1345cc4

Browse files
committed
add some validations to the zoneId delimiter parsing
1 parent b6ae23c commit 1345cc4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

server/src/main/java/org/elasticsearch/common/network/InetAddresses.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,29 @@ private static byte[] ipStringToBytes(String ipString) {
4343
for (int i = 0; i < ipString.length(); i++) {
4444
char c = ipString.charAt(i);
4545
if (c == '.') {
46+
if (hasPercent){
47+
return null; // Dots must not appear after percents.
48+
}
49+
4650
hasDot = true;
4751
} else if (c == ':') {
52+
if (hasPercent){
53+
return null; // Colons must not appear after percents.
54+
}
55+
4856
if (hasDot) {
4957
return null; // Colons must not appear after dots.
5058
}
5159
hasColon = true;
5260
} else if (c == '%'){
61+
if (hasPercent){
62+
return null; // There can only be one percent.
63+
}
64+
65+
if (!hasColon){
66+
return null; // Percents can only appear if there are colons.
67+
}
68+
5369
hasPercent = true;
5470
percentIndex = i;
5571
} else if (Character.digit(c, 16) == -1) {
@@ -60,7 +76,7 @@ private static byte[] ipStringToBytes(String ipString) {
6076
// strip zoneId from the address
6177
if (hasPercent){
6278
String ipStringWithoutZoneId = ipString.substring(0, percentIndex);
63-
return ipStringToBytes(ipStringWithoutZoneId, hasColon, hasDot);
79+
return ipStringToBytes(ipStringWithoutZoneId);
6480
}
6581

6682
return ipStringToBytes(ipString, hasColon, hasDot);

server/src/test/java/org/elasticsearch/common/network/InetAddressesTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,42 @@ public void testToAddrStringIPv6WithZoneId(){
218218
InetAddresses.forString("::1.2.3.4%0")));
219219
}
220220

221+
public void testToAddrStringIPv6WithInvalidZoneId(){
222+
IllegalArgumentException e = null;
223+
224+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("::1%fred"));
225+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
226+
227+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("::1%%"));
228+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
229+
230+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("%::1"));
231+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
232+
233+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("0::0:0:0:0:0:0%:1"));
234+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
235+
236+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("::1%1.2.3.4"));
237+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
238+
}
239+
240+
public void testToAddrStringZoneIdDelimiterCannotAppearRightAfterOtherDelimiters(){
241+
IllegalArgumentException e = null;
242+
243+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("::1:%0"));
244+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
245+
246+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("::1:1.2.3.%0"));
247+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
248+
}
249+
250+
public void testToAddrStringIPv4DoesNotAllowZoneId(){
251+
IllegalArgumentException e = null;
252+
253+
e = expectThrows(IllegalArgumentException.class, () -> InetAddresses.forString("1.2.3.4%0"));
254+
assertThat(e.getMessage(), Matchers.containsString("is not an IP string literal"));
255+
}
256+
221257
public void testToUriStringIPv4() {
222258
String ipStr = "1.2.3.4";
223259
InetAddress ip = InetAddresses.forString(ipStr);

0 commit comments

Comments
 (0)