Skip to content

A free open source IT asset/license management system

Asset Models API: Fixed FD-57831 / 2vq5-hrw4 - tighter controls on assets listings on models API

snipe1d182526ce27
diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php
index 0f01715e89..33ea84dbe4 100644
--- a/app/Http/Controllers/Api/AssetModelsController.php
+++ b/app/Http/Controllers/Api/AssetModelsController.php
@@ -218,7 +218,9 @@ class AssetModelsController extends Controller
*/
public function assets(Request $request, $id): JsonResponse|array
{
+
$this->authorize('view', AssetModel::class);
+ $this->authorize('view', Asset::class);
$model = AssetModel::findOrFail($id);
// Eager-load the same relations AssetsTransformer walks per row.
diff --git a/tests/Feature/AssetModels/Api/AssetsForAssetModelTest.php b/tests/Feature/AssetModels/Api/AssetsForAssetModelTest.php
index bec6b354d8..64945bebb6 100644
--- a/tests/Feature/AssetModels/Api/AssetsForAssetModelTest.php
+++ b/tests/Feature/AssetModels/Api/AssetsForAssetModelTest.php
@@ -34,6 +34,46 @@ class AssetsForAssetModelTest extends TestCase
->assertForbidden();
}
+ public function test_models_view_without_assets_view_cannot_read_the_assets_for_a_model()
+ {
+ // Regression test: pre-fix, the endpoint only authorized
+ // view on AssetModel. A role holding models.view but not
+ // assets.view therefore read full asset rows (serials, notes,
+ // assignment, purchase, audit) through AssetsTransformer. The
+ // canonical GET /api/v1/hardware correctly 403s the same role.
+ $model = AssetModel::factory()->create();
+ Asset::factory()->create([
+ 'model_id' => $model->id,
+ 'serial' => 'LEAK-SERIAL-ABC123',
+ 'notes' => 'LEAK-NOTES-sensitive',
+ ]);
+
+ $modelsViewer = User::factory()->viewAssetModels()->create();
+
+ $this->actingAsForApi($modelsViewer)
+ ->getJson(route('api.models.assets', ['id' => $model->id]))
+ ->assertForbidden();
+ }
+
+ public function test_models_view_combined_with_assets_view_still_reaches_the_endpoint()
+ {
+ // Happy-path companion: fixing the authorization gap must not
+ // over-restrict the roles that legitimately combine both
+ // permissions.
+ $model = AssetModel::factory()->create();
+ Asset::factory()->count(2)->create(['model_id' => $model->id]);
+
+ $viewer = User::factory()->viewAssetModels()->viewAssets()->create();
+
+ $this->actingAsForApi($viewer)
+ ->getJson(route('api.models.assets', ['id' => $model->id]))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json
+ ->where('total', 2)
+ ->has('rows', 2)
+ ->etc());
+ }
+
public function test_error_returned_if_asset_model_does_not_exist()
{
// Snipe-IT's exception handler returns 200 + status "error" for