: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(data-warehouse): detect self-driving marker in proxied MCP header (#71907)
Tom Owers98c9b9e4a75f
diff --git a/posthog/event_usage.py b/posthog/event_usage.pyindex 0ae3d86687c6..1d7fe158e37d 100644--- a/posthog/event_usage.py+++ b/posthog/event_usage.py@@ -355,11 +355,16 @@ def is_wizard_self_driving_program(request) -> bool:can only tell they're "the wizard". The self-driving program additionally tags itsUA with a `program: self-driving` marker, letting callers attribute its work apartfrom other wizard runs.++ When the request is proxied through the PostHog MCP server, that server overwrites+ `User-Agent` with its own token and forwards the wizard's original UA — marker+ included — in `X-Posthog-Mcp-User-Agent`. Inspect both so the marker is found whether+ the wizard called us directly or via the MCP server."""user_agent = request.headers.get("user-agent", "") or ""- if not isinstance(user_agent, str):- return False- return "posthog/wizard" in user_agent and bool(_WIZARD_SELF_DRIVING_PROGRAM_RE.search(user_agent))+ mcp_user_agent = request.headers.get("X-Posthog-Mcp-User-Agent", "") or ""+ combined = "\n".join(part for part in (user_agent, mcp_user_agent) if isinstance(part, str))+ return "posthog/wizard" in combined and bool(_WIZARD_SELF_DRIVING_PROGRAM_RE.search(combined))MAX_HEADER_VALUE_LENGTH = 1000diff --git a/posthog/test/test_event_usage.py b/posthog/test/test_event_usage.pyindex 6fdd3fcec66c..041f7c500928 100644--- a/posthog/test/test_event_usage.py+++ b/posthog/test/test_event_usage.py@@ -12,6 +12,7 @@ from posthog.event_usage import (EventSource,get_event_source,get_mcp_properties,+ is_wizard_self_driving_program,report_user_action,sanitize_header_value,)@@ -352,6 +353,48 @@ class TestGetEventSource(BaseTest):assert get_event_source(request) == expected+class TestIsWizardSelfDrivingProgram(BaseTest):+ @parameterized.expand(+ [+ # Direct wizard → Django: the marker rides on the User-Agent itself.+ ("direct_with_marker", "posthog/wizard; version: 2.44.0; program: self-driving-setup", None, True),+ ("direct_without_marker", "posthog/wizard; version: 2.44.0", None, False),+ ("direct_other_program", "posthog/wizard; version: 2.44.0; program: revenue-analytics", None, False),+ # Proxied via the MCP server: it overwrites User-Agent with its own token and forwards+ # the wizard's real UA (marker included) in X-Posthog-Mcp-User-Agent. The marker must+ # still be found there — this is the case that previously went undetected.+ (+ "proxied_with_marker",+ "posthog/mcp-server; version: 1.0.0; for posthog/wizard",+ "posthog/wizard; version: 2.45.0; program: self-driving-setup",+ True,+ ),+ (+ "proxied_without_marker",+ "posthog/mcp-server; version: 1.0.0; for posthog/wizard",+ "posthog/wizard; version: 2.45.0",+ False,+ ),+ (+ "proxied_other_program",+ "posthog/mcp-server; version: 1.0.0; for posthog/wizard",+ "posthog/wizard; version: 2.45.0; program: web-analytics-doctor",+ False,+ ),+ # A stray marker with no wizard token anywhere must not qualify.+ ("marker_without_wizard", "some-agent/1.0; program: self-driving", None, False),+ ("no_signals", "some-random-agent/1.0", None, False),+ ]+ )+ def test_is_wizard_self_driving_program(self, _name, user_agent, mcp_user_agent, expected):+ factory = APIRequestFactory()+ kwargs = {"HTTP_USER_AGENT": user_agent}+ if mcp_user_agent is not None:+ kwargs["HTTP_X_POSTHOG_MCP_USER_AGENT"] = mcp_user_agent+ request = factory.get("/fake", **kwargs)+ assert is_wizard_self_driving_program(request) is expected++class TestGetMcpProperties(BaseTest):def test_extracts_all_mcp_headers(self):factory = APIRequestFactory()diff --git a/products/data_warehouse/backend/tests/api/test_external_data_source.py b/products/data_warehouse/backend/tests/api/test_external_data_source.pyindex fe0b757ecf44..1bfa703f5594 100644--- a/products/data_warehouse/backend/tests/api/test_external_data_source.py+++ b/products/data_warehouse/backend/tests/api/test_external_data_source.py@@ -510,6 +510,52 @@ class TestExternalDataSource(APIBaseTest):source = ExternalDataSource.objects.get(id=response.json()["id"])assert source.created_via == expected_created_via+ @parameterized.expand(+ [+ # The MCP server overwrites User-Agent with its own token and forwards the wizard's real+ # UA in X-Posthog-Mcp-User-Agent. The self-driving marker lives there, not on User-Agent.+ (+ "self_driving_marker_in_mcp_header",+ "posthog/wizard; version: 2.45.0; program: self-driving-setup",+ ExternalDataSource.CreatedVia.SELF_DRIVING,+ ),+ # Same proxy shape, plain wizard run: stays wizard, not self_driving.+ (+ "plain_wizard_no_marker",+ "posthog/wizard; version: 2.45.0",+ ExternalDataSource.CreatedVia.WIZARD,+ ),+ ]+ )+ @patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",+ return_value=(True, None),+ )+ def test_create_external_data_source_self_driving_via_proxied_mcp_user_agent(+ self, _name, mcp_user_agent, expected_created_via, _mock_validate+ ):+ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_sources/",+ data={+ "source_type": "Stripe",+ "created_via": ExternalDataSource.CreatedVia.MCP,+ "payload": {+ "auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_123"},+ "schemas": [+ {"name": STRIPE_CUSTOMER_RESOURCE_NAME, "should_sync": True, "sync_type": "full_refresh"},+ ],+ },+ },+ headers={+ "user-agent": "posthog/mcp-server; version: 1.0.0; for posthog/wizard",+ "x-posthog-mcp-user-agent": mcp_user_agent,+ },+ )++ assert response.status_code == 201, response.json()+ source = ExternalDataSource.objects.get(id=response.json()["id"])+ assert source.created_via == expected_created_via+@patch("products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",return_value=(True, None),