Skip to content

Further improve robustness of geo shape parser for malformed shapes #34498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,22 @@ private static CoordinateNode parseCoordinates(XContentParser parser, boolean ig
}

List<CoordinateNode> nodes = new ArrayList<>();
while (token != XContentParser.Token.END_ARRAY) {
CoordinateNode node = parseCoordinates(parser, ignoreZValue);
if (nodes.isEmpty() == false && nodes.get(0).numDimensions() != node.numDimensions()) {
throw new ElasticsearchParseException("Exception parsing coordinates: number of dimensions do not match");
try {
while (token != XContentParser.Token.END_ARRAY) {
CoordinateNode node = parseCoordinates(parser, ignoreZValue);
if (nodes.isEmpty() == false && nodes.get(0).numDimensions() != node.numDimensions()) {
throw new ElasticsearchParseException("Exception parsing coordinates: number of dimensions do not match");
}
nodes.add(node);
token = parser.nextToken();
}
} catch (Exception ex) {
// Skip all other fields until the end of the array
while (parser.currentToken() != XContentParser.Token.END_ARRAY && parser.currentToken() != null) {
parser.nextToken();
parser.skipChildren();
}
nodes.add(node);
token = parser.nextToken();
throw ex;
}

return new CoordinateNode(nodes);
Expand Down Expand Up @@ -216,10 +225,20 @@ static GeometryCollectionBuilder parseGeometries(XContentParser parser, GeoShape

XContentParser.Token token = parser.nextToken();
GeometryCollectionBuilder geometryCollection = new GeometryCollectionBuilder();
while (token != XContentParser.Token.END_ARRAY) {
ShapeBuilder shapeBuilder = ShapeParser.parse(parser);
geometryCollection.shape(shapeBuilder);
token = parser.nextToken();
try {

while (token != XContentParser.Token.END_ARRAY) {
ShapeBuilder shapeBuilder = ShapeParser.parse(parser);
geometryCollection.shape(shapeBuilder);
token = parser.nextToken();
}
} catch (Exception ex) {
// Skip all other fields until the end of the array
while (parser.currentToken() != XContentParser.Token.END_ARRAY && parser.currentToken() != null) {
parser.nextToken();
parser.skipChildren();
}
throw ex;
}

return geometryCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1213,4 +1213,33 @@ public void testParseInvalidShapes() throws IOException {
assertNull(parser.nextToken());
}
}

public void testParseInvalidGeometryCollectionShapes() throws IOException {
// single dimensions point
XContentBuilder invalidPoints = XContentFactory.jsonBuilder()
.startObject()
.startObject("foo")
.field("type", "geometrycollection")
.startArray("geometries")
.startObject()
.field("type", "polygon")
.startArray("coordinates")
.startArray().value("46.6022226498514").value("24.7237442867977").endArray()
.startArray().value("46.6031857243798").value("24.722968774929").endArray()
.endArray() // coordinates
.endObject()
.endArray() // geometries
.endObject()
.endObject();


try (XContentParser parser = createParser(invalidPoints)) {
parser.nextToken(); // foo
parser.nextToken(); // start object
parser.nextToken(); // start object
ElasticsearchGeoAssertions.assertValidException(parser, ElasticsearchParseException.class);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); // end of the document
assertNull(parser.nextToken()); // no more elements afterwards
}
}
}