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

Commit

Permalink
fix: improved error message on broken CIDv0
Browse files Browse the repository at this point in the history
move lidel's fix from ipfs/go-cid#116
  • Loading branch information
rvagg committed Dec 3, 2020
1 parent 692649d commit 5e8ad22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 12 additions & 4 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ParsePath(txt string) (Path, error) {
// if the path doesnt begin with a '/'
// we expect this to start with a hash, and be an 'ipfs' path
if parts[0] != "" {
if _, err := cid.Decode(parts[0]); err != nil {
if _, err := decodeCid(parts[0]); err != nil {
return "", &pathError{error: err, path: txt}
}
// The case when the path starts with hash without a protocol prefix
Expand All @@ -114,7 +114,7 @@ func ParsePath(txt string) (Path, error) {
return "", &pathError{error: fmt.Errorf("not enough path components"), path: txt}
}
// Validate Cid.
_, err := cid.Decode(parts[2])
_, err := decodeCid(parts[2])
if err != nil {
return "", &pathError{error: fmt.Errorf("invalid CID: %s", err), path: txt}
}
Expand All @@ -135,7 +135,7 @@ func ParseCidToPath(txt string) (Path, error) {
return "", &pathError{error: fmt.Errorf("empty"), path: txt}
}

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

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

return c, parts[1:], nil
}

func decodeCid(cstr string) (cid.Cid, error) {
c, err := cid.Decode(cstr)
if err != nil && len(cstr) == 46 && cstr[:2] == "qm" { // https://github.com/ipfs/go-ipfs/issues/7792
return cid.Cid{}, fmt.Errorf("%v (possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)", err)
}
return c, err
}
11 changes: 11 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ func TestPopLastSegment(t *testing.T) {
}
}
}

func TestV0ErrorDueToLowercase(t *testing.T) {
badb58 := "/ipfs/qmbwqxbekc3p8tqskc98xmwnzrzdtrlmimpl8wbutgsmnr"
_, err := ParsePath(badb58)
if err == nil {
t.Fatal("should have failed to decode")
}
if !strings.HasSuffix(err.Error(), "(possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)") {
t.Fatal("should have meaningful info about case-insensitive fix")
}
}

0 comments on commit 5e8ad22

Please sign in to comment.