Skip to content

A free open source IT asset/license management system

Fixed jmrm-535m-cx3c - better validation on location validation

snipe6392e0544f00
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index 7b106a5ac7..b9df00720e 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -224,7 +224,12 @@ class LocationsController extends Controller
// Parent company check applies whenever FMCS is on, independent of scope_locations_fmcs.
if (Setting::getSettings()->full_multiple_companies_support) {
- $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ // withoutGlobalScopes: Location::CompanyableScope would filter a
+ // cross-tenant parent out for a scoped non-superuser, so find()
+ // would return null and the null check below would short-circuit
+ // the reject branch, letting the parent_id save through. See
+ // ValidationServiceProvider::fmcs_location for the sibling fix.
+ $parent = $location->parent_id ? Location::withoutGlobalScopes()->find($location->parent_id) : null;
if ($parent && $parent->company_id != $location->company_id) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.error_location_parent_company', [
'parent' => $parent->name,
@@ -338,7 +343,12 @@ class LocationsController extends Controller
// Parent company check applies whenever FMCS is on, independent of scope_locations_fmcs.
// Runs outside the company_id gate so a parent_id-only update is also validated.
if (Setting::getSettings()->full_multiple_companies_support) {
- $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ // withoutGlobalScopes: Location::CompanyableScope would filter a
+ // cross-tenant parent out for a scoped non-superuser, so find()
+ // would return null and the null check below would short-circuit
+ // the reject branch, letting the parent_id save through. See
+ // ValidationServiceProvider::fmcs_location for the sibling fix.
+ $parent = $location->parent_id ? Location::withoutGlobalScopes()->find($location->parent_id) : null;
if ($parent && $parent->company_id != $location->company_id) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.error_location_parent_company', [
'parent' => $parent->name,
diff --git a/app/Http/Controllers/BulkLocationsController.php b/app/Http/Controllers/BulkLocationsController.php
index 884ebd488e..eece6d290b 100644
--- a/app/Http/Controllers/BulkLocationsController.php
+++ b/app/Http/Controllers/BulkLocationsController.php
@@ -120,7 +120,14 @@ class BulkLocationsController extends Controller
: $location->parent_id;
if ($effectiveParentId !== null) {
- $parent = Location::find($effectiveParentId);
+ // withoutGlobalScopes: Location::CompanyableScope would
+ // filter a cross-tenant parent out for a scoped
+ // non-superuser, so find() would return null and the
+ // null check below would short-circuit the reject
+ // branch, letting a cross-company parent link save
+ // through. See ValidationServiceProvider::fmcs_location
+ // for the sibling fix.
+ $parent = Location::withoutGlobalScopes()->find($effectiveParentId);
if ($parent && $parent->company_id != $effectiveCompanyId) {
if (array_key_exists('parent_id', $rowUpdates)) {
unset($rowUpdates['parent_id']);
diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php
index f444006dcf..03dc9c9962 100755
--- a/app/Http/Controllers/LocationsController.php
+++ b/app/Http/Controllers/LocationsController.php
@@ -101,7 +101,12 @@ class LocationsController extends Controller
// Parent company check applies whenever FMCS is on, independent of scope_locations_fmcs.
if (Setting::getSettings()->full_multiple_companies_support) {
- $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ // withoutGlobalScopes: Location::CompanyableScope would filter a
+ // cross-tenant parent out for a scoped non-superuser, so find()
+ // would return null and the null check below would short-circuit
+ // the reject branch, letting the parent_id save through. See
+ // ValidationServiceProvider::fmcs_location for the sibling fix.
+ $parent = $location->parent_id ? Location::withoutGlobalScopes()->find($location->parent_id) : null;
if ($parent && $parent->company_id != $location->company_id) {
return redirect()->back()->withInput()->with('error', trans('general.error_location_parent_company', [
'parent' => $parent->name,
@@ -198,7 +203,12 @@ class LocationsController extends Controller
// Parent company check applies whenever FMCS is on, independent of scope_locations_fmcs.
if (Setting::getSettings()->full_multiple_companies_support) {
- $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ // withoutGlobalScopes: Location::CompanyableScope would filter a
+ // cross-tenant parent out for a scoped non-superuser, so find()
+ // would return null and the null check below would short-circuit
+ // the reject branch, letting the parent_id save through. See
+ // ValidationServiceProvider::fmcs_location for the sibling fix.
+ $parent = $location->parent_id ? Location::withoutGlobalScopes()->find($location->parent_id) : null;
if ($parent && $parent->company_id != $location->company_id) {
return redirect()->back()->withInput()->with('error', trans('general.error_location_parent_company', [
'parent' => $parent->name,
diff --git a/app/Models/Location.php b/app/Models/Location.php
index 3989bb59d7..d8c959d451 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -42,7 +42,7 @@ class Location extends SnipeModel
'country' => 'min:2|max:191|nullable',
'zip' => 'max:10|nullable',
'manager_id' => 'exists:users,id|nullable',
- 'parent_id' => 'nullable|exists:locations,id|non_circular:locations,id',
+ 'parent_id' => 'nullable|exists:locations,id|non_circular:locations,id|parent_matches_location_company',
'company_id' => 'integer|nullable|exists:companies,id|fmcs_company',
];
diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php
index 4a5a3d58d2..c62c4e5fb1 100644
--- a/app/Providers/ValidationServiceProvider.php
+++ b/app/Providers/ValidationServiceProvider.php
@@ -542,12 +542,67 @@ class ValidationServiceProvider extends ServiceProvider
);
});
- // Enforces "Company must be picked" when FMCS is on AND
- // null_company_is_floater is disabled (strict mode). Without this
- // rule a companied non-superuser can save a form with an unset
- // company dropdown, land a row with company_id=NULL, and then
- // have that row instantly filtered out of their own view by the
- // strict-mode scope. See #19192. Passes when:
+ // Enforce that a Location's parent_id references a location in the
+ // same company under FMCS. Controllers already run an equivalent
+ // check (Api/LocationsController::store + update,
+ // LocationsController::store + update, BulkLocationsController::update),
+ // but the model-level rule also catches importer, seeder, and any
+ // future programmatic save that skips the controller-level guard.
+ // withoutGlobalScopes() bypasses CompanyableScope on the parent
+ // lookup so a scoped actor sees the real record and can be
+ // compared against it, matching the pattern fmcs_location already
+ // uses above. See GHSA-jmrm-535m-cx3c.
+ Validator::extend('parent_matches_location_company', function ($attribute, $value, $parameters, $validator) {
+ if ($value === null || $value === '' || (int) $value === 0) {
+ return true;
+ }
+
+ if (!Setting::getSettings()->full_multiple_companies_support) {
+ return true;
+ }
+
+ $parent = Location::withoutGlobalScopes()->find((int) $value);
+ if (!$parent) {
+ return true;
+ }
+
+ $childCompanyIdRaw = $validator->getData()['company_id'] ?? null;
+ $childCompanyId = ($childCompanyIdRaw === null || $childCompanyIdRaw === '')
+ ? null
+ : (int) $childCompanyIdRaw;
+ $parentCompanyId = $parent->company_id === null ? null : (int) $parent->company_id;
+
+ return $parentCompanyId === $childCompanyId;
+ });
+
+ Validator::replacer('parent_matches_location_company', function ($message, $attribute, $rule, $parameters, $validator) {
+ $data = $validator->getData();
+ $parentId = $data[$attribute] ?? null;
+ $parent = $parentId ? Location::withoutGlobalScopes()->find((int) $parentId) : null;
+
+ $childCompanyIdRaw = $data['company_id'] ?? null;
+ $childCompanyId = ($childCompanyIdRaw === null || $childCompanyIdRaw === '')
+ ? null
+ : (int) $childCompanyIdRaw;
+ $childCompany = $childCompanyId ? Company::withoutGlobalScopes()->find($childCompanyId) : null;
+
+ return trans('general.error_location_parent_company', [
+ 'parent' => $parent?->name ?? '?',
+ 'parent_company' => $parent?->company?->name ?? trans('general.unassigned'),
+ 'location_company' => $childCompany?->name ?? trans('general.unassigned'),
+ ]);
+ });
+
+ // Requires a company be picked when a companied non-superuser
+ // submits a form under strict FMCS (FMCS on and
+ // null_company_is_floater off). Null companies stay legal for
+ // null-company users (they operate in the null "pseudo-company"
+ // namespace), superusers, and system contexts (CLI, seeders,
+ // importers). Without this rule a companied user could save a
+ // form with an unset company dropdown, land a row with
+ // company_id=NULL, and then have that row instantly filtered out
+ // of their own view by the strict-mode scope. See #19192. Passes
+ // when:
// - FMCS is off (nothing to enforce)
// - null_company_is_floater is on (nulls are legal floaters)
// - value is present (form was filled in)
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 7869ce54b1..c690ef9aac 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -9621,7 +9621,7 @@ parameters:
-
message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
identifier: nullsafe.neverNull
- count: 2
+ count: 5
path: app/Providers/ValidationServiceProvider.php
-
diff --git a/tests/Feature/Locations/Api/LocationParentCompanyTest.php b/tests/Feature/Locations/Api/LocationParentCompanyTest.php
index 378bc847bd..55a6bff91f 100644
--- a/tests/Feature/Locations/Api/LocationParentCompanyTest.php
+++ b/tests/Feature/Locations/Api/LocationParentCompanyTest.php
@@ -171,4 +171,132 @@ class LocationParentCompanyTest extends TestCase
$this->assertEquals($parentLocation->id, $location->fresh()->parent_id);
}
+
+ public function test_scoped_actor_cannot_create_location_with_cross_company_parent_under_fmcs()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $acme = Company::factory()->create(['name' => 'Acme']);
+ $globex = Company::factory()->create(['name' => 'Globex']);
+
+ $foreignParent = Location::factory()->create(['company_id' => $globex->id]);
+ $scopedActor = User::factory()->createLocations()->forCompany($acme)->create();
+
+ $this->actingAsForApi($scopedActor)
+ ->postJson(route('api.locations.store'), [
+ 'name' => 'Forged Child',
+ 'company_id' => $acme->id,
+ 'parent_id' => $foreignParent->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error');
+
+ $this->assertFalse(Location::where('name', 'Forged Child')->exists());
+ }
+
+ public function test_scoped_actor_cannot_update_location_to_cross_company_parent_under_fmcs()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $acme = Company::factory()->create(['name' => 'Acme']);
+ $globex = Company::factory()->create(['name' => 'Globex']);
+
+ $foreignParent = Location::factory()->create(['company_id' => $globex->id]);
+ $ownLocation = Location::factory()->create(['company_id' => $acme->id]);
+ $scopedActor = User::factory()->editLocations()->forCompany($acme)->create();
+
+ $this->actingAsForApi($scopedActor)
+ ->patchJson(route('api.locations.update', $ownLocation), [
+ 'parent_id' => $foreignParent->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error');
+
+ $this->assertNull($ownLocation->fresh()->parent_id);
+ }
+
+ public function test_scoped_actor_cannot_create_location_with_cross_company_parent_under_scoped_locations_fmcs()
+ {
+ $acme = Company::factory()->create(['name' => 'Acme']);
+ $globex = Company::factory()->create(['name' => 'Globex']);
+
+ $foreignParent = Location::factory()->create(['company_id' => $globex->id]);
+ $scopedActor = User::factory()->createLocations()->forCompany($acme)->create();
+
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ $this->actingAsForApi($scopedActor)
+ ->postJson(route('api.locations.store'), [
+ 'name' => 'Forged Child Scoped',
+ 'company_id' => $acme->id,
+ 'parent_id' => $foreignParent->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error');
+
+ $this->assertFalse(Location::where('name', 'Forged Child Scoped')->exists());
+ }
+
+ // -----------------------------------------------------------------------
+ // Model-rule defense in depth
+ //
+ // The controller checks above are the primary reject path for
+ // API/UI/bulk callers. The same invariant is also enforced by the
+ // parent_matches_location_company validation rule on the Location
+ // model, which catches any save() that skips the controller entirely
+ // (importer, seeder, job, future controller). These tests write
+ // directly to the model to prove the rule fires without any HTTP
+ // request in the mix.
+ // -----------------------------------------------------------------------
+
+ public function test_model_save_rejects_cross_company_parent_under_fmcs()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $acme = Company::factory()->create();
+ $globex = Company::factory()->create();
+ $foreignParent = Location::factory()->create(['company_id' => $globex->id]);
+
+ $location = Location::factory()->make([
+ 'name' => 'Direct-Save Attempt',
+ 'company_id' => $acme->id,
+ 'parent_id' => $foreignParent->id,
+ ]);
+
+ $this->assertFalse($location->save());
+ $this->assertFalse(Location::where('name', 'Direct-Save Attempt')->exists());
+ }
+
+ public function test_model_save_allows_same_company_parent_under_fmcs()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $acme = Company::factory()->create();
+ $sameCompanyParent = Location::factory()->create(['company_id' => $acme->id]);
+
+ $location = Location::factory()->make([
+ 'name' => 'Direct-Save Same Company',
+ 'company_id' => $acme->id,
+ 'parent_id' => $sameCompanyParent->id,
+ ]);
+
+ $this->assertTrue($location->save());
+ }
+
+ public function test_model_save_allows_cross_company_parent_when_fmcs_disabled()
+ {
+ $this->settings->disableMultipleFullCompanySupport();
+
+ $acme = Company::factory()->create();
+ $globex = Company::factory()->create();
+ $foreignParent = Location::factory()->create(['company_id' => $globex->id]);
+
+ $location = Location::factory()->make([
+ 'name' => 'Direct-Save FMCS Off',
+ 'company_id' => $acme->id,
+ 'parent_id' => $foreignParent->id,
+ ]);
+
+ $this->assertTrue($location->save());
+ }
}