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

Be more strict when checking if mimetype is allowed inline #154

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/1167.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Be more strict when checking if mimetype is allowed to be displayed inline.
[maurits]
3 changes: 2 additions & 1 deletion plone/namedfile/browser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from AccessControl.ZopeGuards import guarded_getattr
from plone.namedfile.utils import extract_media_type
from plone.namedfile.utils import set_headers
from plone.namedfile.utils import stream_data
from plone.rfc822.interfaces import IPrimaryFieldInfo
Expand Down Expand Up @@ -174,7 +175,7 @@ class DisplayFile(Download):

def set_headers(self, file):
if hasattr(file, "contentType"):
mimetype = file.contentType
mimetype = extract_media_type(file.contentType)
if self.use_denylist:
if mimetype in self.disallowed_inline_mimetypes:
# Let the Download view handle this.
Expand Down
3 changes: 2 additions & 1 deletion plone/namedfile/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from plone.namedfile.interfaces import IStableImageScale
from plone.namedfile.picture import get_picture_variants
from plone.namedfile.picture import Img2PictureTag
from plone.namedfile.utils import extract_media_type
from plone.namedfile.utils import getHighPixelDensityScales
from plone.namedfile.utils import set_headers
from plone.namedfile.utils import stream_data
Expand Down Expand Up @@ -182,7 +183,7 @@ def _should_force_download(self):
# If this returns True, the caller should call set_headers with a filename.
if not hasattr(self.data, "contentType"):
return
mimetype = self.data.contentType
mimetype = extract_media_type(self.data.contentType)
if self.use_denylist:
# We explicitly deny a few mimetypes, and allow the rest.
return mimetype in self.disallowed_inline_mimetypes
Expand Down
11 changes: 10 additions & 1 deletion plone/namedfile/tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is borrowed from zope.app.file and licensed ZPL.
# This file is partially borrowed from zope.app.file and licensed ZPL.

from DateTime import DateTime
from plone.namedfile.file import NamedImage
Expand Down Expand Up @@ -82,6 +82,15 @@ def testInterface(self):
self.assertTrue(INamedImage.implementedBy(NamedImage))
self.assertTrue(verifyClass(INamedImage, NamedImage))

def test_extract_media_type(self):
from plone.namedfile.utils import extract_media_type as extract

self.assertIsNone(extract(None))
self.assertEqual(extract("text/plain"), "text/plain")
self.assertEqual(extract("TEXT/PLAIN"), "text/plain")
self.assertEqual(extract("text / plain"), "text/plain")
self.assertEqual(extract(" text/plain ; charset=utf-8"), "text/plain")

def test_get_contenttype(self):
self.assertEqual(
get_contenttype(NamedImage(getFile("image.gif"), contentType="image/gif")),
Expand Down
26 changes: 25 additions & 1 deletion plone/namedfile/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
except ImportError:
from Products.CMFPlone.interfaces.controlpanel import IImagingSchema

try:
# Zope 5.8.6+
from OFS.Image import extract_media_type
except ImportError:

def extract_media_type(content_type):
"""extract the proper media type from *content_type*.

Ignore parameters and whitespace and normalize to lower case.
See https://github.com/zopefoundation/Zope/pull/1167
"""
if not content_type:
return content_type
# ignore parameters
content_type = content_type.split(";", 1)[0]
# ignore whitespace
content_type = "".join(content_type.split())
# normalize to lowercase
return content_type.lower()


@implementer(IStreamIterator)
class filestream_range_iterator(Iterable):
Expand Down Expand Up @@ -94,7 +114,11 @@ def safe_basename(filename):


def get_contenttype(file=None, filename=None, default="application/octet-stream"):
"""Get the MIME content type of the given file and/or filename."""
"""Get the MIME content type of the given file and/or filename.

Note: depending on your use case, you may want to call 'extract_media_type'
on the result.
"""

file_type = getattr(file, "contentType", None)
if file_type:
Expand Down
Loading