: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(conversations): rate-limit the support Slack OAuth callback (#67028)
Abhischekbe9e9c9c2170
diff --git a/posthog/rate_limit.py b/posthog/rate_limit.pyindex a4e8eb831cf2..289d23cddd35 100644--- a/posthog/rate_limit.py+++ b/posthog/rate_limit.py@@ -1209,3 +1209,12 @@ class TeamsOAuthCallbackThrottle(IPThrottle):scope = "teams_oauth_callback"rate = "30/minute"+++class SupportSlackOAuthCallbackThrottle(IPThrottle):+ """+ Rate limit the unauthenticated support Slack OAuth callback endpoint by IP.+ """++ scope = "support_slack_oauth_callback"+ rate = "30/minute"diff --git a/products/conversations/backend/api/slack_oauth.py b/products/conversations/backend/api/slack_oauth.pyindex 1aeeb3c0e22d..66f4fc259cc1 100644--- a/products/conversations/backend/api/slack_oauth.py+++ b/products/conversations/backend/api/slack_oauth.py@@ -17,6 +17,7 @@ from posthog.models.instance_setting import get_instance_settingsfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.team.team import Teamfrom posthog.models.user import User+from posthog.rate_limit import SupportSlackOAuthCallbackThrottlefrom products.conversations.backend.models import TeamConversationsSlackConfigfrom products.conversations.backend.permissions import IsConversationsAdmin@@ -123,6 +124,12 @@ class SupportSlackDisconnectView(APIView):@csrf_exemptdef support_slack_oauth_callback(request: HttpRequest) -> HttpResponse:+ # IP throttle in front of any Slack token exchange — otherwise a stranger+ # can loop a valid `state` param and force us to make outbound POSTs.+ throttle = SupportSlackOAuthCallbackThrottle()+ if not throttle.allow_request(Request(request), view=None): # type: ignore[arg-type]+ return JsonResponse({"error": "Too Many Requests"}, status=429)+request_user = getattr(request, "user", None)request_user_id = getattr(request_user, "id", None)if not isinstance(request_user, User) or not isinstance(request_user_id, int):diff --git a/products/conversations/backend/api/tests/test_slack_oauth.py b/products/conversations/backend/api/tests/test_slack_oauth.pynew file mode 100644index 000000000000..40cdea8c3579--- /dev/null+++ b/products/conversations/backend/api/tests/test_slack_oauth.py@@ -0,0 +1,19 @@+from unittest.mock import patch++from django.test import RequestFactory, SimpleTestCase++from posthog.rate_limit import SupportSlackOAuthCallbackThrottle++from products.conversations.backend.api.slack_oauth import support_slack_oauth_callback+++class TestSupportSlackOAuthCallbackThrottle(SimpleTestCase):+ @patch("products.conversations.backend.api.slack_oauth.requests.post")+ @patch.object(SupportSlackOAuthCallbackThrottle, "allow_request", return_value=False)+ def test_throttled_request_returns_429_before_token_exchange(self, _mock_allow, mock_post):+ request = RequestFactory().get("/api/conversations/v1/slack/callback", {"state": "x", "code": "y"})++ response = support_slack_oauth_callback(request)++ assert response.status_code == 429+ mock_post.assert_not_called()