Commit 2f07b0e1 authored by rht's avatar rht

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: default avatarrht <rhtbot@gmail.com>
parent 5c2276a9
......@@ -61,12 +61,15 @@ func ParsePath(txt string) (Path, error) {
}
if parts[0] != "" {
return "", ErrBadPath
if _, err := ParseKeyToPath(parts[0]); err != nil {
return "", ErrBadPath
}
// The case when the path starts with hash without a protocol prefix
return Path("/ipfs/" + txt), nil
}
if parts[1] == "ipfs" {
_, err := ParseKeyToPath(parts[2])
if err != nil {
if _, err := ParseKeyToPath(parts[2]); err != nil {
return "", err
}
} else if parts[1] != "ipns" {
......@@ -77,13 +80,16 @@ func ParsePath(txt string) (Path, error) {
}
func ParseKeyToPath(txt string) (Path, error) {
if txt == "" {
return "", ErrNoComponents
}
chk := b58.Decode(txt)
if len(chk) == 0 {
return "", errors.New("not a key")
}
_, err := mh.Cast(chk)
if err != nil {
if _, err := mh.Cast(chk); err != nil {
return "", err
}
return FromKey(key.Key(chk)), nil
......
......@@ -65,6 +65,11 @@ func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (*merkledag.Node, error) {
// validate path
if err := fpath.IsValid(); err != nil {
return nil, err
}
nodes, err := s.ResolvePathComponents(ctx, fpath)
if err != nil || nodes == nil {
return nil, err
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment