:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(messaging): name the missing fields in push rejection logs (#83501)
Harley Alexandera373a56d38aa
diff --git a/products/messaging/backend/api/push_subscriptions.py b/products/messaging/backend/api/push_subscriptions.pyindex d212f440af96..cccc4c981cdb 100644--- a/products/messaging/backend/api/push_subscriptions.py+++ b/products/messaging/backend/api/push_subscriptions.py@@ -100,6 +100,7 @@ def _rejection_response(status_code: int,team_id: int | None = None,app_id: str | None = None,+ detail: str | None = None,exc_info: bool = False,) -> HttpResponse:# request.method is an arbitrary attacker-controlled token on the method_not_allowed path (any@@ -117,6 +118,7 @@ def _rejection_response(method=request.method,team_id=team_id,app_id=app_id,+ detail=detail,exc_info=exc_info,)return cors_response(@@ -215,6 +217,12 @@ def push_subscriptions(request: Request):if not value or not isinstance(value, str)]if missing_fields:+ # absent vs empty vs invalid separates an SDK that never sends the field (contract drift)+ # from a client bridge passing an empty or mistyped value: they need different fixes.+ field_detail = ",".join(+ f"{field_name}:{'absent' if field_name not in data else 'empty' if data[field_name] == '' else 'invalid'}"+ for field_name in missing_fields+ )return _rejection_response(request,f"Missing required fields: {', '.join(missing_fields)}.",@@ -222,6 +230,7 @@ def push_subscriptions(request: Request):code="missing_fields",status_code=status.HTTP_400_BAD_REQUEST,team_id=team.id,+ detail=field_detail,)assert isinstance(distinct_id, str)diff --git a/products/messaging/backend/api/test/test_push_subscriptions.py b/products/messaging/backend/api/test/test_push_subscriptions.pyindex 88685f419ade..bf82c5380ec9 100644--- a/products/messaging/backend/api/test/test_push_subscriptions.py+++ b/products/messaging/backend/api/test/test_push_subscriptions.py@@ -10,6 +10,7 @@ from cryptography.hazmat.primitives import serializationfrom cryptography.hazmat.primitives.asymmetric import ecfrom parameterized import parameterizedfrom rest_framework import status+from structlog.testing import capture_logsfrom posthog.models.integration import Integrationfrom posthog.models.team.team import Team@@ -489,6 +490,23 @@ class TestPushSubscriptionsAPI(BaseTest):assert response.json()["code"] == codeassert counter._value.get() == before + 1+ @parameterized.expand(+ [+ ("empty_value", {"device_token": ""}, "device_token:empty"),+ ("absent_key", {}, "device_token:absent"),+ ]+ )+ def test_missing_fields_rejection_logs_field_detail(self, _name: str, extra: dict, expected_detail: str):+ payload = {"distinct_id": "user-1", "platform": "android", "app_id": "my-firebase-project", **extra}++ with capture_logs() as logs:+ response = self._post(payload)++ assert response.status_code == status.HTTP_400_BAD_REQUEST+ rejected = [entry for entry in logs if entry["event"] == "push_subscription_rejected"]+ assert len(rejected) == 1+ assert rejected[0]["detail"] == expected_detail+def test_unsupported_method_collapses_counter_label(self):counter = PUSH_SUBSCRIPTION_REJECTION_COUNTER.labels(code="method_not_allowed", method="other")before = counter._value.get()