A free open source IT asset/license management system
A few more gate tweaks for API responses
snipe9daf9605d786
diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.phpindex 13a890af21..5995cca4cb 100644--- a/app/Http/Transformers/AssetsTransformer.php+++ b/app/Http/Transformers/AssetsTransformer.php@@ -187,8 +187,16 @@ class AssetsTransformer$array['components'] = [];foreach ($asset->components as $component) {- $array['components'][] = [+ // 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.+ if (Gate::denies('view', $component)) {+ continue;+ }+ $array['components'][] = ['id' => $component->id,'pivot_id' => $component->pivot->id,'name' => e($component->name),@@ -196,7 +204,6 @@ class AssetsTransformer'purchase_cost' => $component->purchase_cost,'purchase_total' => $component->calculated_purchase_cost,'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime'),-];}}@@ -230,7 +237,7 @@ class AssetsTransformer// stripped is PII (username, email, employee_num, jobtitle,// first/last name split). Instance check so FMCS scoping// applies too.- if (! Gate::allows('view', $asset->assigned)) {+ if (Gate::denies('view', $asset->assigned)) {return ['id' => (int) $asset->assigned->id,'type' => 'user',@@ -430,44 +437,49 @@ class AssetsTransformerpublic function transformCheckedoutComponents(Collection $components_assets, $total){$array = [];+ $suppressed = 0;foreach ($components_assets as $component_checkout) {$component = $component_checkout->component;// Info-disclosure guard: GET /api/v1/hardware/{asset}/assigned/components// is gated only on assets.view, so a caller with assets.view but- // not components.view used to read the component's name / qty /- // note straight off this response. Fall back to `{id, type}`- // when the caller can't view the specific component. Instance- // check so FMCS scoping applies too.- $canViewComponent = $component && Gate::allows('view', $component);+ // an explicit deny on components.view used to read the component's+ // name / qty / note straight off this response. When denied, skip+ // the row entirely so nothing about the component (not even id+ // or existence) is exposed. Instance check so FMCS scoping+ // applies too.+ //+ // The controller-supplied $total still reflects the true row+ // count and would leak "there are N components you can't see",+ // so decrement it by the number of rows suppressed here.+ if (! $component || Gate::denies('view', $component)) {+ $suppressed++;++ continue;+ }$array[] = ['assigned_pivot_id' => $component_checkout->id,- 'name' => $canViewComponent- ? [- 'id' => $component->id,- 'name' => e($component->display_name),- 'type' => 'component',- 'deleted_at' => $component->deleted_at,- ]- : [- 'id' => $component?->id,- 'type' => 'component',- ],- 'assigned_qty' => $canViewComponent ? $component_checkout->assigned_qty : null,- 'note' => $canViewComponent && $component_checkout->note ? e($component_checkout->note) : null,+ 'name' => [+ 'id' => $component->id,+ 'name' => e($component->display_name),+ 'type' => 'component',+ 'deleted_at' => $component->deleted_at,+ ],+ 'assigned_qty' => $component_checkout->assigned_qty,+ 'note' => ($component_checkout->note) ? e($component_checkout->note) : null,'created_at' => Helper::getFormattedDateObject($component_checkout->created_at, 'datetime'),'created_by' => $component_checkout->adminuser ? ['id' => (int) $component_checkout->adminuser->id,'name' => e($component_checkout->adminuser->display_name),] : null,'available_actions' => [- 'checkin' => (($component?->deleted_at == '') && Gate::allows('checkin', Component::class)),- 'view' => (($component?->deleted_at == '') && Gate::allows('view', Component::class)),+ 'checkin' => (($component->deleted_at == '') && Gate::allows('checkin', Component::class)),+ 'view' => (($component->deleted_at == '') && Gate::allows('view', Component::class)),],];}- return (new DatatablesTransformer)->transformDatatables($array, $total);+ return (new DatatablesTransformer)->transformDatatables($array, max(0, $total - $suppressed));}}diff --git a/app/Http/Transformers/LicenseSeatsTransformer.php b/app/Http/Transformers/LicenseSeatsTransformer.phpindex 74f6921c29..6ae34de182 100644--- a/app/Http/Transformers/LicenseSeatsTransformer.php+++ b/app/Http/Transformers/LicenseSeatsTransformer.php@@ -79,7 +79,7 @@ class LicenseSeatsTransformerreturn null;}- if (! Gate::allows('view', $seat->user)) {+ if (Gate::denies('view', $seat->user)) {return ['id' => (int) $seat->user->id,'type' => 'user',diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.phpindex db5a4fcc4f..c3a46e9500 100644--- a/app/Http/Transformers/UsersTransformer.php+++ b/app/Http/Transformers/UsersTransformer.php@@ -42,7 +42,7 @@ class UsersTransformer// seat, an asset) legitimately needs to see WHO has it. What// gets stripped is PII (email, phone, address, department,// companies, permissions, employee_num, jobtitle, etc.).- if (auth()->id() !== $user->id && ! Gate::allows('view', $user)) {+ if (auth()->id() !== $user->id && Gate::denies('view', $user)) {return ['id' => (int) $user->id,'type' => 'user',@@ -186,7 +186,7 @@ class UsersTransformer// Gate check runs against the target instance, not the class, so// FMCS company-scoping applies too - a caller with users.view can// still be denied identity of a user in a company they can't see.- if (! Gate::allows('view', $user)) {+ if (Gate::denies('view', $user)) {return ['id' => (int) $user->id,'type' => 'user',diff --git a/tests/Feature/Assets/Api/AssignedComponentsTest.php b/tests/Feature/Assets/Api/AssignedComponentsTest.phpindex 25ff2bf520..bea323d11c 100644--- a/tests/Feature/Assets/Api/AssignedComponentsTest.php+++ b/tests/Feature/Assets/Api/AssignedComponentsTest.php@@ -43,7 +43,10 @@ class AssignedComponentsTest extends TestCase$componentsAssignedToAsset = $asset->components;- $response = $this->actingAsForApi(User::factory()->viewAssets()->create())+ // Needs viewComponents too - the transformer now omits components+ // the caller can't view, so a viewAssets-only actor would see an+ // empty list under the fix.+ $response = $this->actingAsForApi(User::factory()->viewAssets()->viewComponents()->create())->getJson(route('api.assets.assigned_components', $asset))->assertOk()// ->assertResponseContainsInRows($componentsAssignedToAsset)@@ -60,9 +63,10 @@ class AssignedComponentsTest extends TestCase* AssetsTransformer::transformCheckedoutComponents, a caller with* assets.view but not components.view could read the component's* name / assigned_qty / note through this endpoint (direct- * GET /api/v1/components/{id} correctly returned 403). Now the row- * collapses to just `{id, type: 'component'}` in the name block,- * and qty / note are nulled, when the caller lacks components.view.+ * GET /api/v1/components/{id} correctly returned 403). Now denied+ * rows are omitted entirely - the response neither reveals the+ * component's id nor that it exists at all. total is decremented so+ * it doesn't leak "there are N components you can't see" either.*/public function test_does_not_leak_component_details_when_caller_lacks_components_view(): void{@@ -79,18 +83,11 @@ class AssignedComponentsTest extends TestCase// viewAssets only. No viewComponents.$actor = User::factory()->viewAssets()->create();- $response = $this->actingAsForApi($actor)+ $this->actingAsForApi($actor)->getJson(route('api.assets.assigned_components', $asset))->assertOk()- ->assertJsonPath('total', 1)- ->assertJsonPath('rows.0.name.id', $component->id)- ->assertJsonPath('rows.0.name.type', 'component');-- $nameBlock = $response->json('rows.0.name');- $this->assertArrayNotHasKey('name', $nameBlock, 'Component name must not leak to a caller without components.view');-- $this->assertNull($response->json('rows.0.assigned_qty'), 'assigned_qty must not leak');- $this->assertNull($response->json('rows.0.note'), 'note must not leak');+ ->assertJsonPath('total', 0)+ ->assertJsonCount(0, 'rows');}/**@@ -120,4 +117,32 @@ class AssignedComponentsTest extends TestCase->assertJsonPath('rows.0.assigned_qty', 2)->assertJsonPath('rows.0.note', 'component note');}++ /**+ * Distinct code path from the dedicated /assigned/components endpoint:+ * the main asset detail response inlines full component blocks under+ * the `components` key when the caller passes `?components=true`.+ * Same class of leak, same fix pattern applied inline in+ * AssetsTransformer::transformAsset. Denied components are omitted+ * from the response entirely.+ */+ public function test_show_asset_with_components_true_query_omits_components_when_denied(): void+ {+ $asset = Asset::factory()->create();+ $component = Component::factory()->create(['name' => 'Hidden CPU', 'purchase_cost' => 500]);+ $component->assets()->attach($component->id, [+ 'component_id' => $component->id,+ 'assigned_qty' => 3,+ 'created_by' => User::factory()->superuser()->create()->id,+ 'asset_id' => $asset->id,+ ]);++ $actor = User::factory()->viewAssets()->create();++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.assets.show', ['hardware' => $asset->id, 'components' => 'true']))+ ->assertOk();++ $this->assertSame([], $response->json('components'), 'Denied components must be absent entirely - not even id / pivot_id');+ }}