From 9c92924cd5d164701e2514e1c2d6574126bd7cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeremy=20Lain=C3=A9?= Date: Tue, 25 Aug 2020 14:33:05 +0200 Subject: [PATCH] Fixed #31942 -- Made settings cleansing work with dictionary settings with non-string keys. --- django/views/debug.py | 23 ++++++++++++----------- tests/view_tests/tests/test_debug.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/django/views/debug.py b/django/views/debug.py index 68dba4b500..e45ef01ace 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -91,18 +91,19 @@ class SafeExceptionReporterFilter: value is a dictionary, recursively cleanse the keys in that dictionary. """ try: - if self.hidden_settings.search(key): - cleansed = self.cleansed_substitute - elif isinstance(value, dict): - cleansed = {k: self.cleanse_setting(k, v) for k, v in value.items()} - elif isinstance(value, list): - cleansed = [self.cleanse_setting('', v) for v in value] - elif isinstance(value, tuple): - cleansed = tuple([self.cleanse_setting('', v) for v in value]) - else: - cleansed = value + is_sensitive = self.hidden_settings.search(key) except TypeError: - # If the key isn't regex-able, just return as-is. + is_sensitive = False + + if is_sensitive: + cleansed = self.cleansed_substitute + elif isinstance(value, dict): + cleansed = {k: self.cleanse_setting(k, v) for k, v in value.items()} + elif isinstance(value, list): + cleansed = [self.cleanse_setting('', v) for v in value] + elif isinstance(value, tuple): + cleansed = tuple([self.cleanse_setting('', v) for v in value]) + else: cleansed = value if callable(cleansed): diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index bb5a45224d..6e839b44f5 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -1274,6 +1274,19 @@ class ExceptionReporterFilterTests(ExceptionReportTestMixin, LoggingCaptureMixin {'login': 'cooper', 'password': reporter_filter.cleansed_substitute}, ) + def test_cleanse_setting_recurses_in_dictionary_with_non_string_key(self): + reporter_filter = SafeExceptionReporterFilter() + initial = {('localhost', 8000): {'login': 'cooper', 'password': 'secret'}} + self.assertEqual( + reporter_filter.cleanse_setting('SETTING_NAME', initial), + { + ('localhost', 8000): { + 'login': 'cooper', + 'password': reporter_filter.cleansed_substitute, + }, + }, + ) + def test_cleanse_setting_recurses_in_list_tuples(self): reporter_filter = SafeExceptionReporterFilter() initial = [