Skip to content

Commit

Permalink
[5.4] Use getClientOriginalExtension() when available (#20400)
Browse files Browse the repository at this point in the history
* improve protection

* style tweak
  • Loading branch information
laurencei authored and taylorotwell committed Aug 2, 2017
1 parent bbf5b67 commit 5c1832f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ protected function shouldBlockPhpUpload($value, $parameters)
return false;
}

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

/**
Expand Down
44 changes: 22 additions & 22 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1950,39 +1950,39 @@ public function testValidateImage()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

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

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

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

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

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

$file6 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file6 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file6->expects($this->any())->method('guessExtension')->will($this->returnValue('svg'));
$file6->expects($this->any())->method('getExtension')->will($this->returnValue('svg'));
$file6->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('svg'));
$v = new Validator($trans, ['x' => $file6], ['x' => 'Image']);
$this->assertTrue($v->passes());
}
Expand All @@ -1992,9 +1992,9 @@ public function testValidateImageDoesNotAllowPhpExtensionsOnImageMime()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('jpeg'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('php'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'Image']);
$this->assertFalse($v->passes());
}
Expand Down Expand Up @@ -2085,9 +2085,9 @@ public function testValidatePhpMimetypes()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('rtf'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('rtf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('rtf'));

$v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/*']);
$this->assertTrue($v->passes());
Expand All @@ -2098,9 +2098,9 @@ public function testValidateMime()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$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('getExtension')->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());

Expand All @@ -2116,15 +2116,15 @@ public function testValidateMimeEnforcesPhpCheck()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

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

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

0 comments on commit 5c1832f

Please sign in to comment.