Skip to content

security: validate peer-supplied catalog items and bound replication chunk downloads - #11

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

MatMatt merged 1 commit into
masterfrom
security/pass2-ingest-gate

Conversation

@MatMatt

@MatMatt MatMatt commented Sep 21, 2026

Copy link
Copy Markdown
Owner

What this fixes

Cluster 1 of the security review: the remote-item validation gate (H2) and the bounded chunk download (H4). One commit, b8105bc, on top of master (b0ca845).

H2 - peer-supplied items were stored almost unchecked

  • validate_remote_item runs before anything is stored: the item is filed under the collection we asked the peer for whatever its own collection field says, the id must match ^[A-Za-z0-9._:-]{1,128}$, each earthgrid:* tile property must be an unsigned integer within sanity bounds, and the chunk list is capped. Duplicate hashes, partial tile geometry and a hash count above the tile grid are deliberately accepted, because local ingestion produces all three (identical tiles share a hash; band-level items list every band; ingest_file writes no geometry).
  • items gained an origin column, appended last with a PRAGMA table_info guarded migration so no positional reader shifts. add_remote_item inserts remote rows and only updates an existing row that is itself remote, so a peer can never overwrite a locally ingested item; local add_item still writes origin = 'local' even when it replaces a remote row.
  • An id already held as a non-remote row is not written by replication at all. Its missing chunks are still repaired, and now from the local row's own hash list rather than the peer's, with invalid local hashes rejected before they can reach a request URL and a recorded error when the local list is unusable. The write-time guard stays authoritative.

H4 - unbounded chunk download

  • A 32 MiB transport cap, enforced by declared Content-Length and by a bounded read, so a lying or absent header cannot stream unbounded.
  • Each body is hash-verified and stored as soon as it arrives and the buffer is dropped, instead of collecting every body first; the hash list is deduplicated and spawned in batches of ten times the concurrency, so live tasks and retained clones stay bounded whatever the list length. A storage-limit result stops the run.

Verification

  • cargo build --release clean, zero warnings; cargo test 197 passed, 0 failed, 1 ignored, plus 2 ignored doc-tests (10 new tests cover the gate: collection filed as requested, duplicate hashes accepted, out-of-range and non-integer properties rejected, bad ids rejected, hash list capped but not tied to the tile grid).
  • Five independent read-only adversarial passes. Approach and rationale: [the round-1 diff was PARTIAL on three of four items], round 2 removed checks that would have refused locally producible items, rounds 3-5 corrected the transport cap, the skip logging, the repair source, task batching, a tile_rows lower bound, invalid local hashes reaching a request URL and a lossy catalog decode reading as a complete item. The findings that remain open are listed below.
  • Live on the running node after deploy: /api/health 200, anonymous GET /api/fetch/queue 401, keyed GET 200 with 100 jobs, /dashboard 200, node still registered, and a queued fetch completed 1/1 with no error.

Known limitations, not addressed here

  • No pre-fetch storage-limit check: the limit is only seen when store.put refuses, so up to ten capped bodies can be fetched and discarded first. The limit source is not reachable from the replicator today.
  • resize_storage updates the eviction limit in AppState but never ChunkStore.limit_bytes, so replication keeps enforcing the startup limit until restart. Pre-existing.
  • 32 MiB is a bound for the currently wired fetcher path (4 MiB raw chunks); nothing caps the producers, so ingest_file's caller-supplied chunk size or an unbounded ingest_raster geometry can still produce a chunk a peer will refuse.
  • The gate cannot make the reconstruction path's arithmetic safe: width * height is still computed as u32 and can overflow, small consistent geometry can underflow an offset, earthgrid:band_hashes nested lists escape both caps, raw concatenation has no output cap, and zero spatial dimensions reach a float divide. That is the next cluster's work.

…eplication

H2: replication stored whatever STAC item a peer returned after checking only
the hash format of its chunk list, and wrote it with INSERT OR REPLACE keyed on
the peer-chosen id and collection, so a peer could overwrite a locally ingested
item or file data under a collection it was never allowed to write.

- validate_remote_item files every peer item under the collection we asked for,
  checks the id, requires the earthgrid tile properties to be unsigned integers
  and bounds them, and caps the chunk list. Duplicate hashes, partial tile
  geometry and a hash count above the tile grid stay accepted, because local
  ingestion produces all three.
- items gained an origin column (safe migration, column appended last so no
  positional reader shifts). add_remote_item only ever inserts remote rows or
  updates an existing remote row, so a local item can never be overwritten;
  local add_item still writes origin local.
- an item already held as a non-remote row is never written by replication, and
  its missing chunks are repaired using its own hash list, not the peer's.

H4: the item loop buffered every chunk body before hashing, with no size cap,
one task per hash and no dedup, so a large item could exhaust memory.

- responses above 32 MiB are refused by declared length and by a bounded read;
  each body is hash-verified and stored as it arrives and the buffer is dropped,
  with the list deduplicated and spawned in batches so live tasks stay bounded
  regardless of list length. A storage-limit result stops the run.
@MatMatt
MatMatt merged commit 64bedc3 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