Skip to content
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

[6.x] Fix mime validation for jpeg files #35518

Merged
merged 2 commits into from Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -1206,6 +1206,11 @@ public function validateMimes($attribute, $value, $parameters)
return false;
}

// Make sure both JPG & JPEG validate true for each other.
if (in_array('jpg', $parameters) || in_array('jpeg', $parameters)) {
$parameters = array_unique(array_merge($parameters, ['jpg', 'jpeg']));
}

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

Expand Down
65 changes: 41 additions & 24 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -2665,49 +2665,49 @@ public function testValidateImage()
$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('php');
$file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
$v = new Validator($trans, ['x' => $file], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file], ['x' => 'image']);
$this->assertFalse($v->passes());

$file2 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->willReturn('jpeg');
$file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed this because the recent Symfony change will always return jpg here for jpeg files.

$file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
$v = new Validator($trans, ['x' => $file2], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
$this->assertTrue($v->passes());

$file2 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
$file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
$v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
$this->assertTrue($v->passes());

$file3 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file3->expects($this->any())->method('guessExtension')->willReturn('gif');
$file3->expects($this->any())->method('getClientOriginalExtension')->willReturn('gif');
$v = new Validator($trans, ['x' => $file3], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file3], ['x' => 'image']);
$this->assertTrue($v->passes());

$file4 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file4->expects($this->any())->method('guessExtension')->willReturn('bmp');
$file4->expects($this->any())->method('getClientOriginalExtension')->willReturn('bmp');
$v = new Validator($trans, ['x' => $file4], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file4], ['x' => 'image']);
$this->assertTrue($v->passes());

$file5 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file5->expects($this->any())->method('guessExtension')->willReturn('png');
$file5->expects($this->any())->method('getClientOriginalExtension')->willReturn('png');
$v = new Validator($trans, ['x' => $file5], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file5], ['x' => 'image']);
$this->assertTrue($v->passes());

$file6 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file6->expects($this->any())->method('guessExtension')->willReturn('svg');
$file6->expects($this->any())->method('getClientOriginalExtension')->willReturn('svg');
$v = new Validator($trans, ['x' => $file6], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file6], ['x' => 'image']);
$this->assertTrue($v->passes());

$file7 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file7->expects($this->any())->method('guessExtension')->willReturn('webp');
$file7->expects($this->any())->method('getClientOriginalExtension')->willReturn('webp');
$v = new Validator($trans, ['x' => $file7], ['x' => 'Image']);
$this->assertTrue($v->passes());

$file2 = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
$file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
$v = new Validator($trans, ['x' => $file2], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file7], ['x' => 'image']);
$this->assertTrue($v->passes());
}

Expand All @@ -2719,7 +2719,7 @@ public function testValidateImageDoesNotAllowPhpExtensionsOnImageMime()
$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('jpeg');
$file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
$v = new Validator($trans, ['x' => $file], ['x' => 'Image']);
$v = new Validator($trans, ['x' => $file], ['x' => 'image']);
$this->assertFalse($v->passes());
}

Expand Down Expand Up @@ -2832,20 +2832,25 @@ public function testValidateImageDimensions()
$this->assertFalse($v->passes());
}

/**
* @requires extension fileinfo
*/
public function testValidatePhpMimetypes()
public function testValidateMimetypes()
{
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__DIR__.'/ValidationRuleTest.php', '', null, null, null, true];

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('rtf');
$file->expects($this->any())->method('getClientOriginalExtension')->willReturn('rtf');
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('getMimeType')->willReturn('text/rtf');
$v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/*']);
$this->assertTrue($v->passes());

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('getMimeType')->willReturn('application/pdf');
$v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/rtf']);
$this->assertFalse($v->passes());

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('getMimeType')->willReturn('image/jpeg');
$v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:image/jpeg']);
$this->assertTrue($v->passes());
}

public function testValidateMime()
Expand All @@ -2864,6 +2869,18 @@ public function testValidateMime()
$file2->expects($this->any())->method('isValid')->willReturn(false);
$v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf']);
$this->assertFalse($v->passes());

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('jpg');
$file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpeg']);
$this->assertTrue($v->passes());

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('jpg');
$file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpg']);
$this->assertTrue($v->passes());
}

public function testValidateMimeEnforcesPhpCheck()
Expand Down