GeoJson: Improper Deserialization of Document with a GeoJsonPolygon [DATAMONGO-2664]
See original GitHub issueBjorn Harvold opened DATAMONGO-2664 and commented
I’m trying to attach screenshots but getting “Try again. Invalid token”. Will try to attach after I create the ticket.
The existing test that accompanies Spring Data MongoDb is in the class called GeoJsonModuleUnitTests:
@Test // DATAMONGO-1181 public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException, JsonMappingException, IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(new GeoJsonPolygon( Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)))); }
And that passes just fine.
However, change those points to GeoJsonPoint objects and it fails:
@Test
public void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";
List<Point> points = Arrays.asList(new GeoJsonPoint(100, 0), new GeoJsonPoint(101, 0), new GeoJsonPoint(101, 1), new GeoJsonPoint(100, 1), new GeoJsonPoint(100, 0));
GeoJsonPolygon polygon = new GeoJsonPolygon(points);
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(polygon);
assertThat(mapper.writeValueAsString(polygon)).isEqualTo(json);
}
And that is exactly what happens when I query MongoDb (simple findById()) for a document with Spring Data MongoDb. When I look in the object graph, I see GeoJsonPoint objects as coordinates in my Polygon.
When the Jackson ObjectMapper serializes that data, it looks like this:
"polygon" : {
"points" : [ {
"x" : 105.44848312743373,
"y" : 13.049959719225617,
"type" : "Point",
"coordinates" : [ 105.44848312743373, 13.049959719225617 ]
}, {
"x" : 105.11889328368373,
"y" : 11.891452140925736,
"type" : "Point",
"coordinates" : [ 105.11889328368373, 11.891452140925736 ]
}, {
"x" : 105.69018234618373,
"y" : 11.50415959858532,
"type" : "Point",
"coordinates" : [ 105.69018234618373, 11.50415959858532 ]
}, {
"x" : 105.88793625243373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 105.88793625243373, 11.998936428709188 ]
}, {
"x" : 106.81078781493373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 106.81078781493373, 11.998936428709188 ]
}, {
"x" : 107.29418625243373,
"y" : 12.600046596262514,
"type" : "Point",
"coordinates" : [ 107.29418625243373, 12.600046596262514 ]
}, {
"x" : 107.22826828368373,
"y" : 13.584502655549848,
"type" : "Point",
"coordinates" : [ 107.22826828368373, 13.584502655549848 ]
}, {
"x" : 106.48119797118373,
"y" : 12.964324199217307,
"type" : "Point",
"coordinates" : [ 106.48119797118373, 12.964324199217307 ]
}, {
"x" : 106.41528000243373,
"y" : 14.543620849640376,
"type" : "Point",
"coordinates" : [ 106.41528000243373, 14.543620849640376 ]
} ],
"coordinates" : [ {
"type" : "LineString",
"coordinates" : [ {
"x" : 105.44848312743373,
"y" : 13.049959719225617,
"type" : "Point",
"coordinates" : [ 105.44848312743373, 13.049959719225617 ]
}, {
"x" : 105.11889328368373,
"y" : 11.891452140925736,
"type" : "Point",
"coordinates" : [ 105.11889328368373, 11.891452140925736 ]
}, {
"x" : 105.69018234618373,
"y" : 11.50415959858532,
"type" : "Point",
"coordinates" : [ 105.69018234618373, 11.50415959858532 ]
}, {
"x" : 105.88793625243373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 105.88793625243373, 11.998936428709188 ]
}, {
"x" : 106.81078781493373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 106.81078781493373, 11.998936428709188 ]
}, {
"x" : 107.29418625243373,
"y" : 12.600046596262514,
"type" : "Point",
"coordinates" : [ 107.29418625243373, 12.600046596262514 ]
}, {
"x" : 107.22826828368373,
"y" : 13.584502655549848,
"type" : "Point",
"coordinates" : [ 107.22826828368373, 13.584502655549848 ]
}, {
"x" : 106.48119797118373,
"y" : 12.964324199217307,
"type" : "Point",
"coordinates" : [ 106.48119797118373, 12.964324199217307 ]
}, {
"x" : 106.41528000243373,
"y" : 14.543620849640376,
"type" : "Point",
"coordinates" : [ 106.41528000243373, 14.543620849640376 ]
} ]
} ],
"type" : "Polygon"
}
Please advise,
Bjorn
Affects: 3.1.1 (2020.0.1)
Attachments:
- GeoJsonPolygon_Document.png (343.80 kB)
- GeoJsonPolygon_Object.png (189.50 kB)
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Deserialization of untrusted data - OWASP Foundation
An attempt to serialize and then deserialize a class containing transient fields will result in NULLs where the non-transient data should be. This...
Read more >Changelog - Spring
#3517 - GeoJson: Improper Deserialization of Document with a GeoJsonPolygon [DATAMONGO-2664]. * #3474 - Search by alike() criteria is broken when type alias ......
Read more >Insecure deserialization | Web Security Academy - PortSwigger
In this section, we'll cover what insecure deserialization is and describe how it can potentially expose websites to high-severity attacks.
Read more >CWE-502: Deserialization of Untrusted Data (4.9) - MITRE
In this example derived from [REF-467], the code receives and parses data, and afterwards tries to authenticate a user based on validating a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sure thing @christophstrobl. I’ll create a PR for you guys. I’ve already signed your CLA.
Hi @jwill490
Have a look at the unit tests here: https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersUnitTests.java