A free open source IT asset/license management system
Tighten compact gates in API responses
snipe31993f4eb4a2
diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.phpindex 504a2655ad..13a890af21 100644--- a/app/Http/Transformers/AssetsTransformer.php+++ b/app/Http/Transformers/AssetsTransformer.php@@ -215,8 +215,30 @@ class AssetsTransformerpublic function transformAssignedTo($asset){+ if (! $asset->assigned) {+ return null;+ }+if ($asset->checkedOutToUser()) {- return $asset->assigned ? [+ // Info-disclosure guard: this shape is embedded in every+ // asset response (index, show, checkout-listings). A caller+ // with assets.view but not users.view used to read the+ // assignee's username / email / employee number / jobtitle+ // straight out of a hardware payload. Denied fallback keeps+ // id / type / name because someone with assets.view+ // legitimately needs to know WHO has an asset - what gets+ // 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)) {+ return [+ 'id' => (int) $asset->assigned->id,+ 'type' => 'user',+ 'name' => e($asset->assigned->display_name),+ ];+ }++ return ['id' => (int) $asset->assigned->id,'username' => e($asset->assigned->username),'name' => e($asset->assigned->display_name),@@ -226,14 +248,19 @@ class AssetsTransformer'employee_number' => ($asset->assigned->employee_num) ? e($asset->assigned->employee_num) : null,'jobtitle' => $asset->assigned->jobtitle ? e($asset->assigned->jobtitle) : null,'type' => 'user',- ] : null;+ ];}- return $asset->assigned ? [+ // Assets can be checked out to assets or locations. Basic+ // identity (name) is preserved without a permission gate on the+ // target: someone with assets.view legitimately needs to know+ // which location an asset is at or which parent asset it belongs+ // to, and neither is PII.+ return ['id' => $asset->assigned->id,'name' => e($asset->assigned->display_name),'type' => $asset->assignedType(),- ] : null;+ ];}public function transformRequestedAssets(Collection $assets, $total)@@ -404,24 +431,39 @@ class AssetsTransformer{$array = [];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);+$array[] = ['assigned_pivot_id' => $component_checkout->id,- 'name' => [- 'id' => $component_checkout->component?->id,- 'name' => e($component_checkout->component?->display_name),- 'type' => 'component',- 'deleted_at' => $component_checkout->component?->deleted_at,- ],- 'assigned_qty' => $component_checkout->assigned_qty,- 'note' => ($component_checkout->note) ? e($component_checkout->note) : null,+ '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,'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_checkout->component?->deleted_at == '') && Gate::allows('checkin', Component::class)),- 'view' => (($component_checkout->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)),],];}diff --git a/app/Http/Transformers/LicenseSeatsTransformer.php b/app/Http/Transformers/LicenseSeatsTransformer.phpindex 5cd0e03817..74f6921c29 100644--- a/app/Http/Transformers/LicenseSeatsTransformer.php+++ b/app/Http/Transformers/LicenseSeatsTransformer.php@@ -27,24 +27,7 @@ class LicenseSeatsTransformer'id' => (int) $seat->id,'license_id' => (int) $seat->license->id,'updated_at' => Helper::getFormattedDateObject($seat->updated_at, 'datetime'), // we use updated_at here because the record gets updated when it's checked in or out- 'assigned_user' => ($seat->user) ? [- 'id' => (int) $seat->user->id,- 'name' => e($seat->user->present()->fullName),- 'email' => e($seat->user->email),- 'department' => ($seat->user->department) ?- [- 'id' => (int) $seat->user->department->id,- 'name' => e($seat->user->department->name),- 'tag_color' => $seat->user->department->tag_color ? e($seat->user->department->tag_color) : null,-- ] : null,- 'companies' => $seat->user->companies->map(fn ($c) => [- 'id' => (int) $c->id,- 'name' => e($c->name),- 'tag_color' => $c->tag_color ? e($c->tag_color) : null,- ])->values(),- 'created_at' => Helper::getFormattedDateObject($seat->created_at, 'datetime'),- ] : null,+ 'assigned_user' => $this->transformAssignedUser($seat),'assigned_asset' => ($seat->asset) ? ['id' => (int) $seat->asset->id,'name' => e($seat->asset->present()->fullName),@@ -77,4 +60,48 @@ class LicenseSeatsTransformerreturn $array;}++ /**+ * Info-disclosure guard: a caller with licenses.view but not+ * users.view used to receive the assigned user's PII (email,+ * department, companies) inline in this block. Denied fallback keeps+ * id / type / name (display_name) because someone with licenses.view+ * legitimately needs to know WHO has the seat. What's stripped is+ * PII. Instance-scoped Gate check so FMCS scoping applies too.+ * The sibling assigned_asset / location blocks stay unguarded because+ * asset name and location name are basic identity, not PII, and+ * someone with licenses.view legitimately needs to know which asset+ * a seat is on and where.+ */+ private function transformAssignedUser(LicenseSeat $seat): ?array+ {+ if (! $seat->user) {+ return null;+ }++ if (! Gate::allows('view', $seat->user)) {+ return [+ 'id' => (int) $seat->user->id,+ 'type' => 'user',+ 'name' => e($seat->user->display_name),+ ];+ }++ return [+ 'id' => (int) $seat->user->id,+ 'name' => e($seat->user->present()->fullName),+ 'email' => e($seat->user->email),+ 'department' => ($seat->user->department) ? [+ 'id' => (int) $seat->user->department->id,+ 'name' => e($seat->user->department->name),+ 'tag_color' => $seat->user->department->tag_color ? e($seat->user->department->tag_color) : null,+ ] : null,+ 'companies' => $seat->user->companies->map(fn ($c) => [+ 'id' => (int) $c->id,+ 'name' => e($c->name),+ 'tag_color' => $c->tag_color ? e($c->tag_color) : null,+ ])->values(),+ 'created_at' => Helper::getFormattedDateObject($seat->created_at, 'datetime'),+ ];+ }}diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.phpindex 67e055a878..db5a4fcc4f 100644--- a/app/Http/Transformers/UsersTransformer.php+++ b/app/Http/Transformers/UsersTransformer.php@@ -21,6 +21,35 @@ class UsersTransformerpublic function transformUser(User $user){+ // Info-disclosure guard, matches the pattern on+ // transformUserCompact. This full transformer gets called by+ // nested endpoints (ConsumablesTransformer::transformCheckedoutConsumables,+ // ActionlogsTransformer, LocationsTransformer manager block) where the+ // wrapping endpoint only checked the wrapping resource's `.view`+ // permission. Without this guard, a caller with consumables.view+ // but not users.view reads the full user record (email, phone,+ // address, permissions, employee_num, department, etc.) via+ // GET /api/v1/consumables/{id}/users.+ //+ // Self-view exception: /api/v1/users/me returns $request->user()+ // through this transformer with no controller-level authorize()+ // check, and every authenticated user (regardless of users.view)+ // needs to fetch their own profile. Bypass the gate when the+ // caller is viewing themselves.+ //+ // Denied fallback includes display_name because a caller with+ // view permission on the wrapping thing (an accessory, a license+ // 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)) {+ return [+ 'id' => (int) $user->id,+ 'type' => 'user',+ 'name' => e($user->display_name),+ ];+ }+$role = null;if ($user->isSuperUser()) {$role = 'superadmin';@@ -141,6 +170,29 @@ class UsersTransformer*/public function transformUserCompact(User $user): array{+ // Info-disclosure guard. This method is embedded in the nested+ // assignee payload of accessory / license / component / asset+ // checkout listings, where the wrapping endpoint only checked the+ // wrapping resource's `.view` permission. Without this check, a+ // caller with (for example) accessories.view but not users.view+ // reads the full identity block via+ // GET /api/v1/accessories/{id}/checkedout.+ //+ // Denied fallback keeps id / type / name (display_name) because+ // view permission on the wrapping thing implies you can see WHO+ // it's checked out to. What gets stripped is PII (first_name,+ // last_name, username, email-adjacent, companies, etc.).+ //+ // 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)) {+ return [+ 'id' => (int) $user->id,+ 'type' => 'user',+ 'name' => e($user->display_name),+ ];+ }$array = ['id' => (int) $user->id,diff --git a/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.phpindex b7b500fe42..d79bf9ae52 100644--- a/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php+++ b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php@@ -110,4 +110,70 @@ class IndexAccessoryCheckoutsTest extends TestCase implements TestsFullMultipleC->assertOk()->assertJsonPath('total', 0);}++ /**+ * Security regression pin. Before the fix in UsersTransformer::transformUserCompact,+ * a caller with accessories.view but not users.view could read the+ * assignee's login handle (username), org relationships (companies),+ * and avatar hash through this endpoint. Denied fallback keeps id ++ * type + name (display_name) because someone with accessories.view+ * legitimately needs to know WHO has the accessory. Note: display_name+ * often equals "first_name last_name", so those values still surface+ * via `name` in the fallback. What the fallback actually protects+ * against is enumeration of usernames, company relationships, and+ * avatar hashes that could be used to correlate identity elsewhere.+ */+ public function test_does_not_leak_assignee_pii_when_caller_lacks_users_view(): void+ {+ $assignee = User::factory()->create([+ 'first_name' => 'Hidden',+ 'last_name' => 'Assignee',+ 'username' => 'hidden-assignee',+ ]);+ $accessory = Accessory::factory()->checkedOutToUsers([$assignee])->create();++ // viewAccessories only. No viewUsers.+ $actor = User::factory()->viewAccessories()->create();++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.accessories.checkedout', $accessory))+ ->assertOk()+ ->assertJsonPath('rows.0.assigned_to.id', $assignee->id)+ ->assertJsonPath('rows.0.assigned_to.type', 'user')+ // Display name IS present in the denied fallback - basic identity is fine.+ ->assertJsonPath('rows.0.assigned_to.name', $assignee->display_name);++ $assigned = $response->json('rows.0.assigned_to');+ // The keys that actually get stripped by the denied fallback.+ $this->assertArrayNotHasKey('username', $assigned, 'Login handle must not leak to a caller without users.view');+ $this->assertArrayNotHasKey('companies', $assigned, 'Company relationships must not leak');+ $this->assertArrayNotHasKey('image', $assigned, 'Avatar URL must not leak');+ $this->assertArrayNotHasKey('created_by', $assigned);+ $this->assertArrayNotHasKey('created_at', $assigned);+ }++ /**+ * Same fix, positive-path sanity: a caller who DOES have users.view+ * (in addition to accessories.view) still gets the full identity+ * block. Guards against my info-disclosure guard accidentally+ * stripping identity from authorized callers too.+ */+ public function test_returns_full_assignee_identity_when_caller_has_users_view(): void+ {+ $assignee = User::factory()->create([+ 'first_name' => 'Visible',+ 'last_name' => 'Assignee',+ 'username' => 'visible-assignee',+ ]);+ $accessory = Accessory::factory()->checkedOutToUsers([$assignee])->create();++ $actor = User::factory()->viewAccessories()->viewUsers()->create();++ $this->actingAsForApi($actor)+ ->getJson(route('api.accessories.checkedout', $accessory))+ ->assertOk()+ ->assertJsonPath('rows.0.assigned_to.id', $assignee->id)+ ->assertJsonPath('rows.0.assigned_to.first_name', 'Visible')+ ->assertJsonPath('rows.0.assigned_to.username', 'visible-assignee');+ }}diff --git a/tests/Feature/Assets/Api/AssignedComponentsTest.php b/tests/Feature/Assets/Api/AssignedComponentsTest.phpindex 5fdbee8c3f..25ff2bf520 100644--- a/tests/Feature/Assets/Api/AssignedComponentsTest.php+++ b/tests/Feature/Assets/Api/AssignedComponentsTest.php@@ -54,4 +54,70 @@ class AssignedComponentsTest extends TestCase->etc();});}++ /**+ * Security regression pin. Before the fix in+ * 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.+ */+ public function test_does_not_leak_component_details_when_caller_lacks_components_view(): void+ {+ $asset = Asset::factory()->create();+ $component = Component::factory()->create(['name' => 'Hidden SSD Component']);+ $component->assets()->attach($component->id, [+ 'component_id' => $component->id,+ 'assigned_qty' => 2,+ 'created_by' => User::factory()->superuser()->create()->id,+ 'asset_id' => $asset->id,+ 'note' => 'sensitive component note',+ ]);++ // viewAssets only. No viewComponents.+ $actor = User::factory()->viewAssets()->create();++ $response = $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');+ }++ /**+ * Positive-path sanity: a caller who DOES have components.view still+ * gets the full component details. Guards against the info-disclosure+ * guard accidentally stripping data from authorized callers.+ */+ public function test_returns_full_component_details_when_caller_has_components_view(): void+ {+ $asset = Asset::factory()->create();+ $component = Component::factory()->create(['name' => 'Visible SSD']);+ $component->assets()->attach($component->id, [+ 'component_id' => $component->id,+ 'assigned_qty' => 2,+ 'created_by' => User::factory()->superuser()->create()->id,+ 'asset_id' => $asset->id,+ 'note' => 'component note',+ ]);++ $actor = User::factory()->viewAssets()->viewComponents()->create();++ $this->actingAsForApi($actor)+ ->getJson(route('api.assets.assigned_components', $asset))+ ->assertOk()+ ->assertJsonPath('rows.0.name.id', $component->id)+ ->assertJsonPath('rows.0.name.name', 'Visible SSD')+ ->assertJsonPath('rows.0.assigned_qty', 2)+ ->assertJsonPath('rows.0.note', 'component note');+ }}