Skip to content

Commit

Permalink
backport (#20441)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurencei authored and taylorotwell committed Aug 6, 2017
1 parent 95f72e4 commit cec6731
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,10 @@ protected function validateMimes($attribute, $value, $parameters)
return false;
}

if ($this->shouldBlockPhpUpload($value, $parameters)) {
return false;
}

return $value->getPath() != '' && in_array($value->guessExtension(), $parameters);
}

Expand All @@ -1724,9 +1728,31 @@ protected function validateMimetypes($attribute, $value, $parameters)
return false;
}

if ($this->shouldBlockPhpUpload($value, $parameters)) {
return false;
}

return $value->getPath() != '' && in_array($value->getMimeType(), $parameters);
}

/*
* Check if PHP uploads are explicitly allowed.
*
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function shouldBlockPhpUpload($value, $parameters)
{
if (in_array('php', $parameters)) {
return false;
}

return ($value instanceof UploadedFile)
? strtolower($value->getClientOriginalExtension()) === 'php'
: strtolower($value->getExtension()) === 'php';
}

/**
* Check that the given value is a valid file instance.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,24 @@ public function testValidateMime()
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:php']);
$this->assertTrue($v->passes());

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
$this->assertFalse($v->passes());

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('pdf'));
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
$this->assertTrue($v->passes());

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf,php']);
$this->assertTrue($v->passes());

$file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'isValid'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->will($this->returnValue('php'));
$file2->expects($this->any())->method('isValid')->will($this->returnValue(false));
Expand Down

0 comments on commit cec6731

Please sign in to comment.