Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-path
Commits
3e8c1a86
Unverified
Commit
3e8c1a86
authored
Dec 03, 2020
by
Steven Allen
Committed by
GitHub
Dec 03, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #33 from ipfs/rvagg/bad-cidv0
fix: improved error message on broken CIDv0
parents
1533d95b
5e8ad22f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
4 deletions
+23
-4
path.go
path.go
+12
-4
path_test.go
path_test.go
+11
-0
No files found.
path.go
View file @
3e8c1a86
...
...
@@ -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
.
D
ecode
(
parts
[
0
]);
err
!=
nil
{
if
_
,
err
:=
d
ecode
Cid
(
parts
[
0
]);
err
!=
nil
{
return
""
,
&
pathError
{
error
:
err
,
path
:
txt
}
}
// The case when the path starts with hash without a protocol prefix
...
...
@@ -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
.
D
ecode
(
parts
[
2
])
_
,
err
:=
d
ecode
Cid
(
parts
[
2
])
if
err
!=
nil
{
return
""
,
&
pathError
{
error
:
fmt
.
Errorf
(
"invalid CID: %s"
,
err
),
path
:
txt
}
}
...
...
@@ -135,7 +135,7 @@ func ParseCidToPath(txt string) (Path, error) {
return
""
,
&
pathError
{
error
:
fmt
.
Errorf
(
"empty"
),
path
:
txt
}
}
c
,
err
:=
cid
.
D
ecode
(
txt
)
c
,
err
:=
d
ecode
Cid
(
txt
)
if
err
!=
nil
{
return
""
,
&
pathError
{
error
:
err
,
path
:
txt
}
}
...
...
@@ -172,7 +172,7 @@ func SplitAbsPath(fpath Path) (cid.Cid, []string, error) {
return
cid
.
Cid
{},
nil
,
&
pathError
{
error
:
fmt
.
Errorf
(
"empty"
),
path
:
string
(
fpath
)}
}
c
,
err
:=
cid
.
D
ecode
(
parts
[
0
])
c
,
err
:=
d
ecode
Cid
(
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
)}
...
...
@@ -180,3 +180,11 @@ func SplitAbsPath(fpath Path) (cid.Cid, []string, error) {
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
}
path_test.go
View file @
3e8c1a86
...
...
@@ -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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment