Skip to content

Commit

Permalink
Merge pull request #48 from rminnich/boundOSRoot
Browse files Browse the repository at this point in the history
boundos:insideBaseDirEval: return true if baseDir is "/"
  • Loading branch information
pjbgf committed Apr 28, 2024
2 parents cfca659 + 6872990 commit b7d9906
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion osfs/os_bound.go
Expand Up @@ -246,6 +246,10 @@ func (fs *BoundOS) insideBaseDir(filename string) (bool, error) {
// a dir that is within the fs.baseDir, by first evaluating any symlinks
// that either filename or fs.baseDir may contain.
func (fs *BoundOS) insideBaseDirEval(filename string) (bool, error) {
// "/" contains all others.
if fs.baseDir == "/" {
return true, nil
}
dir, err := filepath.EvalSymlinks(filepath.Dir(filename))
if dir == "" || os.IsNotExist(err) {
dir = filepath.Dir(filename)
Expand All @@ -255,7 +259,7 @@ func (fs *BoundOS) insideBaseDirEval(filename string) (bool, error) {
wd = fs.baseDir
}
if filename != wd && dir != wd && !strings.HasPrefix(dir, wd+string(filepath.Separator)) {
return false, fmt.Errorf("path outside base dir")
return false, fmt.Errorf("%q: path outside base dir %q: %w", filename, fs.baseDir, os.ErrNotExist)
}
return true, nil
}
8 changes: 8 additions & 0 deletions osfs/os_bound_test.go
Expand Up @@ -1105,6 +1105,14 @@ func TestReadDir(t *testing.T) {
g.Expect(dirs).To(gomega.BeNil())
}

func TestInsideBaseDirEval(t*testing.T) {
g := gomega.NewWithT(t)
fs := BoundOS{baseDir: "/"}
b, err := fs.insideBaseDirEval("a")
g.Expect(b).To(gomega.BeTrue())
g.Expect(err).To(gomega.BeNil())
}

func TestMkdirAll(t *testing.T) {
g := gomega.NewWithT(t)
root := t.TempDir()
Expand Down

0 comments on commit b7d9906

Please sign in to comment.