A free open source IT asset/license management system
Guard accessories in assets API
snipe25214f23aa13
diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.phpindex 7d83c08f48..e2afd701e3 100644--- a/app/Http/Transformers/AssetsTransformer.php+++ b/app/Http/Transformers/AssetsTransformer.php@@ -208,9 +208,7 @@ class AssetsTransformerforeach ($asset->components as $component) {// Info-disclosure guard: if the caller is denied view// on this specific component, omit it from the response- // entirely - not even id / pivot_id are exposed, so a- // caller with an explicit components.view deny can't- // enumerate what's on the asset.+ // entirelyif (Gate::denies('view', $component)) {continue;}@@ -386,11 +384,29 @@ class AssetsTransformer{$array = [];+ $suppressed = 0;foreach ($accessory_checkouts as $checkout) {+ // Info-disclosure guard: GET /api/v1/hardware/{asset}/assigned/accessories+ // is gated only on assets.view, so a caller with assets.view but+ // an explicit deny on accessories.view used to read the accessory's+ // name / note / image straight off this response. When denied, skip+ // the row entirely so nothing about the accessory (not even id or+ // existence) is exposed. Same shape as transformCheckedoutComponents+ // below.+ //+ // The controller-supplied $total still reflects the true row count+ // and would leak "there are N accessories you can't see", so+ // decrement it by the number of rows suppressed here.+ if (!$checkout->accessory || Gate::denies('view', $checkout->accessory)) {+ $suppressed++;++ continue;+ }+$array[] = self::transformCheckedoutAccessory($checkout);}- return (new DatatablesTransformer)->transformDatatables($array, $total);+ return (new DatatablesTransformer)->transformDatatables($array, max(0, $total - $suppressed));}public function transformCheckedoutAccessory(AccessoryCheckout $accessory_checkout)diff --git a/tests/Feature/Assets/Api/AssignedAccessoriesTest.php b/tests/Feature/Assets/Api/AssignedAccessoriesTest.phpnew file mode 100644index 0000000000..eba4ec19e3--- /dev/null+++ b/tests/Feature/Assets/Api/AssignedAccessoriesTest.php@@ -0,0 +1,101 @@+<?php++namespace Tests\Feature\Assets\Api;++use App\Models\Accessory;+use App\Models\Asset;+use App\Models\Company;+use App\Models\User;+use Illuminate\Support\Facades\DB;+use Tests\TestCase;++class AssignedAccessoriesTest extends TestCase+{+ public function test_requires_permission(): void+ {+ $this->actingAsForApi(User::factory()->create())+ ->getJson(route('api.assets.assigned_accessories', Asset::factory()->create()))+ ->assertForbidden();+ }++ public function test_adheres_to_company_scoping(): void+ {+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $asset = Asset::factory()->for($companyA)->create();++ $user = User::factory()->forCompany($companyB)->viewAssets()->create();++ $this->actingAsForApi($user)+ ->getJson(route('api.assets.assigned_accessories', $asset))+ ->assertOk()+ ->assertStatusMessageIs('error')+ ->assertMessagesAre('Asset not found');+ }++ /**+ * Security regression pin, companion to+ * AssignedComponentsTest::test_does_not_leak_component_details_when_caller_lacks_components_view+ * for the parallel /assigned/accessories endpoint. Before the fix in+ * AssetsTransformer::transformCheckedoutAccessories, a caller with+ * assets.view but not accessories.view could read the accessory's+ * name / note / image straight off this response even though the+ * direct GET /api/v1/accessories/{id} correctly returned 403. Now+ * denied rows are omitted entirely and total is decremented so the+ * response neither reveals the accessory's id nor that it exists at+ * all.+ */+ public function test_does_not_leak_accessory_details_when_caller_lacks_accessories_view(): void+ {+ $asset = Asset::factory()->create();+ $accessory = Accessory::factory()->create(['name' => 'Hidden Bluetooth Adapter']);+ DB::table('accessories_checkout')->insert([+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $asset->id,+ 'assigned_type' => Asset::class,+ 'note' => 'sensitive accessory note',+ 'created_by' => User::factory()->superuser()->create()->id,+ 'created_at' => now(),+ ]);++ // viewAssets only. No viewAccessories.+ $actor = User::factory()->viewAssets()->create();++ $this->actingAsForApi($actor)+ ->getJson(route('api.assets.assigned_accessories', $asset))+ ->assertOk()+ ->assertJsonPath('total', 0)+ ->assertJsonCount(0, 'rows');+ }++ /**+ * Positive-path sanity: a caller who DOES have accessories.view still+ * gets the full accessory details. Guards against the info-disclosure+ * guard accidentally stripping data from authorized callers.+ */+ public function test_returns_full_accessory_details_when_caller_has_accessories_view(): void+ {+ $asset = Asset::factory()->create();+ $accessory = Accessory::factory()->create(['name' => 'Visible Bluetooth Adapter']);+ DB::table('accessories_checkout')->insert([+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $asset->id,+ 'assigned_type' => Asset::class,+ 'note' => 'accessory note',+ 'created_by' => User::factory()->superuser()->create()->id,+ 'created_at' => now(),+ ]);++ $actor = User::factory()->viewAssets()->viewAccessories()->create();++ $this->actingAsForApi($actor)+ ->getJson(route('api.assets.assigned_accessories', $asset))+ ->assertOk()+ ->assertJsonPath('total', 1)+ ->assertJsonPath('rows.0.accessory.id', $accessory->id)+ ->assertJsonPath('rows.0.accessory.name', 'Visible Bluetooth Adapter')+ ->assertJsonPath('rows.0.note', 'accessory note');+ }+}