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:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user