Skip to content

A free open source IT asset/license management system

Fixed FD-57816 / 825v-x37h - added user_id to the CSV filename to prevent collisions

sniped635834bed59
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php
index 2417e36f2e..4dd0f689e3 100644
--- a/app/Http/Controllers/Api/ImportController.php
+++ b/app/Http/Controllers/Api/ImportController.php
@@ -230,14 +230,17 @@ class ImportController extends Controller
);
}
- $date = date('Y-m-d-his');
-
+ // Namespace the storage key by the uploader so two users
+ // posting the same filename in the same second can never clobber
+ // each other's bytes on disk. H is 24-hour so AM/PM do not
+ // alias to the same timestamp either.
+ $date = now()->format('Y-m-d-His');
$fixed_filename = Str::of($file->getClientOriginalName())->basename('.csv').'.csv';
- $file_name = $date . '-' . $fixed_filename;
+ $file_name = auth()->id().'-'.$date.'-'.$fixed_filename;
// Storage::putFileAs routes through the Filesystem abstraction so it works
// uniformly against local and s3_private drivers.
- if (!Storage::putFileAs($diskPath, $file, $file_name)) {
+ if (! Storage::putFileAs($diskPath, $file, $file_name)) {
$results['error'] = trans('admin/hardware/message.upload.error');
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500);
@@ -464,7 +467,7 @@ class ImportController extends Controller
// the default (private) disk. The pre-fix path 'imports/'
// missed the 'private_uploads/' prefix and silently
// no-op'd on both drivers.
- Storage::delete('private_uploads/imports/' . $import->file_path);
+ Storage::delete('private_uploads/imports/'.$import->file_path);
$import->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.import.file_delete_success')));
diff --git a/tests/Feature/Importing/Api/ImportStorageKeyIsolationTest.php b/tests/Feature/Importing/Api/ImportStorageKeyIsolationTest.php
new file mode 100644
index 0000000000..dfdcc0c4e4
--- /dev/null
+++ b/tests/Feature/Importing/Api/ImportStorageKeyIsolationTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Tests\Feature\Importing\Api;
+
+use App\Models\User;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\Storage;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\Support\Importing\CleansUpImportFiles;
+use Tests\TestCase;
+
+/**
+ * Regression test for cross-user storage-key collision on the import upload
+ * endpoint. The storage key used to be `date('Y-m-d-his').'-'.basename` with
+ * no uploader component, so two callers posting the same filename inside the
+ * same second resolved to the same key and the second putFileAs silently
+ * overwrote the first. The uploader-id prefix added to the key eliminates
+ * that cross-user collision class.
+ */
+class ImportStorageKeyIsolationTest extends TestCase
+{
+ use CleansUpImportFiles;
+
+ #[Test]
+ public function two_users_uploading_the_same_filename_in_the_same_second_do_not_clobber_each_other(): void
+ {
+ Carbon::setTestNow('2026-10-05 12:34:56');
+
+ $victim = User::factory()->canImport()->create();
+ $attacker = User::factory()->canImport()->create();
+
+ $victimBytes = "name,email\nVictim,[email protected]\n";
+ $attackerBytes = "name,email\nAttacker,[email protected]\n";
+
+ $victimResponse = $this->actingAsForApi($victim)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [UploadedFile::fake()->createWithContent('inventory.csv', $victimBytes)],
+ ])
+ ->assertOk();
+
+ $attackerResponse = $this->actingAsForApi($attacker)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [UploadedFile::fake()->createWithContent('inventory.csv', $attackerBytes)],
+ ])
+ ->assertOk();
+
+ $victimKey = $victimResponse->json('files.0.file_path');
+ $attackerKey = $attackerResponse->json('files.0.file_path');
+
+ $this->assertNotSame($victimKey, $attackerKey, 'Two uploaders at the same second must not share a storage key.');
+ $this->assertStringContainsString((string) $victim->id, $victimKey);
+ $this->assertStringContainsString((string) $attacker->id, $attackerKey);
+
+ $this->assertSame($victimBytes, Storage::get('private_uploads/imports/'.$victimKey));
+ $this->assertSame($attackerBytes, Storage::get('private_uploads/imports/'.$attackerKey));
+ }
+}