debug.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import re
  2. from django.views import debug
  3. # We use a negative lookbehind to not escape "wsgi.url_scheme" which is used as source for request schema.
  4. debug.HIDDEN_SETTINGS = re.compile(debug.HIDDEN_SETTINGS.pattern + '|(?<!wsgi\.)URL|CSRF|COOKIE|csrftoken|csrfmiddlewaretoken|sessionid', re.IGNORECASE)
  5. class SafeExceptionReporterFilter(debug.SafeExceptionReporterFilter):
  6. """
  7. Safe exception reporter filter which also filters request environment
  8. (``META``) and cookies (``COOKIES``) so that it is safer to share the
  9. report publicly.
  10. This is useful to not display passwords and other sensitive data passed to
  11. Django through its process environment.
  12. Furthermore, it configures Django to additionally clean settings with ``URL``, ``CSRF``,
  13. ``COOKIE``, ``csrftoken``, ``csrfmiddlewaretoken``, and ``sessionid`` in keys.
  14. To install it, configure Django to::
  15. DEFAULT_EXCEPTION_REPORTER_FILTER = 'missing.debug.SafeExceptionReporterFilter'
  16. and import ``missing.debug`` somewhere in your code, for example, in ``urls.py``
  17. of your project.
  18. """
  19. def get_post_parameters(self, request):
  20. if request is None:
  21. return super(SafeExceptionReporterFilter, self).get_post_parameters(request)
  22. # We hook into this method to modify request in place, not nice, but it works.
  23. for key in request.META:
  24. request.META[key] = debug.cleanse_setting(key, request.META[key])
  25. for key in request.COOKIES:
  26. request.COOKIES[key] = debug.cleanse_setting(key, request.COOKIES[key])
  27. post = super(SafeExceptionReporterFilter, self).get_post_parameters(request).copy()
  28. for key in post:
  29. post[key] = debug.cleanse_setting(key, post[key])
  30. return post