8000 [1.7.x] Fixed #23593 -- Fixed crash in AdminEmailHandler with non-ASC… · alex-python/django@96f0222 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96f0222

Browse files
committed
[1.7.x] Fixed #23593 -- Fixed crash in AdminEmailHandler with non-ASCII characters in request.
Thanks edevil for the report and Simon Charette for review. Backport of 9dff5ce from master
1 parent c250899 commit 96f0222

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

django/utils/log.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import unicode_literals
2+
13
import logging
24
import sys
35
import warnings
@@ -6,6 +8,7 @@
68
from django.core import mail
79
from django.core.mail import get_connection
810
from django.utils.deprecation import RemovedInNextVersionWarning
11+
from django.utils.encoding import force_text
912
from django.utils.module_loading import import_string
1013
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
1114

@@ -106,7 +109,7 @@ def emit(self, record):
106109
record.getMessage()
107110
)
108111
filter = get_exception_reporter_filter(request)
109-
request_repr = '\n{0}'.format(filter.get_request_repr(request))
112+
request_repr = '\n{0}'.format(force_text(filter.get_request_repr(request)))
110113
except Exception:
111114
subject = '%s: %s' % (
112115
record.levelname,

docs/releases/1.7.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ Bugfixes
9797
possible to import arbitrary packages from the Python path. This was not
9898
considered a security issue because ``admindocs`` is only accessible to staff
9999
users (:ticket:`23601`).
100+
101+
* Fixed ``UnicodeDecodeError`` crash in ``AdminEmailHandler`` with non-ASCII
102+
characters in the request (:ticket:`23593`).

tests/logging_tests/tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding:utf-8 -*-
12
from __future__ import unicode_literals
23

34
import logging
@@ -319,6 +320,26 @@ def my_mail_admins(*args, **kwargs):
319320
mail.mail_admins = orig_mail_admins
320321
admin_email_handler.email_backend = orig_email_backend
321322

323+
@override_settings(
324+
ADMINS=(('whatever admin', 'admin@example.com'),),
325+
)
326+
def test_emit_non_ascii(self):
327+
"""
328+
#23593 - AdminEmailHandler should allow Unicode characters in the
329+
request.
330+
"""
331+
handler = self.get_admin_email_handler(self.logger)
332+
record = self.logger.makeRecord('name', logging.ERROR, 'function', 'lno', 'message', None, None)
333+
rf = RequestFactory()
334+
url_path = '/º'
335+
record.request = rf.get(url_path)
336+
handler.emit(record)
337+
self.assertEqual(len(mail.outbox), 1)
338+
msg = mail.outbox[0]
339+
self.assertEqual(msg.to, ['admin@example.com'])
340+
self.assertEqual(msg.subject, "[Django] ERROR (EXTERNAL IP): message")
341+
self.assertIn("path:%s" % url_path, msg.body)
342+
322343

323344
class SettingsConfigTest(AdminScriptTestCase):
324345
"""

0 commit comments

Comments
 (0)
0