Open source video conferencing app powered by LiveKit. Built with Django and React.
🔒️(summary) hide sensitive Celery task details
Florent Chehab58bf9a7d82d6
diff --git a/src/summary/summary/core/celery_worker.py b/src/summary/summary/core/celery_worker.pyindex c40562c3..8f225033 100644--- a/src/summary/summary/core/celery_worker.py+++ b/src/summary/summary/core/celery_worker.py@@ -3,13 +3,14 @@# ruff: noqa: PLR0913import json+import loggingimport timefrom datetime import datetime, timezonefrom typing import Anyfrom urllib.parse import urljoinimport requests-from celery import Celery, signals+from celery import Celery, Task, signalsfrom celery.utils.log import get_task_loggerfrom openai.types.audio import Transcriptionfrom pydantic import SecretStr, ValidationError@@ -72,8 +73,18 @@ def _reveal(secret: SecretStr | None) -> str | None:return secret.get_secret_value() if secret is not None else None+class PrivateTask(Task):+ """Hide task arguments in Celery logs and monitoring events."""++ def apply_async(self, args=None, kwargs=None, **options):+ """Publish with redacted display representations."""+ options.update(argsrepr="[Filtered]", kwargsrepr="[Filtered]")+ return super().apply_async(args=args, kwargs=kwargs, **options)++celery = Celery(__name__,+ task_cls=PrivateTask,broker=settings.celery_broker_url,backend=settings.celery_result_backend,broker_connection_retry_on_startup=True,@@ -85,10 +96,28 @@ celery = Celery(celery.config_from_object("summary.core.celery_config")+class CeleryLogPrivacyFilter(logging.Filter):+ """Prevent Celery trace logs from serializing task results or exceptions."""++ def filter(self, record):+ """Keep task identity and state without upstream values."""+ if record.levelno < logging.INFO:+ return True+ data = getattr(record, "data", None)+ if isinstance(data, dict):+ record.msg = "Task %s[%s] finished or retried (details omitted)"+ record.args = (data.get("name", "unknown"), data.get("id", "unknown"))+ record.data = {}+ record.exc_info = None+ record.exc_text = None+ return True++@signals.celeryd_init.connectdef init_celery_sentry(**_kwargs):"""Initialize Sentry in the Celery worker."""init_sentry()+ logging.getLogger("celery.app.trace").addFilter(CeleryLogPrivacyFilter())file_service = FileService()diff --git a/src/summary/tests/unit/test_privacy.py b/src/summary/tests/unit/test_privacy.pyindex 6699ad51..8b19dd11 100644--- a/src/summary/tests/unit/test_privacy.py+++ b/src/summary/tests/unit/test_privacy.py@@ -90,6 +90,33 @@ def test_analytics_failure_does_not_chain_payload(monkeypatch):assert exc.value.__suppress_context__[email protected]("log_level", [logging.DEBUG, logging.INFO, logging.ERROR])+def test_celery_log_filter_respects_log_level(log_level):+ """Celery trace values remain available in DEBUG only."""+ record = logging.LogRecord(+ "celery.app.trace", log_level, "", 0, "%s", (CANARY,), None+ )+ record.data = {"name": "task", "id": "job", "return_value": URL}+ assert celery_worker.CeleryLogPrivacyFilter().filter(record)+ if log_level == logging.DEBUG:+ assert CANARY in record.getMessage()+ assert record.data["return_value"] == URL+ else:+ assert CANARY not in record.getMessage()+ assert record.data == {}+++def test_task_arguments_have_private_display_representations(monkeypatch):+ """The actual task arguments are delivered but their event repr is hidden."""+ publish = Mock()+ monkeypatch.setattr(celery_worker.Task, "apply_async", publish)+ task = celery_worker.PrivateTask()+ task.apply_async(args=[CANARY], kwargs={"url": URL})+ assert publish.call_args.kwargs["args"] == [CANARY]+ assert publish.call_args.kwargs["argsrepr"] == "[Filtered]"+ assert publish.call_args.kwargs["kwargsrepr"] == "[Filtered]"++def test_analytics_source_url_removes_signature():"""Task analytics keeps the source path without credentials or signature."""from datetime import datetime, timezone # noqa: PLC0415