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

model rule - 'file' extensions seem not 100% work #9888

Closed
ScottHuangZL opened this issue Oct 12, 2015 · 14 comments
Closed

model rule - 'file' extensions seem not 100% work #9888

ScottHuangZL opened this issue Oct 12, 2015 · 14 comments

Comments

@ScottHuangZL
Copy link

Dear Yii2 Team,

I try to upload file with below model. It is work tofilter xls/zip and so on, however, the extensions limitation failed for outlook .msg files.
It allow select .msg file, but failed to pass validate model, and then cannot upload.

Can you help to look into for this issue? Thanks.

public function rules()
    {
        return [
            [['excelFile'], 'file','skipOnEmpty' => false
                ,'extensions' => 'xls, xlsx, xlsm, msg, zip, 7z, rar'
                ,'maxSize'=>1024*1000 //1M
//                , 'mimeTypes' =>[ 'xls' => 'application/vnd.ms-excel'] //text/plain, image/png
            ],
        ];
    }
@samdark
Copy link
Member

samdark commented Oct 12, 2015

It's a known issue because for some files it's not possible to detect type based on contents. For these you can validate against plaintext and check file extension. Not reliable but would work at least.

@samdark samdark closed this as completed Oct 12, 2015
@ScottHuangZL
Copy link
Author

No, I not mention the mimeTypes, you see, I not turn on the 'mimeTypes'.
I just talk about the extensions itself.

For example:
'extensions' => 'xls, xlsx, xlsm, msg, zip, 7z, rar'

I can select xxx.msg to upload at 1st step, but $model->validate() step return false and warning me only can upload 'xls, xlsx, xlsm, msg, zip, 7z, rar' files.

Can you double check?

@samdark samdark reopened this Oct 12, 2015
@SilverFire
Copy link
Member

Can you post a sample of .msg file, please?

@ScottHuangZL
Copy link
Author

.msg file is outlook emails. Usually, I can copy one email from outlook and save to desktop as .msg file.
I cannot attached my company email sample to you due to regulation.

But I think you can rename any xxx.txt file to xxx.msg to testing.
Thanks.

@SilverFire
Copy link
Member

Okay, I got your problem. File extension msg is not described in yii2\helpers\mimeTypes.php, so validator treats your msg file as unacceptable.

I can offer you two solutions:

  • create your own mimeTypes.php and merge ['msg' => 'text/plain'] with contents of default one. Then configure FileHelper and set the mimeMagicFile pointing to your customized file.
  • in model rule of file validation set 'checkExtensionByMimeType' => false. Be careful about this, as @samdark mentioned, it is not reliable but would work at least.

@ScottHuangZL
Copy link
Author

Can you add .msg type into mimeTypes?
Outlook email is popular, thanks.

@ScottHuangZL
Copy link
Author

I select option

'checkExtensionByMimeType' => false

Can Yii2 team at least provide different error message in case I not add above code?
It should be not ext error, it is mimeType checking error.

Or, set that flag default as false.

Or, handle .msg type too, since as I mentioned, it is outlook email, popular. Thanks.

@samdark
Copy link
Member

samdark commented Oct 12, 2015

We're importing Apache mime types: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types. There's no plan to add more types except if the file is updated by Apache project.

@samdark samdark closed this as completed Oct 12, 2015
@ScottHuangZL
Copy link
Author

Ok, thanks.

@luke-
Copy link

luke- commented Nov 29, 2019

What do you think, if we only validate the mimetype of the extension, if we find a corresponding entry for it in the magicfile?

e.g. add 'FileHelper::getMimeTypeByExtension('test.' . $extension) !== null &&'
here https://github.com/yiisoft/yii2/blob/master/framework/validators/FileValidator.php#L402

@samdark
Copy link
Member

samdark commented Nov 30, 2019

@luke- is the problem you're trying to solve the same as the initial problem indicated in this issue?

@luke-
Copy link

luke- commented Nov 30, 2019

@samdark yes, we just had the same issue with ".msg" files.
I think it should be sufficient to check the mime type of the file only if it's known (listed in mime type file).

Iust wanted to hear your opinion about this, here is our "fix":
humhub/humhub@49ee6ff#diff-b6fba767073a31f8509978169968c22bR137

@samdark
Copy link
Member

samdark commented Nov 30, 2019

@luke- while this mode of checking is convenient, it potentially may cause security issues if either using checkExtensionByMimeType = false or falling back to this mode as you are suggesting so I'd not have it in the framework.

@luke-
Copy link

luke- commented Dec 2, 2019

@samdark In our case I want both, best possible security (if the mime type is known --> checkExtensionByMimeType), but also the support of (still) unknown file/mime types. But I understand that you don't want to have this behavior in the framework. Anyway, thanks for the clarification and also thanks for the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants