Permalink
Browse files

Fixed #17281 -- Prevented `AdminErrorHandler` from silently failing i…

…f the log message contains newlines. Thanks to Russell Keith-Magee for the report and to Bartolome Sanchez Salado and Marcin Wróbel for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17501 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
jphalip committed Feb 11, 2012
1 parent a77679d commit 995f7a16a8bf539caf076a7744441a383e9a38b2
Showing with 62 additions and 2 deletions.
  1. +2 −0 AUTHORS
  2. +13 −2 django/utils/log.py
  3. +47 −0 tests/regressiontests/logging_tests/tests.py
View
@@ -450,6 +450,7 @@ answer newbie questions, and generally made Django that much better:
Manuel Saelices <msaelices@yaco.es>
Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/>
Vinay Sajip <vinay_sajip@yahoo.co.uk>
+ Bartolome Sanchez Salado <i42sasab@uco.es>
Kadesarin Sanjek
Massimo Scamarcia <massimo.scamarcia@gmail.com>
Paulo Scardine <paulo@scardine.com.br>
@@ -548,6 +549,7 @@ answer newbie questions, and generally made Django that much better:
Jakub Wiśniowski <restless.being@gmail.com>
Maciej Wiśniowski <pigletto@gmail.com>
wojtek
+ Marcin Wróbel
Jason Yan <tailofthesun@gmail.com>
Lars Yencken <lars.yencken@gmail.com>
ye7cakf02@sneakemail.com
View
@@ -47,18 +47,20 @@ def emit(self, record):
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,6 +74,15 @@ def emit(self, record):
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):
"""
@@ -160,3 +160,50 @@ def test_accepts_args(self):
# Restore original filters
admin_email_handler.filters = orig_filters
+
+ @override_settings(
+ ADMINS=(('admin', 'admin@example.com'),),
+ 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', 'admin@example.com'),),
+ 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)

0 comments on commit 995f7a1

Please sign in to comment.