Validate long item length before loading lengthLength#6418
Validate long item length before loading lengthLength#6418Amxx wants to merge 2 commits intoOpenZeppelin:masterfrom
Conversation
|
WalkthroughThe pull request adds a validity check in the Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)✅ Unit Test PR creation complete.
📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
contracts/utils/RLP.sol (2)
455-466: Consider usingrequire(itemLength > lengthLength, ...)for a more precise check.The added check
require(itemLength > 1, ...)at line 458 correctly prevents the panic whenitemLength = 1by ensuring offset 1 is valid beforeitem.load(1). However, this check is conservative and partially redundant with the check at line 460.Since
lengthLengthis computed at line 456, you could userequire(itemLength > lengthLength, ...)instead ofrequire(itemLength > 1, ...). This would:
- Prevent panics for all insufficient length cases (not just when
itemLength = 1)- Eliminate redundancy with the
itemLength > lengthLengthcheck at line 460- Align with the PR objectives which mention "require(itemLength > lengthLength, RLPInvalidEncoding())"
♻️ More precise validation
// Case: Long string (>55 bytes) uint256 lengthLength = prefix - SHORT_OFFSET - SHORT_THRESHOLD; - require(itemLength > 1, RLPInvalidEncoding()); + require(itemLength > lengthLength, RLPInvalidEncoding()); bytes32 lenChunk = item.load(1); - require(itemLength > lengthLength && bytes1(lenChunk) != 0x00, RLPInvalidEncoding()); + require(bytes1(lenChunk) != 0x00, RLPInvalidEncoding()); uint256 len = uint256(lenChunk) >> (256 - 8 * lengthLength); require(len > SHORT_THRESHOLD && itemLength > lengthLength + len, RLPInvalidEncoding());🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@contracts/utils/RLP.sol` around lines 455 - 466, Replace the conservative check require(itemLength > 1, RLPInvalidEncoding()) with a length-aware validation that ensures the buffer is long enough to read the length bytes: use require(itemLength > lengthLength, RLPInvalidEncoding()). This keeps the existing subsequent checks (the later require(itemLength > lengthLength && bytes1(lenChunk) != 0x00, RLPInvalidEncoding()) and require(len > SHORT_THRESHOLD && itemLength > lengthLength + len, RLPInvalidEncoding())) intact, but prevents the earlier potential panic from item.load(1) by validating against the computed lengthLength first; update the check in the long-string branch (where lengthLength is calculated from prefix, SHORT_OFFSET and SHORT_THRESHOLD) and keep RLPInvalidEncoding as the error.
475-486: Consider usingrequire(itemLength > lengthLength, ...)for a more precise check.Similar to the long string case, the added check
require(itemLength > 1, ...)at line 478 correctly prevents the panic whenitemLength = 1. However, usingrequire(itemLength > lengthLength, ...)would be more precise and eliminate redundancy with line 480.♻️ More precise validation
// Case: Long list uint256 lengthLength = prefix - LONG_OFFSET - SHORT_THRESHOLD; - require(itemLength > 1, RLPInvalidEncoding()); + require(itemLength > lengthLength, RLPInvalidEncoding()); bytes32 lenChunk = item.load(1); - require(itemLength > lengthLength && bytes1(lenChunk) != 0x00, RLPInvalidEncoding()); + require(bytes1(lenChunk) != 0x00, RLPInvalidEncoding()); uint256 len = uint256(lenChunk) >> (256 - 8 * lengthLength); require(len > SHORT_THRESHOLD && itemLength > lengthLength + len, RLPInvalidEncoding());🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@contracts/utils/RLP.sol` around lines 475 - 486, Replace the broad sanity check require(itemLength > 1, RLPInvalidEncoding()) with a precise bounds check require(itemLength > lengthLength, RLPInvalidEncoding()) before loading lenChunk so we ensure the prefix actually contains the full length-of-length bytes; then remove the redundant itemLength > lengthLength part from the subsequent require(...) that currently also checks bytes1(lenChunk) != 0x00, keeping the existing RLPInvalidEncoding() error path and leaving variables lengthLength, itemLength, lenChunk and the return of ItemType.List unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@contracts/utils/RLP.sol`:
- Around line 455-466: Replace the conservative check require(itemLength > 1,
RLPInvalidEncoding()) with a length-aware validation that ensures the buffer is
long enough to read the length bytes: use require(itemLength > lengthLength,
RLPInvalidEncoding()). This keeps the existing subsequent checks (the later
require(itemLength > lengthLength && bytes1(lenChunk) != 0x00,
RLPInvalidEncoding()) and require(len > SHORT_THRESHOLD && itemLength >
lengthLength + len, RLPInvalidEncoding())) intact, but prevents the earlier
potential panic from item.load(1) by validating against the computed
lengthLength first; update the check in the long-string branch (where
lengthLength is calculated from prefix, SHORT_OFFSET and SHORT_THRESHOLD) and
keep RLPInvalidEncoding as the error.
- Around line 475-486: Replace the broad sanity check require(itemLength > 1,
RLPInvalidEncoding()) with a precise bounds check require(itemLength >
lengthLength, RLPInvalidEncoding()) before loading lenChunk so we ensure the
prefix actually contains the full length-of-length bytes; then remove the
redundant itemLength > lengthLength part from the subsequent require(...) that
currently also checks bytes1(lenChunk) != 0x00, keeping the existing
RLPInvalidEncoding() error path and leaving variables lengthLength, itemLength,
lenChunk and the return of ItemType.List unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 7c87bc88-2493-4431-9cb0-10dc0e6f89a9
📒 Files selected for processing (1)
contracts/utils/RLP.sol
|
Note Unit test generation is a public access feature. Expect some limitations and changes as we gather feedback and continue to improve it. Generating unit tests... This may take up to 20 minutes. |
|
Request timed out after 900000ms (requestId=dffe8aea-a338-4806-bb56-e294dd6501a3) |
Fixes L- 22
PR Checklist
npx changeset add)