Skip to content

A free open source IT asset/license management system

Fixed FD-56664 - tightened activated gate

snipe02f62c8dce7b
diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.php
index 1568d483a7..fc98dd36aa 100755
--- a/app/Http/Controllers/Users/UsersController.php
+++ b/app/Http/Controllers/Users/UsersController.php
@@ -306,15 +306,19 @@ class UsersController extends Controller
$user->end_date = $request->input('end_date', null);
$user->autoassign_licenses = $request->input('autoassign_licenses', 0);
- // Set this here so that we can overwrite it later if the user is an admin or superadmin
- $user->activated = $request->input('activated', auth()->user()->is($user) ? 1 : $user->activated);
-
// Update the location of any assets checked out to this user
Asset::where('assigned_type', User::class)
->where('assigned_to', $user->id)
->update(['location_id' => $request->input('location_id', null)]);
- // check for permissions related fields and only set them if the user has permission to edit them
+ // Permission-gated fields: `activated` lives inside this gate too.
+ // An earlier version of this method assigned `activated` right
+ // before the gate on the theory that the gate would overwrite it.
+ // That let anyone with users.edit toggle an admin's activated flag
+ // by POSTing a full edit payload — the gate would deny the second
+ // assignment but the first had already stuck. Every auth-field
+ // write must live inside this branch so an unauthorized caller
+ // can't reach past the gate on any of them.
if (auth()->user()->can('canEditAuthFields', $user) && auth()->user()->can('editableOnDemo')) {
$user->username = trim($request->input('username'));
diff --git a/tests/Feature/Users/Api/UpdateUserTest.php b/tests/Feature/Users/Api/UpdateUserTest.php
index 6d4cec721a..5129d22a18 100644
--- a/tests/Feature/Users/Api/UpdateUserTest.php
+++ b/tests/Feature/Users/Api/UpdateUserTest.php
@@ -205,6 +205,38 @@ class UpdateUserTest extends TestCase
$this->assertEquals(1, $user->refresh()->activated);
}
+ /**
+ * Companion regression to
+ * tests/Feature/Users/Ui/UpdateUserTest::test_editing_users_cannot_toggle_admin_activated_via_full_valid_payload
+ * The API path already excludes `activated` from the pre-gate fill()
+ * and only assigns it inside the canEditAuthFields branch, so this
+ * has always been safe. Pinning the behavior explicitly so a future
+ * refactor to `$user->fill($request->all())` or similar can't
+ * silently regress it.
+ */
+ public function test_api_editing_users_cannot_toggle_admin_activated()
+ {
+ $editing_user = User::factory()->editUsers()->create(['activated' => true]);
+ $admin = User::factory()->admin()->create([
+ 'first_name' => 'Admin',
+ 'last_name' => 'Target',
+ 'username' => 'api_admin_target',
+ 'email' => '[email protected]',
+ 'activated' => true,
+ ]);
+
+ $this->actingAsForApi($editing_user)
+ ->patch(route('api.users.update', $admin), [
+ 'first_name' => $admin->first_name,
+ 'last_name' => $admin->last_name,
+ 'username' => $admin->username,
+ 'email' => $admin->email,
+ 'activated' => 0,
+ ]);
+
+ $this->assertSame(1, (int) $admin->fresh()->activated, 'Non-admin actor must not be able to deactivate an admin via API.');
+ }
+
public function test_api_users_can_be_deactivated_with_number()
{
$admin = User::factory()->editUsers()->create();
diff --git a/tests/Feature/Users/Ui/UpdateUserTest.php b/tests/Feature/Users/Ui/UpdateUserTest.php
index 92137d30d1..ba0d512b38 100644
--- a/tests/Feature/Users/Ui/UpdateUserTest.php
+++ b/tests/Feature/Users/Ui/UpdateUserTest.php
@@ -156,6 +156,45 @@ class UpdateUserTest extends TestCase
$this->assertEquals(1, $admin->refresh()->activated);
}
+ /**
+ * Regression for the pre-gate-activated escalation:
+ * UsersController::update used to assign $user->activated from the
+ * request BEFORE the canEditAuthFields gate, with the intent that the
+ * gated branch would overwrite it for authorized callers. Unauthorized
+ * callers had the first assignment stick through to save(), letting
+ * anyone with `users.edit` deactivate any admin by POSTing a full edit
+ * payload.
+ *
+ * The pre-existing test_editing_users_cannot_edit_escalation_fields_for_admins
+ * passed by accident because its payload omitted first_name / last_name,
+ * so SaveUserRequest rejected the request on validation before the
+ * controller wrote to the model. This test sends a complete valid
+ * payload so the controller actually runs to save() and pins the
+ * unauthorized-cannot-touch-activated behavior directly.
+ */
+ public function test_editing_users_cannot_toggle_admin_activated_via_full_valid_payload()
+ {
+ $editing_user = User::factory()->editUsers()->create(['activated' => true]);
+ $admin = User::factory()->admin()->create([
+ 'first_name' => 'Admin',
+ 'last_name' => 'Target',
+ 'username' => 'admin_target',
+ 'email' => '[email protected]',
+ 'activated' => true,
+ ]);
+
+ $this->actingAs($editing_user)
+ ->put(route('users.update', $admin), [
+ 'first_name' => $admin->first_name,
+ 'last_name' => $admin->last_name,
+ 'username' => $admin->username,
+ 'email' => $admin->email,
+ 'activated' => 0,
+ ]);
+
+ $this->assertSame(1, (int) $admin->fresh()->activated, 'Non-admin actor must not be able to deactivate an admin.');
+ }
+
public function test_editing_users_cannot_edit_escalation_fields_for_admins()
{
$editing_user = User::factory()->editUsers()->create(['activated' => true]);