8000 [Validator] fix showing wrong max file size for upload errors · jfsimon/symfony@7216cb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7216cb0

Browse files
committed
[Validator] fix showing wrong max file size for upload errors
this was because the maxSize option wasn't parsed correctly and simply string comparision could lead to wrong results, e.g. 200 > 1000M
1 parent ee495f8 commit 7216cb0

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/Symfony/Component/Validator/Constraints/FileValidator.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,21 @@ public function validate($value, Constraint $constraint)
3737
if ($value instanceof UploadedFile && !$value->isValid()) {
3838
switch ($value->getError()) {
3939
case UPLOAD_ERR_INI_SIZE:
40-
$maxSize = UploadedFile::getMaxFilesize();
41-
$maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
40+
if ($constraint->maxSize) {
41+
if (ctype_digit((string) $constraint->maxSize)) {
42+
$maxSize = (int) $constraint->maxSize;
43+
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
44+
$maxSize = $constraint->maxSize * 1024;
45+
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
46+
$maxSize = $constraint->maxSize * 1048576;
47+
} else {
48+
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
49+
}
50+
$maxSize = min(UploadedFile::getMaxFilesize(), $maxSize);
51+
} else {
52+
$maxSize = UploadedFile::getMaxFilesize();
53+
}
54+
4255
$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
4356
'{{ limit }}' => $maxSize,
4457
'{{ suffix }}' => 'bytes',
@@ -97,15 +110,15 @@ public function validate($value, Constraint $constraint)
97110
if ($constraint->maxSize) {
98111
if (ctype_digit((string) $constraint->maxSize)) {
99112
$size = filesize($path);
100-
$limit = $constraint->maxSize;
113+
$limit = (int) $constraint->maxSize;
101114
$suffix = 'bytes';
102-
} elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
115+
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
103116
$size = round(filesize($path) / 1000, 2);
104-
$limit = $matches[1];
117+
$limit = (int) $constraint->maxSize;
105118
$suffix = 'kB';
106-
} elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
119+
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
107120
$size = round(filesize($path) / 1000000, 2);
108-
$limit = $matches[1];
121+
$limit = (int) $constraint->maxSize;
109122
$suffix = 'MB';
110123
} else {
111124
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));

src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,13 @@ public function testInvalidWildcardMimeType()
288288
/**
289289
* @dataProvider uploadedFileErrorProvider
290290
*/
291-
public function testUploadedFileError($error, $message, array $params = array())
291+
public function testUploadedFileError($error, $message, array $params = array(), $maxSize = null)
292292
{
293293
$file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
294294

295295
$constraint = new File(array(
296296
$message => 'myMessage',
297+
'maxSize' => $maxSize
297298
));
298299

299300
$this->context->expects($this->once())
@@ -316,10 +317,24 @@ public function uploadedFileErrorProvider()
316317
);
317318

318319
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
320+
// when no maxSize is specified on constraint, it should use the ini value
319321
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
320322
'{{ limit }}' => UploadedFile::getMaxFilesize(),
321323
'{{ suffix }}' => 'bytes',
322324
));
325+
326+
// it should use the smaller limitation (maxSize option in this case)
327+
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
328+
'{{ limit }}' => 1,
329+
'{{ suffix }}' => 'bytes',
330+
), '1');
331+
332+
// it correctly parses the maxSize option and not only uses simple string comparison
333+
// 1000M should be bigger than the ini value
334+
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
335+
'{{ limit }}' => UploadedFile::getMaxFilesize(),
336+
'{{ suffix }}' => 'bytes',
337+
), '1000M');
323338
}
324339

325340
return $tests;

0 commit comments

Comments
 (0)
0