-
-
Notifications
You must be signed in to change notification settings - Fork 164
Respect MAX_FILE_SIZE POST field in MultipartParser #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e20fc99
to
4f6ec08
Compare
src/Io/MultipartParser.php
Outdated
break; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If i remember correctly, it's not that easy. According to http://php.net/manual/en/features.file-upload.post-method.php#example-383, the field must precede the file input field.
The reason is, that you can have multiple MAX_FILE_SIZE
hidden fields. Each field controls the max size of the following file fields.
<input name="file1" type="file">
<input name="file2" type="file">
<!-- "file1" and "file2" will have no MAX_FILE_SIZE (no limit) -->
<input name="MAX_FILE_SIZE" value="1024" type="hidden">
<input name="file3" type="file">
<input name="file4" type="file">
<!-- "file3" and "file4" will have MAX_FILE_SIZE = 1024 bytes -->
<input name="MAX_FILE_SIZE" value="0" type="hidden">
<input name="file5" type="file">
<input name="file6" type="file">
<!-- "file5" and "file6" will have MAX_FILE_SIZE = 0 (no limit) -->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the middleware to support this behavior :+
b0db02a
to
106c358
Compare
src/Io/MultipartParser.php
Outdated
if ($body == '0') { | ||
$this->maxFileSize = null; | ||
} | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the break
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy 🍝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the 🍝 to 🍕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yummy 🍽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice progress, would love to get this in!
Added two minor comments, otherwise LGTM! 👍 Can you add some minimal documentation for this?
src/Io/MultipartParser.php
Outdated
if ($this->maxFileSize !== null && $bodyLength > $this->maxFileSize) { | ||
return new UploadedFile( | ||
Psr7\stream_for(''), | ||
0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't find anything in PHP's documentation about this (http://php.net/manual/en/reserved.variables.files.php), but it's my understanding that we should still set the file size as given by the client here so consumers can show a more descriptive error message ("tried to upload X bytes, only Y allowed")?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM 👍
src/Io/MultipartParser.php
Outdated
|
||
if ($body == '0') { | ||
$this->maxFileSize = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably cast to int
above and use strict comparison here? This seems to be in line with how PHP parses this: https://github.com/php/php-src/blob/1c295d4a9ac78fcc2f77d6695987598bb7abcb83/main/rfc1867.c#L914
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 🎉
Implements / closes #258