This repository was archived by the owner on Oct 5, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -3,9 +3,11 @@ package httpapi
3
3
import (
4
4
"errors"
5
5
"strings"
6
+ "unicode/utf8"
6
7
7
8
"github.com/ipfs/go-cid"
8
9
ipld "github.com/ipfs/go-ipld-format"
10
+ mbase "github.com/multiformats/go-multibase"
9
11
)
10
12
11
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) {
97
99
return strings .ContainsAny (string (r ), cidBreakSet )
98
100
})
99
101
if postIndex < 0 {
102
+ // no breakage meaning the string look like this something + "ipld: could not find bafy"
100
103
postIndex = len (msgPostKey )
101
104
}
102
105
106
+ cidStr := msgPostKey [:postIndex ]
107
+
103
108
var err error
104
- c , err = cid .Decode (msgPostKey [: postIndex ] )
109
+ c , err = cid .Decode (cidStr )
105
110
if err != nil {
106
- // Unknown
111
+ // failed to decode CID give up
107
112
return nil , false
108
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
+ }
109
124
}
110
125
111
126
err := ipld.ErrNotFound {Cid : c }
Original file line number Diff line number Diff line change 7
7
8
8
"github.com/ipfs/go-cid"
9
9
ipld "github.com/ipfs/go-ipld-format"
10
+ mbase "github.com/multiformats/go-multibase"
10
11
mh "github.com/multiformats/go-multihash"
11
12
)
12
13
@@ -40,6 +41,11 @@ func TestParseIPLDNotFound(t *testing.T) {
40
41
cidBreaks [i ] = "%w" + string (v )
41
42
}
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
+
43
49
for _ , wrap := range append (cidBreaks ,
44
50
"" ,
45
51
"merkledag: %w" ,
@@ -49,6 +55,7 @@ func TestParseIPLDNotFound(t *testing.T) {
49
55
for _ , err := range [... ]error {
50
56
errors .New ("ipld: could not find " ),
51
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
52
59
errors .New ("network connection timeout" ),
53
60
ipld.ErrNotFound {Cid : cid .Undef },
54
61
ipld.ErrNotFound {Cid : cid .NewCidV0 (randomSha256MH )},
You can’t perform that action at this time.
0 commit comments