mirror of
https://github.com/golang/go
synced 2024-11-06 06:16:12 -07:00
936084890a
The existing implementation of NameSpace implicitly assumes that a FileSystem with a directory at the top will be mounted at the root mount point "/" of the NameSpace. If this is not the case, then Stat("/") will fail even if ReadDir("/") succeedes. This is unexpected behavior which breaks directory traversal routines (eg. http.FileServer). This CL adds an unexported implementation of FileSystem called emptyVFS that emulates an empty directory and adds a NewNameSpace() function that binds emptyVFS to "/" so that unexpected behavior does not arise even if the use does not mount anything explicitly at "/". Latest patch set causes the FileInfo of the empty top level emulated directory to return "/" for Name() and Zero Time for ModTime() and removes the related struct state fields being used in the previous implementation. Fixes golang/go#14190 Change-Id: Idce2fc3c9b81206847a33840d76b660059d53d18 Reviewed-on: https://go-review.googlesource.com/19445 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package vfs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// NewNameSpace returns a NameSpace pre-initialized with an empty
|
|
// emulated directory mounted on the root mount point "/". This
|
|
// allows directory traversal routines to work properly even if
|
|
// a folder is not explicitly mounted at root by the user.
|
|
func NewNameSpace() NameSpace {
|
|
ns := NameSpace{}
|
|
ns.Bind("/", &emptyVFS{}, "/", BindReplace)
|
|
return ns
|
|
}
|
|
|
|
// type emptyVFS emulates a FileSystem consisting of an empty directory
|
|
type emptyVFS struct{}
|
|
|
|
// Open implements Opener. Since emptyVFS is an empty directory, all
|
|
// attempts to open a file should returns errors.
|
|
func (e *emptyVFS) Open(path string) (ReadSeekCloser, error) {
|
|
if path == "/" {
|
|
return nil, fmt.Errorf("open: / is a directory")
|
|
}
|
|
return nil, os.ErrNotExist
|
|
}
|
|
|
|
// Stat returns os.FileInfo for an empty directory if the path is
|
|
// is root "/" or error. os.FileInfo is implemented by emptyVFS
|
|
func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
|
|
if path == "/" {
|
|
return e, nil
|
|
}
|
|
return nil, os.ErrNotExist
|
|
}
|
|
|
|
func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
|
|
return e.Stat(path)
|
|
}
|
|
|
|
// ReadDir returns an empty os.FileInfo slice for "/", else error.
|
|
func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
|
|
if path == "/" {
|
|
return []os.FileInfo{}, nil
|
|
}
|
|
return nil, os.ErrNotExist
|
|
}
|
|
|
|
func (e *emptyVFS) String() string {
|
|
return "emptyVFS(/)"
|
|
}
|
|
|
|
// These functions below implement os.FileInfo for the single
|
|
// empty emulated directory.
|
|
|
|
func (e *emptyVFS) Name() string {
|
|
return "/"
|
|
}
|
|
|
|
func (e *emptyVFS) Size() int64 {
|
|
return 0
|
|
}
|
|
|
|
func (e *emptyVFS) Mode() os.FileMode {
|
|
return os.ModeDir | os.ModePerm
|
|
}
|
|
|
|
func (e *emptyVFS) ModTime() time.Time {
|
|
return time.Time{}
|
|
}
|
|
|
|
func (e *emptyVFS) IsDir() bool {
|
|
return true
|
|
}
|
|
|
|
func (e *emptyVFS) Sys() interface{} {
|
|
return nil
|
|
}
|