Skip to content

A free open source IT asset/license management system

Handle malformed company IDs payload

snipe3c1cfa4d6e64
diff --git a/app/Models/User.php b/app/Models/User.php
index 815db37b9b..eb2b922404 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -749,6 +749,16 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
*/
public function syncCompaniesWithLogging(array $companyIds): void
{
+ // Belt-and-suspenders coercion so no caller — API, web, importer,
+ // artisan, tinker, a queue job, a future FMCS refactor — can slip
+ // nested arrays or other non-scalars into ->sync(), which would
+ // bind them into SQL query params and trip "Array to string
+ // conversion" (elevated to ErrorException by Laravel's error
+ // handler). Reduce to a flat, unique list of positive int ids.
+ $companyIds = array_values(array_unique(array_filter(
+ array_map('intval', array_filter($companyIds, 'is_scalar'))
+ )));
+
$oldIds = $this->companies()->orderBy('companies.id')->pluck('companies.id')->toArray();
$this->companies()->sync($companyIds);
$newIds = $this->companies()->orderBy('companies.id')->pluck('companies.id')->toArray();
diff --git a/public/mix-manifest.json b/public/mix-manifest.json
index f7779517bc..4e2974836d 100644
--- a/public/mix-manifest.json
+++ b/public/mix-manifest.json
@@ -5,7 +5,7 @@
"/css/build/AdminLTE.css": "/css/build/AdminLTE.css?id=bdf169bc2141f453390614c138cdce95",
"/css/dist/all.css": "/css/dist/all.css?id=954276ea6b8b1267eab1f7140f46c0ff",
"/css/dist/signature-pad.css": "/css/dist/signature-pad.css?id=6a89d3cd901305e66ced1cf5f13147f7",
- "/css/dist/signature-pad.min.css": "/css/dist/signature-pad.min.css?id=6a89d3cd901305e66ced1cf5f13147f7",
+ "/css/dist/signature-pad.min.css": "/css/dist/signature-pad.min.css?id=d41d8cd98f00b204e9800998ecf8427e",
"/js/select2/i18n/af.js": "/js/select2/i18n/af.js?id=4f6fcd73488ce79fae1b7a90aceaecde",
"/js/select2/i18n/ar.js": "/js/select2/i18n/ar.js?id=65aa8e36bf5da57ff4e3f22a835ab035",
"/js/select2/i18n/az.js": "/js/select2/i18n/az.js?id=270c257daf8140a0cf23ad5de6f8ed1b",
diff --git a/tests/Feature/Users/UserCompanyLoggingTest.php b/tests/Feature/Users/UserCompanyLoggingTest.php
index 9d09795f0c..1582c1faec 100644
--- a/tests/Feature/Users/UserCompanyLoggingTest.php
+++ b/tests/Feature/Users/UserCompanyLoggingTest.php
@@ -148,4 +148,61 @@ class UserCompanyLoggingTest extends TestCase
$this->assertEquals(0, $newLogs, 'No changes should produce no new log entries');
}
+
+ /**
+ * Regression for a Rollbar "Array to string conversion" on
+ * /api/v1/users/{id} update. When a malformed payload landed a
+ * nested array inside company_ids and the API path's intval coercion
+ * didn't fully flatten it, ->sync() bound the sub-array as a query
+ * param and PHP tripped array-to-string conversion. The method now
+ * coerces to a flat list of positive-int scalar ids up front.
+ */
+ public function test_sync_coerces_non_scalar_company_ids()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $user = User::factory()->create();
+
+ // Deliberately hostile payload: nested array, string that intvals
+ // to a real id, string that intvals to zero (should drop), null,
+ // boolean, plus real ints. sync() must never see the non-scalars.
+ $user->syncCompaniesWithLogging([
+ $companyA->id,
+ [$companyB->id, 999],
+ (string) $companyB->id,
+ 'not-a-number',
+ null,
+ true,
+ $companyA->id,
+ ]);
+
+ $this->assertEqualsCanonicalizing(
+ [$companyA->id, $companyB->id],
+ $user->companies()->pluck('companies.id')->toArray(),
+ 'Only the scalar-coerceable real company ids should end up on the pivot'
+ );
+ }
+
+ public function test_sync_with_only_non_scalar_ids_detaches_all()
+ {
+ $company = Company::factory()->create();
+
+ $user = User::factory()->create();
+ $user->companies()->sync([$company->id]);
+
+ // Nothing scalar-coerceable to a positive int → an empty sync,
+ // which detaches every existing pivot row. Verifies the coercion
+ // reduces to [] rather than silently keeping the old set.
+ $user->syncCompaniesWithLogging([
+ [1, 2],
+ null,
+ 'x',
+ false,
+ ]);
+
+ $this->assertEmpty(
+ $user->companies()->pluck('companies.id')->toArray(),
+ 'Payload with no scalar-coerceable ids should detach all pivot rows'
+ );
+ }
}