Skip to content

A free open source IT asset/license management system

Fixed g85w-9jhf - Added requestable transformers

snipe7625dcf773ba
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index 92592d36e2..afb1f47bc2 100644
--- a/app/Http/Controllers/Api/AccessoriesController.php
+++ b/app/Http/Controllers/Api/AccessoriesController.php
@@ -583,6 +583,6 @@ class AccessoriesController extends Controller
$rows = $query->orderBy($sort, $order)->skip($offset)->take($limit)->get();
- return (new AccessoriesTransformer)->transformAccessories($rows, $total);
+ return (new AccessoriesTransformer)->transformRequestedAccessories($rows, $total);
}
}
diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php
index 23154f5b54..330c2719ba 100644
--- a/app/Http/Controllers/Api/ComponentsController.php
+++ b/app/Http/Controllers/Api/ComponentsController.php
@@ -584,6 +584,6 @@ class ComponentsController extends Controller
$rows = $query->orderBy($sort, $order)->skip($offset)->take($limit)->get();
- return (new ComponentsTransformer)->transformComponents($rows, $total);
+ return (new ComponentsTransformer)->transformRequestedComponents($rows, $total);
}
}
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 6faead7ce4..2173150d2b 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -523,6 +523,6 @@ class ConsumablesController extends Controller
$rows = $query->orderBy($sort, $order)->skip($offset)->take($limit)->get();
- return (new ConsumablesTransformer)->transformConsumables($rows, $total);
+ return (new ConsumablesTransformer)->transformRequestedConsumables($rows, $total);
}
}
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php
index cabe1dda96..ce46e1a760 100644
--- a/app/Http/Controllers/Api/LicensesController.php
+++ b/app/Http/Controllers/Api/LicensesController.php
@@ -360,13 +360,13 @@ class LicensesController extends Controller
// GHSA-r25g-f428-466r: reject occupied seats on explicit-id
// checkout by default.
if ($licenseSeat->assigned_to !== null || $licenseSeat->asset_id !== null) {
- if (!($validated['reassign'] ?? false)) {
+ if (! ($validated['reassign'] ?? false)) {
$errorResponse = response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/licenses/message.checkout.unavailable')));
return;
}
- if (!$licenseSeat->license->reassignable) {
+ if (! $licenseSeat->license->reassignable) {
$errorResponse = response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/licenses/message.checkout.unavailable')));
return;
@@ -384,7 +384,7 @@ class LicensesController extends Controller
->where('checkoutable_id', $licenseSeat->id)
->where('assigned_to_id', $displaced->id)
->get()
- ->each(fn($a) => $a->delete());
+ ->each(fn ($a) => $a->delete());
}
$licenseSeat->assigned_to = null;
@@ -565,6 +565,6 @@ class LicensesController extends Controller
$rows = $query->orderBy($sort, $order)->skip($offset)->take($limit)->get();
- return (new LicensesTransformer)->transformLicenses($rows, $total);
+ return (new LicensesTransformer)->transformRequestedLicenses($rows, $total);
}
}
diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php
index 019dd9bd42..da49af7d55 100644
--- a/app/Http/Transformers/AccessoriesTransformer.php
+++ b/app/Http/Transformers/AccessoriesTransformer.php
@@ -132,6 +132,44 @@ class AccessoriesTransformer
return $array;
}
+ public function transformRequestedAccessories(Collection $accessories, $total)
+ {
+ $array = [];
+ foreach ($accessories as $accessory) {
+ $array[] = self::transformRequestedAccessory($accessory);
+ }
+
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformRequestedAccessory(Accessory $accessory)
+ {
+ $userHasOpenRequest = auth()->check() && $accessory->relationLoaded('requests') && $accessory->requests->contains(
+ fn (\App\Models\CheckoutRequest $request) => $request->user_id === auth()->id() && $request->canceled_at === null
+ );
+
+ return [
+ 'id' => (int) $accessory->id,
+ 'name' => e($accessory->name),
+ 'image' => ($accessory->image) ? Storage::disk('public')->url(FileStorage::Accessories->publicPath().e($accessory->image)) : null,
+ 'model_number' => ($accessory->model_number) ? e($accessory->model_number) : null,
+ 'location' => ($accessory->location) ? [
+ 'id' => (int) $accessory->location->id,
+ 'name' => e($accessory->location->name),
+ 'tag_color' => ($accessory->location->tag_color) ? e($accessory->location->tag_color) : null,
+ ] : null,
+ 'qty' => ($accessory->qty) ? (int) $accessory->qty : null,
+ 'remaining' => (int) ($accessory->qty - $accessory->checkouts_count),
+ 'remaining_qty' => (int) ($accessory->qty - $accessory->checkouts_count),
+ 'requestable' => (bool) $accessory->requestable,
+ 'assigned_to_self' => $userHasOpenRequest,
+ 'available_actions' => [
+ 'request' => (bool) $accessory->requestable && ! $userHasOpenRequest,
+ 'cancel' => (bool) $accessory->requestable && $userHasOpenRequest,
+ ],
+ ];
+ }
+
public function transformCheckedoutAccessory($accessory_checkouts, $total)
{
$array = [];
diff --git a/app/Http/Transformers/ComponentsTransformer.php b/app/Http/Transformers/ComponentsTransformer.php
index 4ce3df3426..e61bd09812 100644
--- a/app/Http/Transformers/ComponentsTransformer.php
+++ b/app/Http/Transformers/ComponentsTransformer.php
@@ -111,6 +111,44 @@ class ComponentsTransformer
return $array;
}
+ public function transformRequestedComponents(Collection $components, $total)
+ {
+ $array = [];
+ foreach ($components as $component) {
+ $array[] = self::transformRequestedComponent($component);
+ }
+
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformRequestedComponent(Component $component)
+ {
+ $userHasOpenRequest = auth()->check() && $component->relationLoaded('requests') && $component->requests->contains(
+ fn (\App\Models\CheckoutRequest $request) => $request->user_id === auth()->id() && $request->canceled_at === null
+ );
+
+ return [
+ 'id' => (int) $component->id,
+ 'name' => e($component->name),
+ 'image' => ($component->image) ? Storage::disk('public')->url(FileStorage::Components->publicPath().e($component->image)) : null,
+ 'serial' => ($component->serial) ? e($component->serial) : null,
+ 'model_number' => ($component->model_number) ? e($component->model_number) : null,
+ 'location' => ($component->location) ? [
+ 'id' => (int) $component->location->id,
+ 'name' => e($component->location->name),
+ 'tag_color' => $component->location->tag_color ? e($component->location->tag_color) : null,
+ ] : null,
+ 'qty' => ($component->qty != '') ? (int) $component->qty : null,
+ 'remaining' => (int) $component->numRemaining(),
+ 'requestable' => (bool) $component->requestable,
+ 'assigned_to_self' => $userHasOpenRequest,
+ 'available_actions' => [
+ 'request' => (bool) $component->requestable && ! $userHasOpenRequest,
+ 'cancel' => (bool) $component->requestable && $userHasOpenRequest,
+ ],
+ ];
+ }
+
public function transformCheckedoutComponents(Collection $components_assets, $total)
{
$array = [];
diff --git a/app/Http/Transformers/ConsumablesTransformer.php b/app/Http/Transformers/ConsumablesTransformer.php
index 8779856ee1..58fd325a20 100644
--- a/app/Http/Transformers/ConsumablesTransformer.php
+++ b/app/Http/Transformers/ConsumablesTransformer.php
@@ -114,6 +114,43 @@ class ConsumablesTransformer
return $array;
}
+ public function transformRequestedConsumables(Collection $consumables, $total)
+ {
+ $array = [];
+ foreach ($consumables as $consumable) {
+ $array[] = self::transformRequestedConsumable($consumable);
+ }
+
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformRequestedConsumable(Consumable $consumable)
+ {
+ $userHasOpenRequest = auth()->check() && $consumable->relationLoaded('requests') && $consumable->requests->contains(
+ fn (\App\Models\CheckoutRequest $request) => $request->user_id === auth()->id() && $request->canceled_at === null
+ );
+
+ return [
+ 'id' => (int) $consumable->id,
+ 'name' => e($consumable->name),
+ 'image' => ($consumable->getImageUrl()) ? ($consumable->getImageUrl()) : null,
+ 'model_number' => ($consumable->model_number != '') ? e($consumable->model_number) : null,
+ 'location' => ($consumable->location) ? [
+ 'id' => (int) $consumable->location->id,
+ 'name' => e($consumable->location->name),
+ 'tag_color' => $consumable->location->tag_color ? e($consumable->location->tag_color) : null,
+ ] : null,
+ 'qty' => (int) $consumable->qty,
+ 'remaining' => $consumable->numRemaining(),
+ 'requestable' => (bool) $consumable->requestable,
+ 'assigned_to_self' => $userHasOpenRequest,
+ 'available_actions' => [
+ 'request' => (bool) $consumable->requestable && ! $userHasOpenRequest,
+ 'cancel' => (bool) $consumable->requestable && $userHasOpenRequest,
+ ],
+ ];
+ }
+
public function transformCheckedoutConsumables(Collection $consumables_users, $total)
{
$array = [];
diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php
index 776c25bc2e..0a41068316 100644
--- a/app/Http/Transformers/LicensesTransformer.php
+++ b/app/Http/Transformers/LicensesTransformer.php
@@ -109,4 +109,41 @@ class LicensesTransformer
{
return (new DatatablesTransformer)->transformDatatables($licenses);
}
+
+ public function transformRequestedLicenses(Collection $licenses, $total)
+ {
+ $array = [];
+ foreach ($licenses as $license) {
+ $array[] = self::transformRequestedLicense($license);
+ }
+
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformRequestedLicense(License $license)
+ {
+ $unreassignable = $license->reassignable ? 0 : (int) ($license->unreassignable_seats_count ?? License::unReassignableCount($license));
+ $userHasOpenRequest = auth()->check() && $license->relationLoaded('requests') && $license->requests->contains(
+ fn (\App\Models\CheckoutRequest $request) => $request->user_id === auth()->id() && $request->canceled_at === null
+ );
+
+ return [
+ 'id' => (int) $license->id,
+ 'name' => e($license->name),
+ 'category' => ($license->category) ? [
+ 'id' => (int) $license->category->id,
+ 'name' => e($license->category->name),
+ 'tag_color' => ($license->category->tag_color) ? e($license->category->tag_color) : null,
+ ] : null,
+ 'seats' => (int) $license->seats,
+ 'free_seats_count' => (int) $license->free_seats_count - $unreassignable,
+ 'remaining' => (int) $license->free_seats_count,
+ 'requestable' => (bool) $license->requestable,
+ 'assigned_to_self' => $userHasOpenRequest,
+ 'available_actions' => [
+ 'request' => (bool) $license->requestable && ! $userHasOpenRequest,
+ 'cancel' => (bool) $license->requestable && $userHasOpenRequest,
+ ],
+ ];
+ }
}
diff --git a/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
new file mode 100644
index 0000000000..ecdfbd40d0
--- /dev/null
+++ b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
@@ -0,0 +1,133 @@
+<?php
+
+namespace Tests\Feature\Requests\Api;
+
+use App\Models\Accessory;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Regression coverage for GHSA-g85w-9jhf-37h8. The /account/requestable
+ * tabs are open to any authenticated user (that is intentional - the
+ * page itself is open), and previously each tab's API endpoint emitted
+ * the full admin transformer shape, which leaked purchase_cost,
+ * supplier, notes, order_number, purchase_order, and for licenses the
+ * license_name + license_email licensee contact details to callers
+ * without the corresponding `view` permission. The fix adds a reduced
+ * `transformRequested*` method on each transformer, matching the shape
+ * `AssetsTransformer::transformRequestedAsset` already uses for the
+ * hardware tab, and the four `requestable()` controllers now call it.
+ *
+ * Each test here is run as a user with no permissions, which is the
+ * reporter's threat model (any authenticated account should be able to
+ * reach these endpoints and get only the display fields the UI needs).
+ */
+class RequestableTransformerFieldLeakTest extends TestCase
+{
+ public function test_accessories_requestable_response_does_not_expose_sensitive_fields(): void
+ {
+ Accessory::factory()->create(['requestable' => 1]);
+
+ $row = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.accessories.requestable'))
+ ->assertOk()
+ ->json('rows.0');
+
+ $this->assertNotNull($row);
+ foreach (['purchase_cost', 'total_cost', 'supplier', 'notes', 'orders', 'purchase_date', 'min_amt', 'created_by'] as $leakedField) {
+ $this->assertArrayNotHasKey(
+ $leakedField,
+ $row,
+ "Field `{$leakedField}` must not appear in the /account/requestable accessories response. It leaks admin-only data to callers without accessories.view."
+ );
+ }
+
+ // Positive assertions on the fields the UI formatter needs.
+ $this->assertArrayHasKey('id', $row);
+ $this->assertArrayHasKey('name', $row);
+ $this->assertArrayHasKey('remaining_qty', $row);
+ $this->assertArrayHasKey('assigned_to_self', $row);
+ $this->assertArrayHasKey('request', $row['available_actions']);
+ }
+
+ public function test_consumables_requestable_response_does_not_expose_sensitive_fields(): void
+ {
+ Consumable::factory()->create(['requestable' => 1]);
+
+ $row = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.consumables.requestable'))
+ ->assertOk()
+ ->json('rows.0');
+
+ $this->assertNotNull($row);
+ foreach (['purchase_cost', 'total_cost', 'supplier', 'notes', 'orders', 'purchase_date', 'min_amt', 'created_by'] as $leakedField) {
+ $this->assertArrayNotHasKey(
+ $leakedField,
+ $row,
+ "Field `{$leakedField}` must not appear in the /account/requestable consumables response."
+ );
+ }
+
+ $this->assertArrayHasKey('remaining', $row);
+ $this->assertArrayHasKey('assigned_to_self', $row);
+ $this->assertArrayHasKey('request', $row['available_actions']);
+ }
+
+ public function test_components_requestable_response_does_not_expose_sensitive_fields(): void
+ {
+ Component::factory()->create(['requestable' => 1]);
+
+ $row = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.components.requestable'))
+ ->assertOk()
+ ->json('rows.0');
+
+ $this->assertNotNull($row);
+ foreach (['purchase_cost', 'total_cost', 'supplier', 'notes', 'orders', 'purchase_date', 'min_amt', 'created_by'] as $leakedField) {
+ $this->assertArrayNotHasKey(
+ $leakedField,
+ $row,
+ "Field `{$leakedField}` must not appear in the /account/requestable components response."
+ );
+ }
+
+ $this->assertArrayHasKey('remaining', $row);
+ $this->assertArrayHasKey('assigned_to_self', $row);
+ $this->assertArrayHasKey('request', $row['available_actions']);
+ }
+
+ public function test_licenses_requestable_response_does_not_expose_sensitive_fields(): void
+ {
+ License::factory()->create([
+ 'requestable' => 1,
+ 'purchase_cost' => '499.99',
+ 'order_number' => 'ORD-INTERNAL-1234',
+ 'purchase_order' => 'PO-INTERNAL-5678',
+ 'notes' => 'Admin-only context about this license',
+ 'license_name' => 'Alice Admin',
+ 'license_email' => '[email protected]',
+ ]);
+
+ $row = $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.licenses.requestable'))
+ ->assertOk()
+ ->json('rows.0');
+
+ $this->assertNotNull($row);
+ foreach (['purchase_cost', 'purchase_cost_numeric', 'supplier', 'notes', 'order_number', 'purchase_order', 'purchase_date', 'license_name', 'license_email', 'product_key', 'min_amt', 'created_by'] as $leakedField) {
+ $this->assertArrayNotHasKey(
+ $leakedField,
+ $row,
+ "Field `{$leakedField}` must not appear in the /account/requestable licenses response. It leaks admin-only data (including licensee contact details) to callers without licenses.view."
+ );
+ }
+
+ $this->assertArrayHasKey('free_seats_count', $row);
+ $this->assertArrayHasKey('category', $row);
+ $this->assertArrayHasKey('assigned_to_self', $row);
+ $this->assertArrayHasKey('request', $row['available_actions']);
+ }
+}