Add path validation in Resolver.ResolvePath
Add ErrNoComponents in ParsePath validation & remove redundant path validation. Any lines using core.Resolve & Resolver.ResolvePath will have their path validated. License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jul 3, 2015 at 17:31 UTC
dfde18e124edb76b6a3fb014dfe7dc97226418d8
4 files changed
+23
-24
core/commands/get.go
+6
-12
@@ -65,14 +65,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
65
return
66
}
67
68
- // Validate path string
69
- p, err := path.ParsePath(req.Arguments()[0])
70
- if err != nil {
71
- res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal)
72
- return
73
- }
68
+ p := path.Path(req.Arguments()[0])
69
var reader io.Reader
75
-
70
if archive, _, _ := req.Option("archive").Bool(); !archive && cmplvl != gzip.NoCompression {
71
// only use this when the flag is '-C' without '-a'
72
reader, err = getZip(req.Context().Context, node, p, cmplvl)
@@ -169,18 +163,18 @@ func getCompressOptions(req cmds.Request) (int, error) {
163
return gzip.NoCompression, nil
164
}
165
172
-func get(ctx context.Context, node *core.IpfsNode, pathToResolve path.Path, compression int) (io.Reader, error) {
173
- dagnode, err := core.Resolve(ctx, node, pathToResolve)
166
+func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) {
167
+ dagnode, err := core.Resolve(ctx, node, p)
168
if err != nil {
169
return nil, err
170
}
171
178
- return utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
172
+ return utar.NewReader(p, node.DAG, dagnode, compression)
173
}
174
175
// getZip is equivalent to `ipfs getdag $hash | gzip`
182
-func getZip(ctx context.Context, node *core.IpfsNode, pathToResolve path.Path, compression int) (io.Reader, error) {
183
- dagnode, err := core.Resolve(ctx, node, pathToResolve)
176
+func getZip(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) (io.Reader, error) {
177
+ dagnode, err := core.Resolve(ctx, node, p)
178
if err != nil {
179
return nil, err
180
}
core/commands/publish.go
+1
-7
@@ -88,15 +88,9 @@ Publish an <ipfs-path> to another public key (not implemented):
88
pstr = args[0]
89
}
90
91
- p, err := path.ParsePath(pstr)
92
- if err != nil {
93
- res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal)
94
- return
95
- }
96
-
91
// TODO n.Keychain.Get(name).PrivKey
92
// TODO(cryptix): is req.Context().Context a child of n.Context()?
99
- output, err := publish(req.Context().Context, n, n.PrivateKey, p)
93
+ output, err := publish(req.Context().Context, n, n.PrivateKey, path.Path(pstr))
94
if err != nil {
95
res.SetError(err, cmds.ErrNormal)
96
return
path/path.go
+11
-5
@@ -61,12 +61,15 @@ func ParsePath(txt string) (Path, error) {
61
}
62
63
if parts[0] != "" {
64
- return "", ErrBadPath
64
+ if _, err := ParseKeyToPath(parts[0]); err != nil {
65
+ return "", ErrBadPath
66
+ }
67
+ // The case when the path starts with hash without a protocol prefix
68
+ return Path("/ipfs/" + txt), nil
69
}
70
71
if parts[1] == "ipfs" {
68
- _, err := ParseKeyToPath(parts[2])
69
- if err != nil {
72
+ if _, err := ParseKeyToPath(parts[2]); err != nil {
73
return "", err
74
}
75
} else if parts[1] != "ipns" {
@@ -77,13 +80,16 @@ func ParsePath(txt string) (Path, error) {
80
}
81
82
func ParseKeyToPath(txt string) (Path, error) {
83
+ if txt == "" {
84
+ return "", ErrNoComponents
85
+ }
86
+
87
chk := b58.Decode(txt)
88
if len(chk) == 0 {
89
return "", errors.New("not a key")
90
}
91
85
- _, err := mh.Cast(chk)
86
- if err != nil {
92
+ if _, err := mh.Cast(chk); err != nil {
93
return "", err
94
}
95
return FromKey(key.Key(chk)), nil
path/resolver.go
+5
@@ -65,6 +65,11 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
65
// ResolvePath fetches the node for given path. It returns the last item
66
// returned by ResolvePathComponents.
67
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (*merkledag.Node, error) {
68
+ // validate path
69
+ if err := fpath.IsValid(); err != nil {
70
+ return nil, err
71
+ }
72
+
73
nodes, err := s.ResolvePathComponents(ctx, fpath)
74
if err != nil || nodes == nil {
75
return nil, err