Skip to content

A free open source IT asset/license management system

Importer: Fixed FD-57826 / r84c-6xf8 - prevent N x N comparisons when checking for duplicate headers

snipec0ed2d310610
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php
index 5a2e7c8d79..57fa5ea9c5 100644
--- a/app/Http/Controllers/Api/ImportController.php
+++ b/app/Http/Controllers/Api/ImportController.php
@@ -198,20 +198,21 @@ class ImportController extends Controller
);
}
- // duplicate headers check
+ // duplicate headers check: single-pass seen-map keyed by
+ // header name recording the first-seen column index. The
+ // previous shape ran in_array + array_search for every
+ // header, and each of those scans the full array of
+ // values on every call. For N headers that was roughly
+ // N x N comparisons even when no duplicates existed.
$duplicate_headers = [];
+ $seen = [];
+ foreach ($import->header_row as $i => $header) {
+ if (array_key_exists($header, $seen)) {
+ $duplicate_headers[] = "Duplicate header '$header' detected, first at column: ".($seen[$header] + 1).', repeats at column: '.($i + 1);
- for ($i = 0; $i < count($import->header_row); $i++) {
- $header = $import->header_row[$i];
- if (in_array($header, $import->header_row)) {
- $found_at = array_search($header, $import->header_row);
- if ($i > $found_at) {
- // avoid reporting duplicates twice, e.g. "1 is same as 17! 17 is same as 1!!!"
- // as well as "1 is same as 1!!!" (which is always true)
- // has to be > because otherwise the first result of array_search will always be $i itself(!)
- array_push($duplicate_headers, "Duplicate header '$header' detected, first at column: ".($found_at + 1).', repeats at column: '.($i + 1));
- }
+ continue;
}
+ $seen[$header] = $i;
}
if (count($duplicate_headers) > 0) {
return response()->json(Helper::formatStandardApiResponse('error', null, implode('; ', $duplicate_headers)), 422);
diff --git a/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php b/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php
new file mode 100644
index 0000000000..e7db5cebd6
--- /dev/null
+++ b/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace Tests\Feature\Importing\Api;
+
+use App\Models\User;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * The pre-fix duplicate-header check ran `in_array` + `array_search`
+ * for every header, both O(N), so a CSV with N unique headers took
+ * O(N^2) time even though no duplicates existed. The rewrite uses a
+ * single-pass seen-map keyed by header name recording the first-seen
+ * column index. These tests cover the two things the rewrite must
+ * preserve: duplicates are still rejected with the same message shape,
+ * and a wide CSV with distinct headers goes through quickly.
+ */
+class ImportDuplicateHeaderDetectionTest extends TestCase
+{
+ public function test_duplicate_headers_are_rejected_with_first_and_repeat_column_numbers(): void
+ {
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $csv = "asset tag,name,asset tag,status\nAT-1,Example,AT-2,Deployed\n";
+
+ $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('dup.csv', $csv),
+ ],
+ ])
+ ->assertStatus(422)
+ ->assertJsonFragment([
+ 'messages' => "Duplicate header 'asset tag' detected, first at column: 1, repeats at column: 3",
+ ]);
+ }
+
+ public function test_multiple_distinct_duplicate_pairs_are_each_reported(): void
+ {
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $csv = "id,name,id,status,name\nR1,Foo,R2,Deployed,Bar\n";
+
+ $response = $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('two-dups.csv', $csv),
+ ],
+ ])
+ ->assertStatus(422);
+
+ $messages = $response->json('messages');
+ $this->assertStringContainsString("Duplicate header 'id' detected, first at column: 1, repeats at column: 3", $messages);
+ $this->assertStringContainsString("Duplicate header 'name' detected, first at column: 2, repeats at column: 5", $messages);
+ }
+
+ public function test_triple_occurrence_header_reports_both_repeats_against_the_first_occurrence(): void
+ {
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $csv = "name,status,name,notes,name\nA,Deployed,B,n,C\n";
+
+ $response = $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('triple.csv', $csv),
+ ],
+ ])
+ ->assertStatus(422);
+
+ $messages = $response->json('messages');
+ $this->assertStringContainsString("Duplicate header 'name' detected, first at column: 1, repeats at column: 3", $messages);
+ $this->assertStringContainsString("Duplicate header 'name' detected, first at column: 1, repeats at column: 5", $messages);
+ }
+
+ public function test_wide_csv_with_distinct_headers_is_accepted_quickly(): void
+ {
+ // Pre-fix, a 8000-column unique-header CSV ran roughly 64 million
+ // in_array + array_search comparisons against the header array
+ // (both scan the full array of values on every call), which was
+ // the ~10-second case the reporter demonstrated. Post-fix it is a
+ // single pass of 8000 array_key_exists lookups against a
+ // hashtable, which should complete well under one second. A
+ // 10-second budget here still flags a regression into the
+ // scan-per-header shape without being flaky under heavy CI load.
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $columns = array_map(fn ($i) => "column_$i", range(0, 7999));
+ $values = array_fill(0, 8000, 'v');
+ $csv = implode(',', $columns)."\n".implode(',', $values)."\n";
+
+ $start = microtime(true);
+ $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('wide.csv', $csv),
+ ],
+ ])
+ ->assertSuccessful();
+ $elapsed = microtime(true) - $start;
+
+ $this->assertLessThan(
+ 10.0,
+ $elapsed,
+ "Processing 8,000 unique CSV headers took {$elapsed}s. Pre-fix measured ~10s on the same shape, so anything near or past the budget is a regression into quadratic duplicate-detection behavior.",
+ );
+ }
+
+ private function fakeCsvUpload(string $name, string $contents): UploadedFile
+ {
+ $path = tempnam(sys_get_temp_dir(), 'csv');
+ file_put_contents($path, $contents);
+
+ return new UploadedFile($path, $name, 'text/csv', null, true);
+ }
+}