Skip to content

A free open source IT asset/license management system

Maintenances API: Added completed_by to except for mass assignment

snipebd70366e253f
diff --git a/app/Http/Controllers/Api/MaintenancesController.php b/app/Http/Controllers/Api/MaintenancesController.php
index c256dbeabc..4ffe5abc18 100644
--- a/app/Http/Controllers/Api/MaintenancesController.php
+++ b/app/Http/Controllers/Api/MaintenancesController.php
@@ -337,7 +337,7 @@ class MaintenancesController extends Controller
}
}
- $maintenance->fill($request->except(['asset_id', 'item_id', 'item_type']));
+ $maintenance->fill($request->except(['asset_id', 'item_id', 'item_type', 'completed_by']));
if ($newAsset !== null) {
$maintenance->asset_id = $newAsset->id;
diff --git a/tests/Feature/Maintenances/Api/MaintenanceCompletedByMassAssignmentTest.php b/tests/Feature/Maintenances/Api/MaintenanceCompletedByMassAssignmentTest.php
new file mode 100644
index 0000000000..fa28d91a80
--- /dev/null
+++ b/tests/Feature/Maintenances/Api/MaintenanceCompletedByMassAssignmentTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Tests\Feature\Maintenances\Api;
+
+use App\Models\Asset;
+use App\Models\Maintenance;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * PATCH /api/v1/maintenances/{id} previously filled the model from
+ * request input while excluding only asset_id, item_id, and item_type.
+ * completed_by passed through, letting a user with assets.edit forge
+ * the completion attribution on a maintenance they can update
+ * (CWE-915). The fix adds completed_by to the exclusion list so the
+ * attribution column can only be written by the dedicated complete()
+ * action which sets it from the authenticated user.
+ */
+class MaintenanceCompletedByMassAssignmentTest extends TestCase
+{
+ public function test_patch_cannot_set_completed_by_to_another_user()
+ {
+ $editor = User::factory()->editAssets()->create();
+ $otherAdmin = User::factory()->superuser()->create();
+
+ $asset = Asset::factory()->create();
+ $maintenance = Maintenance::factory()->create([
+ 'asset_id' => $asset->id,
+ 'completed_by' => null,
+ ]);
+
+ $this->actingAsForApi($editor)
+ ->patchJson(route('api.maintenances.update', $maintenance->id), [
+ 'completed_by' => $otherAdmin->id,
+ ]);
+
+ $maintenance->refresh();
+
+ $this->assertNull(
+ $maintenance->completed_by,
+ 'completed_by must stay null when it was null before. The update endpoint must not accept a client-supplied value for this attribution column.'
+ );
+ }
+
+ public function test_patch_does_not_overwrite_existing_completed_by()
+ {
+ $editor = User::factory()->editAssets()->create();
+ $existingCompleter = User::factory()->superuser()->create();
+ $otherAdmin = User::factory()->superuser()->create();
+
+ $asset = Asset::factory()->create();
+ $maintenance = Maintenance::factory()->create([
+ 'asset_id' => $asset->id,
+ 'completed_by' => $existingCompleter->id,
+ ]);
+
+ $this->actingAsForApi($editor)
+ ->patchJson(route('api.maintenances.update', $maintenance->id), [
+ 'completed_by' => $otherAdmin->id,
+ 'notes' => 'editing other legitimate fields',
+ ]);
+
+ $maintenance->refresh();
+
+ $this->assertSame(
+ $existingCompleter->id,
+ $maintenance->completed_by,
+ 'An existing completed_by must survive a PATCH that attempts to overwrite it. The dedicated complete() action is the only legitimate writer.'
+ );
+ }
+}