Skip to content

Commit f59f682

Browse files
authored
fix(sandbox): serve files in subdirectories under -sandbox
1 parent 4624642 commit f59f682

2 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎pkg/httpserver/sandboxfs.go‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net/http"
66
"path/filepath"
7+
"strings"
78
)
89

910
// SandboxFileSystem implements superbasic security checks
@@ -38,13 +39,20 @@ func (sbfs SandboxFileSystem) Open(path string) (http.File, error) {
3839
}
3940

4041
// check if the path is within the configured folder
41-
if sbfs.RootFolder != abspath {
42-
pattern := sbfs.RootFolder + string(filepath.Separator) + "*"
43-
matched, err := filepath.Match(pattern, abspath)
44-
if err != nil {
45-
return nil, err
46-
} else if !matched {
47-
return nil, errors.New("invalid file")
42+
rootAbs, err := filepath.Abs(sbfs.RootFolder)
43+
if err != nil {
44+
return nil, err
45+
}
46+
rel, err := filepath.Rel(rootAbs, abspath)
47+
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
48+
return nil, errors.New("invalid file")
49+
}
50+
// rejects paths through hidden folders like .git/config
51+
if rel != "." {
52+
for _, part := range strings.Split(rel, string(filepath.Separator)) {
53+
if strings.HasPrefix(part, ".") {
54+
return nil, errors.New("invalid file")
55+
}
4856
}
4957
}
5058

‎pkg/httpserver/sandboxfs_test.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package httpserver
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
// The sandbox must serve files in subdirectories; only paths escaping the
11+
// root folder, dotfiles and symlinks may be rejected.
12+
func TestSandboxFileSystemNestedPaths(t *testing.T) {
13+
root, err := filepath.EvalSymlinks(t.TempDir())
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
if err := os.MkdirAll(filepath.Join(root, "sub", "dir"), 0755); err != nil {
18+
t.Fatal(err)
19+
}
20+
if err := os.WriteFile(filepath.Join(root, "sub", "dir", "file.txt"), []byte("ok"), 0644); err != nil {
21+
t.Fatal(err)
22+
}
23+
if err := os.WriteFile(filepath.Join(root, "top.txt"), []byte("ok"), 0644); err != nil {
24+
t.Fatal(err)
25+
}
26+
if err := os.MkdirAll(filepath.Join(root, ".hidden", "dir"), 0755); err != nil {
27+
t.Fatal(err)
28+
}
29+
if err := os.WriteFile(filepath.Join(root, ".hidden", "file.txt"), []byte("secret"), 0644); err != nil {
30+
t.Fatal(err)
31+
}
32+
if err := os.WriteFile(filepath.Join(root, ".hidden", "dir", "file.txt"), []byte("secret"), 0644); err != nil {
33+
t.Fatal(err)
34+
}
35+
if err := os.MkdirAll(filepath.Join(root, "sub", ".hidden"), 0755); err != nil {
36+
t.Fatal(err)
37+
}
38+
if err := os.WriteFile(filepath.Join(root, "sub", ".hidden", "file.txt"), []byte("secret"), 0644); err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
sbfs := SandboxFileSystem{fs: http.Dir(root), RootFolder: root}
43+
44+
for _, path := range []string{"/top.txt", "/sub", "/sub/dir", "/sub/dir/file.txt"} {
45+
f, err := sbfs.Open(path)
46+
if err != nil {
47+
t.Errorf("Open(%q) must succeed inside the root folder: %s", path, err)
48+
continue
49+
}
50+
_ = f.Close()
51+
}
52+
53+
for _, path := range []string{"/../outside.txt", "/../../etc/passwd"} {
54+
if f, err := sbfs.Open(path); err == nil {
55+
_ = f.Close()
56+
t.Errorf("Open(%q) must be rejected outside the root folder", path)
57+
}
58+
}
59+
60+
for _, path := range []string{"/.hidden", "/.hidden/file.txt", "/.hidden/dir/file.txt", "/sub/.hidden/file.txt"} {
61+
if f, err := sbfs.Open(path); err == nil {
62+
_ = f.Close()
63+
t.Errorf("Open(%q) must be rejected inside a hidden folder", path)
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)