Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit dbe2d73

Browse files
committed
check multihash format and data in decode()
1 parent 21fbf09 commit dbe2d73

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

multihash.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,23 @@ def decode(mhash, encoding=None):
332332
>>> emh = decode(emhash, 'base64')
333333
>>> emh == mh
334334
True
335+
336+
If the digest has an invalid format or contains invalid data, a
337+
`ValueError` is raised.
335338
"""
339+
mhash = bytes(mhash)
336340
if encoding:
337341
mhash = Codecs.get_decoder(encoding)(mhash)
338-
# TODO: check lenghts
339-
return Multihash(int(mhash[0]), mhash[2:])
342+
try:
343+
func = mhash[0]
344+
length = mhash[1]
345+
digest = mhash[2:]
346+
except IndexError:
347+
raise ValueError("multihash is too short")
348+
if length != len(digest):
349+
raise ValueError(
350+
"multihash length field does not match digest field length")
351+
return Multihash(func, digest)
340352

341353

342354
def _test():

0 commit comments

Comments
 (0)