Skip to content

fix: reject length-prefixed strings that are not followed by CRLF - #2300

Merged
PavelPashov merged 2 commits into
redis:mainfrom
utsxvrai:fix/validate-blob-string-terminator
Nov 23, 2026
Merged

PavelPashov merged 2 commits into
redis:mainfrom
utsxvrai:fix/validate-blob-string-terminator

Conversation

@utsxvrai

@utsxvrai utsxvrai commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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 for GET 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 \n is 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 throw Expected CRLF instead 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 / #decodeCRLF helpers 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.

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T10:44:57.761247Z 51f000c 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: 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".

Comment thread lib/resp/decoder.ts
Comment on lines +520 to +521
this.#cursor = end;
return this.#decodeCRLF(value, 0, chunk);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM

@PavelPashov
PavelPashov merged commit 77f88ee into redis:main Nov 23, 2026
22 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.

Malformed RESP can cause junk responses to the application which cannot be caught.

2 participants