Skip to content

A free open source IT asset/license management system

Fixed FD-57832 / rcf6-v2m4 - tighter scoping on accessory notes in FMCS

snipe1811a85f1c0b
diff --git a/app/Models/AccessoryCheckout.php b/app/Models/AccessoryCheckout.php
index beb65a48ca..beada002e8 100755
--- a/app/Models/AccessoryCheckout.php
+++ b/app/Models/AccessoryCheckout.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Models\Traits\CompanyableChildTrait;
use App\Models\Traits\Searchable;
use App\Presenters\AccessoryPresenter;
use Illuminate\Database\Eloquent\Builder;
@@ -17,6 +18,7 @@ use Illuminate\Database\Eloquent\Relations\Relation;
*/
class AccessoryCheckout extends Model
{
+ use CompanyableChildTrait;
use HasFactory;
use Searchable;
@@ -27,6 +29,19 @@ class AccessoryCheckout extends Model
'note',
];
+ /**
+ * CompanyableChildScope walks these relation names at query time
+ * and inner-joins against the parent, applying CompanyableScope on
+ * the parent so pivot rows pointing to accessories in another FMCS
+ * tenant drop out of every Eloquent query against this model.
+ *
+ * @return array<int, string>
+ */
+ public function getCompanyableParents()
+ {
+ return ['accessory'];
+ }
+
protected $presenter = AccessoryPresenter::class;
protected $table = 'accessories_checkout';
diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php
index e711c3e5a7..7ac913a855 100644
--- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php
+++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php
@@ -39,9 +39,15 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie
$this->settings->enableMultipleFullCompanySupport();
+ // Cross-tenant checkout is hidden from the caller by the
+ // CompanyableChildScope on AccessoryCheckout, so AccessoryCheckout::find()
+ // in the controller returns null and we fall into the standard
+ // does-not-exist error envelope rather than a 403. Line 50
+ // asserts the checkin didn't actually happen, which is the
+ // security property that matters.
$this->actingAsForApi($userInCompanyA)
->postJson(route('api.accessories.checkin', $accessoryForCompanyB->checkouts->first()))
- ->assertForbidden();
+ ->assertStatusMessageIs('error');
$this->actingAsForApi($superUser)
->postJson(route('api.accessories.checkin', $anotherAccessoryForCompanyB->checkouts->first()))
diff --git a/tests/Feature/Locations/Api/AssignedAccessoriesFmcsLeakTest.php b/tests/Feature/Locations/Api/AssignedAccessoriesFmcsLeakTest.php
new file mode 100644
index 0000000000..a0c3d58f0f
--- /dev/null
+++ b/tests/Feature/Locations/Api/AssignedAccessoriesFmcsLeakTest.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace Tests\Feature\Locations\Api;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Company;
+use App\Models\Location;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+/**
+ * Regression test for cross-tenant AccessoryCheckout
+ * pivot leak on GET /api/v1/locations/{location-id}/assigned/accessories.
+ *
+ * Before the fix, AccessoryCheckout had no company scope. The endpoint
+ * queried pivots by assigned_to=location_id, applied CompanyableScope
+ * only on the nested accessory relation at serialization time, and
+ * leaked pivot id, note, timestamp, created_by, assigned_to, and total
+ * for cross-company checkouts whose parent accessory was invisible.
+ *
+ * Fix is a global CompanyableChildScope on AccessoryCheckout that
+ * joins against accessories and applies CompanyableScope on the parent.
+ */
+class AssignedAccessoriesFmcsLeakTest extends TestCase
+{
+ public function test_cross_company_accessory_checkout_at_shared_location_is_hidden_under_fmcs(): void
+ {
+ // scope_locations_fmcs = 0 (default) means locations are shared
+ // across tenants, so Company B's accessory can be checked out at
+ // a Company A location. Before the fix, the pivot id / note /
+ // timestamp / created_by / assigned_to leaked to the Company A
+ // actor on GET /api/v1/locations/{id}/assigned/accessories, even
+ // though the nested accessory relation came back null because
+ // the per-model CompanyableScope hid it.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+ $locationA = Location::factory()->create(['company_id' => $companyA->id]);
+
+ $foreignAccessory = Accessory::factory()->create([
+ 'company_id' => $companyB->id,
+ 'name' => 'CAND18 Company B Accessory',
+ ]);
+
+ AccessoryCheckout::create([
+ 'accessory_id' => $foreignAccessory->id,
+ 'assigned_to' => $locationA->id,
+ 'assigned_type' => Location::class,
+ 'note' => 'CAND18-CROSS-TENANT-PIVOT-NOTE',
+ ]);
+
+ $actorA = $this->userInCompany($companyA);
+
+ $response = $this->actingAsForApi($actorA)
+ ->getJson(route('api.locations.assigned_accessories', ['location' => $locationA->id]))
+ ->assertOk();
+
+ $this->assertSame(0, $response->json('total'), 'Total must not count cross-company pivot rows.');
+ $this->assertSame([], $response->json('rows'), 'Rows must not include cross-company pivot metadata.');
+ $response->assertDontSee('CAND18-CROSS-TENANT-PIVOT-NOTE');
+ }
+
+ public function test_same_company_accessory_checkout_still_visible_under_fmcs(): void
+ {
+ // Control. The fix must not hide pivots the caller legitimately
+ // sees.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $locationA = Location::factory()->create(['company_id' => $companyA->id]);
+
+ $ownAccessory = Accessory::factory()->create([
+ 'company_id' => $companyA->id,
+ 'name' => 'CAND18 Company A Accessory',
+ ]);
+
+ AccessoryCheckout::create([
+ 'accessory_id' => $ownAccessory->id,
+ 'assigned_to' => $locationA->id,
+ 'assigned_type' => Location::class,
+ 'note' => 'CAND18-OWN-TENANT-NOTE',
+ ]);
+
+ $actorA = $this->userInCompany($companyA);
+
+ $response = $this->actingAsForApi($actorA)
+ ->getJson(route('api.locations.assigned_accessories', ['location' => $locationA->id]))
+ ->assertOk();
+
+ $this->assertSame(1, $response->json('total'));
+ $response->assertSee('CAND18-OWN-TENANT-NOTE');
+ }
+
+ public function test_fmcs_off_user_sees_cross_company_pivots_as_before(): void
+ {
+ // When FMCS is off, there is no tenant boundary to enforce.
+ // Cross-company checkouts remain visible.
+ $this->settings->disableMultipleFullCompanySupport();
+
+ $companyB = Company::factory()->create();
+ $location = Location::factory()->create();
+
+ $foreignAccessory = Accessory::factory()->create([
+ 'company_id' => $companyB->id,
+ 'name' => 'CAND18 Company B Accessory',
+ ]);
+
+ AccessoryCheckout::create([
+ 'accessory_id' => $foreignAccessory->id,
+ 'assigned_to' => $location->id,
+ 'assigned_type' => Location::class,
+ 'note' => 'CAND18-FMCS-OFF-NOTE',
+ ]);
+
+ $actor = User::factory()->viewAccessories()->viewLocationHistory()->create();
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.locations.assigned_accessories', ['location' => $location->id]))
+ ->assertOk();
+
+ $this->assertSame(1, $response->json('total'));
+ $response->assertSee('CAND18-FMCS-OFF-NOTE');
+ }
+
+ private function userInCompany(Company $company): User
+ {
+ $user = User::factory()->viewAccessories()->viewLocationHistory()->create();
+ DB::table('company_user')->insert([
+ 'company_id' => $company->id,
+ 'user_id' => $user->id,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ return $user;
+ }
+}