Skip to content

: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.

fix(signals): mark sandbox trial API calls as private (#111546)

Alex Lebedevd92959e206df
diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.py
index 80fdcfd23ad3..489746fe377e 100644
--- a/posthog/api/test/test_authentication.py
+++ b/posthog/api/test/test_authentication.py
@@ -50,7 +50,7 @@ from posthog.auth import (
WidgetAuthentication,
_extract_phs_token,
)
-from posthog.clickhouse.query_tagging import AccessMethod
+from posthog.clickhouse.query_tagging import AccessMethod, get_query_tags, tags_context
from posthog.helpers.user_devices import (
KNOWN_DEVICE_COOKIE,
build_known_device_cookie_value,
@@ -2746,6 +2746,36 @@ class TestProjectSecretAPIKeyAuthentication(APIBaseTest):
self.assertGreater(self.psak.last_used_at, old + timedelta(hours=1))
+class TestScoutPrivateCaptureAuthentication(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("ordinary", "query:read", True, False),
+ ("unbound", "scout_experiment_internal:read", False, False),
+ ("trial", "query:read scout_experiment_internal:read", True, True),
+ ]
+ )
+ def test_capture_policy_requires_the_bound_scope(self, _name: str, scope: str, bound: bool, private: bool) -> None:
+ token = OAuthAccessToken(
+ user=User(id=1, current_team_id=2),
+ scope=scope,
+ sandbox_task_id=uuid.uuid4() if bound else None,
+ )
+ request = Request(
+ APIRequestFactory().get(
+ "/?is_scout_experiment=true",
+ HTTP_AUTHORIZATION="Bearer pha_synthetic",
+ HTTP_X_POSTHOG_SUPPRESS_ANALYTICS="true",
+ )
+ )
+ with (
+ tags_context(is_scout_experiment=False),
+ patch.object(OAuthAccessTokenAuthentication, "_validate_token", return_value=token),
+ patch("posthog.auth.activity_storage.is_request_scoped", return_value=False),
+ ):
+ self.assertIsNotNone(OAuthAccessTokenAuthentication().authenticate(request))
+ self.assertEqual(get_query_tags().is_scout_experiment, private)
+
+
class TestOAuthAccessTokenAuthentication(APIBaseTest):
def setUp(self):
super().setUp()
diff --git a/posthog/auth.py b/posthog/auth.py
index af2f0c24e7b0..87e065765f75 100644
--- a/posthog/auth.py
+++ b/posthog/auth.py
@@ -28,7 +28,7 @@ from rest_framework.request import Request
from webauthn.helpers import base64url_to_bytes
from zxcvbn import zxcvbn
-from posthog.clickhouse.query_tagging import AccessMethod, tag_authentication
+from posthog.clickhouse.query_tagging import AccessMethod, tag_authentication, tag_queries
from posthog.constants import AvailableFeature
from posthog.exceptions_capture import capture_exception
from posthog.helpers.two_factor_session import enforce_two_factor
@@ -1009,6 +1009,8 @@ class OAuthAccessTokenAuthentication(ActivityCredentialMixin, authentication.Bas
team_id=user.current_team_id,
access_method=AccessMethod.OAUTH,
)
+ if access_token.sandbox_task_id is not None and "scout_experiment_internal:read" in access_token.scope.split():
+ tag_queries(is_scout_experiment=True)
# ActivityLoggingMiddleware only captures session-authenticated users (it runs
# before DRF auth), so signal-driven activity logging would otherwise record