Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Sanitize get output paths for external file systems #4850

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"os"
gopath "path"
"path/filepath"
"strings"

core "github.com/ipfs/go-ipfs/core"
Expand Down Expand Up @@ -183,8 +183,8 @@ func getOutPath(req *cmds.Request) string {
outPath, _ := req.Options["output"].(string)
if outPath == "" {
trimmed := strings.TrimRight(req.Arguments[0], "/")
_, outPath = gopath.Split(trimmed)
outPath = gopath.Clean(outPath)
_, outPath = filepath.Split(trimmed)
outPath = filepath.Clean(outPath)
}
return outPath
}
Expand Down
26 changes: 13 additions & 13 deletions thirdparty/tar/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,33 @@ func (te *Extractor) Extract(reader io.Reader) error {
return nil
}

// outputPath returns the path at whicht o place tarPath
func (te *Extractor) outputPath(tarPath string) string {
elems := strings.Split(tarPath, "/") // break into elems
elems = elems[1:] // remove original root

path := fp.Join(elems...) // join elems
path = fp.Join(te.Path, path) // rebase on extractor root
return path
// outputPath returns the path at which to place tarPath
func (te *Extractor) outputPath(header *tar.Header) string {
elems := strings.Split(header.Name, "/") // break into elems
elems = elems[1:] // remove IPFS root
safePath := platformSanitize(elems) // assure that our output path is platform legal
fmt.Printf("DBG: %q -> %q\n", header.Name, safePath)
safePath = fp.Join(te.Path, safePath) // rebase on to extraction target root
return safePath
}

func (te *Extractor) extractDir(h *tar.Header, depth int) error {
path := te.outputPath(h.Name)

path := te.outputPath(h)
if depth == 0 {
// if this is the root root directory, use it as the output path for remaining files
// if this is the root directory, use it as the output path for remaining files
te.Path = path
}

//path := platformSanitizeDir(h.Name)
return os.MkdirAll(path, 0755)
}

func (te *Extractor) extractSymlink(h *tar.Header) error {
return os.Symlink(h.Linkname, te.outputPath(h.Name))
return os.Symlink(h.Linkname, te.outputPath(h))
}

func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootExists bool, rootIsDir bool) error {
path := te.outputPath(h.Name)
path := te.outputPath(h)

if depth == 0 { // if depth is 0, this is the only file (we aren't 'ipfs get'ing a directory)
if rootExists && rootIsDir {
Expand Down
9 changes: 9 additions & 0 deletions thirdparty/tar/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !windows,!darwin

package tar

import "path/filepath"

func platformSanitize(pathElements []string) string {
return filepath.Join(pathElements...)
}
11 changes: 11 additions & 0 deletions thirdparty/tar/sanitize_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tar

import (
"path/filepath"
"strings"
)

func platformSanitize(pathElements []string) string {
res := filepath.Join(pathElements...)
return strings.Replace(res, ":", "-", -1)
}
47 changes: 47 additions & 0 deletions thirdparty/tar/sanitize_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tar

import (
"net/url"
"path/filepath"
"regexp"
"strings"
)

//https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
var reservedNames = [...]string{"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}

const reservedCharsRegex = `[<>:"\\|?*]` //NOTE: `/` is not included, files with this in the name will cause problems

func platformSanitize(pathElements []string) string {
// first pass: scan and prefix reserved names CON -> _CON
for i, pe := range pathElements {
for _, rn := range reservedNames {
if pe == rn {
pathElements[i] = "_" + rn
break
}
}
pathElements[i] = strings.TrimRight(pe, ". ") //MSDN: Do not end a file or directory name with a space or a period
}
//second pass: scan and encode reserved characters ? -> %3F
res := strings.Join(pathElements, `/`) // intentionally avoiding [file]path.Clean(), we want `\`'s intact
re := regexp.MustCompile(reservedCharsRegex)
illegalIndices := re.FindAllStringIndex(res, -1)

if illegalIndices != nil {
var lastIndex int
var builder strings.Builder
allocAssist := (len(res) - len(illegalIndices)) + (len(illegalIndices) * 3) // 3 = encoded length
builder.Grow(allocAssist)

for _, si := range illegalIndices {
builder.WriteString(res[lastIndex:si[0]]) // append up to problem char
builder.WriteString(url.QueryEscape(res[si[0]:si[1]])) // escape and append problem char
lastIndex = si[1]
}
builder.WriteString(res[lastIndex:]) // append remainder
res = builder.String()
}

return filepath.FromSlash(res)
}