Repository navigation
cold-store-reaper(worker): bound zstd decompression by the manifest expected size (#6590) - #6726
Conversation
…xpected size (#6590) decompressFrame was a bare fzstd decompress(): the whole expansion was allocated before the sha256 check, so a small frame could expand past the Worker memory limit and kill the isolate (and every joined fetch on the single-flight Durable Object) instead of producing the designed clean 502. The decoder now streams the frame in slices and counts output as it is produced: past the limit the output is dropped and ExpansionLimitError is thrown, so peak memory tracks the expected size. rehydrate finally reads the job size the entry point already selects (o.size): a missing or non-numeric size fails closed with a 502 before any fetch, and an over-expansion maps to the existing 502 HttpError shape. dist/worker.js rebuilt. Tests: new test/bounded-decompress.test.mjs (wired into npm test) -- a 4 MiB-of-zeros frame (a few KiB compressed) is refused at a 1 MiB expected size at both decoder and rehydrate level, served at exactly its size with a matching sha, refused one byte under; missing/non-numeric job size fails closed; unusable decoder limits are TypeErrors. run-local-proof now passes the exact sample size, and the range-ignored job helper carries size like a real manifest row; the whole npm test suite passes.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 3 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c5cf19713
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…ize up front (Codex P1 x2 on PR #6726) Finding 1: fzstd Decompress allocates the frame header's declared window on the first push, before the output bound can fire -- a header could declare a 512 MiB window over a tiny payload. The decoder now parses the frame header (skippable frames included) and refuses up front when the declared content size exceeds the limit, or the declared window exceeds max(limit, 8 MiB) -- the allowance covers legitimate encoders (the reaper packer runs zstd -3, window <= 2 MiB). Unparseable headers fall through to the streaming decoder exactly as before. Finding 2: property coverage per the testing doctrine -- 92 generated cases (fixed-seed mulberry32; zero/random/periodic/text plaintexts, lengths 0..65536) with named oracles (round-trip: the plaintext itself; bound: a sub-length limit must throw ExpansionLimitError), plus hand-built raw-block frames pinning the window and declared-content-size refusals. dist/worker.js rebuilt.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d0ec6a1e16
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… P1, round 2, PR #6726) The header pre-check covered only the first frame, but fzstd's streaming decoder walks concatenated frames on its own, allocating each later frame's declared window unchecked. The decoder's contract is exactly one frame per object (the reaper stores one self-contained frame per object), so the first frame's block chain is now walked to its exact end (raw/compressed blocks by size, RLE by one byte, plus the optional content checksum) and any trailing bytes are rejected before fzstd runs. Leading skippable frames still decode, since the header parser already skipped them. Tests pin the reported bypass shape (small frame + 512 MiB-window frame), two well-formed frames, and the skippable-prefix control. dist/worker.js rebuilt.
Fixes #6590.
The bug
The rehydrator Worker verified an object's sha256 only after decompressing its zstd frame whole, with no size bound:
decompressFramewas exactlyreturn decompress(frameBytes). A small frame could expand past the Worker's memory limit and kill the isolate (and every joined fetch on the single-flight Durable Object) instead of producing the designed clean 502. The manifest's expected plaintext size was already selected (o.sizein the entry-point SQL) and posted to the Durable Object in the job — and never read.The fix
src/decoder.js:decompressFrame(frameBytes, maxPlainBytes)now streams the frame through fzstd'sDecompressin 16 KiB input slices, counting output as it is produced. Past the limit the output is dropped and the newExpansionLimitErroris thrown, so peak memory tracks the expected size (plus an output block of slack), never the expansion. An unusable limit (missing, negative, non-integer) is aTypeError— the decoder fails closed too.src/index.js:rehydratevalidates the job'ssizefirst (numeric strings from the row are accepted; missing/non-numeric/non-integer fails closed with a 502 before any fetch), passes it as the decompression bound, and mapsExpansionLimitErrorto the existing 502HttpErrorshape ("expands past the expected N bytes — refusing to return unverified bytes").dist/worker.jsrebuilt (tracked artifact).Tests
npm testinpkgs/cold-store-reaper/worker— all green on the laptop:test/bounded-decompress.test.mjs(wired intonpm test, so the cold-store-reaper workflow runs it): a 4 MiB-of-zeros frame (a few KiB compressed, vianode:zlibzstd like the existing suite) is refused at a 1 MiB expected size at both decoder andrehydratelevel; served at exactly its size with matching sha256; refused one byte under; missing/non-numeric job size fails closed with a 502; unusable decoder limits rejected.test/run-local-proof.mjsnow passes the exact sample size to the decoder (exact-size control on the real pack path);test/range-ignored.test.mjs's job helper carriessizelike a real manifest row — all its perf(cold-store-reaper): bound memory when Drive ignores Range requests #6182 checks still pass.Filed from a queue-worker fallback shift (broker backend down, #6619): the issue's comments carry the claim and the release.
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.