Skip to content

A free open source IT asset/license management system

Added string to dept validation

snipeb56ff6adba02
diff --git a/app/Models/Department.php b/app/Models/Department.php
index e9583f776f..14252ef72a 100644
--- a/app/Models/Department.php
+++ b/app/Models/Department.php
@@ -44,7 +44,7 @@ class Department extends SnipeModel
];
protected $rules = [
- 'name' => 'required|max:255|is_unique_across_company_and_location:departments,name',
+ 'name' => 'required|string|max:255|is_unique_across_company_and_location:departments,name',
'location_id' => 'numeric|nullable|exists:locations,id',
'company_id' => 'numeric|nullable|exists:companies,id',
'manager_id' => 'numeric|nullable|exists:users,id',
diff --git a/tests/Feature/Departments/Api/UpdateDepartmentsTest.php b/tests/Feature/Departments/Api/UpdateDepartmentsTest.php
index 92b4df0d97..b7493e79ef 100644
--- a/tests/Feature/Departments/Api/UpdateDepartmentsTest.php
+++ b/tests/Feature/Departments/Api/UpdateDepartmentsTest.php
@@ -35,4 +35,28 @@ class UpdateDepartmentsTest extends TestCase
$this->assertEquals('Test Note', $department->notes, 'Note was not updated');
}
+
+ public function test_array_name_is_rejected_and_does_not_corrupt_row()
+ {
+ // Regression: the API PATCH endpoint used the generic ImageUploadRequest
+ // which does not validate `name`, so a caller could send name as an
+ // array. The model's max:255 rule counted array items instead of string
+ // length, save() proceeded, JSON-encoded the array as the DB column
+ // value, and then the transformer TypeErrored inside htmlspecialchars.
+ // Fixed by tightening the model rule to require a string.
+ $department = Department::factory()->create(['name' => 'Original Name']);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.departments.update', $department), [
+ 'name' => ['hax', 'hax2'],
+ ])
+ ->assertStatusMessageIs('error');
+
+ $department->refresh();
+ $this->assertSame(
+ 'Original Name',
+ $department->name,
+ 'Row was corrupted by an array payload — validation failed to reject it.'
+ );
+ }
}