changeset 986:26de15fb5a80

Guard against bad image urls in ssl_images.
author Brian Neal <bgneal@gmail.com>
date Sun, 25 Oct 2015 14:47:29 -0500
parents 9b197dbba34b
children 76525f5ac2b1
files core/management/commands/ssl_images.py core/tests/test_ssl_images.py
diffstat 2 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Sun Oct 25 13:54:56 2015 -0500
+++ b/core/management/commands/ssl_images.py	Sun Oct 25 14:47:29 2015 -0500
@@ -191,7 +191,11 @@
 
     new_src = None
     if src:
-        r = urlparse.urlparse(src)
+        try:
+            r = urlparse.urlparse(src)
+        except ValueError:
+            return u'{bad image}'
+
         if r.hostname in SG101_HOSTS:
             new_src = r.path        # convert to relative path
         elif r.scheme == 'http':
--- a/core/tests/test_ssl_images.py	Sun Oct 25 13:54:56 2015 -0500
+++ b/core/tests/test_ssl_images.py	Sun Oct 25 14:47:29 2015 -0500
@@ -234,6 +234,15 @@
         expected_args = [mock.call(urlparse(c)) for c in old_src[:2]]
         self.assertEqual(upload_mock.call_args_list, expected_args)
 
+    @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
+    def test_bad_image_url(self, upload_mock):
+        test_str = u'![image](http://[url=http://www.flickr.com/photos/85447101@N04/8025176073/][img]http://farm9.staticflickr.com/8456/8025176073_91ac0c3d18_b.jpg[/img][/url] [url=http://www.flickr.com/photos/85447101@N04/8025176073/]IMG_0398[/url] by [url=http://www.flickr.com/people/85447101@N04/]friendshipmaster[/url], on Flick)'
+        expected = u'{bad image}'
+
+        result = process_post(test_str)
+        self.assertEqual(expected, result)
+        self.assertEqual(upload_mock.call_count, 0)
+
 
 class HtmlCheckTestCase(unittest.TestCase):