lib/osutil: Fix TraversesSymlink with symlinked fs root on windows (fixes #4875) (#4886)

This commit is contained in:
Simon Frei
2018-04-17 22:53:06 +02:00
committed by Jakob Borg
parent 17e3608865
commit 1f70f7e37c
2 changed files with 55 additions and 22 deletions
+9 -16
View File
@@ -32,29 +32,22 @@ func (e NotADirectoryError) Error() string {
return fmt.Sprintf("not a directory: %s", e.path)
}
// TraversesSymlink returns an error if base and any path component of name up to and
// including filepath.Join(base, name) traverses a symlink.
// Base and name must both be clean and name must be relative to base.
// TraversesSymlink returns an error if any path component of name (including name
// itself) traverses a symlink.
func TraversesSymlink(filesystem fs.Filesystem, name string) error {
base := "."
path := base
info, err := filesystem.Lstat(path)
var err error
name, err = fs.Canonicalize(name)
if err != nil {
return err
}
if !info.IsDir() {
return &NotADirectoryError{
path: base,
}
}
if name == "." {
// The result of calling TraversesSymlink("some/where", filepath.Dir("foo"))
// The result of calling TraversesSymlink(filesystem, filepath.Dir("foo"))
return nil
}
parts := strings.Split(name, string(fs.PathSeparator))
for _, part := range parts {
var path string
for _, part := range strings.Split(name, string(fs.PathSeparator)) {
path = filepath.Join(path, part)
info, err := filesystem.Lstat(path)
if err != nil {
@@ -65,12 +58,12 @@ func TraversesSymlink(filesystem fs.Filesystem, name string) error {
}
if info.IsSymlink() {
return &TraversesSymlinkError{
path: strings.TrimPrefix(path, base),
path: path,
}
}
if !info.IsDir() {
return &NotADirectoryError{
path: strings.TrimPrefix(path, base),
path: path,
}
}
}