fix: restore symlink behavior as opt-in followExternalSymlinks

This commit is contained in:
Henrique Dias committed 2026-06-23 13:19:51 +02:00
1 parent 64511ce45e
commit a1063925e1
23 files changed
+384 -93

No files matched your search

+1 -1
View File
@@ -102,7 +102,7 @@ func withUser(fn handleFunc) handleFunc {
w.Header().Add("X-Renew-Token", "true")
}
d.user, err = d.store.Users.Get(d.server.Root, tk.User.ID)
d.user, err = d.store.Users.Get(d.server.Root, d.server.FollowExternalSymlinks, tk.User.ID)
if err != nil {
return http.StatusInternalServerError, err
}
+7 -6
View File
@@ -27,7 +27,7 @@ var withHashFile = func(fn handleFunc) handleFunc {
return status, err
}
user, err := d.store.Users.Get(d.server.Root, link.UserID)
user, err := d.store.Users.Get(d.server.Root, d.server.FollowExternalSymlinks, link.UserID)
if err != nil {
return errToStatus(err), err
}
@@ -63,11 +63,12 @@ var withHashFile = func(fn handleFunc) handleFunc {
filePath = ifPath
}
// set fs root to the shared file/folder. ScopedFs (not a bare
// BasePathFs) so the share is also symlink-confined: a link inside the
// shared subtree that points elsewhere in the owner's scope — outside
// the share — must not be followed.
d.user.Fs = files.NewScopedFs(d.user.Fs, basePath)
// set fs root to the shared file/folder. Unless external symlinks are
// explicitly allowed, this is a ScopedFs (not a bare BasePathFs) so the
// share is also symlink-confined: a link inside the shared subtree that
// points elsewhere in the owner's scope — outside the share — must not be
// followed.
d.user.Fs = files.NewFs(d.user.Fs, basePath, d.server.FollowExternalSymlinks)
// the filesystem is now rebased onto basePath, so paths handed to the
// rule checker are relative to it. Resolve them back to the user's
+80
View File
@@ -126,6 +126,86 @@ func TestPublicShareSymlinkListingOmitsEscapingLink(t *testing.T) {
}
}
// With Server.FollowExternalSymlinks enabled (the opt-in for issue #5998), a
// symlink inside a public share that points outside it is followed: the file
// behind it downloads and the link shows up in the share listing. This is the
// inverse of TestPublicShareSymlinkDescendantDisclosure / ...ListingOmitsEscapingLink.
func TestPublicShareSymlinkFollowedWhenEnabled(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, "outside"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(scope, "outside", "data.txt"), []byte("payload"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(scope, "outside"), 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)
}
// Follow-external mode: the user filesystem is a bare BasePathFs.
st.Users = &customFSUser{
Store: st.Users,
fs: files.NewFs(afero.NewOsFs(), scope, true),
followExternal: true,
}
srv := &settings.Server{FollowExternalSymlinks: true}
// The file behind the symlink downloads.
req := newHTTPRequest(t, func(r *http.Request) { r.URL.Path = "h/link/data.txt" })
recorder := httptest.NewRecorder()
handle(publicDlHandler, "", st, srv).ServeHTTP(recorder, req)
result := recorder.Result()
defer result.Body.Close()
body, _ := io.ReadAll(result.Body)
if result.StatusCode != http.StatusOK {
t.Fatalf("expected to download file behind followed symlink, got status=%d body=%q", result.StatusCode, string(body))
}
if !strings.Contains(string(body), "payload") {
t.Fatalf("expected payload content, got %q", string(body))
}
// The link shows up in the share root listing.
req = newHTTPRequest(t, func(r *http.Request) { r.URL.Path = "h/" })
recorder = httptest.NewRecorder()
handle(publicShareHandler, "", st, srv).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.Fatalf("expected followed symlink to appear in listing: %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.
+13 -5
View File
@@ -269,16 +269,24 @@ func newHTTPRequest(t *testing.T, requestModifiers ...func(*http.Request)) *http
type customFSUser struct {
users.Store
fs afero.Fs
// followExternal mirrors Server.FollowExternalSymlinks: when set, the
// provided fs is used as-is (a bare BasePathFs that follows symlinks);
// otherwise it is wrapped in a symlink-confining ScopedFs.
followExternal bool
}
func (cu *customFSUser) Get(baseScope string, id interface{}) (*users.User, error) {
user, err := cu.Store.Get(baseScope, id)
func (cu *customFSUser) Get(baseScope string, followExternalSymlinks bool, id interface{}) (*users.User, error) {
user, err := cu.Store.Get(baseScope, followExternalSymlinks, id)
if err != nil {
return nil, err
}
// Mirror production (users.User init), where a user's filesystem is always a
// scoped, symlink-confining ScopedFs rather than a bare afero.Fs.
user.Fs = files.NewScopedFs(cu.fs, "/")
// Inject a filesystem rooted at the test's temp scope, standing in for the
// one users.User.Clean would build in production.
if cu.followExternal {
user.Fs = cu.fs
} else {
user.Fs = files.NewScopedFs(cu.fs, "/")
}
return user, nil
}
+3 -3
View File
@@ -71,7 +71,7 @@ func withSelfOrAdmin(fn handleFunc) handleFunc {
}
var usersGetHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
users, err := d.store.Users.Gets(d.server.Root)
users, err := d.store.Users.Gets(d.server.Root, d.server.FollowExternalSymlinks)
if err != nil {
return http.StatusInternalServerError, err
}
@@ -88,7 +88,7 @@ var usersGetHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *
})
var userGetHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
u, err := d.store.Users.Get(d.server.Root, d.raw.(uint))
u, err := d.store.Users.Get(d.server.Root, d.server.FollowExternalSymlinks, d.raw.(uint))
if errors.Is(err, fberrors.ErrNotExist) {
return http.StatusNotFound, err
}
@@ -228,7 +228,7 @@ var userPutHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request
}
} else {
var suser *users.User
suser, err = d.store.Users.Get(d.server.Root, d.raw.(uint))
suser, err = d.store.Users.Get(d.server.Root, d.server.FollowExternalSymlinks, d.raw.(uint))
if err != nil {
return http.StatusInternalServerError, err
}