Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Guard against demoting the last remaining admin in both is_admin mutation paths

Murtaza Nasir152539fe13e3
diff --git a/src/api/admin.py b/src/api/admin.py
index 6299caca..bf75c796 100644
--- a/src/api/admin.py
+++ b/src/api/admin.py
@@ -260,7 +260,14 @@ def admin_update_user(user_id):
user.password = bcrypt.generate_password_hash(data['password']).decode('utf-8')
if 'is_admin' in data:
- user.is_admin = data['is_admin']
+ new_is_admin = bool(data['is_admin'])
+ # Guard against demoting the last remaining admin, which would lock
+ # everyone out of the admin panel with no in-app recovery.
+ if user.is_admin and not new_is_admin:
+ other_admins = User.query.filter(User.is_admin.is_(True), User.id != user.id).count()
+ if other_admins == 0:
+ return jsonify({'error': 'Cannot remove admin rights from the last remaining admin.'}), 400
+ user.is_admin = new_is_admin
if 'can_share_publicly' in data:
user.can_share_publicly = data['can_share_publicly']
@@ -376,11 +383,18 @@ def admin_toggle_admin(user_id):
user = db.session.get(User, user_id)
if not user:
return jsonify({'error': 'User not found'}), 404
-
+
+ # Guard against demoting the last remaining admin (defence in depth; the
+ # self-status check above already prevents the acting admin demoting itself).
+ if user.is_admin:
+ other_admins = User.query.filter(User.is_admin.is_(True), User.id != user.id).count()
+ if other_admins == 0:
+ return jsonify({'error': 'Cannot remove admin rights from the last remaining admin.'}), 400
+
# Toggle admin status
user.is_admin = not user.is_admin
db.session.commit()
-
+
return jsonify({'success': True, 'is_admin': user.is_admin})
diff --git a/tests/test_cov_admin.py b/tests/test_cov_admin.py
index 2dfc95da..c443f57b 100644
--- a/tests/test_cov_admin.py
+++ b/tests/test_cov_admin.py
@@ -336,6 +336,69 @@ def test_toggle_admin_not_found_404(admin_client):
assert resp.status_code == 404
+# --- last-admin lockout guard ----------------------------------------------
+# The admin flag is global, so these tests control the global admin set: the
+# "last admin" trigger demotes every other admin (restored afterwards) so the
+# target is genuinely the only one left.
+
+def _demote_other_admins(keep_id):
+ others = User.query.filter(User.is_admin.is_(True), User.id != keep_id).all()
+ ids = [u.id for u in others]
+ for u in others:
+ u.is_admin = False
+ db.session.commit()
+ return ids
+
+
+def _restore_admins(ids):
+ for uid in ids:
+ u = db.session.get(User, uid)
+ if u:
+ u.is_admin = True
+ db.session.commit()
+
+
+def test_update_user_promote_to_admin(admin_client, normal_user):
+ resp = admin_client.put(f'/admin/users/{normal_user.id}', json={'is_admin': True})
+ assert resp.status_code == 200
+ db.session.refresh(normal_user)
+ assert normal_user.is_admin is True
+
+
+def test_update_user_cannot_demote_last_admin(admin_client, admin_user):
+ saved = _demote_other_admins(admin_user.id)
+ try:
+ resp = admin_client.put(f'/admin/users/{admin_user.id}', json={'is_admin': False})
+ assert resp.status_code == 400
+ db.session.refresh(admin_user)
+ assert admin_user.is_admin is True
+ finally:
+ _restore_admins(saved)
+
+
+def test_update_user_can_demote_when_other_admin_exists(admin_client, admin_user):
+ other = _mk_user(is_admin=True)
+ try:
+ resp = admin_client.put(f'/admin/users/{other.id}', json={'is_admin': False})
+ assert resp.status_code == 200
+ db.session.refresh(other)
+ assert other.is_admin is False
+ finally:
+ db.session.delete(other)
+ db.session.commit()
+
+
+def test_toggle_admin_demote_other_admin_ok(admin_client, admin_user):
+ other = _mk_user(is_admin=True)
+ try:
+ resp = admin_client.post(f'/admin/users/{other.id}/toggle-admin')
+ assert resp.status_code == 200
+ assert resp.get_json()['is_admin'] is False
+ finally:
+ db.session.delete(other)
+ db.session.commit()
+
+
def test_delete_user_self_blocked(admin_client, admin_user):
resp = admin_client.delete(f'/admin/users/{admin_user.id}')
assert resp.status_code == 400