Skip to content

A free open source IT asset/license management system

Fixed wwp4-qx8p-62g8 - Bulk user edit and CSV import detach users from FMCS companies outside editors

snipe0930440bbd37
diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php
index f2a0e0cda6..17c12b4a07 100644
--- a/app/Http/Controllers/Users/BulkUsersController.php
+++ b/app/Http/Controllers/Users/BulkUsersController.php
@@ -331,14 +331,22 @@ class BulkUsersController extends Controller
if ($bulkCompanyIds || $clearCompanies) {
if ($clearCompanies && ! auth()->user()->isSuperUser() && Company::isFullMultipleCompanySupportEnabled()) {
- // Non-superusers can only detach companies they belong to; sync([]) would
- // also wipe memberships for companies outside their scope.
+ // Non-superusers can only detach companies they belong to. A raw
+ // sync([]) would also wipe memberships for companies outside their
+ // scope.
$user->companies()->detach(Company::getIdsForCurrentUser(
$user->companies()->pluck('companies.id')->toArray()
));
$user->syncLegacyCompanyIdMirror();
} else {
- $user->syncCompaniesWithLogging($allowedIds);
+ // GHSA-wwp4-qx8p-62g8: route through the FMCS-safe helper so a
+ // scoped editor cannot strip the target's memberships in
+ // companies the editor cannot see. Raw sync() treats its
+ // argument as the full new pivot set and deletes anything
+ // else. The helper reads the target's current pivot
+ // unscoped, splits into visible + invisible-to-editor,
+ // and merges the invisible slice back before syncing.
+ $user->syncCompaniesPreservingInvisibleTo(auth()->user(), $bulkCompanyIds);
}
}
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 1d7ac6a0f4..3125af6840 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -291,9 +291,13 @@ class UserImporter extends ItemImporter
$user->save();
// Sync company pivot when companies were specified in this row.
+ // GHSA-wwp4-qx8p-62g8: route through the FMCS-safe helper so a
+ // scoped operator running the interactive importer cannot strip
+ // the target's memberships in companies the operator cannot see.
+ // The helper short-circuits to a raw sync when the editor is
+ // null (CLI-run importer), superuser, or FMCS is off.
if (! empty($companyIds)) {
- $user->companies()->sync($companyIds);
- $user->syncLegacyCompanyIdMirror();
+ $user->syncCompaniesPreservingInvisibleTo(Auth::user(), $companyIds);
}
// Update the location of any assets checked out to this user
@@ -369,11 +373,16 @@ class UserImporter extends ItemImporter
$this->recordCreated();
// Sync all resolved companies to the pivot. For single-company rows the
- // User::created event already added company_id; sync() here is idempotent
+ // User::created event already added company_id, and sync here is idempotent
// for that case and adds any additional companies for multi-company rows.
+ // GHSA-wwp4-qx8p-62g8: same reasoning as the update branch above.
+ // Route through the FMCS-safe helper so a scoped operator running
+ // the interactive importer cannot strip the target's memberships
+ // in companies the operator cannot see. Helper short-circuits to
+ // a raw sync when the editor is null (CLI), superuser, or FMCS
+ // is off.
if (! empty($companyIds)) {
- $user->companies()->sync($companyIds);
- $user->syncLegacyCompanyIdMirror();
+ $user->syncCompaniesPreservingInvisibleTo(Auth::user(), $companyIds);
}
if (($user->email) && ($user->activated == '1')) {
diff --git a/app/Models/User.php b/app/Models/User.php
index 8c71928864..e4ef314f4d 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -988,16 +988,20 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
* Superuser editors skip the merge because they can see every
* company, so their submission already represents full intent.
*/
- public function syncCompaniesPreservingInvisibleTo(User $editor, array $submittedCompanyIds): void
+ public function syncCompaniesPreservingInvisibleTo(?User $editor, array $submittedCompanyIds): void
{
$submitted = array_map('intval', $submittedCompanyIds);
// FMCS is off, so every user can see every company
// and there is no invisible-to-editor set to preserve. Superuser
// editors also skip because their submission already
- // represents full intent across all tenants.
+ // represents full intent across all tenants. A null editor
+ // means no scope context (CLI-run importer, seeder, artisan
+ // command), which behaves like a superuser edit: sync the
+ // submission verbatim because there is no scope to preserve
+ // against.
$fmcsOn = (bool) Setting::getSettings()->full_multiple_companies_support;
- if (! $fmcsOn || $editor->isSuperUser()) {
+ if (! $fmcsOn || $editor === null || $editor->isSuperUser()) {
$this->syncCompaniesWithLogging($submitted);
return;
diff --git a/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php b/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php
index 64abf7bca6..c25a5bb8e6 100644
--- a/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php
+++ b/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php
@@ -134,4 +134,95 @@ class ImportUsersMultiCompanyTest extends ImportDataTestCase
'username' => $row['username'],
]);
}
+
+ /**
+ * GHSA-wwp4-qx8p-62g8 regression coverage for the CSV importer.
+ *
+ * Before the fix, the importer's update branch used a raw
+ * $user->companies()->sync($companyIds), which strips any target
+ * pivot rows for companies not in the submitted set. A scoped
+ * importer running the interactive Livewire path could re-import a
+ * co-tenant target with only their own company in the row and
+ * silently detach the target from co-companies the importer never
+ * saw. The fix routes the update branch through
+ * User::syncCompaniesPreservingInvisibleTo, which reads the target's
+ * pivot unscoped, splits into visible + invisible-to-editor, and
+ * merges the invisible slice back before syncing.
+ */
+ public function test_importer_update_preserves_target_memberships_editor_cannot_see()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id, $companyB->id]);
+
+ $scopedImporter = User::factory()->canImport()->forCompany($companyA)->create();
+
+ // Drop the auto-generated location column: under strict FMCS, a
+ // companied non-superuser can't create a new null-company Location
+ // via the importer (fmcs_company rule on Location.company_id
+ // rejects it), and the test isn't about that constraint.
+ $importFileBuilder = ImportFileBuilder::new([
+ 'username' => $target->username,
+ 'companyName' => $companyA->name,
+ ])->forget('location');
+ // created_by must be the acting user or the process endpoint refuses
+ // the request (ImportController security control that prevents an
+ // import-permission holder from processing someone else's file).
+ $import = Import::factory()->users()->create([
+ 'file_path' => $importFileBuilder->saveToImportsDirectory(),
+ 'created_by' => $scopedImporter->id,
+ ]);
+
+ $this->actingAsForApi($scopedImporter)
+ ->importFileResponse(['import' => $import->id, 'import-update' => true])
+ ->assertOk();
+
+ $membershipIds = \DB::table('company_user')
+ ->where('user_id', $target->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
+ ->all();
+
+ $this->assertEqualsCanonicalizing(
+ [$companyA->id, $companyB->id],
+ $membershipIds,
+ 'CSV re-import by a company-A-scoped operator must not strip the target from company B, which the operator cannot see.',
+ );
+ }
+
+ public function test_importer_create_by_scoped_importer_still_works_for_own_company()
+ {
+ // Sanity: the fix must not break the legitimate create branch. A
+ // scoped importer creating a new user with their own company on
+ // the row should succeed. The invisible-to-editor merge is a
+ // no-op on a create because a brand-new user has no prior pivot
+ // rows to preserve.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $companyA = Company::factory()->create();
+ $scopedImporter = User::factory()->canImport()->forCompany($companyA)->create();
+
+ $importFileBuilder = ImportFileBuilder::new([
+ 'companyName' => $companyA->name,
+ ])->forget('location');
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->users()->create([
+ 'file_path' => $importFileBuilder->saveToImportsDirectory(),
+ 'created_by' => $scopedImporter->id,
+ ]);
+
+ $this->actingAsForApi($scopedImporter)
+ ->importFileResponse(['import' => $import->id])
+ ->assertOk();
+
+ $newUser = User::where('username', $row['username'])->firstOrFail();
+ $membershipIds = $newUser->companies->pluck('id')->map(fn ($id) => (int) $id)->all();
+
+ $this->assertEqualsCanonicalizing([$companyA->id], $membershipIds);
+ }
}
diff --git a/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php b/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
index 3fa447d736..94cdee1891 100644
--- a/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
+++ b/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
@@ -3,8 +3,10 @@
namespace Tests\Feature\Users\Ui\BulkActions;
use App\Models\Actionlog;
+use App\Models\Company;
use App\Models\Group;
use App\Models\User;
+use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class BulkEditUsersTest extends TestCase
@@ -266,4 +268,110 @@ class BulkEditUsersTest extends TestCase
$this->assertEquals(1, $count, "User {$target->id} should have exactly one update log entry");
}
}
+
+ // -----------------------------------------------------------------------
+ // Cross-tenant company detachment (GHSA-wwp4-qx8p-62g8)
+ //
+ // Before the fix, the bulk edit's company-set branch went through a
+ // raw ->companies()->sync(), which treats its argument as the full
+ // new pivot set and deletes anything else. A scoped editor submitting
+ // a company they belong to (a non-empty submission that passes the
+ // clear-mode guards above) would silently strip the target's
+ // memberships in companies the editor never saw. The fix routes the
+ // set-companies branch through User::syncCompaniesPreservingInvisibleTo,
+ // which reads the target's pivot unscoped, splits into visible +
+ // invisible-to-editor, and merges the invisible slice back before the
+ // final sync. These tests pin that behavior across the FMCS modes
+ // called out in the standing FMCS-adversarial-tests rule.
+ // -----------------------------------------------------------------------
+
+ public function test_bulk_edit_preserves_target_memberships_editor_cannot_see_under_strict_fmcs()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id, $companyB->id]);
+
+ $scopedEditor = User::factory()->editUsers()->forCompany($companyA)->create();
+
+ $this->actingAs($scopedEditor)
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'company_ids' => [$companyA->id],
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $membershipIds = DB::table('company_user')
+ ->where('user_id', $target->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
+ ->all();
+
+ $this->assertEqualsCanonicalizing(
+ [$companyA->id, $companyB->id],
+ $membershipIds,
+ 'Bulk edit by a company-A-scoped editor must not strip the target from company B, which the editor cannot see.',
+ );
+ }
+
+ public function test_bulk_edit_preserves_target_memberships_editor_cannot_see_under_floater_mode()
+ {
+ $this->settings->enableFloaterMode();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id, $companyB->id]);
+
+ $scopedEditor = User::factory()->editUsers()->forCompany($companyA)->create();
+
+ $this->actingAs($scopedEditor)
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'company_ids' => [$companyA->id],
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $membershipIds = DB::table('company_user')
+ ->where('user_id', $target->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
+ ->all();
+
+ $this->assertEqualsCanonicalizing([$companyA->id, $companyB->id], $membershipIds);
+ }
+
+ public function test_bulk_edit_superuser_still_gets_full_replacement_semantics()
+ {
+ // Sanity: the fix must not accidentally block the legitimate
+ // "superuser changes a target's full company set" flow. Superusers
+ // bypass the preserve-invisible merge because they can see every
+ // company by definition.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id, $companyB->id]);
+
+ $superuser = User::factory()->superuser()->create();
+
+ $this->actingAs($superuser)
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'company_ids' => [$companyC->id],
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $membershipIds = DB::table('company_user')
+ ->where('user_id', $target->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
+ ->all();
+
+ $this->assertEqualsCanonicalizing([$companyC->id], $membershipIds);
+ }
}