Stop an owner deleting the account they are signed in as
Deleting your own user succeeded, and logged you out doing it: local_sessions.user_id is ON DELETE CASCADE, so the delete took the caller's own live session with it. Nothing refused this. The last-owner guard is the only thing in the path, and it passes cleanly as soon as a second owner exists -- which is exactly the state you are in just after creating one. The way back in was then whatever other account happened to exist, and -seed-admin could not help: it skipped whenever *any* local user was present, so the command documented as the way to create an administrator refused precisely when there was no usable one, because some other account still existed. It now asks whether the admin account itself is missing, which is what its own help text always claimed, and what makes it useful as recovery rather than only as first-run bootstrap. TestCanDeleteAnOwnerWhenAnotherRemains signed in as admin1 and deleted admin1, asserting 204 -- it encoded the lockout as intended behaviour. It now deletes the other owner, which is what it meant to cover, and a new test holds the refusal in place. runSeedAdmin takes a small interface so the bootstrap path is tested without a Postgres pool; it had no tests before. Signed-off-by: John Coffey <[email protected]>
This commit is contained in:
@@ -129,10 +129,6 @@ func (f *fakeStore) SetDisplayTimezone(_ context.Context, userID, tz string) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CountLocalUsers(_ context.Context) (int, error) {
|
||||
return len(f.users), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CountUsersWithRole(_ context.Context, role authz.Role) (int, error) {
|
||||
n := 0
|
||||
for _, u := range f.users {
|
||||
|
||||
@@ -329,6 +329,18 @@ func (h *Handler) handleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
// owner-only -- and anyone at all deleting the last remaining owner.
|
||||
func (h *Handler) handleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
// Deleting yourself is refused before anything else, including the
|
||||
// last-owner check below -- local_sessions.user_id is ON DELETE
|
||||
// CASCADE, so a successful self-delete destroys the caller's own
|
||||
// live session as a side effect, logging them out mid-request with
|
||||
// no warning. With a second owner present the last-owner guard
|
||||
// passes cleanly, so nothing else here would have stopped it, and
|
||||
// the way back in is whatever other account happens to exist. Same
|
||||
// posture as handleResetPassword's self-target refusal above.
|
||||
if identity, ok := authz.IdentityFromContext(r.Context()); ok && id == identity.UserID {
|
||||
writeError(w, http.StatusConflict, "cannot delete the account you are signed in as")
|
||||
return
|
||||
}
|
||||
target, err := h.store.GetUserByID(r.Context(), id)
|
||||
if err != nil {
|
||||
h.writeStoreErr(w, err, "deleting user")
|
||||
|
||||
@@ -543,16 +543,49 @@ func TestCannotDeleteTheLastOwner(t *testing.T) {
|
||||
|
||||
func TestCanDeleteAnOwnerWhenAnotherRemains(t *testing.T) {
|
||||
fs := newFakeStore()
|
||||
owner1 := mustCreateUser(t, fs, "admin1", "adminpass1", authz.RoleOwner)
|
||||
mustCreateUser(t, fs, "admin1", "adminpass1", authz.RoleOwner)
|
||||
// The caller deletes the *other* owner, not itself. This test used
|
||||
// to sign in as admin1 and delete admin1, which passed only because
|
||||
// self-deletion was unguarded -- it asserted the lockout as if it
|
||||
// were the intended behaviour. What it means to test is that the
|
||||
// last-owner guard doesn't fire while a second owner remains, and
|
||||
// that holds without deleting the caller.
|
||||
owner2 := mustCreateUser(t, fs, "admin2", "adminpass2", authz.RoleOwner)
|
||||
_, mux := newTestHandler(t, fs)
|
||||
|
||||
login := doRequest(t, mux, http.MethodPost, "/auth/login", `{"username":"admin1","password":"adminpass1"}`, nil)
|
||||
cookie := sessionCookieFrom(login)
|
||||
|
||||
rec := doRequest(t, mux, http.MethodDelete, "/auth/users/"+owner2.ID, "", cookie)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("deleting one of two owners: status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// The lockout this guards against: an owner creates a second owner,
|
||||
// deletes their own account, and is signed out by the resulting
|
||||
// local_sessions cascade with no supported way back in unless they
|
||||
// already know the other account's password.
|
||||
func TestCannotDeleteYourOwnAccount(t *testing.T) {
|
||||
fs := newFakeStore()
|
||||
owner := mustCreateUser(t, fs, "admin1", "adminpass1", authz.RoleOwner)
|
||||
// A second owner exists, so the last-owner guard is satisfied and
|
||||
// cannot be what refuses this.
|
||||
mustCreateUser(t, fs, "admin2", "adminpass2", authz.RoleOwner)
|
||||
_, mux := newTestHandler(t, fs)
|
||||
|
||||
login := doRequest(t, mux, http.MethodPost, "/auth/login", `{"username":"admin1","password":"adminpass1"}`, nil)
|
||||
cookie := sessionCookieFrom(login)
|
||||
|
||||
rec := doRequest(t, mux, http.MethodDelete, "/auth/users/"+owner1.ID, "", cookie)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("deleting one of two owners: status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
||||
rec := doRequest(t, mux, http.MethodDelete, "/auth/users/"+owner.ID, "", cookie)
|
||||
if rec.Code != http.StatusConflict {
|
||||
t.Fatalf("deleting your own account: status = %d, want 409, body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Still signed in, and the account still exists.
|
||||
sess := doRequest(t, mux, http.MethodGet, "/auth/session", "", cookie)
|
||||
if sess.Code != http.StatusOK {
|
||||
t.Fatalf("session after a refused self-delete: status = %d, want 200, body=%s", sess.Code, sess.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-7
@@ -314,13 +314,18 @@ func (s *Store) GetPasswordHashByID(ctx context.Context, id string) (string, err
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
// CountLocalUsers backs -seed-admin's idempotency check (see
|
||||
// cmd/api/main.go's runSeedAdmin): a deployment that already has at
|
||||
// least one local user never gets a second auto-created admin account.
|
||||
func (s *Store) CountLocalUsers(ctx context.Context) (int, error) {
|
||||
var n int
|
||||
err := s.pool.QueryRow(ctx, `SELECT count(*) FROM users WHERE username IS NOT NULL`).Scan(&n)
|
||||
return n, err
|
||||
// UsernameExists backs -seed-admin's idempotency check (see
|
||||
// cmd/api/main.go's runSeedAdmin). It asks whether the account that
|
||||
// command would create is already provisioned -- deliberately not
|
||||
// whether the deployment has any local user at all, which is the
|
||||
// question it used to ask: an operator who deleted the seeded admin
|
||||
// account was then refused by the very command documented as the way
|
||||
// to create one, because some *other* account still existed.
|
||||
func (s *Store) UsernameExists(ctx context.Context, username string) (bool, error) {
|
||||
var exists bool
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)`, username).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// CreateSession mints a fresh opaque token for an already-authenticated
|
||||
|
||||
Reference in New Issue
Block a user