Skip to content

A free open source IT asset/license management system

Reverter tighter scope, updated tests

sniped58e757d3747
diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php
index 6cfef6f042..6bbf3f2e57 100644
--- a/app/Importer/Importer.php
+++ b/app/Importer/Importer.php
@@ -9,6 +9,7 @@ use App\Models\User;
use Carbon\CarbonImmutable;
use ForceUTF8\Encoding;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use League\Csv\Reader;
@@ -393,6 +394,18 @@ abstract class Importer
}
// No luck finding a user on username or first name, let's create one.
+
+ // Floater-mode escalation guard (#19200). A side-effect of an asset
+ // import is that referenced users get minted with no company pivot —
+ // under floater mode that promotes them to system-wide visibility.
+ // Refuse to create the user when the importer's actor isn't trusted
+ // to grant floater status. Same guard the User CSV importer uses.
+ if (Auth::check() && ! auth()->user()->canGrantFloaterStatus()) {
+ $this->log('Skipping creation of referenced user "'.($user_array['username'] ?? '').'": cannot create a user with no company assignment while floater mode is enabled (#19200).');
+
+ return false;
+ }
+
$user = new User;
$user->first_name = $user_array['first_name'];
diff --git a/app/Models/User.php b/app/Models/User.php
index b3466b0000..8d75e733bc 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -569,10 +569,14 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
/**
* Whether this user is allowed to save another user with no company
* assignment. Under floater mode, "no companies" means the target gains
- * system-wide visibility — only superusers are trusted to do that.
- * When floater mode is off, anyone with `users.edit` can clear the pivot
- * (no escalation possible, the target just becomes unscoped). When FMCS
- * itself is off the whole notion is moot. See #19200.
+ * system-wide visibility, so the actor needs to already be at that
+ * privilege level: superusers can always grant it; floater actors
+ * (themselves uncompanied) can grant it to others without escalating —
+ * they already have the same access. The only blocked case is a
+ * *companied* non-superuser trying to elevate someone to floater, which
+ * would be a genuine escalation. When floater mode itself is off — or
+ * FMCS is off — there's no floater to grant, so the answer is yes. See
+ * #19200.
*/
public function canGrantFloaterStatus(): bool
{
@@ -584,8 +588,11 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
if (! $settings->null_company_is_floater) {
return true;
}
+ if ($this->isSuperUser()) {
+ return true;
+ }
- return $this->isSuperUser();
+ return ! $this->companies()->exists();
}
/**
diff --git a/tests/Feature/Importing/Api/ImportAssetsTest.php b/tests/Feature/Importing/Api/ImportAssetsTest.php
index 54e95293e2..95a209b842 100644
--- a/tests/Feature/Importing/Api/ImportAssetsTest.php
+++ b/tests/Feature/Importing/Api/ImportAssetsTest.php
@@ -726,4 +726,38 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$newAsset = Asset::where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();
$this->assertEquals($user->id, $newAsset->assigned_to, 'Asset should be checked out to a no-company user when floater mode is on');
}
+
+ #[Test]
+ public function asset_import_does_not_mint_a_floater_user_side_effect_for_non_superuser(): void
+ {
+ // #19200: when an asset CSV references a brand-new username, the base
+ // Importer::createOrFetchUser side-effects a new user with no company
+ // pivot. Under floater mode that previously promoted the new user to
+ // system-wide visibility — exploitable as an asset import path. The
+ // guard refuses the user creation for actors who can't grant floater
+ // status; the asset still imports, it just doesn't get checked out.
+ $this->settings->enableFloaterMode();
+
+ $company = Company::factory()->create();
+ $importer = $company->users()->save(
+ User::factory()->createAssets()->editAssets()->canImport()->create(),
+ );
+
+ $importFileBuilder = ImportFileBuilder::new([
+ 'companyName' => $company->name,
+ 'assigneeUsername' => 'phantom-floater-user',
+ 'assigneeFullName' => 'Phantom Floater',
+ 'assigneeEmail' => '[email protected]',
+ ]);
+
+ $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi($importer);
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $this->assertDatabaseMissing('users', ['username' => 'phantom-floater-user']);
+
+ $newAsset = Asset::where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();
+ $this->assertNull($newAsset->assigned_to, 'Asset imports but is not checked out to the rejected user');
+ }
}
diff --git a/tests/Feature/Users/Ui/FloaterModeGateTest.php b/tests/Feature/Users/Ui/FloaterModeGateTest.php
index 6db829752d..e9f8bea754 100644
--- a/tests/Feature/Users/Ui/FloaterModeGateTest.php
+++ b/tests/Feature/Users/Ui/FloaterModeGateTest.php
@@ -109,27 +109,97 @@ class FloaterModeGateTest extends TestCase
$this->assertNotEmpty($victim->fresh()->companies);
}
- public function test_uncompanied_non_superuser_cannot_save_user_with_no_companies()
+ public function test_non_superuser_cannot_create_a_new_user_with_no_companies_in_floater_mode()
{
- // Per spec: under floater mode, ONLY superusers may grant floater
- // status. An uncompanied non-superuser (themselves a floater) was
- // previously trusted to manage other floaters — that's now closed off
- // because it lets the chain "clear pivot → reset target's password
- // (via canEditAuthFields gaps) → log in as a new floater" complete.
+ // Covers the web POST path — SaveUserRequest's withValidator fires on
+ // both store and update because it's the same FormRequest.
$this->settings->enableFloaterMode();
- $actor = User::factory()->editUsers()->create();
- $actor->companies()->sync([]);
+ $company = Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->createUsers()->viewUsers()->create());
+
+ $this->actingAs($actor)
+ ->post(route('users.store'), [
+ 'first_name' => 'New',
+ 'username' => 'newfloateruser',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'company_ids' => [],
+ ])
+ ->assertSessionHasErrors('company_ids');
+
+ $this->assertDatabaseMissing('users', ['username' => 'newfloateruser']);
+ }
+
+ public function test_non_superuser_cannot_create_a_new_user_with_no_companies_via_api_in_floater_mode()
+ {
+ // Covers the API POST path. Same SaveUserRequest underneath, but
+ // worth pinning the JSON entry point separately so the gate isn't
+ // accidentally bypassed by a future API rewrite.
+ $this->settings->enableFloaterMode();
+
+ $company = Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->createUsers()->viewUsers()->create());
+
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.users.store'), [
+ 'first_name' => 'New',
+ 'username' => 'newfloateruserapi',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'company_ids' => [],
+ ])
+ ->assertJsonStructure(['messages' => ['company_ids']]);
+
+ $this->assertDatabaseMissing('users', ['username' => 'newfloateruserapi']);
+ }
+
+ public function test_superuser_can_create_a_user_with_no_companies_in_floater_mode()
+ {
+ // Sanity counterpart to the two preceding tests — confirms the gate
+ // only fires for non-superusers, not as an unconditional block on
+ // empty-pivot user creation.
+ $this->settings->enableFloaterMode();
+
+ $superuser = User::factory()->superuser()->create();
+
+ $this->actingAs($superuser)
+ ->post(route('users.store'), [
+ 'first_name' => 'New',
+ 'username' => 'superminted',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'company_ids' => [],
+ ])
+ ->assertSessionDoesntHaveErrors('company_ids');
+
+ $this->assertDatabaseHas('users', ['username' => 'superminted']);
+ }
+
+ public function test_floater_actor_can_save_another_user_with_no_companies()
+ {
+ // An actor who is themselves uncompanied (already a floater under
+ // floater mode) is trusted to manage other floaters — they can't
+ // escalate their privileges because they already have none above
+ // floater-level. Legitimate use case: an HR / onboarding role that
+ // sits outside any specific sub-company and is responsible for
+ // creating users across the org (see PR review thread for #19200).
+ // Only the *companied → uncompanied* transition by a companied actor
+ // is blocked.
+ $this->settings->enableFloaterMode();
+
+ $floaterActor = User::factory()->editUsers()->create();
+ $floaterActor->companies()->sync([]);
$target = User::factory()->create();
$target->companies()->sync([]);
- $this->actingAs($actor)
+ $this->actingAs($floaterActor)
->put(route('users.update', $target), [
'first_name' => $target->first_name,
'username' => $target->username,
'company_ids' => [],
])
- ->assertSessionHasErrors('company_ids');
+ ->assertSessionDoesntHaveErrors('company_ids');
}
public function test_non_superuser_can_change_their_companies_to_a_non_empty_set_in_floater_mode()