question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GeoJson: Improper Deserialization of Document with a GeoJsonPolygon [DATAMONGO-2664]

See original GitHub issue

Bjorn 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:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
bjornharvoldcommented, Jan 20, 2021

Sure thing @christophstrobl. I’ll create a PR for you guys. I’ve already signed your CLA.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found