[Django] #18543: Non image file can be saved to ImageField

23 views
Skip to first unread message

Django

unread,
Jun 29, 2012, 7:56:44 PM6/29/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
----------------------------------------------+--------------------
Reporter: johnsmith | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
https://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield
Inherits all attributes and methods from FileField, but also validates
that the uploaded object is a valid image.


I was trying to find out the exact exception that would be raised if a
file is not an image and it appears that no such image validation is done.
So I did a test, with a model such as this
{{{
class Image(models.Model):
caption = models.Charfield(max_length=60)
image = models.ImageField(upload_to='somewhere')

}}}

I tried this
{{{
f = django.core.files.base.ContentFile('not an image')
i = myapp.models.Image()
i.caption("This should throw an error but doesn't")
i.image.save('bar.jpg', f)
i.save()
}}}

I don't get any errors and the file is saved. Maybe I'm doing something
wrong but from what the docs say I'm expecting to be prevented from doing
this.

From pip freeze
Django==1.4
PIL==1.1.7

--
Ticket URL: <https://code.djangoproject.com/ticket/18543>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 5, 2012, 4:28:19 PM7/5/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by niwi):

* cc: niwi@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I tried to reproduce the bug with this test:

{{{
#!python
@skipUnless(test_images, "PIL not installed")
def test_model_image_field(self):
from django.core.files.base import ContentFile
f = ContentFile(b'not an image')
with open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb')
as fp:
image_data = fp.read()

instance = ImageFile()
instance.image.save("foo.png", SimpleUploadedFile("foo.png",
image_data))

instance2 = ImageFile()
instance2.image.save("foo.png", f)
}}}


And I get is this:
{{{
======================================================================
ERROR: test_model_image_field
(modeltests.model_forms.tests.OldFormForXTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/niwi/django/tests/modeltests/model_forms/tests.py", line
1250, in test_model_image_field
instance2.image.save("foo.png", f)
File "../django/db/models/fields/files.py", line 348, in save
super(ImageFieldFile, self).save(name, content, save)
File "../django/db/models/fields/files.py", line 89, in save
setattr(self.instance, self.field.name, self.name)
File "../django/db/models/fields/files.py", line 334, in __set__
self.field.update_dimension_fields(instance, force=True)
File "../django/db/models/fields/files.py", line 412, in
update_dimension_fields
width = file.width
File "../django/core/files/images.py", line 15, in _get_width
return self._get_image_dimensions()[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

----------------------------------------------------------------------
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:1>

Django

unread,
Jul 6, 2012, 5:58:18 AM7/6/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by claudep):

* stage: Unreviewed => Accepted


Comment:

Currently, the verification that the content is an image is done by the
`ImageField` form field (see its `to_python` method), not in the model
field.

Accepted on the base that we should at least:
* Improve the error message as shown in the previous comment.
* Clarify the documentation

Validating that the file is really an image file at the model level might
be something to evaluate, but with great care, because of the performance
issue involved by this operation. Opinions/design decision needed here.

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:2>

Django

unread,
Sep 7, 2012, 2:14:17 PM9/7/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Adam DePrince <deprince@…>):

What constitutes a valid image will depend on how the image is processed
--- Imagemagick, netpnm and PIL all disagree slightly on the definition of
"an image". We should also consider what it means to "validate" the
image --- basic magic number checking will catch the common cases where a
user uploads something decidedly not an image, like an mp3 file, but we
should stress in our documentation that just because the image passed our
"validation" does not mean what was uploaded is actually an image or even
safe to process.

What if ImageField accepted anything for which "file -b --mime-type"
responds with a match to "$image/" ?

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:3>

Django

unread,
Sep 7, 2012, 2:48:08 PM9/7/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Adam DePrince <deprince@…>):

* cc: deprince@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:4>

Django

unread,
Sep 7, 2012, 4:12:05 PM9/7/12
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: deprince
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Adam DePrince <deprince@…>):

* owner: nobody => deprince


Comment:

A quick survey shows that the python options for file seem to be lib magic
based. I don't think we really want to see yet another c dependencey in
django so I'll spend the rest of my djangocon 2012 sprint time banging out
a pure python port.

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:5>

Django

unread,
Apr 26, 2023, 3:41:58 AM4/26/23
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: (none)
Type: Bug | Status: assigned

Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: deprince => (none)
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:6>

Django

unread,
Apr 26, 2023, 3:42:01 AM4/26/23
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:7>

Django

unread,
Nov 11, 2024, 10:04:05 AM11/11/24
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

I would like to work on this, it is my first attempt at a bug fix.
I will need extra time, Im going to replicate the bug, come up with a
proposal
and communicate my ideas.
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:8>

Django

unread,
Nov 11, 2024, 10:24:54 AM11/11/24
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner:
| jburns6789
Type: Bug | Status: assigned
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jburns6789):

* owner: (none) => jburns6789
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:9>

Django

unread,
Nov 18, 2024, 3:08:00 PM11/18/24
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner:
| jburns6789
Type: Bug | Status: assigned
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jburns6789):

Hi, from what I can deduce from the previous comments, In this context,
the Django image.field is used to validate the uploaded image file and no
other third party libraries. The fix in this case would be to have the
error message refer users to the ImageField documentation needing pillow
to be installed?
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:10>

Django

unread,
Nov 19, 2024, 9:50:31 PM11/19/24
to django-...@googlegroups.com
#18543: Non image file can be saved to ImageField
-------------------------------------+-------------------------------------
Reporter: johnsmith | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jburns6789):

* owner: jburns6789 => (none)
* status: assigned => new

--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:11>
Reply all
Reply to author
Forward
0 new messages