Skip to content

A free open source IT asset/license management system

Fixed 4jj6-58j6 - Check scope for depts and manager

snipe0825d200c6c3
diff --git a/app/Http/Requests/SaveUserRequest.php b/app/Http/Requests/SaveUserRequest.php
index f7641e45a4..b91cc67356 100644
--- a/app/Http/Requests/SaveUserRequest.php
+++ b/app/Http/Requests/SaveUserRequest.php
@@ -34,8 +34,8 @@ class SaveUserRequest extends FormRequest
public function rules()
{
$rules = [
- 'department_id' => 'nullable|integer|exists:departments,id',
- 'manager_id' => 'nullable|integer|exists:users,id',
+ 'department_id' => 'nullable|integer|exists:departments,id|fmcs_department',
+ 'manager_id' => 'nullable|integer|exists:users,id|fmcs_manager',
'company_id' => ['nullable', 'integer', 'exists:companies,id'],
'company_ids' => 'nullable|array',
'company_ids.*' => 'integer|exists:companies,id',
diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php
index a4ac37e4b4..8d67abf5f1 100644
--- a/app/Providers/ValidationServiceProvider.php
+++ b/app/Providers/ValidationServiceProvider.php
@@ -4,8 +4,10 @@ namespace App\Providers;
use App\Models\Company;
use App\Models\CustomField;
+use App\Models\Department;
use App\Models\Location;
use App\Models\Setting;
+use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
@@ -668,6 +670,50 @@ class ValidationServiceProvider extends ServiceProvider
return str_replace(':attribute', trans('general.company'), $message);
});
+ /*
+ * Confirms a manager_id write references a user visible within the
+ * acting user's FMCS scope.
+ */
+ Validator::extend('fmcs_manager', function ($attribute, $value, $parameters, $validator) {
+ if (empty($value)) {
+ return true;
+ }
+ // CLI contexts (snipeit:import, seeders, queue workers) have no
+ // auth user.
+ if (! auth()->check()) {
+ return true;
+ }
+ $manager = User::withoutGlobalScopes()->find((int) $value);
+ if (! $manager) {
+ return true;
+ }
+
+ return (bool) Company::isCurrentUserHasAccess($manager);
+ });
+
+ Validator::replacer('fmcs_manager', function ($message) {
+ return str_replace(':attribute', trans('general.manager'), $message);
+ });
+
+ Validator::extend('fmcs_department', function ($attribute, $value, $parameters, $validator) {
+ if (empty($value)) {
+ return true;
+ }
+ if (! auth()->check()) {
+ return true;
+ }
+ $department = Department::withoutGlobalScopes()->find((int) $value);
+ if (! $department) {
+ return true;
+ }
+
+ return (bool) Company::isCurrentUserHasAccess($department);
+ });
+
+ Validator::replacer('fmcs_department', function ($message) {
+ return str_replace(':attribute', trans('general.department'), $message);
+ });
+
// Validates that the company of the validated object matches the company of the location in case of scoped locations
Validator::extend('fmcs_location', function ($attribute, $value, $parameters, $validator) {
$settings = Setting::getSettings();
diff --git a/tests/Feature/Fmcs/FmcsUserScopeValidatorTest.php b/tests/Feature/Fmcs/FmcsUserScopeValidatorTest.php
new file mode 100644
index 0000000000..203a4054e2
--- /dev/null
+++ b/tests/Feature/Fmcs/FmcsUserScopeValidatorTest.php
@@ -0,0 +1,160 @@
+<?php
+
+namespace Tests\Feature\Fmcs;
+
+use App\Models\Company;
+use App\Models\Department;
+use App\Models\User;
+use Tests\TestCase;
+
+/*
+ * Regression coverage for GHSA-4jj6-58j6-qw5w. SaveUserRequest's manager_id
+ * and department_id rules had exists:... but no FMCS scope check, so a
+ * company-A editor could persist a victim's manager_id as a company-B
+ * user or move the victim into a company-B department. Both outcomes
+ * leaked cross-tenant data: foreign manager read access through the
+ * Manager View feature on the first, department-roster PII on the second.
+ */
+class FmcsUserScopeValidatorTest extends TestCase
+{
+ public function test_editor_cannot_set_cross_company_manager_id(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $actor = User::factory()->editUsers()->forCompany($companyA)->create();
+ $victim = User::factory()->forCompany($companyA)->create();
+ $foreignManager = User::factory()->forCompany($companyB)->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'company_ids' => [$companyA->id],
+ 'manager_id' => $foreignManager->id,
+ ])
+ ->assertStatusMessageIs('error');
+
+ $victim->refresh();
+ $this->assertNull($victim->manager_id, 'Cross-company manager_id must not persist.');
+ }
+
+ public function test_editor_cannot_set_cross_company_department_id(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $actor = User::factory()->editUsers()->forCompany($companyA)->create();
+ $victim = User::factory()->forCompany($companyA)->create();
+
+ $foreignDepartment = Department::factory()->make();
+ $foreignDepartment->company_id = $companyB->id;
+ $foreignDepartment->setValidating(false);
+ $foreignDepartment->save();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'company_ids' => [$companyA->id],
+ 'department_id' => $foreignDepartment->id,
+ ])
+ ->assertStatusMessageIs('error');
+
+ $victim->refresh();
+ $this->assertNull($victim->department_id, 'Cross-company department_id must not persist.');
+ }
+
+ public function test_editor_can_set_same_company_manager_id(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $company = Company::factory()->create();
+ $actor = User::factory()->editUsers()->forCompany($company)->create();
+ $victim = User::factory()->forCompany($company)->create();
+ $sameCompanyManager = User::factory()->forCompany($company)->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'company_ids' => [$company->id],
+ 'manager_id' => $sameCompanyManager->id,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertSame($sameCompanyManager->id, $victim->fresh()->manager_id);
+ }
+
+ public function test_editor_can_set_same_company_department_id(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $company = Company::factory()->create();
+ $actor = User::factory()->editUsers()->forCompany($company)->create();
+ $victim = User::factory()->forCompany($company)->create();
+
+ $sameCompanyDepartment = Department::factory()->make();
+ $sameCompanyDepartment->company_id = $company->id;
+ $sameCompanyDepartment->setValidating(false);
+ $sameCompanyDepartment->save();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'company_ids' => [$company->id],
+ 'department_id' => $sameCompanyDepartment->id,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertSame($sameCompanyDepartment->id, $victim->fresh()->department_id);
+ }
+
+ public function test_superuser_can_set_cross_company_manager_id(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = User::factory()->superuser()->create();
+ $victim = User::factory()->forCompany($companyA)->create();
+ $foreignManager = User::factory()->forCompany($companyB)->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'company_ids' => [$companyA->id],
+ 'manager_id' => $foreignManager->id,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertSame($foreignManager->id, $victim->fresh()->manager_id);
+ }
+
+ public function test_fmcs_off_allows_cross_company_manager_id(): void
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = User::factory()->editUsers()->forCompany($companyA)->create();
+ $victim = User::factory()->forCompany($companyA)->create();
+ $foreignManager = User::factory()->forCompany($companyB)->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.users.update', $victim), [
+ 'first_name' => $victim->first_name,
+ 'last_name' => $victim->last_name,
+ 'username' => $victim->username,
+ 'manager_id' => $foreignManager->id,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertSame($foreignManager->id, $victim->fresh()->manager_id);
+ }
+}