Skip to content

Commit d88273a

Browse files
committed
minor
1 parent 6167d3e commit d88273a

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

aliases.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"io/fs"
55
"net/http"
66
"net/url"
7-
"path"
87
"regexp"
98
"strings"
109
"time"
@@ -326,16 +325,16 @@ type prefixedDir struct {
326325
}
327326

328327
func (p *prefixedDir) Open(name string) (http.File, error) {
329-
destPath, filename, ok, err := context.SafeFilename(p.prefix, name)
328+
destPath, _, ok, err := context.SafeFilename(p.prefix, name)
330329
if err != nil {
331330
return nil, err
332331
}
333332
if !ok {
334333
return nil, http.ErrMissingFile // unsafe.
335334
}
336335

337-
name = path.Join(destPath, filename)
338-
return p.fs.Open(name)
336+
// name = path.Join(destPath, filename)
337+
return p.fs.Open(destPath)
339338
}
340339

341340
type partyConfiguratorMiddleware struct {

context/context.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,19 +2418,22 @@ func SafeFilename(prefixDir string, name string) (string, string, bool, error) {
24182418
return prefixDir, name, false, nil
24192419
}
24202420

2421-
// Join the sanitized input with the destination directory.
2422-
destPath := filepath.Join(prefixDir, filename)
2421+
var destPath string
2422+
if prefixDir != "" {
2423+
// Join the sanitized input with the destination directory.
2424+
destPath = filepath.Join(prefixDir, filename)
24232425

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

2430-
// Check if the destination path is within the destination directory.
2431-
if !strings.HasPrefix(destPath, canonicalDestDir) {
2432-
// Reject the input as it is a path traversal attempt.
2433-
return prefixDir, name, false, nil
2432+
// Check if the destination path is within the destination directory.
2433+
if !strings.HasPrefix(destPath, canonicalDestDir) {
2434+
// Reject the input as it is a path traversal attempt.
2435+
return prefixDir, name, false, nil
2436+
}
24342437
}
24352438

24362439
return destPath, filename, true, nil

context/fs.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ var ResolveHTTPFS = func(fsOrDir interface{}) http.FileSystem {
134134
// FindNames accepts a "http.FileSystem" and a root name and returns
135135
// the list containing its file names.
136136
func FindNames(fileSystem http.FileSystem, name string) ([]string, error) {
137-
f, err := fileSystem.Open(name)
137+
_, filename, ok, err := SafeFilename("", name)
138+
if err != nil {
139+
return nil, err
140+
}
141+
if !ok {
142+
return nil, fmt.Errorf("invalid file name: %s", name)
143+
}
144+
145+
f, err := fileSystem.Open(filename)
138146
if err != nil {
139147
return nil, err
140148
}
@@ -160,8 +168,8 @@ func FindNames(fileSystem http.FileSystem, name string) ([]string, error) {
160168
// Note:
161169
// go-bindata has absolute names with os.Separator,
162170
// http.Dir the basename.
163-
filename := toBaseName(info.Name())
164-
fullname := path.Join(name, filename)
171+
baseFilename := toBaseName(info.Name())
172+
fullname := path.Join(name, baseFilename)
165173
if fullname == name { // prevent looping through itself.
166174
continue
167175
}

0 commit comments

Comments
 (0)