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

Basic image validation for logo/favicon #112

Closed
havardox opened this issue Feb 1, 2021 · 10 comments
Closed

Basic image validation for logo/favicon #112

havardox opened this issue Feb 1, 2021 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@havardox
Copy link

havardox commented Feb 1, 2021

Currently, a person can upload any file as a favicon or logo under theme settings. This could be a security issue if a malicious user gains access to the theme settings.

Suggestion: there should be some basic header validation to prevent users from uploading non-image files. Also should limit the size of the upload (i.e. under 3 MB).

@havardox havardox added the enhancement New feature or request label Feb 1, 2021
@merwok
Copy link
Contributor

merwok commented Feb 1, 2021

Would migrating the favicon field from FileField to ImageField do the job here?

@havardox
Copy link
Author

havardox commented Feb 1, 2021

I think that would work. Although the file size would need to be manually checked.

@merwok
Copy link
Contributor

merwok commented Feb 1, 2021

I’m not sure about that after all. Maybe using proper size for images is a documentation / teaching issue between developers and users of an admin panel, not a technical constraint.

This could be a security issue if a malicious user gains access to the theme settings.

If a malicious user is in your admin, you have bigger problems!

@havardox
Copy link
Author

havardox commented Feb 2, 2021

Still, it would be nice if there were some basic sanity checks. E.g. if it's a internal tool and some idiot decides to upload a 2GB+ file. Stuff like that.

@fabiocaccamo
Copy link
Owner

@havardox

  • the favicon field is a FileField to support also .ico files
  • to avoid the upload of 2GB+ file it is possible to use the FILE_UPLOAD_MAX_MEMORY_SIZE django setting
  • as @merwok said, if a malicious user is in your admin, probably you will have bigger problems

I think that the best thing that could be done here is to add 2 validators to the fields, one for check file size limit and another one for check file extension.

@merwok
Copy link
Contributor

merwok commented Feb 2, 2021

Ah, I didn’t think that it would be important to support the old ICO type for a modern website.

check file extension

If you rely on extension, please validate that the actual file contents match the type guessed from extension.

@fabiocaccamo
Copy link
Owner

@merwok when I started to abstract this library in my existing projects I used .ico files, actually the FileField could be replaced by an ImageField.

@merwok
Copy link
Contributor

merwok commented Feb 2, 2021

Turns out that django checks file extension only*, and relies on Pillow to determine all extensions supported by the system. ICO is marked as always supported in Pillow docs.

(* but it also uses PIL.ImageFile.Parser to get the image’s dimensions, so maybe that step rejects fake images, or maybe not because RuntimeError is caught there. needs to be tested!)

@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Feb 2, 2021

It was long time ago (maybe 8 years), but I remember that the .ico with ImageField raised an Exception, so I used a FileField.

@fabiocaccamo
Copy link
Owner

You can upgrade to 0.16.2 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants