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

Commit 34cc489

Browse files
committed
correctness: only match CIDs matching go-cid.Cid.String output
1 parent 75f597a commit 34cc489

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

errors.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package httpapi
33
import (
44
"errors"
55
"strings"
6+
"unicode/utf8"
67

78
"github.com/ipfs/go-cid"
89
ipld "github.com/ipfs/go-ipld-format"
10+
mbase "github.com/multiformats/go-multibase"
911
)
1012

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

106+
cidStr := msgPostKey[:postIndex]
107+
103108
var err error
104-
c, err = cid.Decode(msgPostKey[:postIndex])
109+
c, err = cid.Decode(cidStr)
105110
if err != nil {
106-
// Unknown
111+
// failed to decode CID give up
107112
return nil, false
108113
}
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+
}
109124
}
110125

111126
err := ipld.ErrNotFound{Cid: c}

errors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/ipfs/go-cid"
99
ipld "github.com/ipfs/go-ipld-format"
10+
mbase "github.com/multiformats/go-multibase"
1011
mh "github.com/multiformats/go-multihash"
1112
)
1213

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

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+
4349
for _, wrap := range append(cidBreaks,
4450
"",
4551
"merkledag: %w",
@@ -49,6 +55,7 @@ func TestParseIPLDNotFound(t *testing.T) {
4955
for _, err := range [...]error{
5056
errors.New("ipld: could not find "),
5157
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
5259
errors.New("network connection timeout"),
5360
ipld.ErrNotFound{Cid: cid.Undef},
5461
ipld.ErrNotFound{Cid: cid.NewCidV0(randomSha256MH)},

0 commit comments

Comments
 (0)