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

fix: improved error message on broken CIDv0 #33

Merged
merged 4 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ os:
language: go

go:
- 1.11.x
- 1.14.x
- 1.15.x

env:
global:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/ipfs/go-path

go 1.14

require (
github.com/ipfs/go-cid v0.0.2
github.com/ipfs/go-ipld-format v0.0.2
Expand Down
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")
}
}
4 changes: 4 additions & 0 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.
return cid.Cid{}, nil, err
}

if len(rest) == 0 {
return lnk.Cid, nil, nil
}

next, err := lnk.GetNode(ctx, r.DAG)
if err != nil {
return cid.Cid{}, nil, err
Expand Down
40 changes: 40 additions & 0 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,43 @@ func TestRecurivePathResolution(t *testing.T) {
p.String(), rCid.String(), cKey.String()))
}
}

func TestResolveToLastNode_NoUnnecessaryFetching(t *testing.T) {
ctx := context.Background()
dagService := dagmock.Mock()

a := randNode()
b := randNode()

err := a.AddNodeLink("child", b)
if err != nil {
t.Fatal(err)
}

err = dagService.Add(ctx, a)
if err != nil {
t.Fatal(err)
}

aKey := a.Cid()

segments := []string{aKey.String(), "child"}
p, err := path.FromSegments("/ipfs/", segments...)
if err != nil {
t.Fatal(err)
}

resolver := resolver.NewBasicResolver(dagService)
resolvedCID, remainingPath, err := resolver.ResolveToLastNode(ctx, p)
if err != nil {
t.Fatal(err)
}

if len(remainingPath) > 0 {
t.Fatal("cannot have remaining path")
}

if !resolvedCID.Equals(b.Cid()) {
t.Fatal("resolved to the wrong CID")
}
}