From 9c0968ad1267045f6c8c6841086e64a55bc695ab Mon Sep 17 00:00:00 2001 From: Ricky Robinett Date: Thu, 28 Jul 2016 10:52:47 -0400 Subject: [PATCH] Fix bug where filenames with no extension cause an infinite loop --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/1.7.rst | 1 + wagtail/wagtailimages/models.py | 5 +++- wagtail/wagtailimages/tests/test_models.py | 32 ++++++++++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 2b963f941..7436c8196 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -35,6 +35,7 @@ Changelog * Fix: Wagtail's UserProfile model now sets a related_name of ``wagtail_userprofile`` to avoid naming collisions with other user profile models (Matt Westcott) * Fix: Non-text content is now preserved when adding or editing a link within rich text (Matt Westcott) * Fix: Fixed preview when `SECURE_SSL_REDIRECT = True` (Aymeric Augustin) + * Fix: Prevent hang when truncating an image filename without an extension (Ricky Robinett) 1.6.3 (30.09.2016) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 40025dff8..82d1fb4d7 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -170,6 +170,7 @@ Contributors * Stein Strindhaug * Žan Anderle * Mattias Loverot +* Ricky Robinett Translators =========== diff --git a/docs/releases/1.7.rst b/docs/releases/1.7.rst index 749590c3a..eb0fbe48d 100644 --- a/docs/releases/1.7.rst +++ b/docs/releases/1.7.rst @@ -70,6 +70,7 @@ Bug fixes * Wagtail's UserProfile model now sets a related_name of ``wagtail_userprofile`` to avoid naming collisions with other user profile models (Matt Westcott) * Non-text content is now preserved when adding or editing a link within rich text (Matt Westcott) * Fixed preview when ``SECURE_SSL_REDIRECT = True`` (Aymeric Augustin) + * Prevent hang when truncating an image filename without an extension (Ricky Robinett) Upgrade considerations diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index a4ed3550e..32da6d97d 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -127,7 +127,10 @@ class AbstractImage(CollectionMember, index.Indexed, models.Model): # https://code.djangoproject.com/ticket/9893 while len(os.path.join(folder_name, filename)) >= 95: prefix, dot, extension = filename.rpartition('.') - filename = prefix[:-1] + dot + extension + if prefix: + filename = prefix[:-1] + dot + extension + else: + filename = extension[:-1] return os.path.join(folder_name, filename) def get_usage(self): diff --git a/wagtail/wagtailimages/tests/test_models.py b/wagtail/wagtailimages/tests/test_models.py index 1d1fe98e9..b38787341 100644 --- a/wagtail/wagtailimages/tests/test_models.py +++ b/wagtail/wagtailimages/tests/test_models.py @@ -492,3 +492,35 @@ class TestIssue312(TestCase): height=rend1.height, focal_point_key=rend1.focal_point_key, ) + + +class TestFilenameReduction(TestCase): + """ + This tests for a bug which results in filenames without extensions + causing an infinite loop + """ + def test_filename_reduction_no_ext(self): + # Create an image with a big filename and no extension + image = Image.objects.create( + title="Test image", + file=get_test_image_file( + 'thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong' + ) + ) + + # Saving file will result in infinite loop when bug is present + image.save() + self.assertEqual("original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpiali", image.file.name) + + # Test for happy path. Long filename with extension + def test_filename_reduction_ext(self): + # Create an image with a big filename and extensions + image = Image.objects.create( + title="Test image", + file=get_test_image_file( + 'thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong.png' + ) + ) + + image.save() + self.assertEqual("original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexp.png", image.file.name)