Skip to content

Commit be17038

Browse files
committed
fix: make sure Multihashes are always valid
When a Multihash is created, we make sure that the supplied code is supported, hence part of the code table. Throw an error if a passed in multihash contains an unsupported code. Fixes #70.
1 parent 5072807 commit be17038

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/digests.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ impl<'a, T: TryFrom<u64>> MultihashRefGeneric<'a, T> {
321321
return Err(DecodeError::BadInputLength);
322322
}
323323

324-
let (_code, bytes) = varint_decode::u64(&input).map_err(|_| DecodeError::BadInputLength)?;
324+
let (code, bytes) = varint_decode::u64(&input).map_err(|_| DecodeError::BadInputLength)?;
325+
// Make sure it's a code that is part of the codec table
326+
T::try_from(code).map_err(|_| DecodeError::UnknownCode)?;
325327

326328
let (hash_len, bytes) =
327329
varint_decode::u64(&bytes).map_err(|_| DecodeError::BadInputLength)?;

tests/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ fn multihash_ref_errors() {
286286
MultihashRef::from_slice(&[identity_code, identity_length, 1, 2, 3, 4]).is_err(),
287287
"Should error on wrong hash length"
288288
);
289+
290+
let unsupported_code = 0x04;
291+
let hash_length = 3;
292+
assert_eq!(
293+
MultihashRef::from_slice(&[unsupported_code, hash_length, 1, 2, 3]),
294+
Err(DecodeError::UnknownCode),
295+
"Should error on codes that are not part of the code table"
296+
);
289297
}
290298

291299
#[test]

0 commit comments

Comments
 (0)