Skip to content

Commit

Permalink
Add link traversal option
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Dominic Della Valle <[email protected]>
  • Loading branch information
djdv committed Sep 18, 2018
1 parent 6aaaa76 commit bceb240
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
14 changes: 9 additions & 5 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"sort"
"strings"

"github.com/ipfs/go-ipfs-cmds"

osh "github.com/Kubuxu/go-os-helper"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmdkit/files"
"github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
)

Expand Down Expand Up @@ -79,6 +78,11 @@ func isRecursive(req *cmds.Request) bool {
return rec && ok
}

func linkResolveDepth(req *cmds.Request) int {
linkOpt, _ := req.Options[cmds.DerefLong].(int) //TODO: safety concern
return linkOpt
}

type parseState struct {
cmdline []string
i int
Expand Down Expand Up @@ -265,7 +269,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
fpath = stdin.Name()
file = files.NewReaderFile("", fpath, r, nil)
} else {
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req))
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req), linkResolveDepth(req))
if err != nil {
return err
}
Expand Down Expand Up @@ -455,7 +459,7 @@ func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
const notRecursiveFmtStr = "'%s' is a directory, use the '-%s' flag to specify directories"
const dirNotSupportedFmtStr = "Invalid path '%s', argument '%s' does not support directories"

func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (files.File, error) {
func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool, resolveDepth int) (files.File, error) {
if fpath == "." {
cwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -484,7 +488,7 @@ func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (
}
}

return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat)
return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat, resolveDepth)
}

// Inform the user if a file is waiting on input
Expand Down
38 changes: 38 additions & 0 deletions cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -518,3 +519,40 @@ func TestBodyArgs(t *testing.T) {
}
}
}

func TestLinkArgs(t *testing.T) {
root, err := ioutil.TempDir("", "cmds-link-test")
if err != nil {
t.Fatalf("cannot create symlink test root: %v", err)
}
defer os.RemoveAll(root)

realDir := filepath.Join(root, "RealDir")
dirLink := filepath.Join(root, "DirLink")
os.Mkdir(realDir, 0755)
err = os.Symlink(realDir, dirLink)
if err != nil {
t.Fatalf("cannot create symlink %q->%q: %v", dirLink, realDir, err)
}

dirArgs := &cmdkit.Argument{Type: cmdkit.ArgFile, Recursive: true}
linkArgs := &cmdkit.Argument{Type: cmdkit.ArgFile}

ld, err := appendFile(realDir, dirArgs, true, true, true)
if err != nil {
t.Fatalf("cannot append directory %q: %v", realDir, err)
}

if !ld.IsDirectory() {
t.Fatalf("symlink was not resolved when intended")
}

ld, err = appendFile(dirLink, linkArgs, true, true, false)
if err != nil {
t.Fatalf("cannot append link %q: %v", dirLink, err)
}

if ld.IsDirectory() {
t.Fatalf("symlink was resolved when not intended")
}
}
1 change: 0 additions & 1 deletion cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func Run(ctx context.Context, root *cmds.Command,
if timeoutStr, ok := req.Options[cmds.TimeoutOpt]; ok {
timeout, err := time.ParseDuration(timeoutStr.(string))
if err != nil {
printErr(err)
return err
}
req.Context, cancel = context.WithTimeout(req.Context, timeout)
Expand Down
14 changes: 2 additions & 12 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package http

import (
"context"
"crypto/rand"
"encoding/base32"
"errors"
"net/http"
"runtime/debug"
Expand All @@ -12,6 +10,7 @@ import (

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-loggables"
cors "github.com/rs/cors"
)

Expand Down Expand Up @@ -133,7 +132,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer cancel()

req.Context = logging.ContextWithLoggable(req.Context, uuidLoggable())
req.Context = logging.ContextWithLoggable(req.Context, loggables.Uuid("requestId"))
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
Expand Down Expand Up @@ -161,15 +160,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.root.Call(req, re, h.env)
}

func uuidLoggable() logging.Loggable {
ids := make([]byte, 16)
rand.Read(ids)

return logging.Metadata{
"requestId": base32.HexEncoding.EncodeToString(ids),
}
}

func sanitizedErrStr(err error) string {
s := err.Error()
s = strings.Split(s, "\n")[0]
Expand Down
2 changes: 0 additions & 2 deletions http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
}
e.Message = string(mes)
e.Code = cmdkit.ErrNormal
case res.dec == nil:
return nil, fmt.Errorf("unknown error content type: %s", contentType)
default:
// handle marshalled errors
err := res.dec.Decode(&e)
Expand Down
5 changes: 4 additions & 1 deletion opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ const (
TimeoutOpt = "timeout"
OptShortHelp = "h"
OptLongHelp = "help"
//DerefShort = "H" //RESERVED
DerefLong = "dereference-command-line"
)

// options that are used by this package
var OptionEncodingType = cmdkit.StringOption(EncLong, EncShort, "The encoding type the output should be encoded with (json, xml, or text)").WithDefault("text")
var OptionRecursivePath = cmdkit.BoolOption(RecLong, RecShort, "Add directory paths recursively").WithDefault(false)
var OptionStreamChannels = cmdkit.BoolOption(ChanOpt, "Stream channel output")
var OptionTimeout = cmdkit.StringOption(TimeoutOpt, "set a global timeout on the command")
var OptionTimeout = cmdkit.StringOption(TimeoutOpt, "Set a global timeout on the command")
var OptionDerefArgs = cmdkit.IntOption(DerefLong, "Resolve symlinks instead of adding them as-is").WithDefault(0)

0 comments on commit bceb240

Please sign in to comment.