Skip to content

A free open source IT asset/license management system

Normalize permissions array

snipe3fe8600a7019
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index f855cc2004..357a3c4a03 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -571,31 +571,30 @@ class UsersController extends Controller
if ($request->has('permissions')) {
- $permissions_array = $request->input('permissions');
- $orig_permissions_array = $user->decodePermissions();
+ $permissions_array = $this->normalizePermissionsPayload($request->input('permissions'));
+ $orig_permissions_array = $this->normalizePermissionsPayload($user->decodePermissions());
- // Strip out the individual superuser permission if the API user isn't a superadmin
+ // Strip out the individual superuser permission if the API user isn't a superadmin.
+ // If the target user did not already have a superuser key, remove any injected value.
if (! auth()->user()->isSuperUser()) {
-
- if (is_array($orig_permissions_array)) {
- if (array_key_exists('superuser', $orig_permissions_array)) {
- $permissions_array['superuser'] = $orig_permissions_array['superuser'];
- }
+ if (array_key_exists('superuser', $orig_permissions_array)) {
+ $permissions_array['superuser'] = $orig_permissions_array['superuser'];
+ } else {
+ unset($permissions_array['superuser']);
}
-
}
- // Strip out the individual admin permission if the API user isn't an admin
+ // Strip out the individual admin permission if the API user isn't an admin.
+ // If the target user did not already have an admin key, remove any injected value.
if ((! auth()->user()->isAdmin()) && (! auth()->user()->isSuperUser())) {
-
- if (is_array($orig_permissions_array)) {
- if (array_key_exists('admin', $orig_permissions_array)) {
- $permissions_array['admin'] = $orig_permissions_array['admin'];
- }
+ if (array_key_exists('admin', $orig_permissions_array)) {
+ $permissions_array['admin'] = $orig_permissions_array['admin'];
+ } else {
+ unset($permissions_array['admin']);
}
}
- // This is going to update the whole thing, not just what was passed
+ // This is going to update the whole thing, not just what was passed.
$user->permissions = $permissions_array;
}
@@ -978,4 +977,28 @@ class UsersController extends Controller
return response()->json((new ActionlogsTransformer)->transformActionlogs($history, $total), 200, ['Content-Type' => 'application/json;charset=utf8'], JSON_UNESCAPED_UNICODE);
}
+
+ /**
+ * Normalize permissions payloads from request/model to a consistent associative array.
+ *
+ * @return array<string, mixed>
+ */
+ private function normalizePermissionsPayload(mixed $permissions): array
+ {
+ if (is_string($permissions)) {
+ $decoded = json_decode($permissions, true);
+
+ return is_array($decoded) ? $decoded : [];
+ }
+
+ if (is_array($permissions)) {
+ return $permissions;
+ }
+
+ if ($permissions instanceof \stdClass) {
+ return (array) $permissions;
+ }
+
+ return [];
+ }
}
diff --git a/tests/Feature/Users/Api/UpdateUserTest.php b/tests/Feature/Users/Api/UpdateUserTest.php
index dd019da8ad..9845e96b05 100644
--- a/tests/Feature/Users/Api/UpdateUserTest.php
+++ b/tests/Feature/Users/Api/UpdateUserTest.php
@@ -589,4 +589,57 @@ class UpdateUserTest extends TestCase
'company_id' => $companyB->id,
])->assertStatusMessageIs('error');
}
+
+ public function test_edit_users_permission_cannot_escalate_empty_permissions_user_to_admin_or_superuser_via_api()
+ {
+ $editingUser = User::factory()->editUsers()->create();
+ $targetUser = User::factory()->create([
+ 'permissions' => null,
+ ]);
+
+ $this->actingAsForApi($editingUser)
+ ->putJson(route('api.users.update', $targetUser), [
+ 'first_name' => $targetUser->first_name,
+ 'username' => $targetUser->username,
+ 'permissions' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ 'users.view' => '1',
+ ],
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $decoded = (array) $targetUser->refresh()->decodePermissions();
+
+ $this->assertArrayNotHasKey('admin', $decoded, 'Non-admin user should not be able to grant admin');
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Non-admin user should not be able to grant superuser');
+ $this->assertEquals(1, $decoded['users.view'] ?? null, 'Non-privileged permissions should still be updateable');
+ }
+
+ public function test_admin_cannot_escalate_empty_permissions_user_to_superuser_via_api()
+ {
+ $adminUser = User::factory()->admin()->create();
+ $targetUser = User::factory()->create([
+ 'permissions' => null,
+ ]);
+
+ $this->actingAsForApi($adminUser)
+ ->putJson(route('api.users.update', $targetUser), [
+ 'first_name' => $targetUser->first_name,
+ 'username' => $targetUser->username,
+ 'permissions' => [
+ 'admin' => '1',
+ 'superuser' => '1',
+ ],
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $decoded = (array) $targetUser->refresh()->decodePermissions();
+
+ $this->assertArrayHasKey('admin', $decoded, 'Admin should be able to grant admin');
+ $this->assertSame('1', (string) $decoded['admin']);
+ $this->assertArrayNotHasKey('superuser', $decoded, 'Admin should not be able to grant superuser');
+ }
}