Author: julien
Date: 2012-02-11 01:31:18 -0800 (Sat, 11 Feb 2012)
New Revision: 17501

Modified:
   django/trunk/AUTHORS
   django/trunk/django/utils/log.py
   django/trunk/tests/regressiontests/logging_tests/tests.py
Log:
Fixed #17281 -- Prevented `AdminErrorHandler` from silently failing if the log 
message contains newlines. Thanks to Russell Keith-Magee for the report and to 
Bartolome Sanchez Salado and Marcin Wro?\204?\129bel for the patch.

Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2012-02-11 04:18:15 UTC (rev 17500)
+++ django/trunk/AUTHORS        2012-02-11 09:31:18 UTC (rev 17501)
@@ -450,6 +450,7 @@
     Manuel Saelices <[email protected]>
     Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/>
     Vinay Sajip <[email protected]>
+    Bartolome Sanchez Salado <[email protected]>
     Kadesarin Sanjek
     Massimo Scamarcia <[email protected]>
     Paulo Scardine <[email protected]>
@@ -548,6 +549,7 @@
     Jakub Wiśniowski <[email protected]>
     Maciej Wiśniowski <[email protected]>
     wojtek
+    Marcin Wróbel
     Jason Yan <[email protected]>
     Lars Yencken <[email protected]>
     [email protected]

Modified: django/trunk/django/utils/log.py
===================================================================
--- django/trunk/django/utils/log.py    2012-02-11 04:18:15 UTC (rev 17500)
+++ django/trunk/django/utils/log.py    2012-02-11 09:31:18 UTC (rev 17501)
@@ -47,18 +47,20 @@
             request = record.request
             subject = '%s (%s IP): %s' % (
                 record.levelname,
-                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 
'internal' or 'EXTERNAL'),
+                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
+                 and 'internal' or 'EXTERNAL'),
                 record.msg
             )
             filter = get_exception_reporter_filter(request)
             request_repr = filter.get_request_repr(request)
-        except:
+        except Exception:
             subject = '%s: %s' % (
                 record.levelname,
                 record.getMessage()
             )
             request = None
             request_repr = "Request repr() unavailable."
+        subject = self.format_subject(subject)
 
         if record.exc_info:
             exc_info = record.exc_info
@@ -72,7 +74,16 @@
         html_message = self.include_html and reporter.get_traceback_html() or 
None
         mail.mail_admins(subject, message, fail_silently=True, 
html_message=html_message)
 
+    def format_subject(self, subject):
+        """
+        Escape CR and LF characters, and limit length.
+        RFC 2822's hard limit is 998 characters per line. So, minus "Subject: "
+        the actual subject must be no longer than 989 characters.
+        """
+        formatted_subject = subject.replace('\n', '\\n').replace('\r', '\\r')
+        return formatted_subject[:989]
 
+
 class CallbackFilter(logging.Filter):
     """
     A logging filter that checks the return value of a given callable (which

Modified: django/trunk/tests/regressiontests/logging_tests/tests.py
===================================================================
--- django/trunk/tests/regressiontests/logging_tests/tests.py   2012-02-11 
04:18:15 UTC (rev 17500)
+++ django/trunk/tests/regressiontests/logging_tests/tests.py   2012-02-11 
09:31:18 UTC (rev 17501)
@@ -160,3 +160,50 @@
 
         # Restore original filters
         admin_email_handler.filters = orig_filters
+
+    @override_settings(
+            ADMINS=(('admin', '[email protected]'),),
+            EMAIL_SUBJECT_PREFIX='',
+            DEBUG=False,
+        )
+    def test_subject_accepts_newlines(self):
+        """
+        Ensure that newlines in email reports' subjects are escaped to avoid
+        AdminErrorHandler to fail.
+        Refs #17281.
+        """
+        message = u'Message \r\n with newlines'
+        expected_subject = u'ERROR: Message \\r\\n with newlines'
+
+        self.assertEqual(len(mail.outbox), 0)
+
+        logger = getLogger('django.request')
+        logger.error(message)
+
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertFalse('\n' in mail.outbox[0].subject)
+        self.assertFalse('\r' in mail.outbox[0].subject)
+        self.assertEqual(mail.outbox[0].subject, expected_subject)
+
+    @override_settings(
+            ADMINS=(('admin', '[email protected]'),),
+            EMAIL_SUBJECT_PREFIX='',
+            DEBUG=False,
+        )
+    def test_truncate_subject(self):
+        """
+        RFC 2822's hard limit is 998 characters per line.
+        So, minus "Subject: ", the actual subject must be no longer than 989
+        characters.
+        Refs #17281.
+        """
+        message = 'a' * 1000
+        expected_subject = 'ERROR: aa' + 'a' * 980
+
+        self.assertEqual(len(mail.outbox), 0)
+
+        logger = getLogger('django.request')
+        logger.error(message)
+
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].subject, expected_subject)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to