This commit is contained in:
Shreyas Goenka 2023-06-19 18:26:15 +02:00
parent eb4710f3a9
commit 32786e009a
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 8 additions and 10 deletions

View File

@ -11,19 +11,17 @@ type WindowsRootPath struct {
} }
func NewWindowsRootPath(name string) WindowsRootPath { func NewWindowsRootPath(name string) WindowsRootPath {
// Windows file systems do not have a "root" directory. Instead paths require
// a Volume/Drive letter specified. If a user of this struct specifies "/" then
// we treat it as the "root" and skip any validation
if name == "/" {
return WindowsRootPath{""}
}
return WindowsRootPath{filepath.Clean(name)} return WindowsRootPath{filepath.Clean(name)}
} }
// Join returns the specified path name joined to the root.
// It returns an error if the resulting path is not a strict child of the root path.
func (p WindowsRootPath) Join(name string) (string, error) { func (p WindowsRootPath) Join(name string) (string, error) {
// Windows file systems do not have a "root" directory. Instead paths require
// a Volume/Drive letter specified. If a user of this struct specifies "/" then
// we treat it as the "root" and skip any validation
if p.rootPath == "/" {
return name, nil
}
absPath := filepath.Join(p.rootPath, name) absPath := filepath.Join(p.rootPath, name)
// Don't allow escaping the specified root using relative paths. // Don't allow escaping the specified root using relative paths.

View File

@ -15,7 +15,7 @@ func TestWindowsRootPathForRoot(t *testing.T) {
rp := NewWindowsRootPath("/") rp := NewWindowsRootPath("/")
// Assert root value returned // Assert root value returned
assert.Equal(t, "", rp.Root()) assert.Equal(t, "/", rp.Root())
// case: absolute windows path // case: absolute windows path
path, err := rp.Join(`c:\a\b`) path, err := rp.Join(`c:\a\b`)