Skip to content

A free open source IT asset/license management system

Fixed RB-21850 - handle imports without a header row

snipe9d99bdf220bb
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php
index f6537b50b7..cd96777369 100644
--- a/app/Livewire/Importer.php
+++ b/app/Livewire/Importer.php
@@ -836,6 +836,19 @@ class Importer extends Component
}
$this->headerRow = $this->activeFile->header_row;
+
+ // header_row is populated by the initial upload path but can be null for
+ // legacy imports created before that column was persisted, or for rows
+ // where a background job never wrote it. Without this guard the foreach
+ // below explodes with "foreach() argument must be of type array|object,
+ // null given" and the wizard is unrecoverable.
+ if (! is_array($this->headerRow) || $this->headerRow === []) {
+ $this->message = trans('admin/hardware/message.import.header_row_missing');
+ $this->message_type = 'danger';
+
+ return;
+ }
+
$this->typeOfImport = $this->activeFile->import_type;
$this->field_map = null;
diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php
index 723e3ae82f..c32edca069 100644
--- a/resources/lang/en-US/admin/hardware/message.php
+++ b/resources/lang/en-US/admin/hardware/message.php
@@ -76,6 +76,7 @@ return [
'file_already_deleted' => 'The file selected was already deleted',
'file_missing_on_disk' => 'The file for this import is no longer on disk. It may have been deleted outside of Snipe-IT. Delete this entry and re-upload the file to try again.',
'file_empty' => 'This file has no data rows. Nothing can be imported from it.',
+ 'header_row_missing' => 'This file does not have a recognized header row. Delete this entry and re-upload the file to try again.',
'header_row_has_malformed_characters' => 'One or more attributes in the header row contain malformed UTF-8 characters',
'content_row_has_malformed_characters' => 'One or more attributes in the first row of content contain malformed UTF-8 characters',
'transliterate_failure' => 'Transliteration from :encoding to UTF-8 failed due to invalid characters in input',
diff --git a/tests/Feature/Livewire/ImporterTest.php b/tests/Feature/Livewire/ImporterTest.php
index 1068075a09..4ec21a46ac 100644
--- a/tests/Feature/Livewire/ImporterTest.php
+++ b/tests/Feature/Livewire/ImporterTest.php
@@ -355,6 +355,29 @@ class ImporterTest extends TestCase
->assertSet('activeFileRowCount', 0);
}
+ /**
+ * Regression for Rollbar: selectFile() foreach()-on-null when the Import
+ * row was persisted with header_row = null (legacy imports, or a background
+ * job that never wrote the column). Previously exploded with
+ * "foreach() argument must be of type array|object, null given" at the
+ * headerRow loop. The guard now short-circuits with a translated error.
+ */
+ public function test_selecting_a_file_with_null_header_row_shows_error_and_does_not_crash(): void
+ {
+ $user = User::factory()->canImport()->create();
+ $import = Import::factory()->create([
+ 'created_by' => $user->id,
+ 'header_row' => null,
+ ]);
+ $this->writeFakeImportFile($import, "asset tag\nAH-1\n");
+
+ Livewire::actingAs($user)
+ ->test(Importer::class)
+ ->call('selectFile', $import->id)
+ ->assertSet('message_type', 'danger')
+ ->assertSet('message', trans('admin/hardware/message.import.header_row_missing'));
+ }
+
public function test_next_step_from_type_selection_advances_when_type_is_set(): void
{
Storage::fake();