Skip to content

A free open source IT asset/license management system

Tighter gates on search fields in requestable list

snipe9328fe7b8226
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index 6f917b1feb..0bef286873 100644
--- a/app/Http/Controllers/Api/AccessoriesController.php
+++ b/app/Http/Controllers/Api/AccessoriesController.php
@@ -819,12 +819,18 @@ class AccessoriesController extends Controller
*/
public function requestable(Request $request): array
{
+ $this->authorize('viewRequestable', Accessory::class);
+
$query = Accessory::with('category', 'location', 'company', 'manufacturer', 'requests')
->withCount('checkouts as checkouts_count')
->Requestable();
if ($request->filled('search')) {
- $query->TextSearch($request->input('search'));
+ $search = $request->input('search');
+ $query->where(function ($q) use ($search) {
+ $q->where('accessories.name', 'LIKE', '%'.$search.'%')
+ ->orWhere('accessories.model_number', 'LIKE', '%'.$search.'%');
+ });
}
$total = $query->count();
diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php
index 52f4ad3c0e..e92815a1c4 100644
--- a/app/Http/Controllers/Api/ComponentsController.php
+++ b/app/Http/Controllers/Api/ComponentsController.php
@@ -823,12 +823,18 @@ class ComponentsController extends Controller
*/
public function requestable(Request $request): array
{
+ $this->authorize('viewRequestable', Component::class);
+
$query = Component::with('category', 'location', 'company', 'manufacturer', 'requests')
->withCount('assets as components_assets_count')
->Requestable();
if ($request->filled('search')) {
- $query->TextSearch($request->input('search'));
+ $search = $request->input('search');
+ $query->where(function ($q) use ($search) {
+ $q->where('components.name', 'LIKE', '%'.$search.'%')
+ ->orWhere('components.model_number', 'LIKE', '%'.$search.'%');
+ });
}
$total = $query->count();
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 92adfa15bc..09c3960eae 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -622,13 +622,19 @@ class ConsumablesController extends Controller
*/
public function requestable(Request $request): array
{
+ $this->authorize('viewRequestable', Consumable::class);
+
$query = Consumable::with('category', 'location', 'company', 'manufacturer', 'requests')
->withCount('consumableAssignments as consumables_users_count')
->withCount('outstandingAssignments as outstanding_assignments_count')
->Requestable();
if ($request->filled('search')) {
- $query->TextSearch($request->input('search'));
+ $search = $request->input('search');
+ $query->where(function ($q) use ($search) {
+ $q->where('consumables.name', 'LIKE', '%'.$search.'%')
+ ->orWhere('consumables.model_number', 'LIKE', '%'.$search.'%');
+ });
}
$total = $query->count();
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php
index ce46e1a760..76b1c36003 100644
--- a/app/Http/Controllers/Api/LicensesController.php
+++ b/app/Http/Controllers/Api/LicensesController.php
@@ -549,11 +549,14 @@ class LicensesController extends Controller
*/
public function requestable(Request $request): array
{
+ $this->authorize('viewRequestable', License::class);
+
$query = License::with('category', 'company', 'manufacturer', 'requests')
->Requestable();
if ($request->filled('search')) {
- $query->TextSearch($request->input('search'));
+ $search = $request->input('search');
+ $query->where('licenses.name', 'LIKE', '%'.$search.'%');
}
$total = $query->count();
diff --git a/app/Policies/AccessoryPolicy.php b/app/Policies/AccessoryPolicy.php
index 0a9bcc2a3c..195a972c08 100644
--- a/app/Policies/AccessoryPolicy.php
+++ b/app/Policies/AccessoryPolicy.php
@@ -15,4 +15,16 @@ class AccessoryPolicy extends CheckoutablePermissionsPolicy
{
return $user->hasAccess($this->columnName().'.files');
}
+
+ /**
+ * Gates the /api/v1/account/requestable/accessories endpoint. Reuses
+ * the `assets.view.requestable` permission alongside AssetPolicy /
+ * ConsumablePolicy / ComponentPolicy so the four requestable
+ * endpoints have one consistent gate. This is a bit of a hack,
+ * but it works and avoids creating a new permission just for this one endpoint.
+ */
+ public function viewRequestable(User $user, $item = null)
+ {
+ return $user->hasAccess('assets.view.requestable');
+ }
}
diff --git a/app/Policies/ComponentPolicy.php b/app/Policies/ComponentPolicy.php
index 03102edcea..e352487c71 100644
--- a/app/Policies/ComponentPolicy.php
+++ b/app/Policies/ComponentPolicy.php
@@ -15,4 +15,16 @@ class ComponentPolicy extends CheckoutablePermissionsPolicy
{
return $user->hasAccess($this->columnName().'.files');
}
+
+ /**
+ * Gates the /api/v1/account/requestable/components endpoint. Reuses
+ * the `assets.view.requestable` permission alongside AssetPolicy /
+ * AccessoryPolicy / ConsumablePolicy so the four requestable
+ * endpoints have one consistent gate. This is a bit of a hack,
+ * but it works and avoids creating a new permission just for this one endpoint.
+ */
+ public function viewRequestable(User $user, $item = null)
+ {
+ return $user->hasAccess('assets.view.requestable');
+ }
}
diff --git a/app/Policies/ConsumablePolicy.php b/app/Policies/ConsumablePolicy.php
index 1da9d1d779..e7d7ea0f05 100644
--- a/app/Policies/ConsumablePolicy.php
+++ b/app/Policies/ConsumablePolicy.php
@@ -15,4 +15,16 @@ class ConsumablePolicy extends CheckoutablePermissionsPolicy
{
return $user->hasAccess($this->columnName().'.files');
}
+
+ /**
+ * Gates the /api/v1/account/requestable/consumables endpoint. Reuses
+ * the `assets.view.requestable` permission alongside AssetPolicy /
+ * AccessoryPolicy / ComponentPolicy so the four requestable
+ * endpoints have one consistent gate. This is a bit of a hack,
+ * but it works and avoids creating a new permission just for this one endpoint.
+ */
+ public function viewRequestable(User $user, $item = null)
+ {
+ return $user->hasAccess('assets.view.requestable');
+ }
}
diff --git a/app/Policies/LicensePolicy.php b/app/Policies/LicensePolicy.php
index 3dd0da4790..8258a1a441 100644
--- a/app/Policies/LicensePolicy.php
+++ b/app/Policies/LicensePolicy.php
@@ -44,4 +44,16 @@ class LicensePolicy extends CheckoutablePermissionsPolicy
{
return $user->hasAccess($this->columnName().'.files');
}
+
+ /**
+ * Gates the /api/v1/account/requestable/licenses endpoint. Reuses
+ * the `assets.view.requestable` permission alongside AssetPolicy /
+ * AccessoryPolicy / ConsumablePolicy / ComponentPolicy so the five
+ * requestable endpoints have one consistent gate. This is a bit of a hack,
+ * but it works and avoids creating a new permission just for this one endpoint.
+ */
+ public function viewRequestable(User $user, $item = null)
+ {
+ return $user->hasAccess('assets.view.requestable');
+ }
}
diff --git a/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php b/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php
new file mode 100644
index 0000000000..da504f9eff
--- /dev/null
+++ b/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php
@@ -0,0 +1,194 @@
+<?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-944j-96m5-3vpp (accessories / consumables /
+ * components) and GHSA-wgj5-4mrx-52rx (licenses). The four non-Asset
+ * requestable endpoints had two paired gaps.
+ *
+ * 1. Authz: no `authorize()` call. Any authenticated user could hit them
+ * regardless of role. AssetController::requestable gated on
+ * `assets.view.requestable` but the other four did not. Fix: added
+ * `viewRequestable` policy methods on AccessoryPolicy, ConsumablePolicy,
+ * ComponentPolicy, LicensePolicy, each reusing the same
+ * `assets.view.requestable` permission so the five requestable
+ * endpoints share one consistent gate.
+ *
+ * 2. Search oracle: `TextSearch` on the request `search` param walked
+ * each model's full `$searchableAttributes`, which included
+ * admin-only columns (`notes` on accessories / consumables /
+ * components, `serial` + `notes` + `license_name` + `license_email` +
+ * `order_number` + `purchase_order` on licenses). The output-side
+ * transformer stripped those fields but the result-count
+ * differential still let a caller use the search param as a content
+ * oracle. Fix: scoped the search columns in each `requestable()`
+ * controller to the exact set the UI tab exposes (`name` +
+ * `model_number` on the three items, `name` on licenses).
+ */
+class RequestableAuthzAndSearchOracleTest extends TestCase
+{
+ private const NOTES_SENTINEL = 'INTERNAL-REORDER-FROM-ACME-ONLY';
+
+ private const LICENSE_SERIAL_SENTINEL = 'ZX9Q7M2K-SECRET-PRODUCT-KEY';
+
+ public function test_accessories_requestable_requires_assets_view_requestable_permission(): void
+ {
+ Accessory::factory()->create(['requestable' => 1]);
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.accessories.requestable'))
+ ->assertForbidden();
+ }
+
+ public function test_consumables_requestable_requires_assets_view_requestable_permission(): void
+ {
+ Consumable::factory()->create(['requestable' => 1]);
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.consumables.requestable'))
+ ->assertForbidden();
+ }
+
+ public function test_components_requestable_requires_assets_view_requestable_permission(): void
+ {
+ Component::factory()->create(['requestable' => 1]);
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.components.requestable'))
+ ->assertForbidden();
+ }
+
+ public function test_licenses_requestable_requires_assets_view_requestable_permission(): void
+ {
+ License::factory()->create(['requestable' => 1]);
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.licenses.requestable'))
+ ->assertForbidden();
+ }
+
+ public function test_accessories_requestable_search_cannot_oracle_notes(): void
+ {
+ Accessory::factory()->create([
+ 'requestable' => 1,
+ 'name' => 'Headphones',
+ 'notes' => self::NOTES_SENTINEL,
+ ]);
+
+ $caller = User::factory()->viewRequestableAssets()->create();
+
+ $bySentinelSubstring = $this->actingAsForApi($caller)
+ ->getJson(route('api.accessories.requestable', ['search' => 'REORDER-FROM-ACME']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $bySentinelSubstring['total']);
+
+ $structuredOracle = $this->actingAsForApi($caller)
+ ->getJson(route('api.accessories.requestable', [
+ 'search' => '{"notes":"is:'.self::NOTES_SENTINEL.'"}',
+ ]))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $structuredOracle['total']);
+
+ $byName = $this->actingAsForApi($caller)
+ ->getJson(route('api.accessories.requestable', ['search' => 'Headphones']))
+ ->assertOk()
+ ->json();
+ $this->assertGreaterThan(0, $byName['total']);
+ }
+
+ public function test_consumables_requestable_search_cannot_oracle_notes(): void
+ {
+ Consumable::factory()->create([
+ 'requestable' => 1,
+ 'name' => 'Printer Paper',
+ 'notes' => self::NOTES_SENTINEL,
+ ]);
+
+ $caller = User::factory()->viewRequestableAssets()->create();
+
+ $this->assertSame(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.consumables.requestable', ['search' => 'REORDER-FROM-ACME']))
+ ->assertOk()
+ ->json('total'));
+
+ $this->assertGreaterThan(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.consumables.requestable', ['search' => 'Printer']))
+ ->assertOk()
+ ->json('total'));
+ }
+
+ public function test_components_requestable_search_cannot_oracle_notes(): void
+ {
+ Component::factory()->create([
+ 'requestable' => 1,
+ 'name' => 'RAM Module',
+ 'notes' => self::NOTES_SENTINEL,
+ ]);
+
+ $caller = User::factory()->viewRequestableAssets()->create();
+
+ $this->assertSame(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.components.requestable', ['search' => 'REORDER-FROM-ACME']))
+ ->assertOk()
+ ->json('total'));
+
+ $this->assertGreaterThan(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.components.requestable', ['search' => 'RAM']))
+ ->assertOk()
+ ->json('total'));
+ }
+
+ public function test_licenses_requestable_search_cannot_oracle_serial(): void
+ {
+ License::factory()->create([
+ 'requestable' => 1,
+ 'name' => 'Adobe CC',
+ 'serial' => self::LICENSE_SERIAL_SENTINEL,
+ 'notes' => self::NOTES_SENTINEL,
+ 'license_name' => 'Alice Admin',
+ 'license_email' => '[email protected]',
+ ]);
+
+ $caller = User::factory()->viewRequestableAssets()->create();
+
+ $bySerialSubstring = $this->actingAsForApi($caller)
+ ->getJson(route('api.licenses.requestable', ['search' => 'ZX9Q7M2K']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $bySerialSubstring['total']);
+
+ $byNotesSubstring = $this->actingAsForApi($caller)
+ ->getJson(route('api.licenses.requestable', ['search' => 'REORDER-FROM-ACME']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $byNotesSubstring['total']);
+
+ $byLicenseName = $this->actingAsForApi($caller)
+ ->getJson(route('api.licenses.requestable', ['search' => 'Alice']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $byLicenseName['total']);
+
+ $byLicenseEmail = $this->actingAsForApi($caller)
+ ->getJson(route('api.licenses.requestable', ['search' => 'alice@internal']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $byLicenseEmail['total']);
+
+ $byName = $this->actingAsForApi($caller)
+ ->getJson(route('api.licenses.requestable', ['search' => 'Adobe']))
+ ->assertOk()
+ ->json();
+ $this->assertGreaterThan(0, $byName['total']);
+ }
+}
diff --git a/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
index ecdfbd40d0..a0e1292be7 100644
--- a/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
+++ b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
@@ -21,9 +21,12 @@ use Tests\TestCase;
* `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).
+ * Each test here is run as a user holding only `assets.view.requestable`
+ * (granted via `->viewRequestableAssets()`). That permission now gates
+ * all five requestable endpoints after GHSA-944j-96m5-3vpp +
+ * GHSA-wgj5-4mrx-52rx, which closed the "no authz check at all" gap on
+ * the four non-Asset variants. See `RequestableAuthzAndSearchOracleTest`
+ * for the gate + search-oracle regression coverage.
*/
class RequestableTransformerFieldLeakTest extends TestCase
{
@@ -31,7 +34,7 @@ class RequestableTransformerFieldLeakTest extends TestCase
{
Accessory::factory()->create(['requestable' => 1]);
- $row = $this->actingAsForApi(User::factory()->create())
+ $row = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.accessories.requestable'))
->assertOk()
->json('rows.0');
@@ -57,7 +60,7 @@ class RequestableTransformerFieldLeakTest extends TestCase
{
Consumable::factory()->create(['requestable' => 1]);
- $row = $this->actingAsForApi(User::factory()->create())
+ $row = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.consumables.requestable'))
->assertOk()
->json('rows.0');
@@ -80,7 +83,7 @@ class RequestableTransformerFieldLeakTest extends TestCase
{
Component::factory()->create(['requestable' => 1]);
- $row = $this->actingAsForApi(User::factory()->create())
+ $row = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.components.requestable'))
->assertOk()
->json('rows.0');
@@ -111,7 +114,7 @@ class RequestableTransformerFieldLeakTest extends TestCase
'license_email' => '[email protected]',
]);
- $row = $this->actingAsForApi(User::factory()->create())
+ $row = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.licenses.requestable'))
->assertOk()
->json('rows.0');