Skip to content

A free open source IT asset/license management system

Tightened search on requestable asset models, fixed tests

snipe89b6d4c24537
diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php
index cf5aa897b3..a05d48996e 100644
--- a/app/Http/Controllers/Api/AssetModelsController.php
+++ b/app/Http/Controllers/Api/AssetModelsController.php
@@ -422,12 +422,18 @@ class AssetModelsController extends Controller
*/
public function requestable(Request $request): array
{
+ $this->authorize('viewRequestable', AssetModel::class);
+
$query = AssetModel::with('category', 'manufacturer', 'requests')
->withCount('availableAssets as remaining')
->Requestable();
if ($request->filled('search')) {
- $query->TextSearch($request->input('search'));
+ $search = $request->input('search');
+ $query->where(function ($q) use ($search) {
+ $q->where('models.name', 'LIKE', '%'.$search.'%')
+ ->orWhere('models.model_number', 'LIKE', '%'.$search.'%');
+ });
}
$total = $query->count();
@@ -439,6 +445,6 @@ class AssetModelsController extends Controller
$rows = $query->orderBy($sort, $order)->skip($offset)->take($limit)->get();
- return (new AssetModelsTransformer)->transformAssetModels($rows, $total);
+ return (new AssetModelsTransformer)->transformRequestedModels($rows, $total);
}
}
diff --git a/app/Http/Transformers/AssetModelsTransformer.php b/app/Http/Transformers/AssetModelsTransformer.php
index 590c79221f..e0a6827251 100644
--- a/app/Http/Transformers/AssetModelsTransformer.php
+++ b/app/Http/Transformers/AssetModelsTransformer.php
@@ -130,6 +130,38 @@ class AssetModelsTransformer
return $array;
}
+ public function transformRequestedModels(Collection $assetmodels, $total)
+ {
+ $array = [];
+ foreach ($assetmodels as $assetmodel) {
+ $array[] = self::transformRequestedModel($assetmodel);
+ }
+
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformRequestedModel(AssetModel $assetmodel)
+ {
+ $userHasOpenRequest = auth()->check() && $assetmodel->relationLoaded('requests') && $assetmodel->requests->contains(
+ fn (\App\Models\CheckoutRequest $request) => $request->user_id === auth()->id() && $request->canceled_at === null
+ );
+
+ return [
+ 'id' => (int) $assetmodel->id,
+ 'name' => e($assetmodel->name),
+ 'image' => ($assetmodel->image != '') ? Storage::disk('public')->url(FileStorage::Models->publicPath().e($assetmodel->image)) : null,
+ 'model_number' => ($assetmodel->model_number) ? e($assetmodel->model_number) : null,
+ 'remaining' => $assetmodel->remaining !== null
+ ? (int) $assetmodel->remaining
+ : (int) ($assetmodel->assets_count - (int) $assetmodel->assets_assigned_count) - (int) $assetmodel->assets_archived_count,
+ 'assigned_to_self' => $userHasOpenRequest,
+ 'available_actions' => [
+ 'request' => ! $userHasOpenRequest,
+ 'cancel' => $userHasOpenRequest,
+ ],
+ ];
+ }
+
public function transformAssetModelFiles($assetmodel, $total)
{
diff --git a/app/Policies/AssetModelPolicy.php b/app/Policies/AssetModelPolicy.php
index 2fa90750fa..2f9369c652 100644
--- a/app/Policies/AssetModelPolicy.php
+++ b/app/Policies/AssetModelPolicy.php
@@ -38,4 +38,15 @@ class AssetModelPolicy extends SnipePermissionsPolicy
{
return $user->hasAccess($this->columnName().'.files');
}
+
+ /**
+ * Gates /api/v1/account/requestable/models. Reuses the existing
+ * `assets.view.requestable` permission so the five requestable
+ * endpoints share 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/phpstan-baseline.neon b/phpstan-baseline.neon
index 1d96226db8..b111f8193d 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -432,12 +432,6 @@ parameters:
count: 1
path: app/Console/Commands/SyncAssetLocations.php
- -
- message: '#^Empty array passed to foreach\.$#'
- identifier: foreach.emptyArray
- count: 1
- path: app/Console/Commands/SyncAssetLocations.php
-
-
message: '#^If condition is always true\.$#'
identifier: if.alwaysTrue
@@ -2637,19 +2631,19 @@ parameters:
-
message: '#^Access to an undefined property App\\Models\\AssetModel\:\:\$assets_archived_count\.$#'
identifier: property.notFound
- count: 2
+ count: 3
path: app/Http/Transformers/AssetModelsTransformer.php
-
message: '#^Access to an undefined property App\\Models\\AssetModel\:\:\$assets_assigned_count\.$#'
identifier: property.notFound
- count: 2
+ count: 3
path: app/Http/Transformers/AssetModelsTransformer.php
-
message: '#^Access to an undefined property App\\Models\\AssetModel\:\:\$remaining\.$#'
identifier: property.notFound
- count: 1
+ count: 2
path: app/Http/Transformers/AssetModelsTransformer.php
-
@@ -7710,12 +7704,6 @@ parameters:
count: 1
path: database/migrations/2015_07_29_230054_add_thread_id_to_asset_logs_table.php
- -
- message: '#^Result of static method Illuminate\\Support\\Facades\\Schema\:\:table\(\) \(void\) is used\.$#'
- identifier: staticMethod.void
- count: 1
- path: database/migrations/2015_09_22_003413_migrate_mac_address.php
-
-
message: '#^Property App\\Models\\IdeHelperSetting\:\:\$ldap_enabled \(string\|null\) does not accept int\.$#'
identifier: assign.propertyType
diff --git a/tests/Feature/Consumables/Api/ConsumableIndexTest.php b/tests/Feature/Consumables/Api/ConsumableIndexTest.php
index 43f0416efb..f979261ecc 100644
--- a/tests/Feature/Consumables/Api/ConsumableIndexTest.php
+++ b/tests/Feature/Consumables/Api/ConsumableIndexTest.php
@@ -179,7 +179,7 @@ class ConsumableIndexTest extends TestCase
->checkedOutToUser($userInCompanyB)
->create(['qty' => 3, 'requestable' => true]);
- $this->actingAsForApi(User::factory()->forCompany($companyA)->create())
+ $this->actingAsForApi(User::factory()->viewRequestableAssets()->forCompany($companyA)->create())
->getJson(route('api.consumables.requestable'))
->assertOk()
->assertJsonPath('rows.0.id', $consumableForCompanyA->id)
diff --git a/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php b/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php
index da504f9eff..04eb7091bf 100644
--- a/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php
+++ b/tests/Feature/Requests/Api/RequestableAuthzAndSearchOracleTest.php
@@ -3,6 +3,7 @@
namespace Tests\Feature\Requests\Api;
use App\Models\Accessory;
+use App\Models\AssetModel;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\License;
@@ -75,6 +76,36 @@ class RequestableAuthzAndSearchOracleTest extends TestCase
->assertForbidden();
}
+ public function test_models_requestable_requires_assets_view_requestable_permission(): void
+ {
+ AssetModel::factory()->create(['requestable' => 1]);
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.assetmodels.requestable'))
+ ->assertForbidden();
+ }
+
+ public function test_models_requestable_search_cannot_oracle_notes(): void
+ {
+ AssetModel::factory()->create([
+ 'requestable' => 1,
+ 'name' => 'MacBook Pro M3',
+ 'notes' => self::NOTES_SENTINEL,
+ ]);
+
+ $caller = User::factory()->viewRequestableAssets()->create();
+
+ $this->assertSame(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.assetmodels.requestable', ['search' => 'REORDER-FROM-ACME']))
+ ->assertOk()
+ ->json('total'));
+
+ $this->assertGreaterThan(0, $this->actingAsForApi($caller)
+ ->getJson(route('api.assetmodels.requestable', ['search' => 'MacBook']))
+ ->assertOk()
+ ->json('total'));
+ }
+
public function test_accessories_requestable_search_cannot_oracle_notes(): void
{
Accessory::factory()->create([
diff --git a/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
index a0e1292be7..24a0fef0df 100644
--- a/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
+++ b/tests/Feature/Requests/Api/RequestableTransformerFieldLeakTest.php
@@ -3,6 +3,7 @@
namespace Tests\Feature\Requests\Api;
use App\Models\Accessory;
+use App\Models\AssetModel;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\License;
@@ -102,6 +103,47 @@ class RequestableTransformerFieldLeakTest extends TestCase
$this->assertArrayHasKey('request', $row['available_actions']);
}
+ public function test_models_requestable_response_does_not_expose_sensitive_fields(): void
+ {
+ // GHSA-8m7q-2x28-8wh3 was the fifth requestable endpoint missed by
+ // the original g85w fix. AssetModelsTransformer had no reduced
+ // `transformRequested*` method, so the controller returned the
+ // full admin shape (notes, default_fieldset_values with every
+ // custom field's name / db_column / default / format / required,
+ // min_amt, eol, depreciation, require_serial, assets_count +
+ // _assigned + _archived, remaining, percent_remaining,
+ // created_by, qr_code_url) to any caller holding only
+ // `assets.view.requestable`.
+ AssetModel::factory()->create(['requestable' => 1]);
+
+ $row = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
+ ->getJson(route('api.assetmodels.requestable'))
+ ->assertOk()
+ ->json('rows.0');
+
+ $this->assertNotNull($row);
+ foreach ([
+ 'notes', 'default_fieldset_values', 'min_amt', 'created_by',
+ 'depreciation', 'eol', 'require_serial', 'fieldset',
+ 'assets_count', 'assets_assigned_count', 'assets_archived_count',
+ 'percent_remaining', 'qr_code_url', 'category', 'manufacturer',
+ ] as $leakedField) {
+ $this->assertArrayNotHasKey(
+ $leakedField,
+ $row,
+ "Field `{$leakedField}` must not appear in the /account/requestable models response."
+ );
+ }
+
+ // Positive-path: the fields the UI tab's row formatter needs.
+ $this->assertArrayHasKey('id', $row);
+ $this->assertArrayHasKey('name', $row);
+ $this->assertArrayHasKey('model_number', $row);
+ $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([
diff --git a/tests/Feature/Requests/Ui/AccessoryRequestTest.php b/tests/Feature/Requests/Ui/AccessoryRequestTest.php
index 6ef4d1c878..32d89e7f08 100644
--- a/tests/Feature/Requests/Ui/AccessoryRequestTest.php
+++ b/tests/Feature/Requests/Ui/AccessoryRequestTest.php
@@ -25,7 +25,7 @@ class AccessoryRequestTest extends TestCase
->assertOk()
->assertViewHas('counts', fn ($counts) => ($counts['accessories'] ?? 0) > 0);
- $rows = $this->actingAsForApi(User::factory()->create())
+ $rows = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.accessories.requestable'))
->assertOk()
->json('rows');
diff --git a/tests/Feature/Requests/Ui/ComponentRequestTest.php b/tests/Feature/Requests/Ui/ComponentRequestTest.php
index 496fa2284a..285c0e40e5 100644
--- a/tests/Feature/Requests/Ui/ComponentRequestTest.php
+++ b/tests/Feature/Requests/Ui/ComponentRequestTest.php
@@ -21,7 +21,7 @@ class ComponentRequestTest extends TestCase
->assertOk()
->assertViewHas('counts', fn ($counts) => ($counts['components'] ?? 0) > 0);
- $rows = $this->actingAsForApi(User::factory()->create())
+ $rows = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.components.requestable'))
->assertOk()
->json('rows');
diff --git a/tests/Feature/Requests/Ui/ConsumableRequestTest.php b/tests/Feature/Requests/Ui/ConsumableRequestTest.php
index 223160d639..fab6dfc9e4 100644
--- a/tests/Feature/Requests/Ui/ConsumableRequestTest.php
+++ b/tests/Feature/Requests/Ui/ConsumableRequestTest.php
@@ -29,7 +29,7 @@ class ConsumableRequestTest extends TestCase
->assertOk()
->assertViewHas('counts', fn ($counts) => ($counts['consumables'] ?? 0) > 0);
- $rows = $this->actingAsForApi(User::factory()->create())
+ $rows = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.consumables.requestable'))
->assertOk()
->json('rows');
diff --git a/tests/Feature/Requests/Ui/LicenseRequestTest.php b/tests/Feature/Requests/Ui/LicenseRequestTest.php
index bda1c6e4fc..56ee13d86b 100644
--- a/tests/Feature/Requests/Ui/LicenseRequestTest.php
+++ b/tests/Feature/Requests/Ui/LicenseRequestTest.php
@@ -27,7 +27,7 @@ class LicenseRequestTest extends TestCase
->assertOk()
->assertViewHas('counts', fn ($counts) => ($counts['licenses'] ?? 0) > 0);
- $rows = $this->actingAsForApi(User::factory()->create())
+ $rows = $this->actingAsForApi(User::factory()->viewRequestableAssets()->create())
->getJson(route('api.licenses.requestable'))
->assertOk()
->json('rows');