mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: enforce 2fa only on certain update actions
prevent newer admins from demoting older admins, and also prevent deletion of all admins
This commit is contained in:
@@ -113,43 +113,63 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var jwtUser = HttpContext.GetJwtUser()!;
|
||||
var reqUser = await _userService.GetByUUID(uuid);
|
||||
if (reqUser == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
if (
|
||||
(user.Roles.HasFlag(Role.Admin) && !jwtUser.Roles.HasFlag(Role.Admin))
|
||||
|| (!jwtUser.Roles.HasFlag(Role.Admin) && user.UUID != jwtUser.UUID)
|
||||
(user.Roles.HasFlag(Role.Admin) && !reqUser.Roles.HasFlag(Role.Admin))
|
||||
|| (!reqUser.Roles.HasFlag(Role.Admin) && user.UUID != reqUser.UUID)
|
||||
)
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to modify this user." });
|
||||
|
||||
if (jwtUser.Roles.HasFlag(Role.Admin) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA)
|
||||
bool isSensitiveUpdate =
|
||||
(updatedUser.Roles.HasValue && updatedUser.Roles.Value != user.Roles) // updating roles
|
||||
|| (updatedUser.IsBanned.HasValue && updatedUser.IsBanned.Value != user.IsBanned) // updating ban status
|
||||
|| (
|
||||
updatedUser.Email != null && updatedUser.Email != user.Email // updating email
|
||||
);
|
||||
|
||||
if (reqUser.Roles.HasFlag(Role.Admin) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA && isSensitiveUpdate)
|
||||
return StatusCode(403, new { success = false, message = "2FA is required to perform this action." });
|
||||
|
||||
var updatingAdminFields =
|
||||
(updatedUser.Roles.HasValue && updatedUser.Roles.Value != user.Roles)
|
||||
|| (updatedUser.IsBanned.HasValue && updatedUser.IsBanned.Value != user.IsBanned);
|
||||
|
||||
user.DisplayName = updatedUser.DisplayName ?? user.DisplayName;
|
||||
if (jwtUser.Roles.HasFlag(Role.Admin) || user.UUID == jwtUser.UUID) // only admins or self
|
||||
{
|
||||
user.Email = updatedUser.Email ?? user.Email;
|
||||
user.Visibility = updatedUser.Visibility ?? user.Visibility;
|
||||
}
|
||||
if (jwtUser.Roles.HasFlag(Role.Admin))
|
||||
{
|
||||
if (jwtUser.TotpEnabled && updatingAdminFields)
|
||||
if (isSensitiveUpdate && jwtUser.TotpEnabled)
|
||||
{
|
||||
if (
|
||||
string.IsNullOrEmpty(updatedUser.TotpCode)
|
||||
|| !await _totp.VerifyAsync(new VerificationContext { UserUUID = jwtUser.UUID, Code = updatedUser.TotpCode })
|
||||
|| !await _totp.VerifyAsync(new VerificationContext { UserUUID = reqUser.UUID, Code = updatedUser.TotpCode })
|
||||
)
|
||||
{
|
||||
return Unauthorized(new { message = "Invalid TOTP code." });
|
||||
}
|
||||
}
|
||||
|
||||
if (user.Roles.HasFlag(Role.Admin) && !reqUser.Roles.HasFlag(Role.Admin))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to modify this user." });
|
||||
if (
|
||||
updatedUser.Roles.HasValue
|
||||
&& user.Roles.HasFlag(Role.Admin)
|
||||
&& !updatedUser.Roles.Value.HasFlag(Role.Admin)
|
||||
&& reqUser.UID > user.UID
|
||||
)
|
||||
// prevents demoting of other older admins by newer admins
|
||||
// allows self demotion in case of compromisation
|
||||
{
|
||||
return StatusCode(403, new { success = false, message = "You cannot remove the admin role from this user." });
|
||||
}
|
||||
|
||||
user.DisplayName = updatedUser.DisplayName ?? user.DisplayName;
|
||||
if (reqUser.Roles.HasFlag(Role.Admin) || user.UUID == reqUser.UUID)
|
||||
{
|
||||
user.Email = updatedUser.Email ?? user.Email;
|
||||
user.Visibility = updatedUser.Visibility ?? user.Visibility;
|
||||
}
|
||||
if (reqUser.Roles.HasFlag(Role.Admin))
|
||||
{
|
||||
user.Roles = updatedUser.Roles ?? user.Roles;
|
||||
user.IsBanned = updatedUser.IsBanned ?? user.IsBanned;
|
||||
}
|
||||
@@ -173,6 +193,8 @@ namespace SharpbinV3.Server.Controllers
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to delete this user." });
|
||||
if (jwtUser.Roles.HasFlag(Role.Admin) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA)
|
||||
return StatusCode(403, new { success = false, message = "2FA is required to perform this action." });
|
||||
if (user.Roles.HasFlag(Role.Admin))
|
||||
return StatusCode(403, new { success = false, message = "You cannot delete an admin user." });
|
||||
|
||||
if (jwtUser.TotpEnabled)
|
||||
{
|
||||
|
||||
@@ -30,7 +30,6 @@ namespace SharpbinV3.Server.Services
|
||||
IOptions<JWTSettings> jwtOptions,
|
||||
IOptions<AuthSettings> authOptions,
|
||||
IOptions<AppSettings> appSettings,
|
||||
IOptions<EmailSettings> emailSettings,
|
||||
ILogger<AuthService> logger
|
||||
)
|
||||
{
|
||||
@@ -39,7 +38,6 @@ namespace SharpbinV3.Server.Services
|
||||
private readonly JWTSettings _jwtSettings = jwtOptions.Value;
|
||||
private readonly AuthSettings _authSettings = authOptions.Value;
|
||||
private readonly AppSettings _appSettings = appSettings.Value;
|
||||
private readonly EmailSettings _emailSettings = emailSettings.Value;
|
||||
private readonly ILogger _logger = logger;
|
||||
private const int EmailVerificationTokenLength = 64;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user