Skip to content

A free open source IT asset/license management system

Fixed RB-15563 - better check for empty file in importer

snipe5d0569fe8549
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php
index 4955553443..06d447fbb9 100644
--- a/app/Http/Controllers/Api/ImportController.php
+++ b/app/Http/Controllers/Api/ImportController.php
@@ -57,7 +57,17 @@ class ImportController extends Controller
$import = new Import;
$detector = new EncodingDetector;
+ // No file uploaded
+ if (empty($files)) {
+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.nofiles')), 422);
+ }
+
foreach ($files as $file) {
+ // Reject phantoms and fail early if the file is invalid (e.g. exceeds the server upload limit).
+ // The CSV reader below will reject anything that isn't actually parseable with a more precise error.
+ if (! $file instanceof UploadedFile || ! $file->isValid()) {
+ return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.nofiles')), 422);
+ }
$allowedMimes = [
'application/vnd.ms-excel',
'text/csv',
diff --git a/tests/Feature/FileUploads/UploadFileValidationTest.php b/tests/Feature/FileUploads/UploadFileValidationTest.php
index 4b0406878f..12495d8dc8 100644
--- a/tests/Feature/FileUploads/UploadFileValidationTest.php
+++ b/tests/Feature/FileUploads/UploadFileValidationTest.php
@@ -130,6 +130,29 @@ class UploadFileValidationTest extends TestCase
->assertOk();
}
+ // Regression: POST /api/v1/imports with no `files` field returned
+ // a 500 from Symfony's MIME guesser because the controller iterated
+ // a null $files and then called getMimeType() on a phantom File
+ // with an empty path. The guarded shape should return 422 with a
+ // hint about the missing form field instead.
+ #[Test]
+ public function csv_importer_returns_422_when_files_field_is_missing(): void
+ {
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.imports.store'), [])
+ ->assertStatus(422)
+ ->assertJsonPath('status', 'error');
+ }
+
+ #[Test]
+ public function csv_importer_returns_422_when_files_field_is_empty_array(): void
+ {
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.imports.store'), ['files' => []])
+ ->assertStatus(422)
+ ->assertJsonPath('status', 'error');
+ }
+
// Backstop: a genuine non-CSV file (a PNG here) whose extension is
// also not csv/tsv/txt must still be rejected by the importer.
#[Test]