@cryptotaxi247 / kubo / commits / 60a548b62

correctness: only match CIDs matching go-cid.Cid.String output

This commit was moved from ipfs/go-ipfs-http-client@34cc489461985cf4139819e589fdd09bdc034c24

Jorropo committed Apr 5, 2022 at 20:11 UTC 60a548b621ff229a648a268c4d06f6e206ad6099
2 files changed +24 -2
client/httpapi/errors.go
+17 -2
@@ -3,9 +3,11 @@ package httpapi
3 import (
4 "errors"
5 "strings"
6 + "unicode/utf8"
7
8 "github.com/ipfs/go-cid"
9 ipld "github.com/ipfs/go-ipld-format"
10 + mbase "github.com/multiformats/go-multibase"
11 )
12
13 // This file handle parsing and returning the correct ABI based errors from error messages
@@ -97,15 +99,28 @@ func parseIPLDErrNotFound(msg string) (error, bool) {
99 return strings.ContainsAny(string(r), cidBreakSet)
100 })
101 if postIndex < 0 {
102 + // no breakage meaning the string look like this something + "ipld: could not find bafy"
103 postIndex = len(msgPostKey)
104 }
105
106 + cidStr := msgPostKey[:postIndex]
107 +
108 var err error
104 - c, err = cid.Decode(msgPostKey[:postIndex])
109 + c, err = cid.Decode(cidStr)
110 if err != nil {
106 - // Unknown
111 + // failed to decode CID give up
112 return nil, false
113 }
114 +
115 + // check that the CID is either a CIDv0 or a base32 multibase
116 + // because that what ipld.ErrNotFound.Error() -> cid.Cid.String() do currently
117 + if c.Version() != 0 {
118 + baseRune, _ := utf8.DecodeRuneInString(cidStr)
119 + if baseRune == utf8.RuneError || baseRune != mbase.Base32 {
120 + // not a multibase we expect, give up
121 + return nil, false
122 + }
123 + }
124 }
125
126 err := ipld.ErrNotFound{Cid: c}
client/httpapi/errors_test.go
+7
@@ -7,6 +7,7 @@ import (
7
8 "github.com/ipfs/go-cid"
9 ipld "github.com/ipfs/go-ipld-format"
10 + mbase "github.com/multiformats/go-multibase"
11 mh "github.com/multiformats/go-multihash"
12 )
13
@@ -40,6 +41,11 @@ func TestParseIPLDNotFound(t *testing.T) {
41 cidBreaks[i] = "%w" + string(v)
42 }
43
44 + base58BTCEncoder, err := mbase.NewEncoder(mbase.Base58BTC)
45 + if err != nil {
46 + t.Fatalf("expected to find Base58BTC encoder; got error %q", err.Error())
47 + }
48 +
49 for _, wrap := range append(cidBreaks,
50 "",
51 "merkledag: %w",
@@ -49,6 +55,7 @@ func TestParseIPLDNotFound(t *testing.T) {
55 for _, err := range [...]error{
56 errors.New("ipld: could not find "),
57 errors.New("ipld: could not find Bad_CID"),
58 + errors.New("ipld: could not find " + cid.NewCidV1(cid.Raw, randomSha256MH).Encode(base58BTCEncoder)), // Test that we only accept CIDv0 and base32 CIDs
59 errors.New("network connection timeout"),
60 ipld.ErrNotFound{Cid: cid.Undef},
61 ipld.ErrNotFound{Cid: cid.NewCidV0(randomSha256MH)},