Files
filebrowser/http/public_landing_test.go
T

98 lines
3.4 KiB
Go

package fbhttp
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/asdine/storm/v3"
"github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/share"
"github.com/filebrowser/filebrowser/v2/storage/bolt"
"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/afero"
)
func TestLandingSharePaths(t *testing.T) {
for _, p := range []string{"h/../secret", "h/a/../../secret", "h/%2e%2e/secret", "h/%252e/secret", "h/..\\secret", "h/C:/secret", "h/\x00"} {
if validSharePath(p) {
t.Errorf("accepted unsafe path %q", p)
}
}
for _, p := range []string{"h", "h/", "h/a/b.png", "h/a/../b.png", "h/фото.png"} {
if !validSharePath(p) {
t.Errorf("rejected safe path %q", p)
}
}
}
func TestLandingShellIsolation(t *testing.T) {
w := httptest.NewRecorder()
publicLandingShell(w, httptest.NewRequest("GET", "/api/public/landing-shell", nil))
csp := w.Header().Get("Content-Security-Policy")
for _, rule := range []string{"sandbox allow-scripts;", "default-src 'none'", "connect-src 'none'", "frame-src 'none'", "form-action 'none'", "frame-ancestors 'self'"} {
if !strings.Contains(csp, rule) {
t.Errorf("missing %q in CSP %q", rule, csp)
}
}
if strings.Contains(csp, "allow-same-origin") {
t.Fatal("shell can access session origin")
}
if strings.Contains(w.Body.String(), "token") {
t.Fatal("shell contains credentials")
}
}
func TestPublicLandingAttachmentBoundaries(t *testing.T) {
scope := t.TempDir()
if err := os.MkdirAll(filepath.Join(scope, "site", "images"), 0o755); err != nil {
t.Fatal(err)
}
for name, body := range map[string]string{"site/page.md": "# Landing", "site/images/a.svg": "<svg/>", "single.md": "# Single", "secret.txt": "private"} {
if err := os.WriteFile(filepath.Join(scope, filepath.FromSlash(name)), []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
db, err := storm.Open(filepath.Join(t.TempDir(), "db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
st, err := bolt.NewStorage(db)
if err != nil {
t.Fatal(err)
}
if err := st.Users.Save(&users.User{Username: "owner", Password: "unused", Perm: users.Permissions{Share: true, Download: true}}); err != nil {
t.Fatal(err)
}
if err := st.Settings.Save(&settings.Settings{Key: []byte("key")}); err != nil {
t.Fatal(err)
}
st.Users = &customFSUser{Store: st.Users, fs: files.NewScopedFs(afero.NewOsFs(), scope)}
for _, link := range []*share.Link{{Hash: "folder", UserID: 1, Path: "/site"}, {Hash: "single", UserID: 1, Path: "/single.md"}} {
if err := st.Share.Save(link); err != nil {
t.Fatal(err)
}
}
for _, tc := range []struct {
path string
status int
}{
{"folder/page.md", 200}, {"folder/images/a.svg", 200}, {"folder/../secret.txt", 400},
{"folder/%2e%2e/secret.txt", 400}, {"folder/%252e%252e/secret.txt", 400}, {"folder/..\\secret.txt", 400},
{"single/", 200}, {"single/single.md", 200}, {"single/secret.txt", 404},
} {
for _, handler := range []handleFunc{publicDlHandler, publicShareHandler} {
req := newHTTPRequest(t, func(r *http.Request) { r.URL.Path = tc.path })
recorder := httptest.NewRecorder()
handle(handler, "", st, &settings.Server{}).ServeHTTP(recorder, req)
if recorder.Code != tc.status {
t.Errorf("%s: got %d, want %d: %s", tc.path, recorder.Code, tc.status, recorder.Body.String())
}
}
}
}