Skip to content
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

Geo: better handling of malformed geo_points #35554

Merged
merged 3 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions server/src/main/java/org/elasticsearch/common/geo/GeoPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,18 @@ public GeoPoint resetFromCoordinates(String value, final boolean ignoreZValue) {
throw new ElasticsearchParseException("failed to parse [{}], expected 2 or 3 coordinates "
+ "but found: [{}]", vals.length);
}
double lat = Double.parseDouble(vals[0].trim());
double lon = Double.parseDouble(vals[1].trim());
final double lat;
final double lon;
try {
lat = Double.parseDouble(vals[0].trim());
} catch (NumberFormatException ex) {
throw new ElasticsearchParseException("latitude must be a number");
}
try {
lon = Double.parseDouble(vals[1].trim());
} catch (NumberFormatException ex) {
throw new ElasticsearchParseException("longitude must be a number");
}
if (vals.length > 2) {
GeoPoint.assertZValue(ignoreZValue, Double.parseDouble(vals[2].trim()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ protected void parse(ParseContext context, GeoPoint point) throws IOException {
throw new IllegalArgumentException("illegal longitude value [" + point.lon() + "] for " + name());
}
} else {
GeoUtils.normalizePoint(point);
if (isNormalizable(point.lat()) && isNormalizable(point.lat())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should one of these be point.lon()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, there should probably also be a test added that would have caught this.

GeoUtils.normalizePoint(point);
} else {
throw new ElasticsearchParseException("cannot normalize the point - not a number");
}
}
if (fieldType().indexOptions() != IndexOptions.NONE) {
context.doc().add(new LatLonPoint(fieldType().name(), point.lat(), point.lon()));
Expand Down Expand Up @@ -386,4 +390,8 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
public Explicit<Boolean> ignoreZValue() {
return ignoreZValue;
}

private boolean isNormalizable(double coord) {
return Double.isNaN(coord) == false && Double.isInfinite(coord) == false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,44 @@ public void testInvalidGeohashNotIgnored() throws Exception {
assertThat(ex.getRootCause().getMessage(), equalTo("unsupported symbol [.] in geohash [1234.333]"));
}


public void testInvalidGeopointValuesIgnored() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("location")
.field("type", "geo_point")
.field("ignore_malformed", "true")
.endObject()
.endObject().endObject().endObject());

DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser()
.parse("type", new CompressedXContent(mapping));

assertThat(defaultMapper.parse(SourceToParse.source("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("location", "1234.333").endObject()
), XContentType.JSON)).rootDoc().getField("location"), nullValue());

assertThat(defaultMapper.parse(SourceToParse.source("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("lat", "-").field("lon", 1.3).endObject()
), XContentType.JSON)).rootDoc().getField("location"), nullValue());


assertThat(defaultMapper.parse(SourceToParse.source("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("location", "-,1.3").endObject()
), XContentType.JSON)).rootDoc().getField("location"), nullValue());

assertThat(defaultMapper.parse(SourceToParse.source("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("lat", "NaN").field("lon", "NaN").endObject()
), XContentType.JSON)).rootDoc().getField("location"), nullValue());

assertThat(defaultMapper.parse(SourceToParse.source("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("location", "NaN,NaN").endObject()
), XContentType.JSON)).rootDoc().getField("location"), nullValue());
}

}