: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(api): gate PromQL/LogQL proxy endpoints behind a shared feature flag (#102110)
Frank Hamandfee286881671
diff --git a/posthog/api/snuffle_proxy.py b/posthog/api/snuffle_proxy.pyindex f63272d41f2e..440c70d867cf 100644--- a/posthog/api/snuffle_proxy.py+++ b/posthog/api/snuffle_proxy.py@@ -24,11 +24,16 @@ from rest_framework.parsers import FormParserfrom rest_framework.request import Requestfrom posthog.api.routing import TeamAndOrgViewSetMixin+from posthog.permissions import PostHogFeatureFlagPermissionfrom posthog.rate_limit import ClickHouseBurstRateThrottle, ClickHouseSustainedRateThrottlefrom posthog.security.outbound_proxy import internal_requestslogger = structlog.get_logger(__name__)+# This private-alpha flag gates all Snuffle proxy endpoints.+# New subclasses use the flag by default.+SNUFFLE_API_FEATURE_FLAG = "logs-metrics-snuffle-api"+TEAM_ID_HEADER = "X-Team-ID"# Snuffle also accepts the tenant as a `team_id` parameter, with lower precedence than the header;# strip it so the forwarded request only ever names the team from the URL. PostHog accepts the@@ -63,6 +68,8 @@ class SnuffleProxyViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):allowed_paths: ClassVar[tuple[re.Pattern[str], ...]]scope_object_read_actions = ["proxy"]+ posthog_feature_flag = SNUFFLE_API_FEATURE_FLAG+ permission_classes = [PostHogFeatureFlagPermission]# The Prometheus and Loki APIs take POST bodies as form fields, the same shape as the query string.parser_classes = [FormParser]throttle_classes = [ClickHouseBurstRateThrottle, ClickHouseSustainedRateThrottle]diff --git a/products/logs/backend/test/test_loki_api.py b/products/logs/backend/test/test_loki_api.pyindex 1090d6396a3d..43d27c34cbfc 100644--- a/products/logs/backend/test/test_loki_api.py+++ b/products/logs/backend/test/test_loki_api.py@@ -34,6 +34,11 @@ class TestLokiQueryApi(APIBaseTest):patcher = patch("posthog.api.snuffle_proxy.internal_requests.request", return_value=_upstream())self.request_mock = patcher.start()self.addCleanup(patcher.stop)+ # Enable the Snuffle flag for the Loki proxy tests.+ # The gate test sets the flag to False.+ ff_patcher = patch("posthoganalytics.feature_enabled", return_value=True)+ ff_patcher.start()+ self.addCleanup(ff_patcher.stop)@parameterized.expand([("bare", "query_range"), ("trailing_slash", "query_range/")])def test_get_is_forwarded_with_team_header_and_credentials(self, _name: str, path: str):@@ -145,6 +150,13 @@ class TestLokiQueryApi(APIBaseTest):assert response.status_code == expected_statusassert response.json()["status"] == "error"+ def test_snuffle_flag_gates_the_api(self):+ with patch("posthoganalytics.feature_enabled", return_value=False):+ response = self.client.get(f"{self.base}/labels")++ assert response.status_code == status.HTTP_403_FORBIDDEN+ self.request_mock.assert_not_called()+@parameterized.expand([("logs_read", ["logs:read"], status.HTTP_200_OK),diff --git a/products/metrics/backend/presentation/prometheus_api.py b/products/metrics/backend/presentation/prometheus_api.pyindex 47b06b7bcb6c..fa5f7d539e8a 100644--- a/products/metrics/backend/presentation/prometheus_api.py+++ b/products/metrics/backend/presentation/prometheus_api.py@@ -1,9 +1,6 @@import refrom posthog.api.snuffle_proxy import SnuffleProxyViewSet-from posthog.permissions import PostHogFeatureFlagPermission--from products.metrics.backend.facade.contracts import METRICS_FEATURE_FLAG_LABEL_NAME = r"[A-Za-z_][A-Za-z0-9_]*"@@ -17,9 +14,6 @@ class PrometheusQueryViewSet(SnuffleProxyViewSet):"""scope_object = "metrics"- # Same private-alpha gate as MetricsViewSet.- posthog_feature_flag = METRICS_FEATURE_FLAG- permission_classes = [PostHogFeatureFlagPermission]upstream_prefix = "/api/v1"allowed_paths = (re.compile(r"query"),diff --git a/products/metrics/backend/tests/conftest.py b/products/metrics/backend/tests/conftest.pyindex f1f7afba5c31..477d97eedeb2 100644--- a/products/metrics/backend/tests/conftest.py+++ b/products/metrics/backend/tests/conftest.py@@ -3,18 +3,20 @@ from collections.abc import Iteratorimport pytestfrom unittest.mock import patch+from posthog.api.snuffle_proxy import SNUFFLE_API_FEATURE_FLAG+from products.metrics.backend.facade.contracts import METRICS_FUNDAMENTALS_FEATURE_FLAG@pytest.fixture(autouse=True)def enable_metrics_feature_flag() -> Iterator[None]:- # MetricsViewSet is gated behind the `metrics` feature flag (private alpha), so every- # endpoint test in this package would 403 without it. The explain action carries a- # second flag on top. Enable both and leave all others at their default (False),- # matching production for alpha teams. The dedicated gate tests override this to- # assert the 403.+ # Enable the flags needed by the metrics endpoint tests.+ # MetricsViewSet needs `metrics`.+ # The explain action needs `METRICS_FUNDAMENTALS_FEATURE_FLAG`.+ # The Prometheus proxy needs `SNUFFLE_API_FEATURE_FLAG`.+ # Gate tests set their flag to False.def _feature_enabled(flag_key: str, *args: object, **kwargs: object) -> bool:- return flag_key in ("metrics", METRICS_FUNDAMENTALS_FEATURE_FLAG)+ return flag_key in ("metrics", METRICS_FUNDAMENTALS_FEATURE_FLAG, SNUFFLE_API_FEATURE_FLAG)with patch("posthoganalytics.feature_enabled", side_effect=_feature_enabled):yielddiff --git a/products/metrics/backend/tests/test_prometheus_api.py b/products/metrics/backend/tests/test_prometheus_api.pyindex 3feb2b8918b6..048f6831bb92 100644--- a/products/metrics/backend/tests/test_prometheus_api.py+++ b/products/metrics/backend/tests/test_prometheus_api.py@@ -62,7 +62,7 @@ class TestPrometheusQueryApi(APIBaseTest):assert response.status_code == status.HTTP_404_NOT_FOUNDself.request_mock.assert_not_called()- def test_metrics_flag_gates_the_api(self):+ def test_snuffle_flag_gates_the_api(self):with patch("posthoganalytics.feature_enabled", return_value=False):response = self.client.get(f"{self.base}/query", {"query": "up"})