Skip to content

Commit b7d9906

Browse files
authored
Merge pull request #48 from rminnich/boundOSRoot
boundos:insideBaseDirEval: return true if baseDir is "/"
2 parents cfca659 + 6872990 commit b7d9906

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

osfs/os_bound.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ func (fs *BoundOS) insideBaseDir(filename string) (bool, error) {
246246
// a dir that is within the fs.baseDir, by first evaluating any symlinks
247247
// that either filename or fs.baseDir may contain.
248248
func (fs *BoundOS) insideBaseDirEval(filename string) (bool, error) {
249+
// "/" contains all others.
250+
if fs.baseDir == "/" {
251+
return true, nil
252+
}
249253
dir, err := filepath.EvalSymlinks(filepath.Dir(filename))
250254
if dir == "" || os.IsNotExist(err) {
251255
dir = filepath.Dir(filename)
@@ -255,7 +259,7 @@ func (fs *BoundOS) insideBaseDirEval(filename string) (bool, error) {
255259
wd = fs.baseDir
256260
}
257261
if filename != wd && dir != wd && !strings.HasPrefix(dir, wd+string(filepath.Separator)) {
258-
return false, fmt.Errorf("path outside base dir")
262+
return false, fmt.Errorf("%q: path outside base dir %q: %w", filename, fs.baseDir, os.ErrNotExist)
259263
}
260264
return true, nil
261265
}

osfs/os_bound_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,14 @@ func TestReadDir(t *testing.T) {
11051105
g.Expect(dirs).To(gomega.BeNil())
11061106
}
11071107

1108+
func TestInsideBaseDirEval(t*testing.T) {
1109+
g := gomega.NewWithT(t)
1110+
fs := BoundOS{baseDir: "/"}
1111+
b, err := fs.insideBaseDirEval("a")
1112+
g.Expect(b).To(gomega.BeTrue())
1113+
g.Expect(err).To(gomega.BeNil())
1114+
}
1115+
11081116
func TestMkdirAll(t *testing.T) {
11091117
g := gomega.NewWithT(t)
11101118
root := t.TempDir()

0 commit comments

Comments
 (0)