Added exception to SuspiciousOperation logging.

This allows better debugging and filtering of errors.
This commit is contained in:
Adam Johnson 2022-01-12 12:27:25 +00:00 committed by GitHub
parent 76aefe0fc9
commit 84e98ba194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -98,6 +98,7 @@ def response_for_exception(request, exc):
security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__) security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
security_logger.error( security_logger.error(
str(exc), str(exc),
exc_info=exc,
extra={'status_code': 400, 'request': request}, extra={'status_code': 400, 'request': request},
) )
if settings.DEBUG: if settings.DEBUG:

View File

@ -6,7 +6,9 @@ from admin_scripts.tests import AdminScriptTestCase
from django.conf import settings from django.conf import settings
from django.core import mail 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.files.temp import NamedTemporaryFile
from django.core.management import color from django.core.management import color
from django.http.multipartparser import MultiPartParserError from django.http.multipartparser import MultiPartParserError
@ -498,6 +500,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase):
msg='dubious', msg='dubious',
status_code=400, status_code=400,
logger='django.security.SuspiciousOperation', logger='django.security.SuspiciousOperation',
exc_class=SuspiciousOperation,
) )
def test_suspicious_operation_uses_sublogger(self): def test_suspicious_operation_uses_sublogger(self):
@ -507,6 +510,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase):
msg='dubious', msg='dubious',
status_code=400, status_code=400,
logger='django.security.DisallowedHost', logger='django.security.DisallowedHost',
exc_class=DisallowedHost,
) )
@override_settings( @override_settings(
@ -516,7 +520,7 @@ class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase):
def test_suspicious_email_admins(self): def test_suspicious_email_admins(self):
self.client.get('/suspicious/') self.client.get('/suspicious/')
self.assertEqual(len(mail.outbox), 1) 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): class SettingsCustomLoggingTest(AdminScriptTestCase):