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

Commit 3e8c1a8

Browse files
authored
Merge pull request #33 from ipfs/rvagg/bad-cidv0
fix: improved error message on broken CIDv0
2 parents 1533d95 + 5e8ad22 commit 3e8c1a8

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

path.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func ParsePath(txt string) (Path, error) {
9696
// if the path doesnt begin with a '/'
9797
// we expect this to start with a hash, and be an 'ipfs' path
9898
if parts[0] != "" {
99-
if _, err := cid.Decode(parts[0]); err != nil {
99+
if _, err := decodeCid(parts[0]); err != nil {
100100
return "", &pathError{error: err, path: txt}
101101
}
102102
// The case when the path starts with hash without a protocol prefix
@@ -114,7 +114,7 @@ func ParsePath(txt string) (Path, error) {
114114
return "", &pathError{error: fmt.Errorf("not enough path components"), path: txt}
115115
}
116116
// Validate Cid.
117-
_, err := cid.Decode(parts[2])
117+
_, err := decodeCid(parts[2])
118118
if err != nil {
119119
return "", &pathError{error: fmt.Errorf("invalid CID: %s", err), path: txt}
120120
}
@@ -135,7 +135,7 @@ func ParseCidToPath(txt string) (Path, error) {
135135
return "", &pathError{error: fmt.Errorf("empty"), path: txt}
136136
}
137137

138-
c, err := cid.Decode(txt)
138+
c, err := decodeCid(txt)
139139
if err != nil {
140140
return "", &pathError{error: err, path: txt}
141141
}
@@ -172,11 +172,19 @@ func SplitAbsPath(fpath Path) (cid.Cid, []string, error) {
172172
return cid.Cid{}, nil, &pathError{error: fmt.Errorf("empty"), path: string(fpath)}
173173
}
174174

175-
c, err := cid.Decode(parts[0])
175+
c, err := decodeCid(parts[0])
176176
// first element in the path is a cid
177177
if err != nil {
178178
return cid.Cid{}, nil, &pathError{error: fmt.Errorf("invalid CID: %s", err), path: string(fpath)}
179179
}
180180

181181
return c, parts[1:], nil
182182
}
183+
184+
func decodeCid(cstr string) (cid.Cid, error) {
185+
c, err := cid.Decode(cstr)
186+
if err != nil && len(cstr) == 46 && cstr[:2] == "qm" { // https://github.com/ipfs/go-ipfs/issues/7792
187+
return cid.Cid{}, fmt.Errorf("%v (possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)", err)
188+
}
189+
return c, err
190+
}

path_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,14 @@ func TestPopLastSegment(t *testing.T) {
102102
}
103103
}
104104
}
105+
106+
func TestV0ErrorDueToLowercase(t *testing.T) {
107+
badb58 := "/ipfs/qmbwqxbekc3p8tqskc98xmwnzrzdtrlmimpl8wbutgsmnr"
108+
_, err := ParsePath(badb58)
109+
if err == nil {
110+
t.Fatal("should have failed to decode")
111+
}
112+
if !strings.HasSuffix(err.Error(), "(possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)") {
113+
t.Fatal("should have meaningful info about case-insensitive fix")
114+
}
115+
}

0 commit comments

Comments
 (0)