A free open source IT asset/license management system
Fixed FD-56030 - removed assigned from fillable
snipe2c302d918eeb
diff --git a/app/Http/Requests/StoreAssetRequest.php b/app/Http/Requests/StoreAssetRequest.phpindex 069d4955f4..0a50c6b37f 100644--- a/app/Http/Requests/StoreAssetRequest.php+++ b/app/Http/Requests/StoreAssetRequest.php@@ -52,6 +52,11 @@ class StoreAssetRequest extends ImageUploadRequest{$modelRules = (new Asset)->getRules();+ // assigned_to / assigned_type are intentionally excluded: they must only+ // be written via assigned_user / assigned_asset / assigned_location (which+ // route through checkOut() and produce the required audit-log entry).+ unset($modelRules['assigned_to'], $modelRules['assigned_type']);+return array_merge($modelRules,['status_id' => [new AssetCannotBeCheckedOutToNondeployableStatus]],diff --git a/app/Http/Requests/UpdateAssetRequest.php b/app/Http/Requests/UpdateAssetRequest.phpindex ad7e8ea01d..d1ba3ee4fb 100644--- a/app/Http/Requests/UpdateAssetRequest.php+++ b/app/Http/Requests/UpdateAssetRequest.php@@ -39,9 +39,16 @@ class UpdateAssetRequest extends ImageUploadRequest{$setting = Setting::getSettings();+ $assetRules = (new Asset)->getRules();++ // assigned_to / assigned_type are intentionally excluded: they must only+ // be written via assigned_user / assigned_asset / assigned_location (which+ // route through checkOut() and produce the required audit-log entry).+ unset($assetRules['assigned_to'], $assetRules['assigned_type']);+$rules = array_merge(parent::rules(),- (new Asset)->getRules(),+ $assetRules,// This overwrites the rulesets that are set at the model level (via Watson) but are not necessarily required at the request level when doing a PATCH update.// Confusingly, this skips the unique_undeleted validator at the model level (and therefore the UniqueUndeletedTrait), so we have to re-add those// rules here without the requiredness, since those values will already exist if you're updating an existing asset.diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex d3b1991a5a..23b24eaad9 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -146,8 +146,6 @@ class Asset extends Depreciable*/protected $fillable = ['asset_tag',- 'assigned_to',- 'assigned_type','company_id','image','location_id',diff --git a/tests/Feature/Assets/Api/StoreAssetTest.php b/tests/Feature/Assets/Api/StoreAssetTest.phpindex 83245750d7..7487051f72 100644--- a/tests/Feature/Assets/Api/StoreAssetTest.php+++ b/tests/Feature/Assets/Api/StoreAssetTest.php@@ -166,13 +166,17 @@ class StoreAssetTest extends TestCase$this->assertNotNull($response->json('messages.status_id'));}- public function test_save_with_assigned_to_checks_out()+ public function test_raw_assigned_to_pair_is_ignored_on_store(){- $user = User::factory()->create();+ // Security regression: sending assigned_to + assigned_type on create must+ // not bypass checkOut() — the asset must be created unassigned with only a+ // 'create' log entry. Use assigned_user / assigned_asset / assigned_location+ // instead (those go through the proper checkout workflow).+ $target = User::factory()->create();$response = $this->actingAsForApi(User::factory()->superuser()->create())->postJson(route('api.assets.store'), ['asset_tag' => '1235',- 'assigned_to' => $user->id,+ 'assigned_to' => $target->id,'assigned_type' => User::class,'model_id' => AssetModel::factory()->create()->id,'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,@@ -181,55 +185,64 @@ class StoreAssetTest extends TestCase->assertStatusMessageIs('success');$asset = Asset::find($response->json()['payload']['id']);- $this->assertEquals($user->id, $asset->assigned_to);- $this->assertEquals('Asset created successfully. :)', $response->json('messages'));-- $this->assertHasTheseActionLogs($asset, ['create'/* , 'checkout' */]); // TODO - this _should_ be the two actions+ $this->assertNull($asset->assigned_to, 'assigned_to must not be set via the raw pair');+ $this->assertHasTheseActionLogs($asset, ['create']);+ $this->assertDatabaseMissing('action_logs', [+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ ]);}- public function test_save_with_no_assigned_type_returns_validation_error()+ public function test_raw_assigned_to_without_assigned_type_is_ignored_on_store(){$response = $this->actingAsForApi(User::factory()->superuser()->create())->postJson(route('api.assets.store'), ['asset_tag' => '1235','assigned_to' => '1',- // 'assigned_type' => User::class, //deliberately omit assigned_type+ // 'assigned_type' => User::class — deliberately omit'model_id' => AssetModel::factory()->create()->id,'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,])->assertOk()- ->assertStatusMessageIs('error');- $this->assertNotNull($response->json('messages.assigned_type'));+ ->assertStatusMessageIs('success');++ $asset = Asset::find($response->json()['payload']['id']);+ $this->assertNull($asset->assigned_to);}- public function test_save_with_bad_assigned_type_returns_validation_error()+ public function test_raw_assigned_to_with_bad_assigned_type_is_ignored_on_store(){$response = $this->actingAsForApi(User::factory()->superuser()->create())->postJson(route('api.assets.store'), ['asset_tag' => '1235','assigned_to' => '1',- 'assigned_type' => 'nonsense_string', // deliberately bad assigned_type+ 'assigned_type' => 'nonsense_string','model_id' => AssetModel::factory()->create()->id,'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,])->assertOk()- ->assertStatusMessageIs('error');- $this->assertNotNull($response->json('messages.assigned_type'));+ ->assertStatusMessageIs('success');++ $asset = Asset::find($response->json()['payload']['id']);+ $this->assertNull($asset->assigned_to);}- public function test_save_with_assigned_type_and_no_assigned_to_returns_validation_error()+ public function test_raw_assigned_type_without_assigned_to_is_ignored_on_store(){$response = $this->actingAsForApi(User::factory()->superuser()->create())->postJson(route('api.assets.store'), ['asset_tag' => '1235',- // 'assigned_to' => '1', //deliberately omit assigned_to+ // 'assigned_to' => '1' — deliberately omit'assigned_type' => User::class,'model_id' => AssetModel::factory()->create()->id,'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,])->assertOk()- ->assertStatusMessageIs('error');- $this->assertNotNull($response->json('messages.assigned_to'));+ ->assertStatusMessageIs('success');++ $asset = Asset::find($response->json()['payload']['id']);+ $this->assertNull($asset->assigned_to);}public function test_save_with_pending_status_without_user_is_successful()diff --git a/tests/Feature/Assets/Api/UpdateAssetTest.php b/tests/Feature/Assets/Api/UpdateAssetTest.phpindex 4516fb6f28..1276d764df 100644--- a/tests/Feature/Assets/Api/UpdateAssetTest.php+++ b/tests/Feature/Assets/Api/UpdateAssetTest.php@@ -456,84 +456,87 @@ class UpdateAssetTest extends TestCase]);}- public function test_checkout_to_user_with_assigned_to_and_assigned_type()+ public function test_raw_assigned_to_pair_is_ignored_on_update(){+ // Security regression: sending assigned_to + assigned_type directly must+ // not bypass checkOut() — the assignment must not change and no checkout+ // log must be written. Use assigned_user / assigned_asset / assigned_location+ // instead (those go through the proper checkout workflow).$asset = Asset::factory()->create();$user = User::factory()->editAssets()->create();$assigned_user = User::factory()->create();- $response = $this->actingAsForApi($user)+ $originalAssignedTo = $asset->assigned_to;++ $this->actingAsForApi($user)->patchJson(route('api.assets.update', $asset->id), ['assigned_to' => $assigned_user->id,'assigned_type' => User::class,])->assertOk()- ->assertStatusMessageIs('success')- ->json();+ ->assertStatusMessageIs('success');$asset->refresh();- $this->assertEquals($assigned_user->id, $asset->assigned_to);- $this->assertEquals($asset->assigned_type, 'App\Models\User');+ $this->assertEquals($originalAssignedTo, $asset->assigned_to, 'assigned_to must not change via the raw pair');+ $this->assertDatabaseMissing('action_logs', [+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ ]);}- public function test_checkout_to_user_with_assigned_to_without_assigned_type()+ public function test_raw_assigned_to_without_assigned_type_is_ignored_on_update(){$asset = Asset::factory()->create();$user = User::factory()->editAssets()->create();$assigned_user = User::factory()->create();- $response = $this->actingAsForApi($user)+ $this->actingAsForApi($user)->patchJson(route('api.assets.update', $asset->id), ['assigned_to' => $assigned_user->id,- // 'assigned_type' => User::class //deliberately omit assigned_type+ // 'assigned_type' => User::class — deliberately omit])->assertOk()- ->assertStatusMessageIs('error');+ ->assertStatusMessageIs('success');$asset->refresh();$this->assertNotEquals($assigned_user->id, $asset->assigned_to);- $this->assertNotEquals($asset->assigned_type, 'App\Models\User');- $this->assertNotNull($response->json('messages.assigned_type'));}- public function test_checkout_to_user_with_assigned_to_with_bad_assigned_type()+ public function test_raw_assigned_to_with_bad_assigned_type_is_ignored_on_update(){$asset = Asset::factory()->create();$user = User::factory()->editAssets()->create();$assigned_user = User::factory()->create();- $response = $this->actingAsForApi($user)+ $this->actingAsForApi($user)->patchJson(route('api.assets.update', $asset->id), ['assigned_to' => $assigned_user->id,- 'assigned_type' => 'more_deliberate_nonsense', // deliberately bad assigned_type+ 'assigned_type' => 'more_deliberate_nonsense',])->assertOk()- ->assertStatusMessageIs('error');+ ->assertStatusMessageIs('success');$asset->refresh();$this->assertNotEquals($assigned_user->id, $asset->assigned_to);- $this->assertNotEquals($asset->assigned_type, 'App\Models\User');- $this->assertNotNull($response->json('messages.assigned_type'));}- public function test_checkout_to_user_without_assigned_to_with_assigned_type()+ public function test_raw_assigned_type_without_assigned_to_is_ignored_on_update(){$asset = Asset::factory()->create();$user = User::factory()->editAssets()->create();$assigned_user = User::factory()->create();- $response = $this->actingAsForApi($user)+ $this->actingAsForApi($user)->patchJson(route('api.assets.update', $asset->id), [- // 'assigned_to' => $assigned_user->id, // deliberately omit assigned_to+ // 'assigned_to' => $assigned_user->id — deliberately omit'assigned_type' => User::class,])->assertOk()- ->assertStatusMessageIs('error');+ ->assertStatusMessageIs('success');$asset->refresh();$this->assertNotEquals($assigned_user->id, $asset->assigned_to);- $this->assertNotEquals($asset->assigned_type, 'App\Models\User');- $this->assertNotNull($response->json('messages.assigned_to'));}public function test_checkout_to_deleted_user_fails_on_asset_update()diff --git a/tests/Feature/Console/ValidateAssetsTest.php b/tests/Feature/Console/ValidateAssetsTest.phpindex ead4591ca8..37b60e0ddb 100644--- a/tests/Feature/Console/ValidateAssetsTest.php+++ b/tests/Feature/Console/ValidateAssetsTest.php@@ -5,6 +5,7 @@ namespace Tests\Feature\Console;use App\Models\Asset;use App\Models\User;use Illuminate\Support\Facades\Artisan;+use Illuminate\Support\Facades\DB;use Illuminate\Support\Str;use Tests\TestCase;@@ -51,15 +52,21 @@ class ValidateAssetsTest extends TestCase'serial' => 'GOOD-SERIAL-'.$tagSuffix,]);+ // assigned_to is not in $fillable, so we create the asset normally then+ // inject the inconsistent state directly via DB to simulate legacy data.$invalidAsset = Asset::factory()->canBeInvalidUponCreation()->create(['asset_tag' => 'BROKEN-ASSET-'.$tagSuffix,'serial' => 'BROKEN-SERIAL-'.$tagSuffix,- 'assigned_to' => User::factory()->create()->id,- 'assigned_type' => null,]);+ DB::table('assets')->where('id', $invalidAsset->id)->update([+ 'assigned_to' => User::factory()->create()->id,+ 'assigned_type' => null,+ ]);+ $invalidAsset->refresh();+return [$validAsset, $invalidAsset];}}diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.phpindex 1973aec01b..05c5c99df7 100644--- a/tests/Unit/AssetTest.php+++ b/tests/Unit/AssetTest.php@@ -14,6 +14,7 @@ use App\Models\Setting;use App\Models\Statuslabel;use App\Models\User;use Carbon\Carbon;+use Illuminate\Support\Facades\DB;use Tests\TestCase;class AssetTest extends TestCase@@ -247,11 +248,16 @@ class AssetTest extends TestCasepublic function test_assigned_type_without_assign_to(){+ // Model-level rule: assigned_type is required_with:assigned_to.+ // assigned_to is not in $fillable so we use a raw DB write to simulate+ // legacy / inconsistent data, then verify the model treats the row as invalid.$user = User::factory()->create();- $asset = Asset::factory()->create([- 'assigned_to' => $user->id,- ]);- $this->assertModelMissing($asset);+ $asset = Asset::factory()->create();++ DB::table('assets')->where('id', $asset->id)->update(['assigned_to' => $user->id, 'assigned_type' => null]);+ $asset->refresh();++ $this->assertFalse($asset->isValid());}public function test_get_image_url_method()