Skip to content

cold-store-reaper(worker): bound zstd decompression by the manifest expected size (#6590) - #6726

Merged
dezren39 merged 3 commits into
mainfrom
queue/fallback-6590-bounded-decompress
Nov 7, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/fallback-6590-bounded-decompress

Conversation

@dezren39

@dezren39 dezren39 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #6590.

The bug

The rehydrator Worker verified an object's sha256 only after decompressing its zstd frame whole, with no size bound: decompressFrame was exactly return 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.size in 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's Decompress in 16 KiB input slices, counting output as it is produced. Past the limit the output is dropped and the new ExpansionLimitError is 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 a TypeError — the decoder fails closed too.
  • src/index.js: rehydrate validates the job's size first (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 maps ExpansionLimitError to the existing 502 HttpError shape ("expands past the expected N bytes — refusing to return unverified bytes").
  • dist/worker.js rebuilt (tracked artifact).

Tests

npm test in pkgs/cold-store-reaper/worker — all green on the laptop:

  • New test/bounded-decompress.test.mjs (wired into npm test, so the cold-store-reaper workflow runs it): a 4 MiB-of-zeros frame (a few KiB compressed, via node:zlib zstd like the existing suite) is refused at a 1 MiB expected size at both decoder and rehydrate level; 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.mjs now passes the exact sample size to the decoder (exact-size control on the real pack path); test/range-ignored.test.mjs's job helper carries size like 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.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…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.
@dezren39
dezren39 requested a review from a team as a code owner November 7, 2026 03:22
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 412cef11-1bb4-4152-a600-e80419fa3349
Commits

Reviewing files that changed from the base of the PR and between 6e13f14 and e8a41d2.

Files ignored due to path filters: 1

  • pkgs/cold-store-reaper/worker/dist/worker.js is excluded by !**/dist/**
Files selected for processing: 6

  • pkgs/cold-store-reaper/worker/package.json
  • pkgs/cold-store-reaper/worker/src/decoder.js
  • pkgs/cold-store-reaper/worker/src/index.js
  • pkgs/cold-store-reaper/worker/test/bounded-decompress.test.mjs
  • pkgs/cold-store-reaper/worker/test/range-ignored.test.mjs
  • pkgs/cold-store-reaper/worker/test/run-local-proof.mjs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T03:38:19.961276Z e8a41d2 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant