Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

alanpoulain
Copy link
Contributor

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR TODO

Add an extensions option to the File constraint to validate that the given file has the expected extension.

Even if the extension can be easily changed and that checking the MIME type is a better validation, it can be useful for instance to validate an APK file (the MIME type is often application/zip) or simply to prevent a user mistake.


if ($constraint->extensions) {
if ($value instanceof FileObject) {
$fileExtension = $value->getExtension();
Copy link
Contributor

Choose a reason for hiding this comment

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

$fileExtension = $value instanceof FileObject ? $value->getExtension() : (new FileObject($value))->getExtension();


$extensions = (array) $constraint->extensions;

foreach ($extensions as $extension) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if(in_array($fileExtension, $extensions)) {
return;

@fabpot
Copy link
Member

fabpot commented Oct 30, 2021

I've read this PR many times and I've always been a bit reluctant to accept it. I think I'm πŸ‘Ž as I don't want to promote such a practice. Let see what @symfony/mergers think.

@nicolas-grekas
Copy link
Member

I agree with @fabpot, I'm therefor closing. Thanks for proposing.

@ismail1432
Copy link
Contributor

Hey πŸ‘‹
I don't get what is wrong with the PR?
If you give us more details maybe the author can correct it because it's a good idea to have this option, It can prevent malicious users in a simple way.

@nicolas-grekas
Copy link
Member

This name field is pure user input. As such, it cannot prevent any malicious input. Precisely because this is a common mistake - thinking that it could save from a malicious user as you just did - adding this could create a false sense of added security.

@ismail1432
Copy link
Contributor

Thanks @nicolas-grekas πŸ‘

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

Successfully merging this pull request may close these issues.

6 participants