Skip to content

Commit

Permalink
sia: prevent panic for v0 multihash conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 21, 2023
1 parent cf42e44 commit 6af10cc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions sia/sia.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ func (n *Node) getBlock(ctx context.Context, c cid.Cid) (Block, error) {
return n.store.GetBlock(ctx, v1Cid)
case 1:
h := c.Hash()
if c.Prefix().Codec != multihash.SHA2_256 || len(h) != 32 {
return Block{}, ErrNotFound // CID is not convertible to v0
dec, err := multihash.Decode(h)
if err != nil {
n.log.Debug("failed to decode v1 multihash", zap.Stringer("cid", c), zap.Error(err))
return Block{}, ErrNotFound
}
v0Cid := cid.NewCidV0(c.Hash())
if dec.Code != multihash.SHA2_256 || dec.Length != 32 {
n.log.Debug("cannot convert v1 CID to v0", zap.Stringer("cid", c), zap.String("code", multihash.Codes[dec.Code]), zap.Int("length", dec.Length))
return Block{}, ErrNotFound
}
v0Cid := cid.NewCidV0(h)
return n.store.GetBlock(ctx, v0Cid)
}
return Block{}, ErrNotFound
Expand Down

0 comments on commit 6af10cc

Please sign in to comment.