Skip to content

Commit acbf17e

Browse files
committed
Fix a bug on handling an invalid array value for point type field (#4900)
With this commit, appropriate exception is thrown when an array of four values are provided for point type field. Signed-off-by: Heemin Kim <heemin@amazon.com> (cherry picked from commit 0f477a2)
1 parent e741161 commit acbf17e

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6969
- Fixing Gradle warnings associated with publishPluginZipPublicationToXxx tasks ([#4696](https://github.com/opensearch-project/OpenSearch/pull/4696))
7070
- Fixed randomly failing test ([4774](https://github.com/opensearch-project/OpenSearch/pull/4774))
7171
- Fix recovery path for searchable snapshots ([4813](https://github.com/opensearch-project/OpenSearch/pull/4813))
72+
- Fix a bug on handling an invalid array value for point type field #4900([#4900](https://github.com/opensearch-project/OpenSearch/pull/4900))
73+
7274
### Security
7375
- CVE-2022-25857 org.yaml:snakeyaml DOS vulnerability ([#4341](https://github.com/opensearch-project/OpenSearch/pull/4341))
7476

server/src/main/java/org/opensearch/index/mapper/AbstractPointGeometryFieldMapper.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ default boolean isNormalizable(double coord) {
245245
* @opensearch.internal
246246
*/
247247
public static class PointParser<P extends ParsedPoint> extends Parser<List<P>> {
248+
private static final int MAX_NUMBER_OF_VALUES_IN_ARRAY_FORMAT = 3;
248249
/**
249250
* Note that this parser is only used for formatting values.
250251
*/
@@ -287,7 +288,7 @@ public List<P> parse(XContentParser parser) throws IOException, ParseException {
287288
if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
288289
parser.nextToken();
289290
if (parser.currentToken() == XContentParser.Token.VALUE_NUMBER) {
290-
XContentBuilder xContentBuilder = reConstructArrayXContent(parser);
291+
XContentBuilder xContentBuilder = reconstructArrayXContent(parser);
291292
try (
292293
XContentParser subParser = createParser(
293294
parser.getXContentRegistry(),
@@ -328,18 +329,20 @@ private XContentParser createParser(
328329
return subParser;
329330
}
330331

331-
private XContentBuilder reConstructArrayXContent(XContentParser parser) throws IOException {
332+
private XContentBuilder reconstructArrayXContent(XContentParser parser) throws IOException {
332333
XContentBuilder builder = XContentFactory.jsonBuilder().startArray();
333-
int count = 0;
334+
int numberOfValuesAdded = 0;
334335
while (parser.currentToken() != XContentParser.Token.END_ARRAY) {
335-
if (++count > 3) {
336-
break;
337-
}
338336
if (parser.currentToken() != XContentParser.Token.VALUE_NUMBER) {
339337
throw new OpenSearchParseException("numeric value expected");
340338
}
341339
builder.value(parser.doubleValue());
342340
parser.nextToken();
341+
342+
// Allows one more value to be added so that the error case can be handled by a parser
343+
if (++numberOfValuesAdded > MAX_NUMBER_OF_VALUES_IN_ARRAY_FORMAT) {
344+
break;
345+
}
343346
}
344347
builder.endArray();
345348
return builder;

server/src/test/java/org/opensearch/index/mapper/GeoPointFieldMapperTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public void testLatLonInOneValueArray() throws Exception {
164164
assertThat(doc.rootDoc().getFields("field"), arrayWithSize(4));
165165
}
166166

167+
public void testLatLonInArrayMoreThanThreeValues() throws Exception {
168+
DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "geo_point").field("ignore_z_value", true)));
169+
Exception e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.array("field", 1.2, 1.3, 1.4, 1.5))));
170+
assertThat(e.getCause().getMessage(), containsString("[geo_point] field type does not accept more than 3 values"));
171+
}
172+
167173
public void testLonLatArray() throws Exception {
168174
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
169175
ParsedDocument doc = mapper.parse(source(b -> b.startArray("field").value(1.3).value(1.2).endArray()));

0 commit comments

Comments
 (0)