Repository navigation
Conversation
mb_ltrim re-encodes its whole subject as UTF-8, not only the prefix it strips, so every byte of a multipart body that is not valid UTF-8 was replaced with "?" before the part was written to disk. Binary uploads reached the application corrupted.
|
Confirmed independently on v5.1.2 (PHP 8.4.25, macOS): in a real Laravel 13 app, PNG and ZIP uploads come through |
|
Heads-up: 🤖 Generated with Claude Code |
What's wrong
attach()delivers the file and the multipart body is parsed, but any binary upload reaches the application corrupted.LaravelHttpServer::parseMultipartBody()trims each part withmb_ltrim()(src/Drivers/LaravelHttpServer.php#L491).mb_ltrim()re-encodes its whole subject as UTF-8, not just the prefix it strips, so every byte of the part that is not valid UTF-8 — most of any image, PDF or archive — is replaced with?(0x3f) beforefile_put_contents()writes it out.Reproducing it
Standalone, no browser required:
In an application it surfaces a long way from the cause. A 64x64 WebP uploaded through
attach()arrived with two bytes rewritten, which still left a valid RIFF/WebP header — so Laravel'sextensionsandmimesrules both passed, and the failure only appeared once the image library tried to decode the file:Why the current test doesn't catch it
tests/Browser/Webpage/UploadTest.phpuploads'test content'— 12 bytes of ASCII, which is valid UTF-8 and so passes throughmb_ltrim()untouched.Worth noting the corruption does not reliably change the file's length: for the bytes in the test added here, 13 go in and 13 come out. A size assertion would not catch it, so the new test hashes the content.
The fix
ltrim(). It is byte-based, and the only thing being stripped is an ASCII\r\n, so there is nothing here for a multibyte function to do.Verification
mb_ltrim/ltrimdifference in isolation, as above.5.xand passes with this change.composer installresolved aparatestincompatible with pest'sWrapperRunner(WrapperWorker::__construct()arity), and the non-parallel run hangs. CI will exercise the new test.Related