From 6af10cc85ffc211aa3c99ac4e2b10fd31e4d1282 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Tue, 21 Nov 2023 11:03:28 -0800 Subject: [PATCH] sia: prevent panic for v0 multihash conversion --- sia/sia.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sia/sia.go b/sia/sia.go index 74641e5..9845d97 100644 --- a/sia/sia.go +++ b/sia/sia.go @@ -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