Skip to content

security: cap reconstruction memory, verify peer chunks, allowlist item CRS text - #13

Merged
MatMatt merged 1 commit into
masterfrom
security/pass2-reconstruct
Sep 21, 2026
Merged

MatMatt merged 1 commit into
masterfrom
security/pass2-reconstruct

Conversation

@MatMatt

@MatMatt MatMatt commented Sep 21, 2026

Copy link
Copy Markdown
Owner

What this fixes

Cluster 2 of the security review: reconstruction allocation caps (H3), the missing hash check on the peer-chunk download path (M6), and the CRS allowlist that replaces SpatialRef::from_definition (M7). One commit, 57d0d27, on top of master (b0ca845).

H3 - peer properties sized buffers directly

  • A MAX_RECONSTRUCT_BYTES budget is enforced before allocation in the tiled, raw and NDVI paths, with checked_mul/checked_add on u64, so the u32 width * height overflow and the tile-offset underflow are gone. Zero dimensions are rejected before the geotransform fallback, which otherwise divides by zero in floating point. Chunk references are counted across the top level and every nested earthgrid:band_hashes list against one cap, earthgrid:band_names is bounded before it is cloned, and the NDVI path requires the sample count to equal the computed pixel count before the raster buffer exists.
  • Reconstruction runs under spawn_blocking with the store locked per chunk read rather than for the whole job, and a process-wide limit of two keeps several requests from allocating at once.

M6 - one path stored a peer chunk unverified

The download handler's peer prefetch now hashes each chunk and discards a mismatch with a message naming the hash, matching what get_chunk and replication already do.

M7 - item text could choose the CRS

earthgrid:crs reached SpatialRef::from_definition, which accepts a URL or a local path and so bypassed the outbound URL policy. CRS text now passes a lexical allowlist: EPSG:<1-6 digits>, or WKT whose first token is one of six roots, with fragments that can carry an instruction (://, +init, +proj, +nadgrids, /vsi, a backslash, a control character) refused anywhere, and @/| refused only outside quoted text so legitimate WKT names survive. A value that passes the gate but cannot be built is an error naming the value and the GDAL error, instead of an output written silently without a CRS.

Verification

  • cargo build --release clean, zero warnings; cargo test 208 passed, 0 failed, 1 ignored, plus 2 ignored doc-tests (21 new tests across the four correction rounds).
  • Four independent read-only adversarial passes. The first refuted the allocation ordering and the CRS work outright; the second found that the writers swallow CRS failures, that the filter is lexical rather than semantic, and that the NDVI result was held twice; the third verified everything except the @/| rule, which rejected valid WKT and is fixed in the fourth. Residuals are listed below.
  • Live after deploy: /api/health 200 and a real item downloads as a valid GeoTIFF - GET /api/download/sentinel-2-l2a/S2A_34SFH_20260713_1_L2A_B08 returned 228,357,949 bytes of image/tiff; application=geotiff, read back as 10980x10980, 16 bps, deflate.

Known limitations, not addressed here

  • The byte budget bounds the item's own buffers, not total process memory: band arrays stay live while the writer builds conversion buffers, and the encoded output size is unchecked.
  • A raw item larger than the budget is refused even though ingestion has no file-size cap; a streaming download path is the real fix and is a product decision.
  • A chunk that disappears between two reads can fail a raw reconstruction partway or leave a tiled one zero-filled.
  • The CRS filter is a lexical gate, not WKT validation: a WKT-shaped string carrying +proj inside quoted text is refused, and a spelling that evades a literal match is not proven harmless. OSRSetFromUserInput stays indirectly reachable through the openEO warp path, where the CRS is not item text.
  • The admission limit covers reconstruction only, not peer prefetch or concurrent chunk requests.

…S text

H3: peer-supplied earthgrid properties sized buffers directly. Reconstruction
now enforces a byte budget with checked arithmetic before any allocation, caps
the chunk references across the top level and the nested band lists, rejects
zero dimensions before the geotransform fallback, bounds the band-name list,
and limits in-flight reconstructions so several requests cannot allocate at
once. Reconstruction runs under spawn_blocking with the store locked per read
instead of for the whole job.

M6: the download path stored a peer chunk without checking its hash - the one
path that skipped what get_chunk and replication both do. It is now hashed and
a mismatch is discarded and logged.

M7: spatial references were built with SpatialRef::from_definition, so an
item-provided earthgrid:crs could be a URL or a path. CRS text now passes a
lexical allowlist (EPSG:code, or WKT with one of six roots, with fragments
that can carry an instruction refused outside quoted text), and a value that
passes the gate but cannot be built is an error rather than an output silently
written without a CRS.
@MatMatt
MatMatt merged commit 2e8696b into master Sep 21, 2026
3 checks passed
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.

1 participant