Skip to content

Commit 6872990

Browse files
committed
boundos:insideBaseDirEval: return true if baseDir is "/"
insideBaseDirEval would fail if baseDir was "/". Since "/" contains anything, and can never be a symlink, just return true, nil if baseDir is "/". Add a simple test for this case. Also, while we are at it, have the returned error be a lot more informative: show the name and the base diectory; further, have it wrap os.ErrNotExist should anyone wish to use errors.Is at some point. Signed-off-by: Ronald G Minnich <[email protected]>
1 parent 72e8966 commit 6872990

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)