Skip to content

Commit ff835f4

Browse files
committed
Refs CVE-2025-48432 -- Made SuspiciousOperation logging use log_response() for consistency.
1 parent 9d72e7d commit ff835f4

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

django/core/handlers/exception.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ def response_for_exception(request, exc):
116116
# exception would be raised.
117117
request._mark_post_parse_error()
118118

119-
# The request logger receives events for any problematic request
120-
# The security logger receives events for all SuspiciousOperations
121-
security_logger = logging.getLogger(
122-
"django.security.%s" % exc.__class__.__name__
123-
)
124-
security_logger.error(
125-
str(exc),
126-
exc_info=exc,
127-
extra={"status_code": 400, "request": request},
128-
)
129119
if settings.DEBUG:
130120
response = debug.technical_500_response(
131121
request, *sys.exc_info(), status_code=400
@@ -134,6 +124,17 @@ def response_for_exception(request, exc):
134124
response = get_exception_response(
135125
request, get_resolver(get_urlconf()), 400, exc
136126
)
127+
# The logger is set to django.security, which specifically captures
128+
# SuspiciousOperation events, unlike the default django.request logger.
129+
security_logger = logging.getLogger(f"django.security.{exc.__class__.__name__}")
130+
log_response(
131+
str(exc),
132+
exception=exc,
133+
request=request,
134+
response=response,
135+
level="error",
136+
logger=security_logger,
137+
)
137138

138139
else:
139140
signals.got_request_exception.send(sender=None, request=request)

tests/logging_tests/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,15 @@ def test_suspicious_email_admins(self):
618618
self.assertEqual(len(mail.outbox), 1)
619619
self.assertIn("SuspiciousOperation at /suspicious/", mail.outbox[0].body)
620620

621+
def test_response_logged(self):
622+
with self.assertLogs("django.security.SuspiciousOperation", "ERROR") as handler:
623+
response = self.client.get("/suspicious/")
624+
625+
self.assertLogRecord(
626+
handler, "dubious", logging.ERROR, 400, request=response.wsgi_request
627+
)
628+
self.assertEqual(response.status_code, 400)
629+
621630

622631
class SettingsCustomLoggingTest(AdminScriptTestCase):
623632
"""

0 commit comments

Comments
 (0)