Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Apr 24, 2024
1 parent 6167d3e commit d88273a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
7 changes: 3 additions & 4 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io/fs"
"net/http"
"net/url"
"path"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -326,16 +325,16 @@ type prefixedDir struct {
}

func (p *prefixedDir) Open(name string) (http.File, error) {
destPath, filename, ok, err := context.SafeFilename(p.prefix, name)
destPath, _, ok, err := context.SafeFilename(p.prefix, name)
if err != nil {
return nil, err
}
if !ok {
return nil, http.ErrMissingFile // unsafe.
}

name = path.Join(destPath, filename)
return p.fs.Open(name)
// name = path.Join(destPath, filename)
return p.fs.Open(destPath)
}

type partyConfiguratorMiddleware struct {
Expand Down
25 changes: 14 additions & 11 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2418,19 +2418,22 @@ func SafeFilename(prefixDir string, name string) (string, string, bool, error) {
return prefixDir, name, false, nil
}

// Join the sanitized input with the destination directory.
destPath := filepath.Join(prefixDir, filename)
var destPath string
if prefixDir != "" {
// Join the sanitized input with the destination directory.
destPath = filepath.Join(prefixDir, filename)

// Get the canonical path of the destination directory.
canonicalDestDir, err := filepath.EvalSymlinks(prefixDir) // the prefix dir should exists.
if err != nil {
return prefixDir, name, false, fmt.Errorf("dest directory: %s: eval symlinks: %w", prefixDir, err)
}
// Get the canonical path of the destination directory.
canonicalDestDir, err := filepath.EvalSymlinks(prefixDir) // the prefix dir should exists.
if err != nil {
return prefixDir, name, false, fmt.Errorf("dest directory: %s: eval symlinks: %w", prefixDir, err)
}

// Check if the destination path is within the destination directory.
if !strings.HasPrefix(destPath, canonicalDestDir) {
// Reject the input as it is a path traversal attempt.
return prefixDir, name, false, nil
// Check if the destination path is within the destination directory.
if !strings.HasPrefix(destPath, canonicalDestDir) {
// Reject the input as it is a path traversal attempt.
return prefixDir, name, false, nil
}
}

return destPath, filename, true, nil
Expand Down
14 changes: 11 additions & 3 deletions context/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ var ResolveHTTPFS = func(fsOrDir interface{}) http.FileSystem {
// FindNames accepts a "http.FileSystem" and a root name and returns
// the list containing its file names.
func FindNames(fileSystem http.FileSystem, name string) ([]string, error) {
f, err := fileSystem.Open(name)
_, filename, ok, err := SafeFilename("", name)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("invalid file name: %s", name)
}

f, err := fileSystem.Open(filename)
if err != nil {
return nil, err
}
Expand All @@ -160,8 +168,8 @@ func FindNames(fileSystem http.FileSystem, name string) ([]string, error) {
// Note:
// go-bindata has absolute names with os.Separator,
// http.Dir the basename.
filename := toBaseName(info.Name())
fullname := path.Join(name, filename)
baseFilename := toBaseName(info.Name())
fullname := path.Join(name, baseFilename)
if fullname == name { // prevent looping through itself.
continue
}
Expand Down

0 comments on commit d88273a

Please sign in to comment.