Repository navigation
fix(go): bound zstd window and decoded text size on reads - #276
Conversation
The zstd decoder allocates a frame's declared window before it reads a block, and DecompressPayload allowed windows up to 512 MiB, so a stored value of a few bytes made every read allocate up to 512 MiB. Reads now accept windows up to 128 MiB, Node's default windowLogMax and the largest window the TypeScript reader accepts, and report larger ones as read_over_limit. The read limit counts decoded bytes, and text replacement then turns each ill-formed UTF-8 byte into a 3-byte U+FFFD, so a text frame could return three times the 512 MiB cap. The replaced text must now also fit MaxDecompressedBytes. The caller limit keeps counting decoded bytes, as the shared protocol vectors require.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #276 +/- ##
==========================================
+ Coverage 94.76% 94.77% +0.01%
==========================================
Files 84 84
Lines 13168 13175 +7
Branches 805 805
==========================================
+ Hits 12479 12487 +8
+ Misses 623 622 -1
Partials 66 66
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
WithDecoderMaxWindow also bounds a single-segment frame by its content size. libzstd's default reader limit, which the TypeScript, Rust and Python readers keep, is (1<<27)+1 bytes, so use the same value and test both frame kinds at the boundary.
|
Agent: review-loop round 1 (until-clean; lanes correctness, security, performance, tests, contracts, holistic) on 31554d2, plus rollout-safety-review.
rollout-safety-review: no blocker. One Important item: after Next: round 2 with fresh reviewers on 04e393e. — Levicus Connecto 🤖 |
|
Agent: review-loop round 2 on 04e393e, with fresh reviewers (correctness, security, performance, tests, contracts, holistic): every lane clean, no findings, no questions. This round changed no code, so the review result is review-clean. rollout-safety-review of the 31554d2..04e393e delta: no findings. The delta only raises the reader limit by one byte to libzstd's (1<<27)+1, so it rejects nothing the first head accepted. The reviewer compressed a 2^27+2 byte input with Node, python-zstandard and native libzstd at levels 1 to 22 and with both Go writer paths: every frame was multi-segment with a window of at most 128 MiB, so no supported writer emits a frame this reader now rejects. Next: check-go, integration-go, formal-go and audit locally, and the Replay + Mutations run, once on this head. — Levicus Connecto 🤖 |
Problem
Fuzzing the Go read path found two ways a small stored value makes one read use far more memory and time than the value's size. Both need a crafted or corrupted Redis entry, and both are in v0.27.0 too.
DecompressPayloadaccepted windows up to 512 MiB (WithDecoderMaxMemory, no window limit), so an 11-byte frame made every read allocate 513 MiB. ThroughGetOrLoadatGOMAXPROCS=2, one such key dropped to about 20 calls/s, with 7.4 GB peak RSS at 8 callers. The TypeScript reader rejects these frames.0xFFreturned 1.5 GiB of text and allocated about 7 GiB. Writers never store ill-formed UTF-8 in a compressed text frame, so only a crafted or corrupted entry reaches this.A tracked, remote-only read with compression off on the writer still decodes a frame marked as zstd, so the default first use case is exposed.
Change
Bug fix (no design record: the behavior matches the other ports' readers, and no contract text changes).
DecompressPayloadpasseszstd.WithDecoderMaxWindow((1<<27)+1), libzstd's default reader limit, which the TypeScript (Node), Rust and Python readers keep. A frame whose window exceeds it reads asread_over_limitwithout allocating the window. A single-segment frame's window is its content size, so the limit applies to it too, exactly as in libzstd. No supported writer emits such a frame: libzstd writes windows of at most 128 MiB (level 22), klauspost at most 8 MiB, and both write a single-segment frame only when the content fits the window.0x01) frames, the replaced text must also fitMaxDecompressedBytes; otherwise the read isread_over_limit. The caller's limit keeps counting decoded bytes, as the shared protocol vectors require (a limit of 1 still returns one U+FFFD for one0xFF).replacementUTF8Withinstops once the output passes the bound, andreplacementUTF8calls it with no bound.fallback_raw, and as in every port, a rejected frame's stored bytes still go to the operation's codec. The default JSON codec rejects them, so the read misses, loads the source and overwrites the entry; a custom codec that accepts arbitrary bytes receives them as the value.go/internal/testsuite/compression_test.gofail onmainand pass here:read_over_limitwithin 1 MiB of allocation and 100 ms, and a 128 MiB window still decodes;read_over_limit, matching Node;GetOrLoadwithWithoutCompressionserves the source within 16 MiB of allocation;0xFFin a text frame returnsread_over_limitand the stored bytes within 5 GiB of allocation (about 7 GiB onmain).TestReplacementUTF8Withinpins the bound.formal/catalogs/go-parity.jsonre-pins thego/codec.goandgo/protocol.gohashes; the bound symbols and mapping rationales are unchanged.Ports
toString("utf8"); see Follow-ups.replacement_utf8; see Follow-ups.decode("utf-8", errors="replace"); see Follow-ups.Validation
Head:
04e393emake check-go(vet, gofmt,go test -racewith coverage) at04e393e: passmake integration-go(Redis 6.2, Valkey 8, Cluster, TypeScript interop,-race) at04e393e: passmake formal-go(complete Go replay of a freshmake formal-generatecorpus,-race) at04e393e: passmake auditat04e393e: passmain, 240 by Go v0.27.0; 4095 B to 16 MiB, levels 1, 3, 9, 19 and 22, text and binary) decode with exact hashes at04e393e; for each compressed fixture, a limit one byte under its size returnsread_over_limitand its exact size decodes.TestReplacementUTF8Withinfail onmain(f2b4f86) and pass at04e393e.04e393e: every check passes.formal-full.yamlmutations run 43172721501 (Replay and Mutations) at04e393e: pass (all 33 jobs).mainvs04e393e,GOMAXPROCS=2, 12 interleaved runs each,benchstat:mainDecompressPayloadtext, 6 KB / 20 KB / 1 MiBDecompressPayloadbinary, 6 KB / 20 KB / 1 MiBDecodeFrame, uncompressed text, 6 KB / 20 KB / 1 MiBAllocations are unchanged in every row. The remote hit is a tracked, remote-only, uncoalesced
GetOrLoadthrough a fakeRemote. TheDecodeFramecost is the bounds check in the shared UTF-8 replacement loop, about 1 µs per 6 KB text frame.Not run: none.
Follow-ups
None filed. The TypeScript, Rust and Python readers also apply the read limit before UTF-8 replacement (Ports). TypeScript and Python hold the replaced text as UTF-16 or UCS-2 strings, so at most about twice the decoded bytes, and Node throws
ERR_STRING_TOO_LONGabove 2^29 - 24 characters; Rust'sreplacement_utf8can return up to three times the cap, as Go did. Whether to bound them is the maintainer's call.