Skip to content

Commit e93c0d2

Browse files
authored
Fix error using _terms_enum on ip field (#112872)
When using an ipv6 prefix with shortened "0" entries like "23ec::", we erroneously also matched any IP4 address. This change fixes that problem. Closes #94378
1 parent efdd0c3 commit e93c0d2

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

docs/changelog/112872.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112872
2+
summary: Fix parsing error in `_terms_enum` API
3+
area: Search
4+
type: bug
5+
issues:
6+
- 94378

server/src/main/java/org/elasticsearch/index/mapper/IpPrefixAutomatonUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static Automaton getIpv6Automaton(String ipPrefix) {
108108
} else {
109109
// potentially partial block
110110
if (groupsAdded == 0 && ONLY_ZEROS.matcher(group).matches()) {
111-
// here we have a leading group with only "0" characters. If we would allow this to match
111+
// here we have a leading group with only "0" characters. If we allowed this to match
112112
// ipv6 addresses, this would include things like 0000::127.0.0.1 (and all other ipv4 addresses).
113113
// Allowing this would be counterintuitive, so "0*" prefixes should only expand
114114
// to ipv4 addresses like "0.1.2.3" and we return with an automaton not matching anything here
@@ -129,7 +129,7 @@ private static Automaton getIpv6Automaton(String ipPrefix) {
129129

130130
static Automaton automatonFromIPv6Group(String ipv6Group) {
131131
assert ipv6Group.length() > 0 && ipv6Group.length() <= 4 : "expected a full ipv6 group or prefix";
132-
Automaton result = Automata.makeString("");
132+
Automaton result = Automata.makeEmpty();
133133
for (int leadingZeros = 0; leadingZeros <= 4 - ipv6Group.length(); leadingZeros++) {
134134
int bytesAdded = 0;
135135
String padded = padWithZeros(ipv6Group, leadingZeros);

server/src/test/java/org/elasticsearch/index/mapper/IpPrefixAutomatonUtilTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ public void testBuildPrefixAutomaton() throws UnknownHostException {
174174
assertTrue(accepts(a, "255.27.240.24"));
175175
assertTrue(accepts(a, "255:a360::25bb:828f:ffff:ffff"));
176176
}
177+
{
178+
CompiledAutomaton a = buildIpPrefixAutomaton("23c9::");
179+
assertTrue(accepts(a, "23c9::6063:7ac9:ffff:ffff"));
180+
assertFalse(accepts(a, "0.0.0.0"));
181+
assertFalse(accepts(a, "249.43.32.175"));
182+
}
177183
}
178184

179185
private static boolean accepts(CompiledAutomaton compiledAutomaton, String address) throws UnknownHostException {

0 commit comments

Comments
 (0)