Repository navigation
fix: reject length-prefixed strings that are not followed by CRLF - #2300
Conversation
The RESP decoder skipped the two bytes after a blob string without checking them, so a truncated or malformed reply could be decoded as a junk value and resolved for the next pending command. Validate the terminator (across chunk boundaries too) and fail with a protocol error, which goes through the existing fatal error recovery. Closes redis#1172
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: 42a75c51cc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| this.#cursor = end; | ||
| return this.#decodeCRLF(value, 0, chunk); |
There was a problem hiding this comment.
Preserve a fast path for complete string terminators
For small-string MGET/LRANGE replies fully contained in a chunk, unconditionally calling #decodeCRLF sends every terminator through a loop with repeated private-field accesses. Because the ES2021 build lowers #cursor to WeakMap operations, this adds substantially more than two byte comparisons: in an isolated Node 24 decoder benchmark, decoding 1.2 million 4-byte strings in 20-element arrays with the production Buffer mapping took 1.3–1.5 times the parent's time across repeated runs. This penalizes ordinary string-heavy reads. Compare both bytes directly and advance the cursor once when the complete terminator is available, reserving the resumable loop for terminators that cross chunks.
Useful? React with 👍 / 👎.
Check both terminator bytes directly when they are in the current chunk and only fall back to the resumable check when the terminator crosses a chunk boundary or is invalid.
What
The RESP decoder skipped the two bytes after a blob string without checking them. If a server sends a truncated or malformed reply, the leftover bytes get decoded as a value and resolved for the next pending command (the repro in #1172 gets
"$"back forGET e).The decoder now checks that blob / verbatim / blob error strings end with
\r\n, including when the terminator is split across socket chunks. If it doesn't, it throws a protocol error, which goes through the existing fatal error handling: pending commands are rejected and the client reconnects, instead of returning junk.Closes #1172
Tests
test/unit/resp/decoder.ts: missing terminator throws on a single chunk, at every two-chunk split, and when only the\nis wrong; no reply is emitted. These 3 fail on main.test/functional/fatal_error.ts: mock server sends a truncated reply after a valid one; the next command is rejected with the protocol error and the following command succeeds.npm run lint,npm run build,npm run test:js(2728 passing) all pass locally.Note: this is on the hot parsing path; the extra cost is two byte comparisons per bulk string. Happy to run the benchmarks in
benchmarks/if you'd like numbers.Note
Medium Risk
Touches hot-path RESP parsing; wrong CRLF handling could break replies or reject valid traffic, but behavior is narrowly scoped to terminator validation with broad test coverage.
Overview
The RESP decoder no longer skips the two bytes after bulk/blob strings without validating them. Length-prefixed values must be followed by
\r\n, including when the terminator is split across TCP chunks; malformed or truncated replies now throwExpected CRLFinstead of mis-aligning the stream and returning garbage (e.g."$") on the next command.Parsing keeps a fast path when both terminator bytes are present in the current chunk, with resumable
#decodeStringEnd/#decodeCRLFhelpers for the split case. Unit and functional tests cover missing terminators, all chunk boundaries, and recovery after the protocol error via existing fatal-error handling.Reviewed by Cursor Bugbot for commit 51f000c. Bugbot is set up for automated code reviews on this repo. Configure here.