summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/api/admin.py
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-09-01 17:48:56 +0200
committerChristophe Besson <cbesson@gmail.com>2026-09-01 17:48:56 +0200
commit9f95bf2deaec0c31e5fac95e1068db40b878e217 (patch)
treed8a0f5f5ca2270d047a272b02f04796ee2606bba /packages/meshbay-hub/src/meshbay_hub/api/admin.py
parent8a6294b0412a86f378c6e2e937c28de64a903c91 (diff)
downloadmeshbay-9f95bf2deaec0c31e5fac95e1068db40b878e217.tar.gz
fix(hub): moderator can no longer grant admin or hard-revoke accounts
admin_patch_user was gated by require_moderator but wrote `role` and `status` with no further check. A moderator could promote any account (an accomplice) to admin, demote an existing admin, or set status="revoked" — a straight path from the moderation role to full instance control. Split authorization by field: status between active/suspended stays at require_moderator (reversible content moderation); role changes, status="revoked", and touching an admin's account at all now require user_is_admin(current_user) (new helper in deps.py, alongside the existing require_admin/require_moderator). Regression test: test_moderator_cannot_change_roles_or_revoke. Third security review, finding H1. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011pG75yGK3NthNfyjH74omG
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/api/admin.py')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/admin.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/admin.py b/packages/meshbay-hub/src/meshbay_hub/api/admin.py
index ab6fa07..4960674 100644
--- a/packages/meshbay-hub/src/meshbay_hub/api/admin.py
+++ b/packages/meshbay-hub/src/meshbay_hub/api/admin.py
@@ -14,7 +14,7 @@ from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from meshbay_hub.auth import decrypt_email
-from meshbay_hub.api.deps import require_admin, require_moderator
+from meshbay_hub.api.deps import require_admin, require_moderator, user_is_admin
from meshbay_hub.api.revocation import get_connected_node_count, is_node_connected
from meshbay_hub.db.engine import get_db
from meshbay_hub.db.models import Group, GroupMember, IPLog, Node, User
@@ -196,6 +196,25 @@ async def admin_patch_user(
if user.id == current_user.id:
raise HTTPException(status_code=400, detail="Cannot modify your own account")
+ # A moderator suspends and restores accounts — reversible content moderation.
+ # Changing what someone *is* (their role), and the one irreversible status
+ # (`revoked`, which is signed and broadcast to every node), are administrative.
+ # Without this split a moderator could promote an accomplice to admin, or
+ # revoke every admin, entirely from the moderation role. `admin_delete_user`
+ # already draws this exact line for the same reason.
+ privileged = body.role is not None or body.status == "revoked"
+ if privileged and not user_is_admin(current_user):
+ raise HTTPException(
+ status_code=403,
+ detail="Changing a role, or revoking an account, requires admin rights")
+
+ # An admin's account is not a moderator's to touch at all — not their role,
+ # not their status.
+ if user_is_admin(user) and not user_is_admin(current_user):
+ raise HTTPException(
+ status_code=403,
+ detail="Only an admin can change another admin's account")
+
from meshbay_hub.api.notifications import create_notification
if body.role is not None: