Repository navigation
[RESP] Fix 32-bit RDB length decoding offset and validate RESTORE payload bounds - #2465
Open
Nguyễn Phúc Hưng Thịnh (nphungthinh2601) wants to merge 2 commits into
Conversation
…ORE payload bounds
Copilot started reviewing on behalf of
Nguyễn Phúc Hưng Thịnh (nphungthinh2601)
December 6, 2026 15:10
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The length decoder accepts unsupported 0x81 and invalid markers as 32-bit lengths, allowing RESTORE to accept malformed payloads instead of rejecting them.
1 open issue
What changed in this PR
Fixes Garnet’s 32-bit RDB length decoding and strengthens RESTORE payload validation.
Changes:
- Corrects the decoding offset and rejects negative lengths.
- Checks payload bounds before indexing or slicing.
- Adds malformed-payload tests and corrects boundary coverage.
| File | Description |
|---|---|
test/standalone/Garnet.test/RespTests.cs |
Adds validation tests and corrects length boundaries. |
libs/server/Resp/KeyAdminCommands.cs |
Validates RESTORE payload size before access. |
libs/common/RespLengthEncodingUtils.cs |
Corrects 32-bit decoding and rejects negative lengths. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
RespLengthEncodingUtils.TryReadLength(case 2, 32-bit RDB length prefix0x80):Int32starting atinput[0]instead ofinput.Slice(1), incorporating the0x80header byte into the length and producing a negative value. This breaksDUMP+RESTOREfor any string payload >= 16,384 bytes (existingSingleRestore32Bittested16,383bytes, which actually exercised the 14-bitcase 1branch).input.Length >= 5orlength >= 0before succeeding, andTryWriteLengthdoes not rejectlength < 0.KeyAdminCommands.NetworkRESTORE:valueSpan[0]before checkingvalueSpan.Length < 10, causing anIndexOutOfRangeExceptionon an empty payload.value.ReadOnlySpan.Slice(payloadStart + 1, length)without verifying that the decodedlengthmatches the available payload bytes before the 10-byte RDB version + CRC64 footer.Solution
libs/common/RespLengthEncodingUtils.cs:case 2 when input.Length >= 5, readBinaryPrimitives.TryReadInt32BigEndian(input.Slice(1), out length), and requirelength >= 0.length < 0inTryWriteLength.libs/server/Resp/KeyAdminCommands.cs:if (valueSpan.Length < 10)ahead ofif (valueSpan[0] != 0x00).length >= 0 && availablePayloadLength >= 0 && length == availablePayloadLength(returningERR DUMP payload version or checksum are wrong) before slicing the payload.Testing
SingleRestore32Bitto16_384bytes (true 32-bit lower boundary) andSingleRestore14Bitto16_383bytes intest/standalone/Garnet.test/RespTests.cs.TryRestoreMalformedPayloadsReturnError,RespLengthEncodingRoundTripBoundaries, andRespLengthEncodingRejectsInvalidInputs.Release(net10.0, 0 warnings / 0 errors):