A free open source IT asset/license management system
Fixed 6hxf-hqrc-wfrp - Company-less user check out null-company assets
snipe56c6e9efeed5
diff --git a/app/Models/Traits/CompanyableTrait.php b/app/Models/Traits/CompanyableTrait.phpindex 3aeaba9151..574682cf48 100644--- a/app/Models/Traits/CompanyableTrait.php+++ b/app/Models/Traits/CompanyableTrait.php@@ -56,11 +56,23 @@ trait CompanyableTrait}if (! $this->company_id) {- $targetHasNoCompany = $target instanceof User- ? $target->companies()->count() === 0- : is_null($target->company_id);+ // For a User target, delegate to canReceiveFromCompany(null),+ // which reads company_user directly via DB::table and avoids+ // the FMCS global scope on the companies-table subquery. A+ // naive $target->companies()->count() re-applies the actor's+ // CompanyableScope to the companies table, and for an+ // empty-pivot actor Company::scopeCompanyablesDirectly reduces+ // that subquery to whereNull('companies.id'), which never+ // matches (companies.id is a NOT NULL primary key). Every+ // target then looks like it has no company, flipping the+ // strict-mode deny to allow. See User::canReceiveFromCompany+ // for the comment and GHSA-6hxf-hqrc-wfrp for the+ // specific bypass this closes.+ if ($target instanceof User) {+ return $target->canReceiveFromCompany(null);+ }- if ($targetHasNoCompany) {+ if (is_null($target->company_id)) {return true;}diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.phpindex c62c4e5fb1..a4ac37e4b4 100644--- a/app/Providers/ValidationServiceProvider.php+++ b/app/Providers/ValidationServiceProvider.php@@ -557,12 +557,12 @@ class ValidationServiceProvider extends ServiceProviderreturn true;}- if (!Setting::getSettings()->full_multiple_companies_support) {+ if (! Setting::getSettings()->full_multiple_companies_support) {return true;}$parent = Location::withoutGlobalScopes()->find((int) $value);- if (!$parent) {+ if (! $parent) {return true;}@@ -570,6 +570,23 @@ class ValidationServiceProvider extends ServiceProvider$childCompanyId = ($childCompanyIdRaw === null || $childCompanyIdRaw === '')? null: (int) $childCompanyIdRaw;++ // When the location being validated has no company_id of its+ // own, it inherits its effective company_id from the parent+ // location chain via Location::effectiveFmcsCompanyId. Nested+ // locations where only the top-level ancestor carries a+ // company_id are an intended Snipe-IT hierarchy pattern+ // (e.g. "Acme Corp HQ" -> "Building 3" -> "Server Room" with+ // the company_id set only on "Acme Corp HQ"), so this rule+ // must not reject a null company_id on the location being+ // validated. The CVE this rule closes (GHSA-jmrm-535m-cx3c)+ // required the location being validated to have an explicit+ // company_id pointing at a parent location in a different+ // company_id, which is still caught below.+ if ($childCompanyId === null) {+ return true;+ }+$parentCompanyId = $parent->company_id === null ? null : (int) $parent->company_id;return $parentCompanyId === $childCompanyId;diff --git a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php b/tests/Feature/Checkouts/Api/AssetCheckoutTest.phpindex ce975084ee..c1a637af25 100644--- a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php@@ -527,4 +527,92 @@ class AssetCheckoutTest extends TestCase$this->assertEquals($companiedUser->id, $nullCompanyAsset->fresh()->assigned_to);}++ /**+ * GHSA-6hxf-hqrc-wfrp regression coverage: the reporter's PoC+ * exactly. An empty-pivot ("null tenant") actor holding assets.checkout+ * would previously succeed at checking out a null-company asset to a+ * companied user in any other company under strict FMCS, because+ * CompanyableTrait::canCheckoutTo used $target->companies()->count()+ * to detect an uncompanied target. That subquery re-applied the actor's+ * Company::CompanyableScope, which for an empty-pivot actor degenerates+ * to whereNull('companies.id') and matches zero rows, making every+ * target look pivot-less. The fix delegates to+ * User::canReceiveFromCompany(null), which reads company_user directly+ * via DB::table and correctly rejects a companied target under strict+ * mode.+ */+ public function test_empty_pivot_actor_cannot_check_out_null_company_asset_to_companied_user_under_strict_fmcs()+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $company = Company::factory()->create();+ $nullCompanyAsset = Asset::factory()->create(['company_id' => null]);+ $companiedTarget = $company->users()->save(User::factory()->create());+ $emptyPivotActor = User::factory()->checkoutAssets()->create();+ $emptyPivotActor->companies()->sync([]);++ $this->actingAsForApi($emptyPivotActor)+ ->postJson(route('api.asset.checkout', $nullCompanyAsset), [+ 'checkout_to_type' => 'user',+ 'assigned_user' => $companiedTarget->id,+ ])+ ->assertOk()+ ->assertStatusMessageIs('error');++ $this->assertNull(+ $nullCompanyAsset->fresh()->assigned_to,+ 'Strict FMCS must reject a null-company asset being checked out to a companied user by an empty-pivot actor.'+ );++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }++ public function test_empty_pivot_actor_can_check_out_null_company_asset_to_empty_pivot_user_under_strict_fmcs()+ {+ // Positive control for the fix. The "both pivot-less" case is the+ // null pseudo-company namespace and stays legitimately allowed.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $nullCompanyAsset = Asset::factory()->create(['company_id' => null]);+ $emptyPivotTarget = User::factory()->create();+ $emptyPivotTarget->companies()->sync([]);+ $emptyPivotActor = User::factory()->checkoutAssets()->create();+ $emptyPivotActor->companies()->sync([]);++ $this->actingAsForApi($emptyPivotActor)+ ->postJson(route('api.asset.checkout', $nullCompanyAsset), [+ 'checkout_to_type' => 'user',+ 'assigned_user' => $emptyPivotTarget->id,+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertEquals($emptyPivotTarget->id, $nullCompanyAsset->fresh()->assigned_to);+ }++ public function test_empty_pivot_actor_can_check_out_null_company_asset_to_companied_user_under_floater_mode()+ {+ // Floater mode is the setting that legitimately opens null-company+ // items to companied users. The fix must not accidentally block this.+ $this->settings->enableFloaterMode();++ $company = Company::factory()->create();+ $nullCompanyAsset = Asset::factory()->create(['company_id' => null]);+ $companiedTarget = $company->users()->save(User::factory()->create());+ $emptyPivotActor = User::factory()->checkoutAssets()->create();+ $emptyPivotActor->companies()->sync([]);++ $this->actingAsForApi($emptyPivotActor)+ ->postJson(route('api.asset.checkout', $nullCompanyAsset), [+ 'checkout_to_type' => 'user',+ 'assigned_user' => $companiedTarget->id,+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertEquals($companiedTarget->id, $nullCompanyAsset->fresh()->assigned_to);+ }}diff --git a/tests/Unit/Models/CanCheckoutToEmptyPivotActorTest.php b/tests/Unit/Models/CanCheckoutToEmptyPivotActorTest.phpnew file mode 100644index 0000000000..12f66a68e2--- /dev/null+++ b/tests/Unit/Models/CanCheckoutToEmptyPivotActorTest.php@@ -0,0 +1,113 @@+<?php++namespace Tests\Unit\Models;++use App\Models\Asset;+use App\Models\Company;+use App\Models\User;+use Tests\TestCase;++/**+ * GHSA-6hxf-hqrc-wfrp regression coverage for CompanyableTrait::canCheckoutTo.+ *+ * The uncompanied-item branch used $target->companies()->count() === 0 to+ * detect a pivot-less User target. User::companies() is a plain+ * belongsToMany, so the query for the companies table gets the actor's+ * Company::CompanyableScope applied. For an actor with an empty+ * company_user pivot ("null tenant"), that scope reduces the subquery to+ * whereNull('companies.id'), which never matches because companies.id is a+ * NOT NULL primary key. Every target then looked pivot-less, so the strict+ * mode floater deny was flipped to allow for every target regardless of+ * their actual memberships. The fix delegates the User branch to+ * User::canReceiveFromCompany(null), which reads company_user directly via+ * DB::table and avoids the scope trap.+ *+ * These tests pin canCheckoutTo() while acting as an empty-pivot actor,+ * because that is the specific context that triggered the bug. Running the+ * same assertions without actingAs() would pass even against the vulnerable+ * code because there is no actor scope to recurse through.+ */+class CanCheckoutToEmptyPivotActorTest extends TestCase+{+ public function test_empty_pivot_actor_strict_fmcs_blocks_uncompanied_asset_to_companied_user(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $company = Company::factory()->create();+ $companiedTarget = $company->users()->save(User::factory()->create());+ $emptyPivotActor = User::factory()->create();+ $emptyPivotActor->companies()->sync([]);++ $uncompaniedAsset = Asset::factory()->create(['company_id' => null]);++ $this->actingAs($emptyPivotActor);++ $this->assertFalse(+ $uncompaniedAsset->canCheckoutTo($companiedTarget),+ 'GHSA-6hxf-hqrc-wfrp: strict FMCS must reject a null-company asset being checked out to a companied user by an empty-pivot actor. The vulnerable code returned true here.'+ );+ }++ public function test_empty_pivot_actor_floater_mode_allows_uncompanied_asset_to_companied_user(): void+ {+ $this->settings->enableFloaterMode();++ $company = Company::factory()->create();+ $companiedTarget = $company->users()->save(User::factory()->create());+ $emptyPivotActor = User::factory()->create();+ $emptyPivotActor->companies()->sync([]);++ $uncompaniedAsset = Asset::factory()->create(['company_id' => null]);++ $this->actingAs($emptyPivotActor);++ $this->assertTrue(+ $uncompaniedAsset->canCheckoutTo($companiedTarget),+ 'Floater mode is the setting that legitimately opens null-company items to companied users. The rule must still allow this.'+ );+ }++ public function test_empty_pivot_actor_strict_fmcs_allows_uncompanied_asset_to_empty_pivot_user(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $emptyPivotActor = User::factory()->create();+ $emptyPivotActor->companies()->sync([]);+ $emptyPivotTarget = User::factory()->create();+ $emptyPivotTarget->companies()->sync([]);++ $uncompaniedAsset = Asset::factory()->create(['company_id' => null]);++ $this->actingAs($emptyPivotActor);++ $this->assertTrue(+ $uncompaniedAsset->canCheckoutTo($emptyPivotTarget),+ 'Both sides pivot-less is the "null pseudo-company" case that strict FMCS legitimately allows.'+ );+ }++ public function test_companied_actor_strict_fmcs_blocks_uncompanied_asset_to_companied_user_in_other_company(): void+ {+ // Control: the same reject should fire for a companied actor too,+ // and did on the vulnerable code because a companied actor's scope+ // does not degenerate to whereNull('companies.id'). This test is+ // here to guard the fix from over-correcting in the other direction.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ [$acme, $globex] = Company::factory()->count(2)->create();+ $actor = $acme->users()->save(User::factory()->create());+ $companiedTarget = $globex->users()->save(User::factory()->create());++ $uncompaniedAsset = Asset::factory()->create(['company_id' => null]);++ $this->actingAs($actor);++ $this->assertFalse(+ $uncompaniedAsset->canCheckoutTo($companiedTarget),+ 'Strict FMCS must still reject a null-company asset to a companied user when the actor is companied too.'+ );+ }+}