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

allow absolute paths in BasePathFs when prefix is "" on windows #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ func NewBasePathFs(source Fs, path string) Fs {
// on a file outside the base path it returns the given file name and an error,
// else the given file with the base path prepended
func (b *BasePathFs) RealPath(name string) (path string, err error) {
if err := validateBasePathName(name); err != nil {
if err := validateBasePathName(name, b.path); err != nil {
return name, err
}

bpath := filepath.Clean(b.path)
bpath := b.path
if bpath != "" {
bpath = filepath.Clean(b.path)
}
path = filepath.Clean(filepath.Join(bpath, name))
if !strings.HasPrefix(path, bpath) {
return name, os.ErrNotExist
Expand All @@ -64,7 +67,7 @@ func (b *BasePathFs) RealPath(name string) (path string, err error) {
return path, nil
}

func validateBasePathName(name string) error {
func validateBasePathName(name string, base string) error {
if runtime.GOOS != "windows" {
// Not much to do here;
// the virtual file paths all look absolute on *nix.
Expand All @@ -73,7 +76,7 @@ func validateBasePathName(name string) error {

// On Windows a common mistake would be to provide an absolute OS path
// We could strip out the base part, but that would not be very portable.
if filepath.IsAbs(name) {
if filepath.IsAbs(name) && base != "" {
return os.ErrNotExist
}

Expand Down