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

Commit 2b7faf2

Browse files
authored
Merge pull request #16 from ipfs/fix/cid-validation
nit: validate CIDs in IPLD paths
2 parents 34ebf8a + 261f0f7 commit 2b7faf2

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

path.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func ParsePath(txt string) (Path, error) {
106106
// if the path doesnt begin with a '/'
107107
// we expect this to start with a hash, and be an 'ipfs' path
108108
if parts[0] != "" {
109-
if _, err := ParseCidToPath(parts[0]); err != nil {
109+
if _, err := cid.Decode(parts[0]); err != nil {
110110
return "", ErrBadPath
111111
}
112112
// The case when the path starts with hash without a protocol prefix
@@ -117,11 +117,17 @@ func ParsePath(txt string) (Path, error) {
117117
return "", ErrBadPath
118118
}
119119

120-
if parts[1] == "ipfs" {
121-
if _, err := ParseCidToPath(parts[2]); err != nil {
120+
//TODO: make this smarter
121+
switch parts[1] {
122+
case "ipfs", "ipld":
123+
// Validate Cid.
124+
_, err := cid.Decode(parts[2])
125+
if err != nil {
122126
return "", err
123127
}
124-
} else if parts[1] != "ipns" && parts[1] != "ipld" { //TODO: make this smarter
128+
case "ipns":
129+
// No validation.
130+
default:
125131
return "", ErrBadPath
126132
}
127133

path_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ func TestPathParsing(t *testing.T) {
1818
"QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": true,
1919
"/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": false,
2020
"/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n/a": false,
21-
"/ipfs/": false,
22-
"ipfs/": false,
21+
"/ipfs/foo": false,
22+
"/ipfs/": false,
23+
"ipfs/": false,
2324
"ipfs/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": false,
25+
"/ipld/foo": false,
26+
"/ipld/": false,
27+
"ipld/": false,
28+
"ipld/QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n": false,
2429
}
2530

2631
for p, expected := range cases {

0 commit comments

Comments
 (0)