: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
fix(messaging): let personal API keys read the suppression list (#80759)
Sean Mallia54e2b6222c1a
diff --git a/products/messaging/backend/api/message_suppression.py b/products/messaging/backend/api/message_suppression.pyindex 18a6648e899d..4752730b6ecd 100644--- a/products/messaging/backend/api/message_suppression.py+++ b/products/messaging/backend/api/message_suppression.py@@ -95,8 +95,12 @@ class MessageSuppressionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):"""scope_object = "hog_flow"- # Custom actions must declare their write status so TeamAndOrgViewSetMixin's AccessControlPermission- # checks hog_flow:write on the mutating endpoints; the default 'suppressions' list stays a read.+ # Every custom @action must be listed here or in scope_object_read_actions — APIScopePermission+ # maps the action name to a required scope, and an unlisted action resolves to no scope at all,+ # which rejects every personal API key and OAuth token with "does not support personal API key+ # access". Declaring either attribute replaces the permission class default rather than+ # extending it, so 'list'/'retrieve' are repeated below.+ scope_object_read_actions = ["list", "retrieve", "suppressions"]scope_object_write_actions = ["add_suppression", "remove_suppression"]serializer_class = _FallbackSerializerdiff --git a/products/messaging/backend/api/test/test_message_suppression.py b/products/messaging/backend/api/test/test_message_suppression.pyindex dff6728e39b3..c1bbd3eab6db 100644--- a/products/messaging/backend/api/test/test_message_suppression.py+++ b/products/messaging/backend/api/test/test_message_suppression.py@@ -2,6 +2,11 @@ from posthog.test.base import APIBaseTestfrom django.test import SimpleTestCase+from parameterized import parameterized++from posthog.models.personal_api_key import PersonalAPIKey+from posthog.models.utils import generate_random_token_personal, hash_key_value+from products.messaging.backend.api.message_suppression import MessageSuppressionViewSetfrom products.messaging.backend.models.message_suppression import MessageSuppression, SuppressionSource@@ -24,6 +29,38 @@ class TestMessageSuppressionViewSetScope(SimpleTestCase):}+class TestSuppressionListPersonalAPIKeyAccess(APIBaseTest):+ """+ Guards against the read action losing its scope mapping. `suppressions` is a custom @action, so+ APIScopePermission only resolves a required scope for it if it is named in+ scope_object_read_actions — otherwise it resolves none and rejects every token with "does not+ support personal API key access", while session auth (which skips scope checks) still works.+ """++ def _key(self, scopes: list[str]) -> str:+ value = generate_random_token_personal()+ PersonalAPIKey.objects.create(+ label="Test",+ user=self.user,+ secure_value=hash_key_value(value),+ scopes=scopes,+ )+ return value++ @parameterized.expand([("hog_flow:read", 200), ("insight:read", 403)])+ def test_suppressions_read_requires_hog_flow_read_scope(self, scope: str, expected_status: int) -> None:+ self.client.logout()++ response = self.client.get(+ f"/api/projects/{self.team.id}/messaging_suppressions/suppressions/",+ HTTP_AUTHORIZATION=f"Bearer {self._key([scope])}",+ )++ assert response.status_code == expected_status, response.json()+ if expected_status == 200:+ assert set(response.json()) == {"count", "next", "previous", "results"}++class TestRemoveSuppressionResetsSource(APIBaseTest):"""Guards against a regression where remove_suppression keeps source='MANUAL' on the removed row.