To quote the Django docs:
@sensitive_post_parameters('pass_word', 'credit_card_number')
def record_user_profile(request):
UserProfile.create(user=request.user,
password=request.POST['pass_word'],
credit_card=request.POST['credit_card_number'],
name=request.POST['name'])
In the above example, the values for the pass_word and credit_card_number POST parameters will be hidden and replaced with stars (******) in the request’s representation inside the error reports, whereas the value of the name parameter will be disclosed.
To systematically hide all POST parameters of a request in error reports, do not provide any argument to the sensitive_post_parameters decorator:
@sensitive_post_parameters()
def my_view(request):
...
As a test, I added the following code to my Django 1.6 application:
views.py:
@sensitive_post_parameters('sensitive')
def sensitive(request):
if request.method == 'POST':
raise IntegrityError(unicode(timezone.now()))
return render(request, 'sensitive-test.html',
{'form': forms.SensitiveParamForm()})
forms.py:
class SensitiveParamForm(forms.Form):
not_sensitive = forms.CharField(max_length=255)
sensitive = forms.CharField(max_length=255)
When I submit this form via POST
, I can see the values of both fields (including sensitive
) clear as day in the Sentry report.
What am I doing wrong here? I'm using Django 1.6 and Raven 3.5.2.
Thanks in advance for your help!
Turns out that this stemmed from a bug in Django itself!
If you haven't changed
DEFAULT_EXCEPTION_REPORTER_FILTER
in yoursettings
file, you get the default filter ofSafeExceptionReporterFilter
.If you've used the
sensitive_post_parameters
decorator, this will result in your callingSafeExceptionReporterFilter
'sget_post_parameters
method:The problem with the above is that while it will correctly return a
QuerySet
with the sensitivePOST
parameters set toCLEANSED_SUBSTITUTE
('********************'
)...it won't in any way alterrequest.body
.This is a problem when working with Raven/Sentry for Django, because it turns out that the
get_data_from_request
method of Raven'sDjangoClient
first attempts to get the request'sPOST
parameters fromrequest.body
:The fastest fix turned out to just involve subclassing
DjangoClient
and manually replacing its output with the cleansedQuerySet
produced bySafeExceptionReporterFilter
: