A free open source IT asset/license management system
Fixed #19537 - better handle bad data
snipe7f2cca48d995
diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.phpindex 68b04a5656..f76b9dc244 100644--- a/app/Http/Controllers/Api/StatuslabelsController.php+++ b/app/Http/Controllers/Api/StatuslabelsController.php@@ -108,9 +108,14 @@ class StatuslabelsController extends Controller$statuslabel->deployable = $statusType['deployable'];$statuslabel->pending = $statusType['pending'];$statuslabel->archived = $statusType['archived'];- $statuslabel->color = $request->input('color');- $statuslabel->show_in_nav = $request->input('show_in_nav', 0);- $statuslabel->default_label = $request->input('default_label', 0);+ // Coerce boolean-shaped columns through $request->boolean()+ // so a malformed payload (nested object / array) can't slam+ // an object into a tinyint column and blow up at save() with+ // a 500. Non-scalar payloads land as false, which the caller+ // can correct on retry with a proper boolean value.+ $statuslabel->color = is_scalar($request->input('color')) ? $request->input('color') : null;+ $statuslabel->show_in_nav = $request->boolean('show_in_nav');+ $statuslabel->default_label = $request->boolean('default_label');if ($statuslabel->save()) {return response()->json(Helper::formatStandardApiResponse('success', $statuslabel, trans('admin/statuslabels/message.create.success')));@@ -166,9 +171,14 @@ class StatuslabelsController extends Controller$statuslabel->deployable = $statusType['deployable'];$statuslabel->pending = $statusType['pending'];$statuslabel->archived = $statusType['archived'];- $statuslabel->color = $request->input('color');- $statuslabel->show_in_nav = $request->input('show_in_nav', 0);- $statuslabel->default_label = $request->input('default_label', 0);+ // Coerce boolean-shaped columns through $request->boolean()+ // so a malformed payload (nested object / array) can't slam+ // an object into a tinyint column and blow up at save() with+ // a 500. Non-scalar payloads land as false, which the caller+ // can correct on retry with a proper boolean value.+ $statuslabel->color = is_scalar($request->input('color')) ? $request->input('color') : null;+ $statuslabel->show_in_nav = $request->boolean('show_in_nav');+ $statuslabel->default_label = $request->boolean('default_label');if ($statuslabel->save()) {return response()->json(Helper::formatStandardApiResponse('success', $statuslabel, trans('admin/statuslabels/message.update.success')));diff --git a/app/Http/Requests/StoreAccessoryRequest.php b/app/Http/Requests/StoreAccessoryRequest.phpindex 14f9faf49c..3395ab7cbc 100644--- a/app/Http/Requests/StoreAccessoryRequest.php+++ b/app/Http/Requests/StoreAccessoryRequest.php@@ -26,7 +26,12 @@ class StoreAccessoryRequest extends ImageUploadRequest$this->merge(['purchase_cost' => Helper::ParseCurrency($this->input('purchase_cost'))]);}- if ($this->category_id) {+ // Scalar-guard: Category::find() on an array/object arg returns+ // a Collection (whereIn semantics) which does not expose+ // ->category_type and blows up. Non-scalar inputs land the row+ // in the standard validator with the model's category_id rule+ // (integer|exists), which cleanly rejects with 422.+ if ($this->category_id && is_scalar($this->category_id)) {if ($category = Category::find($this->category_id)) {$this->merge(['category_type' => $category->category_type ?? null,@@ -42,8 +47,20 @@ class StoreAccessoryRequest extends ImageUploadRequest*/public function rules(): array{+ // FK type rules live at the FormRequest layer so a malformed+ // payload (array/object where an int is expected) trips a+ // clean 422 before the controller reaches DB write. The+ // model's own $rules cover the ->save() path (factories,+ // seeders) but don't intercept the HTTP boundary.return array_merge(- ['category_type' => 'in:accessory'],+ [+ 'category_type' => 'in:accessory',+ 'category_id' => 'integer|nullable',+ 'company_id' => 'integer|nullable',+ 'manufacturer_id' => 'integer|nullable',+ 'location_id' => 'integer|nullable',+ 'default_supplier_id' => 'integer|nullable',+ ],parent::rules(),);}diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.phpindex bfe7eb8308..949f8ab714 100644--- a/app/Http/Requests/StoreAssetModelRequest.php+++ b/app/Http/Requests/StoreAssetModelRequest.php@@ -21,7 +21,12 @@ class StoreAssetModelRequest extends ImageUploadRequest{parent::prepareForValidation();- if ($this->category_id) {+ // Scalar-guard: Category::find() on an array/object arg returns+ // a Collection (whereIn semantics) which does not expose+ // ->category_type and blows up. Non-scalar inputs land the row+ // in the standard validator with the model's category_id rule+ // (integer|exists), which cleanly rejects with 422.+ if ($this->category_id && is_scalar($this->category_id)) {if ($category = Category::find($this->category_id)) {$this->merge(['category_type' => $category->category_type ?? null,@@ -38,8 +43,19 @@ class StoreAssetModelRequest extends ImageUploadRequest*/public function rules(): array{+ // FK type rules live at the FormRequest layer so a malformed+ // payload (array/object where an int is expected) trips a+ // clean 422 before the controller reaches DB write. The+ // model's own $rules cover the ->save() path (factories,+ // seeders) but don't intercept the HTTP boundary.return array_merge(- ['category_type' => 'in:asset'],+ [+ 'category_type' => 'in:asset',+ 'category_id' => 'integer|nullable',+ 'manufacturer_id' => 'integer|nullable',+ 'depreciation_id' => 'integer|nullable',+ 'fieldset_id' => 'integer|nullable',+ ],parent::rules(),);}diff --git a/app/Http/Requests/StoreConsumableRequest.php b/app/Http/Requests/StoreConsumableRequest.phpindex 9cb12c6ae4..0b879c6347 100644--- a/app/Http/Requests/StoreConsumableRequest.php+++ b/app/Http/Requests/StoreConsumableRequest.php@@ -26,7 +26,12 @@ class StoreConsumableRequest extends ImageUploadRequest$this->merge(['purchase_cost' => Helper::ParseCurrency($this->input('purchase_cost'))]);}- if ($this->category_id) {+ // Scalar-guard: Category::find() on an array/object arg returns+ // a Collection (whereIn semantics) which does not expose+ // ->category_type and blows up. Non-scalar inputs land the row+ // in the standard validator with the model's category_id rule+ // (integer|exists), which cleanly rejects with 422.+ if ($this->category_id && is_scalar($this->category_id)) {if ($category = Category::find($this->category_id)) {$this->merge(['category_type' => $category->category_type ?? null,@@ -42,8 +47,20 @@ class StoreConsumableRequest extends ImageUploadRequest*/public function rules(): array{+ // FK type rules live at the FormRequest layer so a malformed+ // payload (array/object where an int is expected) trips a+ // clean 422 before the controller reaches DB write. The+ // model's own $rules cover the ->save() path (factories,+ // seeders) but don't intercept the HTTP boundary.return array_merge(- ['category_type' => 'in:consumable'],+ [+ 'category_type' => 'in:consumable',+ 'category_id' => 'integer|nullable',+ 'company_id' => 'integer|nullable',+ 'manufacturer_id' => 'integer|nullable',+ 'location_id' => 'integer|nullable',+ 'default_supplier_id' => 'integer|nullable',+ ],parent::rules(),);}diff --git a/app/Http/Traits/TwoColumnUniqueUndeletedTrait.php b/app/Http/Traits/TwoColumnUniqueUndeletedTrait.phpindex 98d509bf70..16f44f1b12 100644--- a/app/Http/Traits/TwoColumnUniqueUndeletedTrait.php+++ b/app/Http/Traits/TwoColumnUniqueUndeletedTrait.php@@ -16,6 +16,15 @@ trait TwoColumnUniqueUndeletedTrait$column = $parameters[0];$value = $this->{$parameters[0]};+ // Non-scalar values (arrays / objects from a malformed API+ // payload) blow up the string concat below with "Array to+ // string conversion". Coerce to empty so the rule falls+ // through cleanly and the pipe-level `string` rule surfaces+ // the type mismatch as a 422 instead of an uncaught 500.+ if (! is_scalar($value)) {+ $value = '';+ }+// This is an existing model we're updating so ignore the current ID ($this->getKey())if ($this->exists) {return 'two_column_unique_undeleted:'.$this->table.','.$this->getKey().','.$column.','.$value;