Skip to content

Commit e6fb3a5

Browse files
authored
fix index out of bounds error in KV Processor (#22288)
- checks for index-out-of-bounds - added unit tests for failed `field_split` and `value_split` scenarios missed this test in #22272.
1 parent e7444f7 commit e6fb3a5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ public void execute(IngestDocument document) {
9898

9999
String fieldPathPrefix = (targetField == null) ? "" : targetField + ".";
100100
Arrays.stream(oldVal.split(fieldSplit))
101-
.map((f) -> f.split(valueSplit, 2))
101+
.map((f) -> {
102+
String[] kv = f.split(valueSplit, 2);
103+
if (kv.length != 2) {
104+
throw new IllegalArgumentException("field [" + field + "] does not contain value_split [" + valueSplit + "]");
105+
}
106+
return kv;
107+
})
102108
.filter((p) -> includeKeys == null || includeKeys.contains(p[0]))
103109
.forEach((p) -> append(document, fieldPathPrefix + p[0], p[1]));
104110
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,20 @@ public void testNonExistentWithIgnoreMissing() throws Exception {
9393
processor.execute(ingestDocument);
9494
assertIngestDocument(originalIngestDocument, ingestDocument);
9595
}
96+
97+
public void testFailFieldSplitMatch() throws Exception {
98+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
99+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello|second=world|second=universe");
100+
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), fieldName, "&", "=", null, "target", false);
101+
processor.execute(ingestDocument);
102+
assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello|second=world|second=universe"));
103+
assertFalse(ingestDocument.hasField("target.second"));
104+
}
105+
106+
public void testFailValueSplitMatch() throws Exception {
107+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("foo", "bar"));
108+
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), "foo", "&", "=", null, "target", false);
109+
Exception exception = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
110+
assertThat(exception.getMessage(), equalTo("field [foo] does not contain value_split [=]"));
111+
}
96112
}

0 commit comments

Comments
 (0)