A free open source IT asset/license management system
Fixed FD-57269 - tigher checks on kit gates
snipe095f79484179
diff --git a/app/Http/Controllers/Kits/PredefinedKitsController.php b/app/Http/Controllers/Kits/PredefinedKitsController.phpindex 6c6be77124..2027bd628b 100644--- a/app/Http/Controllers/Kits/PredefinedKitsController.php+++ b/app/Http/Controllers/Kits/PredefinedKitsController.php@@ -4,6 +4,10 @@ namespace App\Http\Controllers\Kits;use App\Http\Controllers\Controller;use App\Http\Requests\ImageUploadRequest;+use App\Models\Accessory;+use App\Models\AssetModel;+use App\Models\Consumable;+use App\Models\License;use App\Models\PredefinedKit;use Illuminate\Auth\Access\AuthorizationException;use Illuminate\Contracts\View\View;@@ -193,7 +197,7 @@ class PredefinedKitsController extends Controller* @author [D. Minaev] [<[email protected]>]** @param int $modelId- * @return View+ * @return RedirectResponse*/public function updateModel(Request $request, $kit_id, $model_id){@@ -209,6 +213,16 @@ class PredefinedKitsController extends Controllerreturn redirect()->back()->withInput()->withErrors($validator);}+ // Verify the user can view the+ // model id they are about to write into the pivot. Without this,+ // kits.edit alone would let them re-point the pivot at a model+ // they cannot read directly, including one in another company.+ $targetModel = AssetModel::find($request->input('model_id'));+ if (! $targetModel) {+ return redirect()->back()->withInput()->with('error', trans('admin/models/message.does_not_exist'));+ }+ $this->authorize('view', $targetModel);+$pivot = $kit->models()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;$pivot->model_id = $request->input('model_id');@@ -274,7 +288,7 @@ class PredefinedKitsController extends Controller** @param int $kit_id* @param int $license_id- * @return View+ * @return RedirectResponse*/public function updateLicense(Request $request, $kit_id, $license_id){@@ -290,6 +304,14 @@ class PredefinedKitsController extends Controllerreturn redirect()->back()->withInput()->withErrors($validator);}+ // Verify the caller can view the license id they are about to+ // write into the pivot. Mirrors the API sibling from FD-56594.+ $targetLicense = License::find($request->input('license_id'));+ if (! $targetLicense) {+ return redirect()->back()->withInput()->with('error', trans('admin/licenses/message.does_not_exist'));+ }+ $this->authorize('view', $targetLicense);+$pivot = $kit->licenses()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;$pivot->license_id = $request->input('license_id');@@ -356,7 +378,7 @@ class PredefinedKitsController extends Controller** @param int $kit_id* @param int $accessory_id- * @return View+ * @return RedirectResponse*/public function updateAccessory(Request $request, $kit_id, $accessory_id){@@ -372,6 +394,14 @@ class PredefinedKitsController extends Controllerreturn redirect()->back()->withInput()->withErrors($validator);}+ // Verify the caller can view the accessory id they are about+ // to write into the pivot. Mirrors the API sibling from FD-56594.+ $targetAccessory = Accessory::find($request->input('accessory_id'));+ if (! $targetAccessory) {+ return redirect()->back()->withInput()->with('error', trans('admin/accessories/message.does_not_exist'));+ }+ $this->authorize('view', $targetAccessory);+$pivot = $kit->accessories()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;$pivot->accessory_id = $request->input('accessory_id');@@ -437,7 +467,7 @@ class PredefinedKitsController extends Controller** @param int $kit_id* @param int $consumableId- * @return View+ * @return RedirectResponse*/public function updateConsumable(Request $request, $kit_id, $consumable_id){@@ -453,6 +483,14 @@ class PredefinedKitsController extends Controllerreturn redirect()->back()->withInput()->withErrors($validator);}+ // Verify the caller can view the consumable id they are about+ // to write into the pivot. Mirrors the API sibling from FD-56594.+ $targetConsumable = Consumable::find($request->input('consumable_id'));+ if (! $targetConsumable) {+ return redirect()->back()->withInput()->with('error', trans('admin/consumables/message.does_not_exist'));+ }+ $this->authorize('view', $targetConsumable);+$pivot = $kit->consumables()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;$pivot->consumable_id = $request->input('consumable_id');diff --git a/phpstan-baseline.neon b/phpstan-baseline.neonindex 5701c4ce33..04f64537da 100644--- a/phpstan-baseline.neon+++ b/phpstan-baseline.neon@@ -2826,30 +2826,6 @@ parameters:count: 1path: app/Http/Controllers/Kits/PredefinedKitsController.php- -- message: '#^Method App\\Http\\Controllers\\Kits\\PredefinedKitsController\:\:updateAccessory\(\) should return Illuminate\\Contracts\\View\\View but returns Illuminate\\Http\\RedirectResponse\.$#'- identifier: return.type- count: 3- path: app/Http/Controllers/Kits/PredefinedKitsController.php-- -- message: '#^Method App\\Http\\Controllers\\Kits\\PredefinedKitsController\:\:updateConsumable\(\) should return Illuminate\\Contracts\\View\\View but returns Illuminate\\Http\\RedirectResponse\.$#'- identifier: return.type- count: 3- path: app/Http/Controllers/Kits/PredefinedKitsController.php-- -- message: '#^Method App\\Http\\Controllers\\Kits\\PredefinedKitsController\:\:updateLicense\(\) should return Illuminate\\Contracts\\View\\View but returns Illuminate\\Http\\RedirectResponse\.$#'- identifier: return.type- count: 3- path: app/Http/Controllers/Kits/PredefinedKitsController.php-- -- message: '#^Method App\\Http\\Controllers\\Kits\\PredefinedKitsController\:\:updateModel\(\) should return Illuminate\\Contracts\\View\\View but returns Illuminate\\Http\\RedirectResponse\.$#'- identifier: return.type- count: 3- path: app/Http/Controllers/Kits/PredefinedKitsController.php--message: '#^Negated boolean expression is always false\.$#'identifier: booleanNot.alwaysFalsediff --git a/tests/Feature/PredefinedKits/Ui/UpdateKitItemsAuthorizationTest.php b/tests/Feature/PredefinedKits/Ui/UpdateKitItemsAuthorizationTest.phpnew file mode 100644index 0000000000..2b561528ae--- /dev/null+++ b/tests/Feature/PredefinedKits/Ui/UpdateKitItemsAuthorizationTest.php@@ -0,0 +1,162 @@+<?php++namespace Tests\Feature\PredefinedKits\Ui;++use App\Models\Accessory;+use App\Models\AssetModel;+use App\Models\Consumable;+use App\Models\License;+use App\Models\PredefinedKit;+use App\Models\User;+use Tests\TestCase;++/**+ * Web sibling of tests/Feature/PredefinedKits/Api/UpdateKitItemsTest.+ * FD-56594 added `findOrFail` + `authorize('view', $object)` to the four+ * update handlers on `Api\PredefinedKitsController`, but the parallel+ * handlers on the web `Kits\PredefinedKitsController` (`updateLicense`,+ * `updateModel`, `updateAccessory`, `updateConsumable`) were not covered+ * by that fix. A `kits.edit` holder could re-point an existing pivot at+ * an object id they cannot read directly, including one in another+ * company. This locks in the follow-up guards.+ */+class UpdateKitItemsAuthorizationTest extends TestCase+{+ // -------------------------------------------------------------------------+ // Licenses+ // -------------------------------------------------------------------------++ public function test_web_update_kit_license_requires_view_permission_on_target_license()+ {+ $kit = PredefinedKit::factory()->create();+ $existingLicense = License::factory()->create();+ $kit->licenses()->attach($existingLicense->id, ['quantity' => 1]);+ $pivotId = $kit->licenses()->wherePivot('license_id', $existingLicense->id)->first()->pivot->id;++ // Attacker points the pivot at a different license they cannot view.+ $targetLicense = License::factory()->create();++ $this->actingAs(User::factory()->editPredefinedKits()->create())+ ->put(route('kits.licenses.update', ['kit' => $kit->id, 'license_id' => $existingLicense->id]), [+ 'pivot_id' => $pivotId,+ 'license_id' => $targetLicense->id,+ 'quantity' => 5,+ ])+ ->assertForbidden();++ $this->assertDatabaseMissing('kits_licenses', [+ 'kit_id' => $kit->id,+ 'license_id' => $targetLicense->id,+ ]);+ $this->assertDatabaseHas('kits_licenses', [+ 'kit_id' => $kit->id,+ 'license_id' => $existingLicense->id,+ 'quantity' => 1,+ ]);+ }++ public function test_web_update_kit_license_succeeds_when_view_permission_granted()+ {+ $kit = PredefinedKit::factory()->create();+ $existingLicense = License::factory()->create();+ $kit->licenses()->attach($existingLicense->id, ['quantity' => 1]);+ $pivotId = $kit->licenses()->wherePivot('license_id', $existingLicense->id)->first()->pivot->id;++ $targetLicense = License::factory()->create();++ $this->actingAs(User::factory()->editPredefinedKits()->viewLicenses()->create())+ ->put(route('kits.licenses.update', ['kit' => $kit->id, 'license_id' => $existingLicense->id]), [+ 'pivot_id' => $pivotId,+ 'license_id' => $targetLicense->id,+ 'quantity' => 5,+ ])+ ->assertRedirect(route('kits.edit', $kit->id));++ $this->assertDatabaseHas('kits_licenses', [+ 'kit_id' => $kit->id,+ 'license_id' => $targetLicense->id,+ 'quantity' => 5,+ ]);+ }++ // -------------------------------------------------------------------------+ // Models+ // -------------------------------------------------------------------------++ public function test_web_update_kit_model_requires_view_permission_on_target_model()+ {+ $kit = PredefinedKit::factory()->create();+ $existingModel = AssetModel::factory()->create();+ $kit->models()->attach($existingModel->id, ['quantity' => 1]);+ $pivotId = $kit->models()->wherePivot('model_id', $existingModel->id)->first()->pivot->id;++ $targetModel = AssetModel::factory()->create();++ $this->actingAs(User::factory()->editPredefinedKits()->create())+ ->put(route('kits.models.update', ['kit' => $kit->id, 'model_id' => $existingModel->id]), [+ 'pivot_id' => $pivotId,+ 'model_id' => $targetModel->id,+ 'quantity' => 5,+ ])+ ->assertForbidden();++ $this->assertDatabaseMissing('kits_models', [+ 'kit_id' => $kit->id,+ 'model_id' => $targetModel->id,+ ]);+ }++ // -------------------------------------------------------------------------+ // Accessories+ // -------------------------------------------------------------------------++ public function test_web_update_kit_accessory_requires_view_permission_on_target_accessory()+ {+ $kit = PredefinedKit::factory()->create();+ $existingAccessory = Accessory::factory()->create();+ $kit->accessories()->attach($existingAccessory->id, ['quantity' => 1]);+ $pivotId = $kit->accessories()->wherePivot('accessory_id', $existingAccessory->id)->first()->pivot->id;++ $targetAccessory = Accessory::factory()->create();++ $this->actingAs(User::factory()->editPredefinedKits()->create())+ ->put(route('kits.accessories.update', ['kit' => $kit->id, 'accessory_id' => $existingAccessory->id]), [+ 'pivot_id' => $pivotId,+ 'accessory_id' => $targetAccessory->id,+ 'quantity' => 5,+ ])+ ->assertForbidden();++ $this->assertDatabaseMissing('kits_accessories', [+ 'kit_id' => $kit->id,+ 'accessory_id' => $targetAccessory->id,+ ]);+ }++ // -------------------------------------------------------------------------+ // Consumables+ // -------------------------------------------------------------------------++ public function test_web_update_kit_consumable_requires_view_permission_on_target_consumable()+ {+ $kit = PredefinedKit::factory()->create();+ $existingConsumable = Consumable::factory()->create();+ $kit->consumables()->attach($existingConsumable->id, ['quantity' => 1]);+ $pivotId = $kit->consumables()->wherePivot('consumable_id', $existingConsumable->id)->first()->pivot->id;++ $targetConsumable = Consumable::factory()->create();++ $this->actingAs(User::factory()->editPredefinedKits()->create())+ ->put(route('kits.consumables.update', ['kit' => $kit->id, 'consumable_id' => $existingConsumable->id]), [+ 'pivot_id' => $pivotId,+ 'consumable_id' => $targetConsumable->id,+ 'quantity' => 5,+ ])+ ->assertForbidden();++ $this->assertDatabaseMissing('kits_consumables', [+ 'kit_id' => $kit->id,+ 'consumable_id' => $targetConsumable->id,+ ]);+ }+}