A free open source IT asset/license management system
Fixed API maintenance re-parenting on update
snipe9a59545a6d2f
diff --git a/app/Http/Controllers/Api/MaintenancesController.php b/app/Http/Controllers/Api/MaintenancesController.phpindex ac534a36a5..c256dbeabc 100644--- a/app/Http/Controllers/Api/MaintenancesController.php+++ b/app/Http/Controllers/Api/MaintenancesController.php@@ -307,22 +307,40 @@ class MaintenancesController extends Controllerreturn response()->json(Helper::formatStandardApiResponse('error', null, trans('general.action_permission_denied', ['item_type' => trans('admin/maintenances/general.maintenance'), 'id' => $id, 'action' => trans('general.edit')])));}- // If the request changes asset_id, verify the new asset is accessible- if ($request->filled('asset_id') && (int) $request->input('asset_id') !== $maintenance->asset_id) {- $newAsset = Asset::find($request->input('asset_id'));+ // Re-parenting intent can arrive as the legacy `asset_id` alias+ // or as the polymorphic pair (`item_id` + `item_type`). Both are+ // fillable on the model, so gating only on `asset_id` (as the+ // original CVE-2026-55516 fix did) still let `item_id` through+ // to bypass the FMCS check. Normalize both shapes into a single+ // "requested asset id" and reject any non-Asset item_type since+ // only asset-scoped maintenances are supported.+ $requestedAssetId = null;+ if ($request->filled('asset_id')) {+ $requestedAssetId = (int) $request->input('asset_id');+ } elseif ($request->filled('item_id')) {+ if ($request->filled('item_type') && $request->input('item_type') !== Asset::class) {+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.action_permission_denied', ['item_type' => trans('admin/maintenances/general.maintenance'), 'id' => $id, 'action' => trans('general.edit')])), 403);+ }+ $requestedAssetId = (int) $request->input('item_id');+ }++ $newAsset = null;+ if ($requestedAssetId !== null && $requestedAssetId !== $maintenance->asset_id) {+ $newAsset = Asset::find($requestedAssetId);if (! $newAsset) {- return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.item_not_found', ['item_type' => trans('general.asset'), 'id' => $request->input('asset_id')])));+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.item_not_found', ['item_type' => trans('general.asset'), 'id' => $requestedAssetId])));}if (! Company::isCurrentUserHasAccess($newAsset)) {- return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.action_permission_denied', ['item_type' => trans('general.asset'), 'id' => $request->input('asset_id'), 'action' => trans('general.edit')])), 403);+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.action_permission_denied', ['item_type' => trans('general.asset'), 'id' => $requestedAssetId, 'action' => trans('general.edit')])), 403);}+ }++ $maintenance->fill($request->except(['asset_id', 'item_id', 'item_type']));- $maintenance->fill($request->except('asset_id'));+ if ($newAsset !== null) {$maintenance->asset_id = $newAsset->id;- } else {- $maintenance->fill($request->except('asset_id'));}if ($maintenance->save()) {diff --git a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php b/tests/Feature/Maintenances/Api/EditMaintenanceTest.phpindex fd50bbfa72..32208aa3d7 100644--- a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php+++ b/tests/Feature/Maintenances/Api/EditMaintenanceTest.php@@ -174,4 +174,87 @@ class EditMaintenanceTest extends TestCase'item_type' => \App\Models\Asset::class,]);}++ public function test_cannot_reparent_maintenance_via_item_id_to_asset_in_another_company()+ {+ // Both `asset_id` (legacy alias) and `item_id` are fillable on the+ // Maintenance model. Gating only the legacy alias leaves the+ // polymorphic FK as an alternative FMCS-bypass route to the same+ // effect.+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = $companyA->users()->save(User::factory()->editAssets()->make());+ $assetA = Asset::factory()->create(['company_id' => $companyA->id]);+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);+ $maintenance = Maintenance::factory()->create(['asset_id' => $assetA->id]);++ $this->actingAsForApi($user)+ ->patchJson(route('api.maintenances.update', $maintenance), [+ 'name' => 'Cross-company reparent attempt via item_id',+ 'item_id' => $assetB->id,+ 'start_date' => '2024-01-01',+ ])+ ->assertStatusMessageIs('error');++ $this->assertDatabaseHas('maintenances', [+ 'id' => $maintenance->id,+ 'item_id' => $assetA->id,+ 'item_type' => \App\Models\Asset::class,+ ]);+ }++ public function test_can_reparent_maintenance_via_item_id_within_same_company()+ {+ // Happy path for the polymorphic FK: same-company re-parenting via+ // item_id should still succeed. Guards against a fix that would+ // reject item_id outright.+ $this->settings->enableMultipleFullCompanySupport();++ $company = Company::factory()->create();+ $user = $company->users()->save(User::factory()->editAssets()->make());+ $assetA = Asset::factory()->create(['company_id' => $company->id]);+ $assetB = Asset::factory()->create(['company_id' => $company->id]);+ $maintenance = Maintenance::factory()->create(['asset_id' => $assetA->id]);++ $this->actingAsForApi($user)+ ->patchJson(route('api.maintenances.update', $maintenance), [+ 'name' => 'Moved via item_id',+ 'item_id' => $assetB->id,+ 'start_date' => '2024-01-01',+ ])+ ->assertStatusMessageIs('success');++ $this->assertDatabaseHas('maintenances', [+ 'id' => $maintenance->id,+ 'item_id' => $assetB->id,+ 'item_type' => \App\Models\Asset::class,+ ]);+ }++ public function test_cannot_set_item_type_to_non_asset_class()+ {+ // Maintenances only support Asset-scoped attachments today, so any+ // other item_type gets rejected rather than persisted.+ $this->settings->enableMultipleFullCompanySupport();++ $company = Company::factory()->create();+ $user = $company->users()->save(User::factory()->editAssets()->make());+ $asset = Asset::factory()->create(['company_id' => $company->id]);+ $maintenance = Maintenance::factory()->create(['asset_id' => $asset->id]);++ $this->actingAsForApi($user)+ ->patchJson(route('api.maintenances.update', $maintenance), [+ 'item_id' => $user->id,+ 'item_type' => User::class,+ ])+ ->assertStatusMessageIs('error');++ $this->assertDatabaseHas('maintenances', [+ 'id' => $maintenance->id,+ 'item_id' => $asset->id,+ 'item_type' => \App\Models\Asset::class,+ ]);+ }}