Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
exception-reporter: Add ZulipExceptionReporterFilter with POST masking and settings removal.
Raj Vishwakarma741ea25df544
diff --git a/tools/test-backend b/tools/test-backendindex c0c3560d038..829f74ffe57 100755--- a/tools/test-backend+++ b/tools/test-backend@@ -81,7 +81,6 @@ not_yet_fully_covered = ["zerver/worker/queue_processors.py","zerver/worker/test.py",# Other lib files that ideally would coverage, but aren't sorted- "zerver/filters.py","zerver/middleware.py","zerver/lib/bot_lib.py","zerver/lib/camo.py",diff --git a/zerver/filters.py b/zerver/filters.pyindex 68f42c6490f..b307a42af6f 100644--- a/zerver/filters.py+++ b/zerver/filters.py@@ -1,6 +1,7 @@import refrom typing import Any+from django.conf import settingsfrom django.http import HttpRequestfrom django.views.debug import SafeExceptionReporterFilterfrom typing_extensions import override@@ -14,8 +15,8 @@ class ZulipExceptionReporterFilter(SafeExceptionReporterFilter):@overridedef get_post_parameters(self, request: HttpRequest | None) -> dict[str, Any]:- post_data = SafeExceptionReporterFilter.get_post_parameters(self, request)- assert isinstance(post_data, dict)+ post_data = super().get_post_parameters(request)+filtered_post = post_data.copy()filtered_vars = ["content",@@ -37,3 +38,19 @@ class ZulipExceptionReporterFilter(SafeExceptionReporterFilter):if var in filtered_post:filtered_post[var] = "**********"return filtered_post++ # We do not include Django settings in exception reports at all.+ # Settings can contain secrets and other environment-specific+ # details, and we rely on other mechanisms to inspect them when+ # debugging. Returning an empty dict here tells ExceptionReporter+ # to omit settings-related context entirely.+ @override+ def get_safe_settings(self) -> dict[str, Any]:+ # In production, we do not include Django settings in exception reports at all.+ # Settings can contain secrets and other environment-specific+ # details.+ if settings.PRODUCTION:+ return {}++ # In development, return the safe settings (secrets masked by hidden_settings regex)+ return super().get_safe_settings()diff --git a/zerver/tests/test_exception_filter.py b/zerver/tests/test_exception_filter.pynew file mode 100644index 00000000000..8f1c2b9481c--- /dev/null+++ b/zerver/tests/test_exception_filter.py@@ -0,0 +1,104 @@+from __future__ import annotations++import sys++from django.test import RequestFactory, override_settings+from django.views.debug import ExceptionReporter++from zerver.filters import ZulipExceptionReporterFilter+from zerver.lib.test_classes import ZulipTestCase+++class TestExceptionFilter(ZulipTestCase):+ def test_zulip_filter_masks_sensitive_post_data(self) -> None:+ """+ Verifies that specific sensitive POST parameters are masked.+ """+ rf = RequestFactory()+ request = rf.post(+ "/test",+ {+ "password": "sneaky",+ "api_key": "abc123",+ "content": "secret msg",+ "realm_counts": "private",+ "installation_counts": "private",+ "normal_field": "safe",+ },+ )++ filt = ZulipExceptionReporterFilter()+ cleaned = filt.get_post_parameters(request)++ for var in [+ "password",+ "api_key",+ "content",+ "realm_counts",+ "installation_counts",+ ]:+ self.assertEqual(cleaned.get(var), "**********")++ self.assertEqual(cleaned.get("normal_field"), "safe")++ def test_exception_reporter_returns_settings_in_dev(self) -> None:+ """+ In non-production, settings should be present and non-empty.+ """+ rf = RequestFactory()+ request = rf.get("/")++ try:+ raise ValueError("test error")+ except ValueError:+ exc_type, exc_value, tb = sys.exc_info()++ reporter = ExceptionReporter(+ request,+ exc_type,+ exc_value,+ tb,+ is_email=True,+ )+ reporter.filter = ZulipExceptionReporterFilter()++ data = reporter.get_traceback_data()++ self.assertIn("settings", data)+ self.assertIsInstance(data["settings"], dict)+ self.assertNotEqual(data["settings"], {})++ @override_settings(+ PRODUCTION=True,+ DEPLOY_ROOT="/home/zulip/deployments/2024-01-01-00-00-00",+ )+ def test_exception_reporter_omits_settings_in_production(self) -> None:+ """+ In production, settings must be omitted (empty dict).+ """+ rf = RequestFactory()+ request = rf.get("/")++ try:+ raise RuntimeError("production error")+ except RuntimeError:+ exc_type, exc_value, tb = sys.exc_info()++ reporter = ExceptionReporter(+ request,+ exc_type,+ exc_value,+ tb,+ is_email=True,+ )+ reporter.filter = ZulipExceptionReporterFilter()++ data = reporter.get_traceback_data()++ self.assertEqual(data.get("settings"), {})++ html = reporter.get_traceback_html()++ self.assertNotIn("LANGUAGE_CODE", html)+ self.assertNotIn("SECRET_KEY", html)+ self.assertNotIn("DATABASES", html)