Skip to content

Commit 8206af0

Browse files
committed
Filter sensitive POST parameters in error reports
1 parent 8e5a695 commit 8206af0

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/django_registration/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from django.core.exceptions import ImproperlyConfigured
88
from django.http import HttpResponseRedirect
99
from django.urls import reverse_lazy
10+
from django.utils.decorators import method_decorator
1011
from django.utils.encoding import force_str
12+
from django.views.decorators.debug import sensitive_post_parameters
1113
from django.views.generic.base import TemplateView
1214
from django.views.generic.edit import FormView
1315

@@ -40,6 +42,7 @@ class RegistrationView(FormView):
4042
success_url = None
4143
template_name = "django_registration/registration_form.html"
4244

45+
@method_decorator(sensitive_post_parameters())
4346
def dispatch(self, *args, **kwargs):
4447
"""
4548
Check that user signup is allowed before even bothering to

tests/test_views.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
44
"""
55

6+
import logging
7+
import sys
8+
69
from django.contrib.auth import get_user_model
7-
from django.core import signing
10+
from django.contrib.auth.models import AnonymousUser
11+
from django.core import mail, signing
812
from django.core.exceptions import ImproperlyConfigured
9-
from django.test import override_settings
13+
from django.test import RequestFactory, override_settings
1014
from django.urls import reverse
1115

1216
from django_registration import forms
@@ -86,3 +90,69 @@ def test_user_mismatch_breaks_view(self):
8690
)
8791
with self.assertRaisesMessage(ImproperlyConfigured, message):
8892
view.get_form()
93+
94+
95+
class RegistrationError(Exception):
96+
"""
97+
Distinct exception class to simulate an unhandled error in the below
98+
tests.
99+
100+
"""
101+
102+
103+
class BuggyRegistrationView(base_views.RegistrationView):
104+
"""
105+
Registration view that simulates an unhandled exception.
106+
107+
"""
108+
109+
def registration_allowed(self):
110+
raise RegistrationError("catch me if you can")
111+
112+
113+
buggy_view = BuggyRegistrationView.as_view()
114+
115+
116+
@override_settings(ADMINS=[("Admin", "admin@localhost")])
117+
class SensitiveParameterFilterTests(RegistrationTestCase):
118+
"""
119+
Test filtering of sensitive POST parameters in error reports for the
120+
registration view.
121+
122+
"""
123+
124+
logger = logging.getLogger("django")
125+
factory = RequestFactory()
126+
127+
def test_sensitive_post_parameters_are_filtered(self):
128+
"""
129+
When an unexpected exception occurs during a POST request to the
130+
registration view, the default email report to ADMINS must not
131+
contain the submitted passwords.
132+
133+
"""
134+
request = self.factory.post("/raise/", data=self.valid_data)
135+
request.user = AnonymousUser()
136+
# we cannot use self.assertRaises(...) here because of sys.exc_info()
137+
try:
138+
buggy_view(request)
139+
self.fail("expected exception not thrown")
140+
except RegistrationError as error:
141+
self.assertEqual(str(error), "catch me if you can")
142+
# based on code in Django (tests/view_tests/views.py)
143+
self.logger.error(
144+
"Internal Server Error: %s" % request.path,
145+
exc_info=sys.exc_info(),
146+
extra={"status_code": 500, "request": request},
147+
)
148+
self.assertEqual(len(mail.outbox), 1)
149+
email = mail.outbox[0]
150+
self.assertIn("RegistrationError at /raise/", email.body)
151+
self.assertIn("catch me if you can", email.body)
152+
self.assertIn("No GET data", email.body)
153+
self.assertNotIn("No POST data", email.body)
154+
self.assertIn("password1", email.body)
155+
self.assertIn("password2", email.body)
156+
self.assertNotIn(self.valid_data["password1"], email.body)
157+
self.assertNotIn(self.valid_data["password2"], email.body)
158+
self.assertNotIn(self.valid_data["email"], email.body)

0 commit comments

Comments
 (0)