From 84e98ba19456a03f355c3f01ba6c70d5f45ee260 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 12 Jan 2022 12:27:25 +0000 Subject: [PATCH] Added exception to SuspiciousOperation logging. This allows better debugging and filtering of errors. --- django/core/handlers/exception.py | 1 + tests/logging_tests/tests.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/core/handlers/exception.py b/django/core/handlers/exception.py index 87a66505428..8b40f920053 100644 --- a/django/core/handlers/exception.py +++ b/django/core/handlers/exception.py @@ -98,6 +98,7 @@ def response_for_exception(request, exc): security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__) security_logger.error( str(exc), + exc_info=exc, extra={'status_code': 400, 'request': request}, ) if settings.DEBUG: diff --git a/tests/logging_tests/tests.py b/tests/logging_tests/tests.py index 666105baff3..2764917f150 100644 --- a/tests/logging_tests/tests.py +++ b/tests/logging_tests/tests.py @@ -6,7 +6,9 @@ from admin_scripts.tests import AdminScriptTestCase from django.conf import settings from django.core import mail -from django.core.exceptions import PermissionDenied +from django.core.exceptions import ( + DisallowedHost, PermissionDenied, SuspiciousOperation, +) from django.core.files.temp import NamedTemporaryFile from django.core.management import color from django.http.multipartparser import MultiPartParserError @@ -498,6 +500,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase): msg='dubious', status_code=400, logger='django.security.SuspiciousOperation', + exc_class=SuspiciousOperation, ) def test_suspicious_operation_uses_sublogger(self): @@ -507,6 +510,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase): msg='dubious', status_code=400, logger='django.security.DisallowedHost', + exc_class=DisallowedHost, ) @override_settings( @@ -516,7 +520,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase): def test_suspicious_email_admins(self): self.client.get('/suspicious/') self.assertEqual(len(mail.outbox), 1) - self.assertIn('Report at /suspicious/', mail.outbox[0].body) + self.assertIn('SuspiciousOperation at /suspicious/', mail.outbox[0].body) class SettingsCustomLoggingTest(AdminScriptTestCase):