Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
correctness: only match CIDs matching go-cid.Cid.String output
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Apr 5, 2022
1 parent 75f597a commit 34cc489
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 17 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package httpapi
import (
"errors"
"strings"
"unicode/utf8"

"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
mbase "github.com/multiformats/go-multibase"
)

// This file handle parsing and returning the correct ABI based errors from error messages
Expand Down Expand Up @@ -97,15 +99,28 @@ func parseIPLDErrNotFound(msg string) (error, bool) {
return strings.ContainsAny(string(r), cidBreakSet)
})
if postIndex < 0 {
// no breakage meaning the string look like this something + "ipld: could not find bafy"
postIndex = len(msgPostKey)
}

cidStr := msgPostKey[:postIndex]

var err error
c, err = cid.Decode(msgPostKey[:postIndex])
c, err = cid.Decode(cidStr)
if err != nil {
// Unknown
// failed to decode CID give up
return nil, false
}

// check that the CID is either a CIDv0 or a base32 multibase
// because that what ipld.ErrNotFound.Error() -> cid.Cid.String() do currently
if c.Version() != 0 {
baseRune, _ := utf8.DecodeRuneInString(cidStr)
if baseRune == utf8.RuneError || baseRune != mbase.Base32 {
// not a multibase we expect, give up
return nil, false
}
}
}

err := ipld.ErrNotFound{Cid: c}
Expand Down
7 changes: 7 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
mbase "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash"
)

Expand Down Expand Up @@ -40,6 +41,11 @@ func TestParseIPLDNotFound(t *testing.T) {
cidBreaks[i] = "%w" + string(v)
}

base58BTCEncoder, err := mbase.NewEncoder(mbase.Base58BTC)
if err != nil {
t.Fatalf("expected to find Base58BTC encoder; got error %q", err.Error())
}

for _, wrap := range append(cidBreaks,
"",
"merkledag: %w",
Expand All @@ -49,6 +55,7 @@ func TestParseIPLDNotFound(t *testing.T) {
for _, err := range [...]error{
errors.New("ipld: could not find "),
errors.New("ipld: could not find Bad_CID"),
errors.New("ipld: could not find " + cid.NewCidV1(cid.Raw, randomSha256MH).Encode(base58BTCEncoder)), // Test that we only accept CIDv0 and base32 CIDs
errors.New("network connection timeout"),
ipld.ErrNotFound{Cid: cid.Undef},
ipld.ErrNotFound{Cid: cid.NewCidV0(randomSha256MH)},
Expand Down

0 comments on commit 34cc489

Please sign in to comment.