[4.4.2+] TemporaryUploadedFile::getMimeType() returns application/octet-stream for MP3s with ID3 tags larger than 64 KiB
Livewire version
v4.4.5
Laravel version
v13.32
PHP version
8.4
Browser and operating system
Firefox on windows wsl2/ Firefox on ubuntu server
Describe the issue you're experiencing
Since upgrading to Livewire 4.4.2, our users can no longer upload many commercially released MP3s: validation fails with "The file field must be a file of type: mp3.", even though the files are perfectly valid and passed the same mimes:mp3 rule up to 4.4.1.
The cause is the content-based MIME detection introduced in #10581 (ported from #10440). TemporaryUploadedFile::getMimeType() now samples only the first 64 KiB of the upload. MP3 files store their ID3v2 tag at the very beginning, and when the tag holds embedded cover art it easily grows past 64 KiB. The sample then contains nothing but tag data, libmagic cannot find an MPEG frame, and the file is reported as application/octet-stream. The same misdetection makes temporaryUrl() throw FileNotPreviewableException, because isPreviewable() relies on guessExtension().
I understand the security reasoning behind the change and I am not asking to revert it, only to make detection reliable for this very common case.
Possible fixes
- When the temporary disk is local, detect from the file itself (
finfo::file()/detectMimeTypeFromFile()ongetRealPath()) instead of a sampled buffer: the bounded read only matters for remote disks. - For remote disks, if the sample starts with an ID3v2 header, read the tag size from bytes 6-9 (synchsafe integer) and sample past the end of the tag.
- Alternatively, when the sample yields an inconclusive type (
application/octet-stream), retry with a larger read before giving up.
I don't know if those are viable solutions...
Related: #10605 (another regression from the same change, .msg files).
Code snippets to reproduce the issue
- Take an MP3 with embedded cover art larger than ~64 KB (very common on commercially released tracks: our real-world file has an 89 KB
APICframe, ID3v2.3 tag of 90,906 bytes). - Upload it through a
WithFileUploadscomponent (local temporary disk). - Validate it with
mimes:mp3(ormimetypes:audio/mpeg).
public $file;
public function save()
{
$this->validate(['file' => ['required', 'file', 'mimes:mp3']]);
}Minimal reproduction (no Livewire needed)
Generates a synthetic MP3 with a 70 KB cover and compares whole-file detection with the 64 KiB sample:
<?php
function synchsafe(int $n): string
{
return chr(($n >> 21) & 0x7F).chr(($n >> 14) & 0x7F).chr(($n >> 7) & 0x7F).chr($n & 0x7F);
}
// ID3v2.3 tag with a single APIC frame (70 KB of picture data).
$picture = "image/jpeg\x00\x03\x00".str_repeat("\x00", 70 * 1024);
$apic = 'APIC'.pack('N', strlen("\x00".$picture))."\x00\x00"."\x00".$picture;
$tag = 'ID3'."\x03\x00\x00".synchsafe(strlen($apic)).$apic;
// 200 MPEG-1 Layer III frames (128 kbps, 44.1 kHz).
$frame = "\xFF\xFB\x90\x64".str_repeat("\x00", 413);
$mp3 = $tag.str_repeat($frame, 200);
file_put_contents('/tmp/cover.mp3', $mp3);
$finfo = new finfo(FILEINFO_MIME_TYPE);
echo 'file(): ', $finfo->file('/tmp/cover.mp3'), PHP_EOL; // audio/mpeg
echo 'buffer(64 KiB): ', $finfo->buffer(substr($mp3, 0, 64 * 1024)), PHP_EOL; // application/octet-streamScreenshots/screen recordings
No response
How do you expect it to work?
The file passes validation, as it did up to v4.4.1: it is a valid MP3 and finfo::file() on the temporary file returns audio/mpeg.
Please confirm (incomplete submissions will not be addressed)
- I have provided easy and step-by-step instructions to reproduce the bug.
- I have provided code samples as text and NOT images.
- I understand my bug report will be removed if I haven't met the criteria above.
Source: livewire/livewire