events: fix false-y values being stripped

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-08-16 22:29:09 +02:00
parent f7601d9571
commit 1281e842d1
1 changed files with 21 additions and 18 deletions

View File

@ -27,19 +27,18 @@ def cleanse_item(key: str, value: Any) -> Any:
"""Cleanse a single item""" """Cleanse a single item"""
if isinstance(value, dict): if isinstance(value, dict):
return cleanse_dict(value) return cleanse_dict(value)
elif isinstance(value, list): if isinstance(value, list):
for idx, item in enumerate(value): for idx, item in enumerate(value):
if isinstance(value, dict): value[idx] = cleanse_item(key, item)
value[idx] = cleanse_dict(item) return value
try: try:
if SafeExceptionReporterFilter.hidden_settings.search( if SafeExceptionReporterFilter.hidden_settings.search(
key key
) and not ALLOWED_SPECIAL_KEYS.search(key): ) and not ALLOWED_SPECIAL_KEYS.search(key):
return SafeExceptionReporterFilter.cleansed_substitute return SafeExceptionReporterFilter.cleansed_substitute
else:
return value
except TypeError: # pragma: no cover except TypeError: # pragma: no cover
return value return value
return value
def cleanse_dict(source: dict[Any, Any]) -> dict[Any, Any]: def cleanse_dict(source: dict[Any, Any]) -> dict[Any, Any]:
@ -47,7 +46,7 @@ def cleanse_dict(source: dict[Any, Any]) -> dict[Any, Any]:
final_dict = {} final_dict = {}
for key, value in source.items(): for key, value in source.items():
new_value = cleanse_item(key, value) new_value = cleanse_item(key, value)
if new_value: if new_value is not ...:
final_dict[key] = new_value final_dict[key] = new_value
return final_dict return final_dict
@ -93,22 +92,26 @@ def sanitize_item(value: Any) -> Any:
value = asdict(value) value = asdict(value)
if isinstance(value, dict): if isinstance(value, dict):
return sanitize_dict(value) return sanitize_dict(value)
elif isinstance(value, list): if isinstance(value, list):
for idx, item in enumerate(value): new_values = []
value[idx] = sanitize_item(item) for item in value:
elif isinstance(value, (User, AnonymousUser)): new_value = sanitize_item(item)
if new_value:
new_values.append(new_value)
return new_values
if isinstance(value, (User, AnonymousUser)):
return sanitize_dict(get_user(value)) return sanitize_dict(get_user(value))
elif isinstance(value, models.Model): if isinstance(value, models.Model):
return sanitize_dict(model_to_dict(value)) return sanitize_dict(model_to_dict(value))
elif isinstance(value, UUID): if isinstance(value, UUID):
return value.hex return value.hex
elif isinstance(value, (HttpRequest, WSGIRequest)): if isinstance(value, (HttpRequest, WSGIRequest)):
return None return ...
elif isinstance(value, City): if isinstance(value, City):
return GEOIP_READER.city_to_dict(value) return GEOIP_READER.city_to_dict(value)
elif isinstance(value, Path): if isinstance(value, Path):
return str(value) return str(value)
elif isinstance(value, type): if isinstance(value, type):
return { return {
"type": value.__name__, "type": value.__name__,
"module": value.__module__, "module": value.__module__,
@ -126,6 +129,6 @@ def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]:
final_dict = {} final_dict = {}
for key, value in source.items(): for key, value in source.items():
new_value = sanitize_item(value) new_value = sanitize_item(value)
if new_value: if new_value is not ...:
final_dict[key] = new_value final_dict[key] = new_value
return final_dict return final_dict