A free open source IT asset/license management system
Updated asset model restore guard to delete permission
snipec1c6aae9d1b9
diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.phpindex bb37465579..9b17e1030f 100755--- a/app/Http/Controllers/AssetModelsController.php+++ b/app/Http/Controllers/AssetModelsController.php@@ -231,7 +231,13 @@ class AssetModelsController extends Controller*/public function getRestore($id): RedirectResponse{- $this->authorize('create', AssetModel::class);+ // Restoring a soft-deleted record is a delete-tier action across the+ // project (users, locations, manufacturers, assets, and this same+ // model's API sibling Api\AssetModelsController::restore all+ // authorize `delete`). The `create` gate was inconsistent and let+ // an account holding models.create but not models.delete reverse+ // an admin's soft-delete.+ $this->authorize('delete', AssetModel::class);if ($model = AssetModel::withTrashed()->find($id)) {@@ -317,6 +323,25 @@ class AssetModelsController extends Controller*/public function getCustomFields($modelId): View{+ // Endpoint fires from the asset create/edit form's model-select+ // change handler (hardware/edit.blade.php's fetchCustomFields+ // XHR) to swap in the picked model's custom-field form partial.+ // Zero-permission accounts must be blocked. This used to have+ // no guard at all, so anyone authenticated could enumerate every+ // model's schema by iterating IDs. The guard has to accept+ // the three legit caller shapes: asset creators (from /hardware/+ // create), asset editors (from /hardware/{id}/edit), and model+ // viewers (from /models/{id}/edit's custom-field preview). Any-of+ // is cheaper than falling back on Gate::authorize's single-perm+ // check because none of those three alone covers every legit+ // caller.+ $user = auth()->user();+ if (! $user?->can('view', AssetModel::class)+ && ! $user?->can('create', Asset::class)+ && ! $user?->can('update', Asset::class)) {+ abort(403);+ }+return view('models.custom_fields_form')->with('model', AssetModel::find($modelId));}diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.phpindex aa91bfaf75..d92c7cb183 100644--- a/database/factories/UserFactory.php+++ b/database/factories/UserFactory.php@@ -290,6 +290,11 @@ class UserFactory extends Factoryreturn $this->appendPermission(['assets.view.encrypted_custom_fields' => '1']);}+ public function createAssetModels()+ {+ return $this->appendPermission(['models.create' => '1']);+ }+public function deleteAssetModels(){return $this->appendPermission(['models.delete' => '1']);diff --git a/tests/Feature/AssetModels/Ui/GetCustomFieldsFormAuthTest.php b/tests/Feature/AssetModels/Ui/GetCustomFieldsFormAuthTest.phpnew file mode 100644index 0000000000..4f02dd7f74--- /dev/null+++ b/tests/Feature/AssetModels/Ui/GetCustomFieldsFormAuthTest.php@@ -0,0 +1,75 @@+<?php++namespace Tests\Feature\AssetModels\Ui;++use App\Models\AssetModel;+use App\Models\User;+use Tests\TestCase;++/**+ * Regression coverage for GHSA-cpxg-ch29-fq53.+ *+ * AssetModelsController::getCustomFields (route models/{id}/custom_fields,+ * name custom_fields/model) shipped without any authorize() call. Every+ * other action in that controller has one. Any authenticated account,+ * including a user whose permission JSON was literally `{}`, could hit+ * the endpoint and enumerate every model's custom-field schema (labels,+ * generated column names, help text, listbox/checkbox/radio option sets,+ * required flags, per-model defaults) by iterating IDs.+ *+ * Fix authorizes any-of (assets.create, assets.update, models.view) to+ * match the three legit caller shapes: the asset create form's model+ * change XHR, the asset edit form's model change XHR, and the model+ * edit page's custom-field preview.+ */+class GetCustomFieldsFormAuthTest extends TestCase+{+ public function test_zero_permission_user_cannot_load_custom_fields_form(): void+ {+ $model = AssetModel::factory()->create();++ $this->actingAs(User::factory()->create())+ ->get(route('custom_fields/model', $model->id))+ ->assertForbidden();+ }++ public function test_user_with_models_view_can_load_custom_fields_form(): void+ {+ $model = AssetModel::factory()->create();++ $this->actingAs(User::factory()->viewAssetModels()->create())+ ->get(route('custom_fields/model', $model->id))+ ->assertOk();+ }++ public function test_asset_creator_can_load_custom_fields_form(): void+ {+ // The endpoint is fired from the asset create form's model-select+ // XHR. An account with assets.create but no models.view still has+ // to be able to load the model's custom-field partial.+ $model = AssetModel::factory()->create();++ $this->actingAs(User::factory()->createAssets()->create())+ ->get(route('custom_fields/model', $model->id))+ ->assertOk();+ }++ public function test_asset_editor_can_load_custom_fields_form(): void+ {+ // Same shape as the create case for the /hardware/{id}/edit form.+ $model = AssetModel::factory()->create();++ $this->actingAs(User::factory()->editAssets()->create())+ ->get(route('custom_fields/model', $model->id))+ ->assertOk();+ }++ public function test_superuser_can_load_custom_fields_form(): void+ {+ $model = AssetModel::factory()->create();++ $this->actingAs(User::factory()->superuser()->create())+ ->get(route('custom_fields/model', $model->id))+ ->assertOk();+ }+}diff --git a/tests/Feature/AssetModels/Ui/RestoreAssetModelTest.php b/tests/Feature/AssetModels/Ui/RestoreAssetModelTest.phpindex b35e7793ca..9012730c05 100644--- a/tests/Feature/AssetModels/Ui/RestoreAssetModelTest.php+++ b/tests/Feature/AssetModels/Ui/RestoreAssetModelTest.php@@ -34,6 +34,39 @@ class RestoreAssetModelTest extends TestCase->assertForbidden();}+ public function test_create_permission_alone_cannot_restore_asset_model(): void+ {+ // Regression: getRestore() used to authorize `create`, so a caller+ // holding models.create but not models.delete could reverse an+ // admin's soft-delete. Every sibling restore path in the project+ // (users, locations, manufacturers, assets, bulk assets, and this+ // model's API sibling Api\AssetModelsController::restore) authorizes+ // `delete` instead. Pinning the correct behavior here.+ $model = AssetModel::factory()->create();+ $model->delete();++ $this->actingAs(User::factory()->createAssetModels()->create())+ ->post(route('models.restore.store', $model->id))+ ->assertForbidden();++ $this->assertNotNull($model->fresh()->deleted_at, 'Restore should not have run.');+ }++ public function test_delete_permission_can_restore_asset_model(): void+ {+ // The paired positive case: models.delete IS the right permission+ // for restore, matching every sibling restore action.+ $model = AssetModel::factory()->create();+ $model->delete();++ $this->actingAs(User::factory()->deleteAssetModels()->create())+ ->post(route('models.restore.store', $model->id))+ ->assertRedirect()+ ->assertSessionHas('success');++ $this->assertNull($model->fresh()->deleted_at);+ }+public function test_soft_deleted_asset_model_can_be_restored_via_web(): void{$model = AssetModel::factory()->create();