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

Support for composite file extension validation #18137

Merged
merged 19 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@ Yii Framework 2 Change Log
- Bug #17985: Convert migrationNamespaces to array if needed (darkdef)
- Bug #18134: Expression as columnName should not be quoted in likeCondition (darkdef)
- Bug #18147: Fixed parameters binding for MySQL when prepare emulation is off (rskrzypczak)
- Bug #18094: Support for composite file extension validation (darkdef)


2.0.35 May 02, 2020
Expand Down
14 changes: 11 additions & 3 deletions framework/assets/yii.validation.js
Expand Up @@ -408,10 +408,18 @@ yii.validation = (function ($) {

function validateFile(file, messages, options) {
if (options.extensions && options.extensions.length > 0) {
var index = file.name.lastIndexOf('.');
var ext = !~index ? '' : file.name.substr(index + 1, file.name.length).toLowerCase();
var found = false;
var filename = file.name.toLowerCase();

if (!~options.extensions.indexOf(ext)) {
for (var index=0; index < options.extensions.length; index++) {
var ext = options.extensions[index].toLowerCase();
samdark marked this conversation as resolved.
Show resolved Hide resolved
if (filename.substr(filename.length - options.extensions[index].length - 1) === ('.' + ext)) {
found = true;
break;
}
}

if (!found) {
messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
}
}
Expand Down
8 changes: 7 additions & 1 deletion framework/validators/FileValidator.php
Expand Up @@ -11,6 +11,7 @@
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\web\JsExpression;
use yii\web\UploadedFile;

Expand Down Expand Up @@ -412,7 +413,12 @@ protected function validateExtension($file)
}
}

if (!in_array($extension, $this->extensions, true)) {
if (!empty($this->extensions)) {
foreach ((array) $this->extensions as $ext) {
if (StringHelper::endsWith($file->name, ".$ext", false)) {
return true;
}
}
return false;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/framework/validators/FileValidatorTest.php
Expand Up @@ -452,6 +452,32 @@ public function testValidateAttributeType()
$this->assertNotFalse(stripos(current($m->getErrors('attr_exe')), 'Only files with these extensions '));
}

public function testValidateAttributeDoubleType()
{
$val = new FileValidator([
'extensions' => 'tar.gz, tar.xz',
'checkExtensionByMimeType' => false,
samdark marked this conversation as resolved.
Show resolved Hide resolved
]);

$m = FakedValidationModel::createWithAttributes(
[
'attr_tar' => $this->createTestFiles([['name' => 'one.tar.gz']]),
'attr_bar' => $this->createTestFiles([['name' => 'bad.bar.xz']]),
'attr_badtar' => $this->createTestFiles([['name' => 'badtar.xz']]),
]
);
$val->validateAttribute($m, 'attr_tar');
$this->assertFalse($m->hasErrors('attr_tar'));

$val->validateAttribute($m, 'attr_bar');
$this->assertTrue($m->hasErrors('attr_bar'));
$this->assertNotFalse(stripos(current($m->getErrors('attr_bar')), 'Only files with these extensions '));

$val->validateAttribute($m, 'attr_badtar');
$this->assertTrue($m->hasErrors('attr_badtar'));
$this->assertNotFalse(stripos(current($m->getErrors('attr_badtar')), 'Only files with these extensions '));
}

public function testIssue11012()
{
$baseName = '飛兒樂團光茫';
Expand Down Expand Up @@ -496,6 +522,7 @@ public function validMimeTypes()
['test.txt', 'text/*', 'txt'],
['test.xml', '*/xml', 'xml'],
['test.odt', 'application/vnd*', 'odt'],
['test.tar.xz', 'application/x-xz', 'tar.xz'],
samdark marked this conversation as resolved.
Show resolved Hide resolved
]);
}

Expand Down
Binary file added tests/framework/validators/data/mimeType/test.tar.xz
Binary file not shown.