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

[mediabundle] Do not allow any and all filetypes to be uploaded to the cms. #2680

Closed
wants to merge 16 commits into from
Closed

[mediabundle] Do not allow any and all filetypes to be uploaded to the cms. #2680

wants to merge 16 commits into from

Conversation

Numkil
Copy link
Contributor

@Numkil Numkil commented May 22, 2020

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Fixed tickets

Security testing advised us to remove the ability to upload any and all sorts of media to the cms that could then be used for nefarious purposed through the media/uploads folder.

Copy link

@ProfessorKuma ProfessorKuma left a comment

Choose a reason for hiding this comment

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

Hi @, your PR needs some changes

  • This PR seems to need a milestone of a minor release.

@ProfessorKuma ProfessorKuma added this to the 5.6.0 milestone May 22, 2020
Copy link

@ProfessorKuma ProfessorKuma left a comment

Choose a reason for hiding this comment

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

Hi @, your PR passed all our requirements.

Thank you for contributing!

src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
src/Kunstmaan/MediaBundle/Model/ImageUploadModel.php Outdated Show resolved Hide resolved
@Numkil
Copy link
Contributor Author

Numkil commented Jun 1, 2020

same issue with symfonyinsight complaining about movemedia not being called movemediaaction

@Numkil Numkil requested a review from acrobat June 1, 2020 10:06
@Numkil Numkil requested a review from acrobat June 4, 2020 16:59
@acrobat acrobat modified the milestones: 5.6.0, 5.7.0 Jun 25, 2020
@Numkil
Copy link
Contributor Author

Numkil commented Jul 5, 2020

@acrobat indentation restored and symfony way used for validator error id


$errors = $this->validator->validate($file, new Image(['detectCorrupted' => true]));

return 0 === $errors->count();
Copy link
Member

Choose a reason for hiding this comment

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

I don't like the fact that when I upload a corrected file, the ui shows "The extension of this file is not allowed on this server." as the error. In my case a .gif file is actually allowed, but the file is corrupted.

Maybe we need to discus this case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@acrobat zou het voor jouw ok zijn als we de translation gewoon wat meer inclusive maken? Bv

"Your file has been rejected by the server because either the extension is not allowed or the file is corrupt"

?

Copy link
Member

Choose a reason for hiding this comment

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

Imo we shouldn't group exception messages as it's not clear to the user what the exact problem is and how they can resolve it.

$mimeType = $this->mimeTypes->guessMimeType($file->getRealPath());
$extensions = $this->mimeTypes->getExtensions($mimeType);
$extension = array_shift($extensions);
if (!in_array($extension, $this->allowedExtensions, true)) {
Copy link
Member

Choose a reason for hiding this comment

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

The code itself looks ok, but it yields unexpected results. Forexample if I only allow .txt files to be uploaded and I upload a .json file, this checks allows it.
This is because $this->mimeTypes->guessMimeType() for a json files returns text/plain, which results in "txt", "text","conf","def","list","log","in","asc" as extensions from $this->mimeTypes->getExtensions and so is .txt picked. And the upload is allowed. Although there is technically no bug in this code, the result is unexpected.

So we should think about a solution so that allowed extensions list is actually the only extensions allowed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ik heb een soort van fallback gemaakt waarbij we kijken dat indien de locale fileinfo text/plain teruggeeft en het een uploadedfile is we fallbacken naar de clientmimetype. Dat is natuurlijk niet 100% foolproof want is spoofable maar at least kunnen we toch iets meer onderscheid maken tussen al die files die anders als "text/plain" gewoon zomaar toegelaten worden.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this update as symfony indicates (and you) this value can't be trusted. https://github.com/symfony/symfony/blob/0baa58d4e4bb006c4ae68f75833b586bd3cb6e6f/src/Symfony/Component/HttpFoundation/File/UploadedFile.php#L128-L129

Would it be a solution to move away from the idea of listing extensions and have the user provide an allowed list of mimetypes? Like you would do with the File constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thing is that its just a server thing. because of the way the upload works the uploaded file will be put in the temp folder as "9d0329d30" or something like that without an extension. Because files like json,xml etc are not executable nor have a header in the file to specify what file type they are this effectively makes them just a plaintext file. The good thing is that because of that they are not a threat to the server as they wouldn't be able to be executed. Therefore i think its more a matter of cleanliness to do this optional extra check for files we have already verified are not a threat to the system to deny them from being uploaded if we have not allowed that extension.

Using mimetypes would give us the exact same "problem" because someone might be confused as to why a file with mimetype "text/json" is allowed on the server if he hasn't put it in his allowed configuration.

But yeah tldr: files that get detected by fileinfo on the server as plaintext are no threat and therefore its just a matter of cleanliness to choose to allow/block them from being uploaded which we can do using the clientinfo.

$mimeType = $this->mimeTypes->guessMimeType($file->getRealPath());
$extensions = $this->mimeTypes->getExtensions($mimeType);
$extension = array_shift($extensions);
if (!in_array($extension, $this->allowedExtensions, true)) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this update as symfony indicates (and you) this value can't be trusted. https://github.com/symfony/symfony/blob/0baa58d4e4bb006c4ae68f75833b586bd3cb6e6f/src/Symfony/Component/HttpFoundation/File/UploadedFile.php#L128-L129

Would it be a solution to move away from the idea of listing extensions and have the user provide an allowed list of mimetypes? Like you would do with the File constraint?


$errors = $this->validator->validate($file, new Image(['detectCorrupted' => true]));

return 0 === $errors->count();
Copy link
Member

Choose a reason for hiding this comment

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

Imo we shouldn't group exception messages as it's not clear to the user what the exact problem is and how they can resolve it.

src/Kunstmaan/MediaBundle/Helper/MediaManager.php Outdated Show resolved Hide resolved
@acrobat
Copy link
Member

acrobat commented Sep 22, 2020

@Numkil I was doing some fixes in the mediabundle and came across this config. Can't this existing feature be used implement the required behaviour?

->arrayNode('blacklisted_extensions')
->defaultValue(array('php', 'htaccess'))
->prototype('scalar')->end()
->end()

@acrobat acrobat modified the milestones: 5.7.0, Unknown (WIP) Sep 22, 2020
@Numkil Numkil closed this May 21, 2021
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.

3 participants