Skip to content

[Validator] Fix #10648 : Incorrect validation for maxSize option in the FileValidator #10661

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static function formatMemory($memory)
}

if ($memory >= 1024) {
return sprintf('%d kB', $memory / 1024);
return sprintf('%d KB', $memory / 1024);
}

return sprintf('%d B', $memory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ public function testAnsiColorsAndEmojis()
$this->generateOutput(
" \033[44;37m Looks good to me... \033[0m\n".
" 4/15 ".str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
" 🏁 1 sec \033[41;37m 97 kB \033[0m"
" 🏁 1 sec \033[41;37m 97 KB \033[0m"
).
$this->generateOutput(
" \033[44;37m Thanks, bye \033[0m\n".
" 15/15 ".str_repeat($done, 28)." 100%\n".
" 🏁 1 sec \033[41;37m 195 kB \033[0m"
" 🏁 1 sec \033[41;37m 195 KB \033[0m"
),
stream_get_contents($output->getStream())
);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public static function getMaxFilesize()
public function getErrorMessage()
{
static $errors = array(
UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d kb).',
UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KB).',
UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function validate($value, Constraint $constraint)
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1024;
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1048576;
$maxSize = $constraint->maxSize * 1024 * 1024;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
Expand Down Expand Up @@ -117,11 +117,11 @@ public function validate($value, Constraint $constraint)
$limit = (int) $constraint->maxSize;
$suffix = 'bytes';
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000, 2);
$size = round(filesize($path) / 1024, 2);
$limit = (int) $constraint->maxSize;
$suffix = 'kB';
$suffix = 'KB';
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000000, 2);
$size = round(filesize($path) / (1024 * 1024), 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 1048576, same like it's used when error: UPLOAD_ERR_INI_SIZE occurs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, I prefer 1024 * 1024 as everyone understands where it comes from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then other parts should be changed too I guess =)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stloyd The first code (before the commit) was using the computed value. I already asked the change when reviewing the PR live.

And anyway, OPCache will probably be able to optimize this computation according to what @jpauli presented yesterday (unless it works only for additions, which would be weird)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and yeah, changing it on line 50 would be logical

$limit = (int) $constraint->maxSize;
$suffix = 'MB';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public function testTooLargeKiloBytes()
->method('addViolation')
->with('myMessage', array(
'{{ limit }}' => '1',
'{{ size }}' => '1.4',
'{{ suffix }}' => 'kB',
'{{ size }}' => '1.37',
'{{ suffix }}' => 'KB',
'{{ file }}' => $this->path,
));

Expand All @@ -137,14 +137,38 @@ public function testTooLargeMegaBytes()
->method('addViolation')
->with('myMessage', array(
'{{ limit }}' => '1',
'{{ size }}' => '1.4',
'{{ size }}' => '1.34',
'{{ suffix }}' => 'MB',
'{{ file }}' => $this->path,
));

$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testMaxSizeKiloBytes()
{
fwrite($this->file, str_repeat('0', 1010));

$constraint = new File(array(
'maxSize' => '1k',
));

$this->context->expects($this->never())->method('addViolation');
$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testMaxSizeMegaBytes()
{
fwrite($this->file, str_repeat('0', (1024 * 1022)));

$constraint = new File(array(
'maxSize' => '1M',
));

$this->context->expects($this->never())->method('addViolation');
$this->validator->validate($this->getFile($this->path), $constraint);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
Expand Down