Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Scrub the database hostname from Sentry events (#5592)
Felipe Santosb9037e4cdd34
diff --git a/api/api/utils/sentry.py b/api/api/utils/sentry.pynew file mode 100644index 000000000..a96b955a0--- /dev/null+++ b/api/api/utils/sentry.py@@ -0,0 +1,58 @@+"""+Utilities for scrubbing sensitive infrastructure values out of Sentry events.++``send_default_pii=False`` and Sentry's server-side scrubbing only cover known+PII fields by key. Infrastructure hostnames such as the database DNS name can+still reach Sentry as free text — inside breadcrumbs, span descriptions or+exception messages — where key-based scrubbing never sees them. This redacts+those known-sensitive values from an event before it leaves the process.++See https://github.com/WordPress/openverse/issues/670.+"""++from collections.abc import Callable, Iterable+from typing import Any+++FILTERED = "[Filtered]"++# Hostnames that are not sensitive and are too broad to be worth redacting.+IGNORED_HOSTS = frozenset({"", "localhost", "127.0.0.1", "::1"})+++def _redact(value: Any, secrets: list[str]) -> Any:+ if isinstance(value, str):+ for secret in secrets:+ value = value.replace(secret, FILTERED)+ return value+ if isinstance(value, dict):+ return {key: _redact(item, secrets) for key, item in value.items()}+ if isinstance(value, list):+ return [_redact(item, secrets) for item in value]+ if isinstance(value, tuple):+ return tuple(_redact(item, secrets) for item in value)+ return value+++def make_sensitive_value_scrubber(hostnames: Iterable[str]) -> Callable:+ """+ Build a Sentry ``before_send`` / ``before_send_transaction`` hook that+ replaces the given hostnames wherever they appear, as substrings, anywhere+ in the event.++ Longer hostnames are redacted first so that a host which is a substring of+ another (e.g. ``db.example.com`` inside ``replica.db.example.com``) does not+ leave a partial value behind.+ """+ secrets = sorted(+ {host for host in hostnames if host not in IGNORED_HOSTS},+ key=len,+ reverse=True,+ )++ def before_send(event: Any, hint: Any = None) -> Any:+ if not secrets:+ return event+ return _redact(event, secrets)++ return before_senddiff --git a/api/conf/settings/sentry.py b/api/conf/settings/sentry.pyindex 39c61c256..2a16e4658 100644--- a/api/conf/settings/sentry.py+++ b/api/conf/settings/sentry.py@@ -4,7 +4,9 @@ from psycopg_pool import PoolTimeoutfrom sentry_sdk.integrations.django import DjangoIntegrationfrom sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger+from api.utils.sentry import make_sensitive_value_scrubberfrom conf.settings.base import ENVIRONMENT+from conf.settings.databases import DATABASESfrom conf.settings.security import DEBUG@@ -46,6 +48,11 @@ def _is_pool_timeout(exc: BaseException | None) -> bool:return False+# Redact infrastructure hostnames (e.g. the database DNS name) that can leak+# into events as free text, where key-based scrubbing never sees them.+scrub_sensitive_values = make_sensitive_value_scrubber([DATABASES["default"]["HOST"]])++def before_send(event, hint):"""Drop events that only report infrastructure noise.@@ -55,6 +62,8 @@ def before_send(event, hint):- uvloop's "Fatal error on SSL protocol" log: the remote end of an outboundHTTPS request closed the connection. The request itself succeeds, butasyncio logs this at ERROR, which ``SentryProcessor`` turns into an event.++ Events that are kept have sensitive values scrubbed from them."""exc_info = hint.get("exc_info")if exc_info and _is_pool_timeout(exc_info[1]):@@ -66,7 +75,7 @@ def before_send(event, hint):):return None- return event+ return scrub_sensitive_values(event, hint)INTEGRATIONS = [@@ -88,6 +97,7 @@ if not DEBUG and SENTRY_DSN:before_send=before_send,send_default_pii=False,environment=ENVIRONMENT,+ before_send_transaction=scrub_sensitive_values,)# ALLOW_HOSTS is correctly configured so ignore this to preventdiff --git a/api/test/unit/configuration/test_sentry.py b/api/test/unit/configuration/test_sentry.pyindex 78cd84c48..292b7e744 100644--- a/api/test/unit/configuration/test_sentry.py+++ b/api/test/unit/configuration/test_sentry.py@@ -5,6 +5,7 @@ from django.db.utils import OperationalErrorimport pytestfrom psycopg_pool import PoolTimeout+from api.utils.sentry import FILTERED, make_sensitive_value_scrubberfrom conf.settings.sentry import before_send@@ -71,4 +72,14 @@ def test_drops_noise(event, raise_exc):def test_keeps_other_events(event, raise_exc):hint = {"exc_info": _exc_info(raise_exc)} if raise_exc else {}- assert before_send(event, hint) is event+ assert before_send(event, hint) == event+++def test_scrubs_kept_events(monkeypatch):+ monkeypatch.setattr(+ "conf.settings.sentry.scrub_sensitive_values",+ make_sensitive_value_scrubber(["db.internal.example.com"]),+ )+ event = {"message": "could not connect to db.internal.example.com"}++ assert before_send(event, {}) == {"message": f"could not connect to {FILTERED}"}diff --git a/api/test/unit/utils/test_sentry_scrubber.py b/api/test/unit/utils/test_sentry_scrubber.pynew file mode 100644index 000000000..11e2e89ca--- /dev/null+++ b/api/test/unit/utils/test_sentry_scrubber.py@@ -0,0 +1,48 @@+import pytest++from api.utils.sentry import FILTERED, make_sensitive_value_scrubber+++def test_redacts_hostname_anywhere_in_a_nested_event():+ scrub = make_sensitive_value_scrubber(["db.internal.example.com"])+ event = {+ "message": "could not connect to db.internal.example.com:5432",+ "breadcrumbs": {+ "values": [{"message": "SELECT 1 -- db.internal.example.com"}],+ },+ "extra": {"hosts": ("db.internal.example.com", "unrelated-host")},+ }++ scrubbed = scrub(event)++ assert "db.internal.example.com" not in repr(scrubbed)+ assert FILTERED in scrubbed["message"]+ assert scrubbed["breadcrumbs"]["values"][0]["message"].endswith(FILTERED)+ # unrelated values are left untouched (and tuples stay tuples)+ assert scrubbed["extra"]["hosts"] == (FILTERED, "unrelated-host")++[email protected]("hostname", ["", "localhost", "127.0.0.1", "::1"])+def test_ignores_non_sensitive_default_hosts(hostname):+ scrub = make_sensitive_value_scrubber([hostname])+ event = {"message": f"connected to {hostname}"}++ # nothing to redact, so the event is returned unchanged+ assert scrub(event) is event+++def test_returns_event_unchanged_when_no_hostnames_given():+ scrub = make_sensitive_value_scrubber([])+ event = {"message": "hello"}++ assert scrub(event) is event+++def test_redacts_longer_overlapping_hostname_first():+ scrub = make_sensitive_value_scrubber(["db.example.com", "replica.db.example.com"])+ event = {"message": "replica.db.example.com and db.example.com"}++ scrubbed = scrub(event)++ assert "example.com" not in scrubbed["message"]+ assert scrubbed["message"] == f"{FILTERED} and {FILTERED}"