Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
998bd95bfa | ||
|
|
c1abe8f561 | ||
|
|
0bb2768754 | ||
|
|
7b7ff8ae8f | ||
|
|
1086903c14 | ||
|
|
3471ec2c4b |
@@ -2,6 +2,22 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
||||
|
||||
## [2.63.12](https://github.com/filebrowser/filebrowser/compare/v2.63.11...v2.63.12) (2026-06-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* await copy move conflict detection ([#5978](https://github.com/filebrowser/filebrowser/issues/5978)) ([c1abe8f](https://github.com/filebrowser/filebrowser/commit/c1abe8f561208bf36bde70879d1a15ef9de998fa))
|
||||
* keep mobile file sort controls visible ([#5977](https://github.com/filebrowser/filebrowser/issues/5977)) ([0bb2768](https://github.com/filebrowser/filebrowser/commit/0bb2768754d11b865d68e72dcd7cebb232a6308a))
|
||||
* skip inaccessible children when listing directories ([#5958](https://github.com/filebrowser/filebrowser/issues/5958)) ([7b7ff8a](https://github.com/filebrowser/filebrowser/commit/7b7ff8ae8f97393b2e6ae6e061c1f780077c32b6))
|
||||
|
||||
## [2.63.11](https://github.com/filebrowser/filebrowser/compare/v2.63.10...v2.63.11) (2026-06-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* incomplete fix for symlinked directories let scopes users and public-share recipients read and write files outside of scope ([3471ec2](https://github.com/filebrowser/filebrowser/commit/3471ec2c4b6473831c72ee889cb3c1a6849a1fb1))
|
||||
|
||||
## [2.63.10](https://github.com/filebrowser/filebrowser/compare/v2.63.9...v2.63.10) (2026-06-03)
|
||||
|
||||
|
||||
|
||||
+68
-12
@@ -18,6 +18,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -128,22 +129,18 @@ func stat(opts *FileOptions) (*FileInfo, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// regular file
|
||||
if file != nil && !file.IsSymlink {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// The path is a symlink. Refuse to follow it if its on-disk target escapes
|
||||
// the user's scoped root; otherwise a symlink that lives lexically inside
|
||||
// the scope but points outside it would let a restricted user read, write,
|
||||
// or share files beyond their boundary.
|
||||
if file != nil && file.IsSymlink {
|
||||
if file != nil {
|
||||
ok, scopeErr := WithinScope(opts.Fs, opts.Path)
|
||||
if scopeErr != nil || !ok {
|
||||
return nil, os.ErrPermission
|
||||
}
|
||||
}
|
||||
|
||||
// regular file
|
||||
if file != nil && !file.IsSymlink {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// fs doesn't support afero.Lstater interface or the file is a symlink
|
||||
info, err := opts.Fs.Stat(opts.Path)
|
||||
if err != nil {
|
||||
@@ -456,8 +453,7 @@ func (i *FileInfo) addSubtitle(fPath string) {
|
||||
}
|
||||
|
||||
func (i *FileInfo) readListing(checker rules.Checker, readHeader bool, calcImgRes bool) error {
|
||||
afs := &afero.Afero{Fs: i.Fs}
|
||||
dir, err := afs.ReadDir(i.Path)
|
||||
dir, err := readDir(i.Fs, i.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -479,6 +475,13 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool, calcImgRe
|
||||
isSymlink, isInvalidLink := false, false
|
||||
if IsSymlink(f.Mode()) {
|
||||
isSymlink = true
|
||||
// A symlink whose on-disk target escapes the scoped root must not be
|
||||
// followed, otherwise the listing would leak the target's metadata
|
||||
// (and downstream access) for files outside the user's scope or the
|
||||
// shared subtree.
|
||||
if ok, scopeErr := WithinScope(i.Fs, fPath); scopeErr != nil || !ok {
|
||||
continue
|
||||
}
|
||||
// It's a symbolic link. We try to follow it. If it doesn't work,
|
||||
// we stay with the link information instead of the target's.
|
||||
info, err := i.Fs.Stat(fPath)
|
||||
@@ -532,3 +535,56 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool, calcImgRe
|
||||
i.Listing = listing
|
||||
return nil
|
||||
}
|
||||
|
||||
func readDir(afs afero.Fs, dirname string) ([]os.FileInfo, error) {
|
||||
dir, err := afero.ReadDir(afs, dirname)
|
||||
if err == nil {
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
dir, fallbackErr := readDirNames(afs, dirname)
|
||||
if fallbackErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func readDirNames(afs afero.Fs, dirname string) ([]os.FileInfo, error) {
|
||||
file, err := afs.Open(dirname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names, err := file.Readdirnames(-1)
|
||||
if closeErr := file.Close(); err == nil && closeErr != nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sort.Strings(names)
|
||||
dir := make([]os.FileInfo, 0, len(names))
|
||||
for _, name := range names {
|
||||
fPath := path.Join(dirname, name)
|
||||
info, err := lstatIfPossible(afs, fPath)
|
||||
if err != nil {
|
||||
log.Printf("Skipping inaccessible file %s: %v", fPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
dir = append(dir, info)
|
||||
}
|
||||
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func lstatIfPossible(afs afero.Fs, name string) (os.FileInfo, error) {
|
||||
if lstaterFs, ok := afs.(afero.Lstater); ok {
|
||||
info, _, err := lstaterFs.LstatIfPossible(name)
|
||||
return info, err
|
||||
}
|
||||
|
||||
return afs.Stat(name)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package files
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -80,4 +81,142 @@ func TestWithinScope(t *testing.T) {
|
||||
t.Fatal("expected escaping symlink to a sibling directory to be rejected")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("symlink whose target stays within scope is allowed", func(t *testing.T) {
|
||||
scope := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(scope, "real"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(scope, "real", "f.txt"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(scope, "real"), filepath.Join(scope, "link")); err != nil {
|
||||
t.Skipf("cannot create symlink: %v", err)
|
||||
}
|
||||
bfs := afero.NewBasePathFs(afero.NewOsFs(), scope)
|
||||
|
||||
ok, err := WithinScope(bfs, "/link/f.txt")
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("expected (true, nil) for an in-scope symlink target, got (%v, %v)", ok, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// stat must reject a regular file reached through a symlinked ancestor that
|
||||
// escapes the scope (GHSA-hf77-9m7w-fq8q), while still serving in-scope files.
|
||||
func TestStatRejectsLinkedAncestorEscape(t *testing.T) {
|
||||
scope := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(scope, "shared"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(scope, "private"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(scope, "private", "secret.txt"), []byte("secret"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(scope, "shared", "ok.txt"), []byte("ok"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(scope, "private"), filepath.Join(scope, "shared", "link")); err != nil {
|
||||
t.Skipf("cannot create symlink: %v", err)
|
||||
}
|
||||
|
||||
// Filesystem scoped to the shared directory, as a public share would be.
|
||||
bfs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(scope, "shared"))
|
||||
|
||||
if _, err := stat(&FileOptions{Fs: bfs, Path: "/link/secret.txt"}); !os.IsPermission(err) {
|
||||
t.Fatalf("expected permission error for linked-ancestor escape, got %v", err)
|
||||
}
|
||||
if _, err := stat(&FileOptions{Fs: bfs, Path: "/ok.txt"}); err != nil {
|
||||
t.Fatalf("expected in-scope file to be served, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type allowAllChecker struct{}
|
||||
|
||||
func (allowAllChecker) Check(string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type inaccessibleChildFs struct {
|
||||
afero.Fs
|
||||
child string
|
||||
}
|
||||
|
||||
func (fs inaccessibleChildFs) Open(name string) (afero.File, error) {
|
||||
file, err := fs.Fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if path.Clean(name) == "/" {
|
||||
return inaccessibleChildDir{File: file}, nil
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (fs inaccessibleChildFs) Stat(name string) (os.FileInfo, error) {
|
||||
if path.Clean(name) == fs.child {
|
||||
return nil, os.ErrPermission
|
||||
}
|
||||
|
||||
return fs.Fs.Stat(name)
|
||||
}
|
||||
|
||||
func (fs inaccessibleChildFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
|
||||
if path.Clean(name) == fs.child {
|
||||
return nil, false, os.ErrPermission
|
||||
}
|
||||
|
||||
if lstater, ok := fs.Fs.(afero.Lstater); ok {
|
||||
return lstater.LstatIfPossible(name)
|
||||
}
|
||||
|
||||
info, err := fs.Fs.Stat(name)
|
||||
return info, false, err
|
||||
}
|
||||
|
||||
type inaccessibleChildDir struct {
|
||||
afero.File
|
||||
}
|
||||
|
||||
func (dir inaccessibleChildDir) Readdir(int) ([]os.FileInfo, error) {
|
||||
return nil, os.ErrPermission
|
||||
}
|
||||
|
||||
func TestReadListingSkipsInaccessibleChildren(t *testing.T) {
|
||||
memFs := afero.NewMemMapFs()
|
||||
for _, dir := range []string{"/media", "/proton-mount"} {
|
||||
if err := memFs.Mkdir(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
file, err := NewFileInfo(&FileOptions{
|
||||
Fs: inaccessibleChildFs{Fs: memFs, child: "/proton-mount"},
|
||||
Path: "/",
|
||||
Expand: true,
|
||||
Checker: allowAllChecker{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if file.Listing == nil {
|
||||
t.Fatal("expected root listing")
|
||||
}
|
||||
|
||||
if got := len(file.Items); got != 1 {
|
||||
t.Fatalf("expected one accessible child, got %d", got)
|
||||
}
|
||||
|
||||
if got := file.Items[0].Name; got != "media" {
|
||||
t.Fatalf("expected accessible child to be listed, got %q", got)
|
||||
}
|
||||
|
||||
if got := file.NumDirs; got != 1 {
|
||||
t.Fatalf("expected one listed directory, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ export default {
|
||||
to: this.dest + encodeURIComponent(this.req.items[item].name),
|
||||
name: this.req.items[item].name,
|
||||
size: this.req.items[item].size,
|
||||
isDir: this.req.items[item].isDir,
|
||||
modified: this.req.items[item].modified,
|
||||
overwrite: false,
|
||||
rename: this.$route.path === this.dest,
|
||||
@@ -122,7 +123,7 @@ export default {
|
||||
});
|
||||
};
|
||||
|
||||
const conflict = upload.checkConflict(items, this.dest);
|
||||
const conflict = await upload.checkConflict(items, this.dest);
|
||||
|
||||
if (conflict.length > 0) {
|
||||
this.showHover({
|
||||
|
||||
@@ -98,6 +98,7 @@ export default {
|
||||
to: this.dest + encodeURIComponent(this.req.items[item].name),
|
||||
name: this.req.items[item].name,
|
||||
size: this.req.items[item].size,
|
||||
isDir: this.req.items[item].isDir,
|
||||
modified: this.req.items[item].modified,
|
||||
overwrite: false,
|
||||
rename: false,
|
||||
@@ -122,7 +123,7 @@ export default {
|
||||
});
|
||||
};
|
||||
|
||||
const conflict = upload.checkConflict(items, this.dest);
|
||||
const conflict = await upload.checkConflict(items, this.dest);
|
||||
|
||||
if (conflict.length > 0) {
|
||||
this.showHover({
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import CopyPrompt from "@/components/prompts/Copy.vue";
|
||||
import MovePrompt from "@/components/prompts/Move.vue";
|
||||
import { files as api } from "@/api";
|
||||
import { checkConflict } from "@/utils/upload";
|
||||
|
||||
vi.mock("@/api", () => ({
|
||||
files: {
|
||||
copy: vi.fn().mockResolvedValue(undefined),
|
||||
move: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/api/utils", () => ({
|
||||
removePrefix: (value: string) => value.replace(/^\/files/, ""),
|
||||
}));
|
||||
|
||||
vi.mock("@/utils/buttons", () => ({
|
||||
default: {
|
||||
loading: vi.fn(),
|
||||
success: vi.fn(),
|
||||
done: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/utils/upload", () => ({
|
||||
checkConflict: vi.fn(),
|
||||
}));
|
||||
|
||||
const conflict = [
|
||||
{
|
||||
index: 0,
|
||||
name: "/target/file.txt",
|
||||
origin: { size: 12 },
|
||||
dest: { size: 10 },
|
||||
checked: ["origin"],
|
||||
isSmallerOnServer: true,
|
||||
},
|
||||
];
|
||||
|
||||
function makeContext() {
|
||||
return {
|
||||
selected: [0],
|
||||
req: {
|
||||
items: [
|
||||
{
|
||||
url: "/files/source/file.txt",
|
||||
name: "file.txt",
|
||||
size: 12,
|
||||
isDir: false,
|
||||
modified: "2026-06-04T00:00:00Z",
|
||||
},
|
||||
],
|
||||
},
|
||||
dest: "/files/target/",
|
||||
user: { redirectAfterCopyMove: false },
|
||||
$route: { path: "/files/source/" },
|
||||
$router: { push: vi.fn() },
|
||||
reload: false,
|
||||
preselect: "",
|
||||
showHover: vi.fn(),
|
||||
closeHovers: vi.fn(),
|
||||
$showError: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
const event = {
|
||||
preventDefault: vi.fn(),
|
||||
};
|
||||
|
||||
describe("copy and move conflict prompts", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(checkConflict).mockResolvedValue(conflict);
|
||||
});
|
||||
|
||||
it("waits for copy conflict detection before calling the copy API", async () => {
|
||||
const context = makeContext();
|
||||
|
||||
await (CopyPrompt as any).methods.copy.call(context, event);
|
||||
|
||||
expect(checkConflict).toHaveBeenCalledWith(
|
||||
[
|
||||
expect.objectContaining({
|
||||
to: "/files/target/file.txt",
|
||||
isDir: false,
|
||||
}),
|
||||
],
|
||||
"/files/target/"
|
||||
);
|
||||
expect(context.showHover).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
prompt: "resolve-conflict",
|
||||
props: { conflict },
|
||||
})
|
||||
);
|
||||
expect(api.copy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("waits for move conflict detection before calling the move API", async () => {
|
||||
const context = makeContext();
|
||||
|
||||
await (MovePrompt as any).methods.move.call(context, event);
|
||||
|
||||
expect(checkConflict).toHaveBeenCalledWith(
|
||||
[
|
||||
expect.objectContaining({
|
||||
to: "/files/target/file.txt",
|
||||
isDir: false,
|
||||
}),
|
||||
],
|
||||
"/files/target/"
|
||||
);
|
||||
expect(context.showHover).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
prompt: "resolve-conflict",
|
||||
props: expect.objectContaining({
|
||||
conflict,
|
||||
files: expect.any(Array),
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(api.move).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const mobileCss = readFileSync(resolve(__dirname, "../mobile.css"), "utf8");
|
||||
|
||||
const normalizedCss = mobileCss.replace(/\s+/g, " ");
|
||||
|
||||
describe("mobile file listing styles", () => {
|
||||
it("hides file row metadata without hiding list header sort controls", () => {
|
||||
expect(normalizedCss).toContain(
|
||||
"#listing.list .item:not(.header) .size { display: none;"
|
||||
);
|
||||
expect(normalizedCss).toContain(
|
||||
"#listing.list .item:not(.header) .modified { display: none;"
|
||||
);
|
||||
|
||||
expect(normalizedCss).not.toMatch(
|
||||
/#listing\.list \.item \.(size|modified) \{ display: none;/
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -14,12 +14,27 @@
|
||||
body {
|
||||
padding-bottom: 5em;
|
||||
}
|
||||
#listing.list .item .size {
|
||||
#listing.list .item:not(.header) .size {
|
||||
display: none;
|
||||
}
|
||||
#listing.list .item .name {
|
||||
#listing.list .item:not(.header) .name {
|
||||
width: 60%;
|
||||
}
|
||||
#listing.list .item.header .name {
|
||||
margin-right: 0;
|
||||
width: 45%;
|
||||
}
|
||||
#listing.list .item.header .size {
|
||||
width: 25%;
|
||||
}
|
||||
#listing.list .item.header .modified {
|
||||
width: 30%;
|
||||
}
|
||||
#listing.list .item.header p {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
#more {
|
||||
display: inherit;
|
||||
}
|
||||
@@ -162,10 +177,22 @@
|
||||
}
|
||||
|
||||
@media (max-width: 450px) {
|
||||
#listing.list .item .modified {
|
||||
#listing.list .item:not(.header) .modified {
|
||||
display: none;
|
||||
}
|
||||
#listing.list .item .name {
|
||||
#listing.list .item:not(.header) .name {
|
||||
width: 100%;
|
||||
}
|
||||
#listing.list .item.header {
|
||||
padding: 0.75em 0.5em;
|
||||
}
|
||||
#listing.list .item.header .name {
|
||||
width: 34%;
|
||||
}
|
||||
#listing.list .item.header .size {
|
||||
width: 24%;
|
||||
}
|
||||
#listing.list .item.header .modified {
|
||||
width: 42%;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/share"
|
||||
"github.com/filebrowser/filebrowser/v2/storage"
|
||||
"github.com/filebrowser/filebrowser/v2/storage/bolt"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
// symlinkShareStorage builds a storage whose single user is rooted at a real
|
||||
// on-disk scope containing a public share "/shared" with a symlinked
|
||||
// descendant "link -> ../private". Skips the test if symlinks are unavailable.
|
||||
func symlinkShareStorage(t *testing.T) *storage.Storage {
|
||||
t.Helper()
|
||||
scope := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(scope, "shared"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(scope, "private"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(scope, "private", "secret.txt"), []byte("symlink-secret"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(scope, "private"), filepath.Join(scope, "shared", "link")); err != nil {
|
||||
t.Skipf("cannot create symlink on this platform: %v", err)
|
||||
}
|
||||
|
||||
db, err := storm.Open(filepath.Join(t.TempDir(), "db"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open db: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
st, err := bolt.NewStorage(db)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get storage: %v", err)
|
||||
}
|
||||
if err := st.Share.Save(&share.Link{Hash: "h", UserID: 1, Path: "/shared"}); err != nil {
|
||||
t.Fatalf("failed to save share: %v", err)
|
||||
}
|
||||
if err := st.Users.Save(&users.User{
|
||||
Username: "username",
|
||||
Password: "pw",
|
||||
Perm: users.Permissions{Share: true, Download: true},
|
||||
}); err != nil {
|
||||
t.Fatalf("failed to save user: %v", err)
|
||||
}
|
||||
if err := st.Settings.Save(&settings.Settings{Key: []byte("key")}); err != nil {
|
||||
t.Fatalf("failed to save settings: %v", err)
|
||||
}
|
||||
st.Users = &customFSUser{
|
||||
Store: st.Users,
|
||||
fs: afero.NewBasePathFs(afero.NewOsFs(), scope),
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
// Reproduces GHSA-hf77-9m7w-fq8q: a public directory share whose subtree
|
||||
// contains a symlink to a directory outside the share. Requesting a regular
|
||||
// file behind that linked ancestor must NOT disclose its contents.
|
||||
func TestPublicShareSymlinkDescendantDisclosure(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
handler handleFunc
|
||||
path string
|
||||
}{
|
||||
"direct file download via dl handler": {handler: publicDlHandler, path: "h/link/secret.txt"},
|
||||
"share info via share handler": {handler: publicShareHandler, path: "h/link/secret.txt"},
|
||||
"listing of linked dir": {handler: publicShareHandler, path: "h/link/"},
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(name, func(t *testing.T) {
|
||||
st := symlinkShareStorage(t)
|
||||
|
||||
req := newHTTPRequest(t, func(r *http.Request) { r.URL.Path = tc.path })
|
||||
recorder := httptest.NewRecorder()
|
||||
handler := handle(tc.handler, "", st, &settings.Server{})
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
body, _ := io.ReadAll(result.Body)
|
||||
|
||||
t.Logf("status=%d body=%q", result.StatusCode, string(body))
|
||||
if result.StatusCode == http.StatusOK {
|
||||
t.Errorf("VULNERABLE: leaked path outside share (status 200, body=%q)", string(body))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The listing of the public share root must omit the escaping symlink "link"
|
||||
// entirely (no target metadata leak).
|
||||
func TestPublicShareSymlinkListingOmitsEscapingLink(t *testing.T) {
|
||||
st := symlinkShareStorage(t)
|
||||
|
||||
req := newHTTPRequest(t, func(r *http.Request) { r.URL.Path = "h/" })
|
||||
recorder := httptest.NewRecorder()
|
||||
handler := handle(publicShareHandler, "", st, &settings.Server{})
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
body, _ := io.ReadAll(result.Body)
|
||||
if result.StatusCode != http.StatusOK {
|
||||
t.Fatalf("share root listing failed: status=%d body=%q", result.StatusCode, string(body))
|
||||
}
|
||||
if strings.Contains(string(body), "\"link\"") {
|
||||
t.Errorf("VULNERABLE: listing exposes escaping symlink: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
// Reproduces the archive variant of GHSA-hf77-9m7w-fq8q: downloading the whole
|
||||
// public share as a zip must not pull in files reached through a symlinked
|
||||
// descendant.
|
||||
func TestPublicShareSymlinkArchiveDisclosure(t *testing.T) {
|
||||
st := symlinkShareStorage(t)
|
||||
|
||||
// Request the whole share root as an archive.
|
||||
req := newHTTPRequest(t, func(r *http.Request) { r.URL.Path = "h/" })
|
||||
recorder := httptest.NewRecorder()
|
||||
handler := handle(publicDlHandler, "", st, &settings.Server{})
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
body, _ := io.ReadAll(result.Body)
|
||||
if result.StatusCode != http.StatusOK {
|
||||
t.Fatalf("archive request failed: status=%d body=%q", result.StatusCode, string(body))
|
||||
}
|
||||
|
||||
zr, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read zip: %v", err)
|
||||
}
|
||||
for _, f := range zr.File {
|
||||
if strings.Contains(f.Name, "secret.txt") {
|
||||
t.Errorf("VULNERABLE: archive includes file behind symlinked descendant: %q", f.Name)
|
||||
}
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
content, _ := io.ReadAll(rc)
|
||||
rc.Close()
|
||||
if bytes.Contains(content, []byte("symlink-secret")) {
|
||||
t.Errorf("VULNERABLE: archive entry %q leaks out-of-scope content", f.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -220,7 +220,13 @@ func TestPublicShareHandlerRules(t *testing.T) {
|
||||
t.Fatalf("failed to save settings: %v", err)
|
||||
}
|
||||
|
||||
fs := afero.NewMemMapFs()
|
||||
fs := afero.NewBasePathFs(afero.NewOsFs(), t.TempDir())
|
||||
if err := fs.MkdirAll("/projects/private", 0o755); err != nil {
|
||||
t.Fatalf("failed to create private dir: %v", err)
|
||||
}
|
||||
if err := fs.MkdirAll("/projects/public", 0o755); err != nil {
|
||||
t.Fatalf("failed to create public dir: %v", err)
|
||||
}
|
||||
if err := afero.WriteFile(fs, "/projects/private/secret.txt", []byte("top secret"), 0o600); err != nil {
|
||||
t.Fatalf("failed to write secret file: %v", err)
|
||||
}
|
||||
|
||||
@@ -115,6 +115,10 @@ func getFiles(d *data, path, commonPath string) ([]archives.FileInfo, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if ok, err := files.WithinScope(d.user.Fs, path); err != nil || !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
info, err := d.user.Fs.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -44,6 +44,11 @@ func tusPostHandler(cache UploadCache) handleFunc {
|
||||
if !d.user.Perm.Create || !d.Check(r.URL.Path) {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
if ok, scopeErr := files.WithinScope(d.user.Fs, r.URL.Path); scopeErr != nil || !ok {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
file, err := files.NewFileInfo(&files.FileOptions{
|
||||
Fs: d.user.Fs,
|
||||
Path: r.URL.Path,
|
||||
@@ -161,6 +166,11 @@ func tusPatchHandler(cache UploadCache) handleFunc {
|
||||
if r.Header.Get("Content-Type") != "application/offset+octet-stream" {
|
||||
return http.StatusUnsupportedMediaType, nil
|
||||
}
|
||||
// Defense in depth: refuse to write through a symlink that escapes the
|
||||
// scope, in case the target path is (or sits behind) an escaping link.
|
||||
if ok, scopeErr := files.WithinScope(d.user.Fs, r.URL.Path); scopeErr != nil || !ok {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
||||
uploadOffset, err := getUploadOffset(r)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/storage/bolt"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
// Reproduces the TUS write vector of GHSA-v9g6-9pp4-3w22: a scoped user must
|
||||
// not be able to create or write files through a symlinked directory that
|
||||
// escapes their scope.
|
||||
func TestTusHandlersRejectSymlinkScopeEscape(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
userScope := filepath.Join(root, "user")
|
||||
outside := filepath.Join(root, "otheruser")
|
||||
for _, d := range []string{userScope, outside} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// A directory symlink inside the user's scope pointing outside it.
|
||||
if err := os.Symlink(outside, filepath.Join(userScope, "escape_link")); err != nil {
|
||||
t.Skipf("cannot create symlink: %v", err)
|
||||
}
|
||||
|
||||
key := []byte("test-signing-key")
|
||||
|
||||
db, err := storm.Open(filepath.Join(t.TempDir(), "db"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open db: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
st, err := bolt.NewStorage(db)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get storage: %v", err)
|
||||
}
|
||||
if err := st.Users.Save(&users.User{
|
||||
Username: "u",
|
||||
Password: "pw",
|
||||
Perm: users.Permissions{Create: true, Modify: true},
|
||||
}); err != nil {
|
||||
t.Fatalf("failed to save user: %v", err)
|
||||
}
|
||||
if err := st.Settings.Save(&settings.Settings{Key: key}); err != nil {
|
||||
t.Fatalf("failed to save settings: %v", err)
|
||||
}
|
||||
st.Users = &customFSUser{
|
||||
Store: st.Users,
|
||||
fs: afero.NewBasePathFs(afero.NewOsFs(), userScope),
|
||||
}
|
||||
|
||||
// Forge a valid auth token for user ID 1.
|
||||
claims := &authToken{
|
||||
User: userInfo{ID: 1, Username: "u", Perm: users.Permissions{Create: true, Modify: true}},
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
IssuedAt: jwt.NewNumericDate(time.Now().Add(-time.Minute)),
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
|
||||
},
|
||||
}
|
||||
signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign token: %v", err)
|
||||
}
|
||||
|
||||
cases := map[string]struct {
|
||||
method string
|
||||
handler handleFunc
|
||||
headers map[string]string
|
||||
}{
|
||||
"POST create through symlinked dir": {
|
||||
method: http.MethodPost,
|
||||
handler: tusPostHandler(newMemoryUploadCache()),
|
||||
headers: map[string]string{"Upload-Length": "20"},
|
||||
},
|
||||
"PATCH write through symlinked dir": {
|
||||
method: http.MethodPatch,
|
||||
handler: tusPatchHandler(newMemoryUploadCache()),
|
||||
headers: map[string]string{"Content-Type": "application/offset+octet-stream", "Upload-Offset": "0"},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(name, func(t *testing.T) {
|
||||
req, err := http.NewRequest(tc.method, "escape_link/injected.txt", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("X-Auth", signed)
|
||||
for k, v := range tc.headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
handler := handle(tc.handler, "", st, &settings.Server{})
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Errorf("expected 403, got %d", recorder.Code)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(outside, "injected.txt")); statErr == nil {
|
||||
t.Errorf("VULNERABLE: file was created outside the user's scope")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user