livewire icon indicating copy to clipboard operation
livewire copied to clipboard

Failing Test: Incorrect mime types guessing

Open Jubeki opened this issue 1 year ago • 2 comments

Review the contribution guide first at: https://livewire.laravel.com/docs/contribution-guide

1️⃣ Is this something that is wanted/needed? Did you create a discussion about it first? At least I need it for testing files with an allowed file extension, but an not allowed mime type of the file: https://github.com/filamentphp/filament/discussions/14102

I am not sure, if my way of testing this, should be done in this, way and I am also willing to simply update my test case, if there is a better method.

2️⃣ Did you create a branch for your fix/feature? (Main branch PR's will be closed) Yes.

3️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out. No.

4️⃣ Does it include tests? (Required) It includes a failing test.

5️⃣ Please include a thorough description (including small code snippets if possible) of the improvement and reasons why it's useful.

I want to test in a form, that a user can not upload a file with a correct file extension, but in incorrect mime type. The issue is, that in the TemporaryUploadedFile Class of Livewire, the mime type is guessed incorrectly (based on the file extensions, instead of the provided mime type with UploadedFile).

I would expect that TemporaryUploadedFile uses the mime type which is provided during upload (In this case with UploadedFile), as an additional fallback, before going to guess the mime type based on the extension.

Here is the relevant code snippet in Livewire, were this probably must be adjusted.

https://github.com/livewire/livewire/blob/597a2808d8d3001cc3ed5ce89a6ebab00f83b80f/src/Features/SupportFileUploads/TemporaryUploadedFile.php#L55-L68

Thanks for contributing! 🙌

Jubeki avatar Sep 01 '24 10:09 Jubeki

https://github.com/thephpleague/mime-type-detection

I think the check is actually correct. It first checks the file, and if it's getting steamed, it will try based on extension.

francoism90 avatar Sep 02 '24 20:09 francoism90

What I mean is the following:

If you create a uploaded file for testing like the following

$file = UploadedFile::fake()->create('file1.jpg', 1024, 'application/pdf');

And then call directly getMimeType():

dd($file->getMimeType());

You will get application/pdf, were as if you use the same in an Livewire Component, it will create a copy and use the class TemporaryUploadedFile, which will not return the mime application/pdf but image/jpg.

That is the Problem I am having.

Jubeki avatar Sep 03 '24 06:09 Jubeki

I've worked out what is going on with this issue, what happens is Laravel's fake file no longer exists after Livewire uploads it, as it gets stored into Livewire's livewire-tmp directory as an actual file. The problem is the file is actually an empty file and the filesystem cannot use it to detect the mimeType so it falls back to using the file extension as the mimeType.

Thankfully we already had provision to send fake file data through the TemporaryUploadedFile path, so I have added the fake file mimeType to this. I then updated the TemporaryUploadedFile to use the fake mimeType data if running in unit tests (the same as how size and hashName work).

joshhanley avatar Dec 02 '24 07:12 joshhanley

Thanks @joshhanley !

Jubeki avatar Dec 02 '24 07:12 Jubeki

Thanks @joshhanley !

calebporzio avatar Dec 02 '24 15:12 calebporzio