Skip to content

Commit 8e155b7

Browse files
fix: address Devin Review - privilege escalation guards, read_user_by_id role check, AuditLogPublic type
- Add privilege escalation check in create_user: only Super Admin can create Super Admin - Add privilege escalation checks in update_user: only Super Admin can modify/promote to Super Admin - Fix read_user_by_id to use role-based check instead of is_superuser - Add target_user_email and performed_by_email fields to frontend AuditLogPublic type Co-Authored-By: daniel.resgate <daniel.rider69@gmail.com>
1 parent 807fc45 commit 8e155b7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

backend/app/api/routes/users.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def create_user(
6565
Create new user. Requires email and role at minimum.
6666
Password is optional (generated automatically for passwordless flow).
6767
"""
68+
# Only Super Admin can create another Super Admin
69+
if (
70+
user_in.role == UserRole.super_admin
71+
and current_user.role != UserRole.super_admin
72+
):
73+
raise HTTPException(
74+
status_code=403,
75+
detail="Only a Super Admin can create another Super Admin",
76+
)
77+
6878
user = crud.get_user_by_email(session=session, email=user_in.email)
6979
if user:
7080
raise HTTPException(
@@ -199,7 +209,13 @@ def read_user_by_id(
199209
user = session.get(User, user_id)
200210
if user == current_user:
201211
return user
202-
if not current_user.is_superuser:
212+
if not current_user.role or current_user.role not in [
213+
UserRole.comercial,
214+
UserRole.juridico,
215+
UserRole.financeiro,
216+
UserRole.rh,
217+
UserRole.super_admin,
218+
]:
203219
raise HTTPException(
204220
status_code=403,
205221
detail="The user doesn't have enough privileges",
@@ -231,6 +247,24 @@ def update_user(
231247
status_code=404,
232248
detail="The user with this id does not exist in the system",
233249
)
250+
# Only Super Admin can modify a Super Admin user
251+
if (
252+
db_user.role == UserRole.super_admin
253+
and current_user.role != UserRole.super_admin
254+
):
255+
raise HTTPException(
256+
status_code=403,
257+
detail="Only a Super Admin can modify another Super Admin",
258+
)
259+
# Only Super Admin can assign the Super Admin role
260+
if (
261+
user_in.role == UserRole.super_admin
262+
and current_user.role != UserRole.super_admin
263+
):
264+
raise HTTPException(
265+
status_code=403,
266+
detail="Only a Super Admin can assign the Super Admin role",
267+
)
234268
if user_in.email:
235269
existing_user = crud.get_user_by_email(session=session, email=user_in.email)
236270
if existing_user and existing_user.id != user_id:

frontend/src/client/types.gen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ export type AuditLogPublic = {
236236
performed_by_id: string;
237237
changes: string;
238238
created_at?: (string | null);
239+
target_user_email?: (string | null);
240+
performed_by_email?: (string | null);
239241
};
240242

241243
export type AuditLogsPublic = {

0 commit comments

Comments
 (0)