fix: verify CID hash before decoding in Block.create#340
Merged
rvagg merged 1 commit intoJun 24, 2026
Conversation
Block.create is the verifying block constructor, but it decoded bytes before checking they matched the CID, so the codec parser ran on unverified, potentially untrusted input. Hash and compare first, then delegate decoding to createUnsafe. Valid blocks are unchanged; bytes that do not match the CID are now rejected without ever being decoded, and such input always throws the hash mismatch error rather than possibly a codec error first. Adds a test asserting mismatched, codec-invalid bytes reject with the hash error. Closes multiformats#339 Signed-off-by: tabcat <tabcat00@proton.me>
tabcat
commented
Jun 23, 2026
rvagg
reviewed
Jun 24, 2026
| await assert.isRejected(main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes') | ||
| }) | ||
|
|
||
| it('create verifies the cid hash before decoding the bytes', async () => { |
rvagg
approved these changes
Jun 24, 2026
|
🎉 This PR is included in version 14.0.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Block.createis the verifying block constructor, but it decodedbytesbefore checking they matched the CID, so the codec parser ran on unverified, potentially untrusted input.This hashes and compares first, then delegates decoding to
createUnsafe. Valid blocks are unchanged; bytes that don't match the CID are now rejected without ever being decoded, and such input always throws the hash-mismatch error rather than possibly a codec error first.Adds a test asserting mismatched, codec-invalid bytes reject with the hash error (i.e. decode never runs).
Closes #339