|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\FileUploads; |
| 4 | + |
| 5 | +use App\Models\Actionlog; |
| 6 | +use App\Models\Asset; |
| 7 | +use App\Models\License; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Http\UploadedFile; |
| 10 | +use Illuminate\Support\Facades\Storage; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +// Regression coverage for issues #12460 and #10387: legitimate uploads |
| 15 | +// that were rejected because Laravel's built-in `mimes:` rule (and the |
| 16 | +// hand-rolled MIME allowlist in the CSV importer) rely on finfo content |
| 17 | +// sniffing that misidentifies ordinary files. Fake UploadedFiles bypass |
| 18 | +// finfo entirely (their getMimeType() reads MimeType::from($name)), so |
| 19 | +// these tests use real temp files. |
| 20 | +class UploadFileValidationTest extends TestCase |
| 21 | +{ |
| 22 | + private array $tempFiles = []; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + Storage::fake(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function tearDown(): void |
| 31 | + { |
| 32 | + foreach ($this->tempFiles as $path) { |
| 33 | + @unlink($path); |
| 34 | + } |
| 35 | + |
| 36 | + parent::tearDown(); |
| 37 | + } |
| 38 | + |
| 39 | + private function realUpload(string $clientName, string $content): UploadedFile |
| 40 | + { |
| 41 | + $path = tempnam(sys_get_temp_dir(), 'snipeit_upload_'); |
| 42 | + file_put_contents($path, $content); |
| 43 | + $this->tempFiles[] = $path; |
| 44 | + |
| 45 | + return new UploadedFile($path, $clientName, null, null, true); |
| 46 | + } |
| 47 | + |
| 48 | + // Issue #12460 TechWilk reproduction: plain-text .txt whose bytes |
| 49 | + // trigger libmagic's INI heuristic (leading `;`, tab-separated |
| 50 | + // values). Before the fix, UploadFileRequest's `mimes:txt,...` rule |
| 51 | + // rejected this because finfo returned application/x-wine-extension-ini |
| 52 | + // and Symfony's guesser had no reverse mapping to `txt`. |
| 53 | + #[Test] |
| 54 | + public function accepts_txt_file_that_libmagic_misidentifies_as_ini(): void |
| 55 | + { |
| 56 | + $license = License::factory()->create(); |
| 57 | + |
| 58 | + $this->actingAsForApi(User::factory()->superuser()->create()) |
| 59 | + ->post( |
| 60 | + route('api.files.store', ['object_type' => 'licenses', 'id' => $license->id]), |
| 61 | + ['file' => [$this->realUpload('sample.txt', ";Bob[A]\tSmith[B]\r\n50\t0.8")]] |
| 62 | + ) |
| 63 | + ->assertOk(); |
| 64 | + |
| 65 | + $log = Actionlog::where('item_id', $license->id) |
| 66 | + ->where('item_type', License::class) |
| 67 | + ->where('action_type', 'uploaded') |
| 68 | + ->latest('id') |
| 69 | + ->firstOrFail(); |
| 70 | + |
| 71 | + // Stored filename must still carry a .txt extension. Before the |
| 72 | + // handleFile fallback, guessExtension() returned null on this |
| 73 | + // input and the stored name ended in a bare "." with no |
| 74 | + // extension, breaking the eventual download. |
| 75 | + $this->assertStringEndsWith('.txt', $log->filename); |
| 76 | + } |
| 77 | + |
| 78 | + // Issue #12460 primary: empty .txt file. finfo returns |
| 79 | + // application/x-empty for zero-byte files. |
| 80 | + #[Test] |
| 81 | + public function accepts_empty_txt_file(): void |
| 82 | + { |
| 83 | + $asset = Asset::factory()->create(); |
| 84 | + |
| 85 | + $this->actingAsForApi(User::factory()->superuser()->create()) |
| 86 | + ->post( |
| 87 | + route('api.files.store', ['object_type' => 'assets', 'id' => $asset->id]), |
| 88 | + ['file' => [$this->realUpload('empty.txt', '')]] |
| 89 | + ) |
| 90 | + ->assertOk(); |
| 91 | + } |
| 92 | + |
| 93 | + // The extension allowlist is still authoritative: an .exe rename |
| 94 | + // must not slip past just because we deferred to the client |
| 95 | + // extension. Sniff returns application/x-dosexec which reverse-maps |
| 96 | + // to `exe`, and `exe` is not on the extensions allowlist. |
| 97 | + #[Test] |
| 98 | + public function still_rejects_files_whose_extension_is_not_on_the_allowlist(): void |
| 99 | + { |
| 100 | + $asset = Asset::factory()->create(); |
| 101 | + |
| 102 | + $peHeader = "MZ\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xff\xff\x00\x00"; |
| 103 | + |
| 104 | + $this->actingAsForApi(User::factory()->superuser()->create()) |
| 105 | + ->post( |
| 106 | + route('api.files.store', ['object_type' => 'assets', 'id' => $asset->id]), |
| 107 | + ['file' => [$this->realUpload('installer.exe', $peHeader)]] |
| 108 | + ) |
| 109 | + ->assertSessionHasErrors('file.0'); |
| 110 | + } |
| 111 | + |
| 112 | + // Issue #10387: CSV importer used to reject anything whose sniffed |
| 113 | + // MIME wasn't on a small hand-rolled list. Windows/IIS commonly |
| 114 | + // sniffs .csv as application/octet-stream because the platform's |
| 115 | + // magic database is thinner than Linux's. Real CSV content of the |
| 116 | + // shape below also sniffs as octet-stream on the current test |
| 117 | + // environment, which is exactly the scenario the reporter hit. |
| 118 | + #[Test] |
| 119 | + public function csv_importer_accepts_csv_that_content_sniffs_as_octet_stream(): void |
| 120 | + { |
| 121 | + // Leading NULs guarantee finfo returns application/octet-stream, |
| 122 | + // reproducing the Windows-sniff behavior deterministically. |
| 123 | + $csv = "\x00\x01\x02header1,header2\nvalue1,value2\n"; |
| 124 | + |
| 125 | + $this->actingAsForApi(User::factory()->superuser()->create()) |
| 126 | + ->post( |
| 127 | + route('api.imports.store'), |
| 128 | + ['files' => [$this->realUpload('inventory.csv', $csv)]] |
| 129 | + ) |
| 130 | + ->assertOk(); |
| 131 | + } |
| 132 | + |
| 133 | + // Backstop: a genuine non-CSV file (a PNG here) whose extension is |
| 134 | + // also not csv/tsv/txt must still be rejected by the importer. |
| 135 | + #[Test] |
| 136 | + public function csv_importer_still_rejects_non_csv_extensions(): void |
| 137 | + { |
| 138 | + $png = base64_decode( |
| 139 | + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' |
| 140 | + ); |
| 141 | + |
| 142 | + $this->actingAsForApi(User::factory()->superuser()->create()) |
| 143 | + ->post( |
| 144 | + route('api.imports.store'), |
| 145 | + ['files' => [$this->realUpload('picture.png', $png)]] |
| 146 | + ) |
| 147 | + ->assertStatus(422); |
| 148 | + } |
| 149 | +} |
0 commit comments