Skip to content

fix: prevent StackOverflowError from deeply nested GeometryCollection in GeoJsonParser#1699

Open
ZeroX-404 wants to merge 4 commits into
googlemaps:mainfrom
ZeroX-404:fix/geojson-stack-overflow
Open

fix: prevent StackOverflowError from deeply nested GeometryCollection in GeoJsonParser#1699
ZeroX-404 wants to merge 4 commits into
googlemaps:mainfrom
ZeroX-404:fix/geojson-stack-overflow

Conversation

@ZeroX-404

Copy link
Copy Markdown

Description:

## Problem
`GeoJsonParser.parseGeometry()` and `createGeometryCollection()` call each 
other recursively with no depth limit. A malicious GeoJSON file containing 
deeply nested `GeometryCollection` objects triggers a `StackOverflowError`, 
crashing any Android app that loads the file via `GeoJsonLayer`.

**Vulnerable call chain:**

parseGeometry()
→ createGeometry()
→ createGeometryCollection()
→ parseGeometry() // unbounded recursion


A PoC GeoJSON file of ~93KB with 2000 levels of nesting is sufficient 
to trigger the crash.

## Fix
- Add `MAX_GEOMETRY_DEPTH = 20` constant
- Add private `parseGeometry(JSONObject, int depth)` overload
- Pass depth counter through `createGeometry()` and `createGeometryCollection()`
- Geometries exceeding the limit are ignored with a `Log.w()` warning

## Testing
Verified that parsing a GeoJSON with 2000 nesting levels no longer 
throws `StackOverflowError` after this fix.

Add MAX_GEOMETRY_DEPTH = 20 limit to parseGeometry() to prevent
StackOverflowError when parsing malicious GeoJSON with deeply nested
GeometryCollection objects.

Geometries exceeding the depth limit are silently ignored and a
warning is logged via Log.w().
@google-cla

google-cla Bot commented Jun 7, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ZeroX-404

Copy link
Copy Markdown
Author

Hi maintainers, could someone please review this fix? I verified that it prevents the StackOverflowError caused by deeply nested GeometryCollections.

@ZeroX-404

Copy link
Copy Markdown
Author

Hi @dkhawk, gentle follow up — could you take a look when you have a moment? Happy to adjust anything. Thanks!

Add three tests to GeoJsonParserTest to verify the fix for
StackOverflowError caused by deeply nested GeometryCollection objects
(introduced in the accompanying fix commit):

- testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow:
  Ensures parsing 2000-level nesting no longer throws StackOverflowError

- testGeometryBeyondMaxDepth_returnsNull:
  Ensures geometry exceeding MAX_GEOMETRY_DEPTH (20) returns null
  instead of crashing

- testShallowNestedGeometryCollection_parsedCorrectly:
  Ensures normal shallow nesting still parses correctly (regression guard)

Relates to: googlemaps#1699
@ZeroX-404

Copy link
Copy Markdown
Author

Hi @dkhawk, I've updated the PR with unit tests covering the depth-limit behavior:

  • testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow — verifies 2000-level nesting no longer crashes
  • testGeometryBeyondMaxDepth_returnsNull — verifies geometry beyond MAX_GEOMETRY_DEPTH (20) returns null
  • testShallowNestedGeometryCollection_parsedCorrectly — regression guard for normal shallow nesting

Happy to adjust anything if needed. Thanks!

@dkhawk

dkhawk commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Hi @dkhawk, I've updated the PR with unit tests covering the depth-limit behavior:

  • testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow — verifies 2000-level nesting no longer crashes
  • testGeometryBeyondMaxDepth_returnsNull — verifies geometry beyond MAX_GEOMETRY_DEPTH (20) returns null
  • testShallowNestedGeometryCollection_parsedCorrectly — regression guard for normal shallow nesting

Happy to adjust anything if needed. Thanks!

Thank you for your contribution! And sorry for the delay in reviewing it as I have been away.

BTW, this version of code is slated to be replaced soon. We have a complete rewrite in https://github.com/googlemaps/android-maps-utils/tree/feat/rewrite-android-maps-utils. We have put out the first RC and expect RC2 soon.

I'll see if the problem also exists in that rewrite.

@ZeroX-404 ZeroX-404 force-pushed the fix/geojson-stack-overflow branch from 8bb2c5b to f37823d Compare June 24, 2026 22:29
@ZeroX-404

Copy link
Copy Markdown
Author

Hi @dkhawk, I've applied your suggested changes — countdown maxDepth logic and updated test assertions. All tests pass locally. Ready for merge whenever you are!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants