fix(users): make the provisioned scope check atomic with the save
The check that stops two usernames normalizing to the same home directory ran as a storage operation separate from the save, so two first-time users provisioned concurrently could both observe a free scope and both be saved into it, ending up sharing one home directory. Move the check into users.Storage.SaveProvisioned, which holds a single lock across the lookup and the save, and reduce CreateUserHome to deriving and creating the directory. This covers all three provisioning paths: signup, proxy auth and hook auth. Refs GHSA-j7jh-37pf-mf8h
This commit is contained in:
7 files changed
+83
-68
No files matched your search
+12
-41
@@ -1,73 +1,44 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
fberrors "github.com/filebrowser/filebrowser/v2/errors"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
// stubStore is a minimal users.Store used to exercise CreateUserHome.
|
||||
type stubStore struct {
|
||||
byScope map[string]*users.User
|
||||
}
|
||||
|
||||
func (s *stubStore) Get(_ string, _ bool, _ interface{}) (*users.User, error) {
|
||||
return nil, fberrors.ErrNotExist
|
||||
}
|
||||
|
||||
func (s *stubStore) GetByScope(scope string) (*users.User, error) {
|
||||
if u, ok := s.byScope[scope]; ok {
|
||||
return u, nil
|
||||
}
|
||||
return nil, fberrors.ErrNotExist
|
||||
}
|
||||
|
||||
func (s *stubStore) Gets(_ string, _ bool) ([]*users.User, error) { return nil, nil }
|
||||
func (s *stubStore) Update(_ *users.User, _ ...string) error { return nil }
|
||||
func (s *stubStore) Save(_ *users.User) error { return nil }
|
||||
func (s *stubStore) Delete(_ interface{}) error { return nil }
|
||||
func (s *stubStore) LastUpdate(_ uint) int64 { return 0 }
|
||||
|
||||
// A user provisioned with CreateUserDir must receive a per-user home directory
|
||||
// derived from its username, not the default scope which normalizes to the
|
||||
// server root.
|
||||
func TestCreateUserHomeDerivesPerUserScope(t *testing.T) {
|
||||
s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"}
|
||||
store := &stubStore{byScope: map[string]*users.User{}}
|
||||
|
||||
user := &users.User{Username: "alice", Scope: "."}
|
||||
if err := s.CreateUserHome(user, store, t.TempDir(), false); err != nil {
|
||||
derived, err := s.CreateUserHome(user, t.TempDir(), false)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !derived {
|
||||
t.Error("expected the scope to be reported as derived")
|
||||
}
|
||||
if user.Scope != "/users/alice" {
|
||||
t.Errorf("expected derived scope /users/alice, got %q", user.Scope)
|
||||
}
|
||||
}
|
||||
|
||||
// When the derived scope is already owned by another user, provisioning must be
|
||||
// rejected so distinct usernames cannot silently share one home directory.
|
||||
func TestCreateUserHomeRejectsCollision(t *testing.T) {
|
||||
s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"}
|
||||
store := &stubStore{byScope: map[string]*users.User{"/users/alice": {Username: "alice"}}}
|
||||
|
||||
user := &users.User{Username: "alice", Scope: "."}
|
||||
if err := s.CreateUserHome(user, store, t.TempDir(), false); !errors.Is(err, fberrors.ErrExist) {
|
||||
t.Fatalf("expected ErrExist on scope collision, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A scope explicitly supplied by the caller (e.g. returned by an auth hook) must
|
||||
// be preserved instead of being replaced by a derived home directory.
|
||||
// be preserved instead of being replaced by a derived home directory, and must
|
||||
// not be reported as derived: it is legitimate for several users to share it.
|
||||
func TestCreateUserHomePreservesExplicitScope(t *testing.T) {
|
||||
s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"}
|
||||
store := &stubStore{byScope: map[string]*users.User{"/users/alice": {Username: "alice"}}}
|
||||
|
||||
user := &users.User{Username: "alice", Scope: "/custom"}
|
||||
if err := s.CreateUserHome(user, store, t.TempDir(), true); err != nil {
|
||||
derived, err := s.CreateUserHome(user, t.TempDir(), true)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if derived {
|
||||
t.Error("an explicit scope must not be reported as derived")
|
||||
}
|
||||
if user.Scope != "/custom" {
|
||||
t.Errorf("explicit scope should be preserved, got %q", user.Scope)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user