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(insights): return actionable errors for invalid filter overrides (#118498)

posthog[bot]5196f1208da3
diff --git a/posthog/test/test_utils.py b/posthog/test/test_utils.py
index 6cf12954d59f..7e82ba0b3cd4 100644
--- a/posthog/test/test_utils.py
+++ b/posthog/test/test_utils.py
@@ -1093,13 +1093,26 @@ class TestSharingOverrideProtection(SimpleTestCase):
assert result == {"date_from": "-30d"}
- def test_filters_override_allowed_for_normal_auth(self):
- request = self._make_request(None, query_params={"filters_override": json.dumps({"date_from": "-7d"})})
+ @parameterized.expand(
+ [
+ ("date", {"date_from": "-7d"}),
+ (
+ "grouped_property_list",
+ {"properties": [{"type": "AND", "values": [{"type": "event", "key": "browser", "value": "Chrome"}]}]},
+ ),
+ (
+ "grouped_property_dict",
+ {"properties": {"type": "AND", "values": [{"type": "event", "key": "browser", "value": "Chrome"}]}},
+ ),
+ ]
+ )
+ def test_filters_override_allowed_for_normal_auth(self, _name: str, override: dict[str, Any]) -> None:
+ request = self._make_request(None, query_params={"filters_override": json.dumps(override)})
dashboard = type("Dashboard", (), {"filters": {"date_from": "-30d"}})()
result = filters_override_requested_by_client(request, dashboard)
- assert result == {"date_from": "-7d"}
+ assert result == {"date_from": "-30d", **override}
def test_filters_override_gets_saved_metric_variable_definitions(self):
saved = {"name": "env", "source": "sql", "query": "SELECT 1", "current": {"values": ["a"]}}
@@ -1119,6 +1132,35 @@ class TestSharingOverrideProtection(SimpleTestCase):
with pytest.raises(serializers.ValidationError):
filters_override_requested_by_client(request, dashboard)
+ @parameterized.expand(
+ [
+ (
+ "unknown_keys",
+ {"date_from": "-7d", "display": "ActionsLineGraph"},
+ "Unknown key(s) in filters_override: display.",
+ ),
+ ("not_an_object", ["date_from"], "filters_override must be a JSON object."),
+ ("invalid_value", {"interval": "fortnight"}, "Invalid value in filters_override at 'interval'"),
+ (
+ "nested_unknown_key",
+ {"compareFilter": {"surprise": True}},
+ "Invalid value in filters_override at 'compareFilter.surprise': Extra inputs are not permitted.",
+ ),
+ (
+ "invalid_property_group",
+ {"properties": [{"type": "OR", "values": []}]},
+ "Invalid value in filters_override: Only AND property groups are supported.",
+ ),
+ ]
+ )
+ def test_filters_override_rejects_invalid_filters(self, _name: str, override: Any, expected_detail: str) -> None:
+ request = self._make_request(None, query_params={"filters_override": json.dumps(override)})
+
+ with pytest.raises(serializers.ValidationError) as exc_info:
+ filters_override_requested_by_client(request, None)
+
+ assert expected_detail in str(exc_info.value.detail)
+
@parameterized.expand(
[
("access_token_auth",),
diff --git a/posthog/utils.py b/posthog/utils.py
index 985717352689..592f772b424f 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -47,6 +47,7 @@ from dateutil import parser
from dateutil.relativedelta import relativedelta
from opentelemetry import trace
from prometheus_client import Histogram
+from pydantic import ValidationError as PydanticValidationError
from rest_framework import serializers
from rest_framework.request import Request
from rest_framework.utils.encoders import JSONEncoder
@@ -1990,9 +1991,43 @@ def filters_override_requested_by_client(
if isinstance(request_filters, dict):
request_filters = _with_saved_metric_variables(request_filters, dashboard_filters or {})
+ validate_filters_override(request_filters)
return {**dashboard_filters, **request_filters}
+def validate_filters_override(request_filters: Any) -> None:
+ from posthog.schema import DashboardFilter
+
+ from posthog.hogql_queries.apply_dashboard_filters import (
+ dashboard_filter_from_dict,
+ normalize_dashboard_filters_properties,
+ )
+
+ if not isinstance(request_filters, dict):
+ raise serializers.ValidationError({"filters_override": "filters_override must be a JSON object."})
+
+ try:
+ dashboard_filter_from_dict(normalize_dashboard_filters_properties(request_filters))
+ except PydanticValidationError as e:
+ accepted_keys = ", ".join(DashboardFilter.model_fields)
+ unknown_keys = sorted(
+ {
+ str(error["loc"][0])
+ for error in e.errors()
+ if error["type"] == "extra_forbidden" and len(error["loc"]) == 1
+ }
+ )
+ if unknown_keys:
+ detail = f"Unknown key(s) in filters_override: {', '.join(unknown_keys)}. Accepted keys: {accepted_keys}."
+ else:
+ first_error = e.errors()[0]
+ location = ".".join(str(part) for part in first_error["loc"])
+ detail = f"Invalid value in filters_override at '{location}': {first_error['msg']}."
+ raise serializers.ValidationError({"filters_override": detail})
+ except ValueError as e:
+ raise serializers.ValidationError({"filters_override": f"Invalid value in filters_override: {e}."})
+
+
def variables_override_requested_by_client(
request: Optional[Request],
dashboard: Optional["Dashboard"],
diff --git a/products/product_analytics/backend/tests/api/test_insight.py b/products/product_analytics/backend/tests/api/test_insight.py
index 4e94d4d050e8..dcedca89bf43 100644
--- a/products/product_analytics/backend/tests/api/test_insight.py
+++ b/products/product_analytics/backend/tests/api/test_insight.py
@@ -3412,6 +3412,18 @@ class TestInsight(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert isinstance(response["query"]["source"], dict)
assert response["query"]["source"]["dateRange"]["date_from"] == "-7d"
+ invalid_response = self.client.get(
+ f"/api/projects/{self.team.id}/insights/",
+ data={
+ **request_data,
+ "short_id": insight.short_id,
+ "filters_override": json.dumps({"display": "ActionsLineGraph"}),
+ },
+ )
+
+ assert invalid_response.status_code == status.HTTP_400_BAD_REQUEST
+ assert invalid_response.json()["attr"] == "filters_override"
+
def test_tile_filters_merge_with_dashboard_filters_in_returned_query(self) -> None:
insight = Insight.objects.create(
filters={},
diff --git a/services/mcp/schema/tool-inputs.json b/services/mcp/schema/tool-inputs.json
index 5f2c16599e47..0103f76e02fa 100644
--- a/services/mcp/schema/tool-inputs.json
+++ b/services/mcp/schema/tool-inputs.json
@@ -739,7 +739,7 @@
]
},
"filters_override": {
- "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
+ "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepted keys: `date_from`, `date_to`, `explicitDate`, `interval`, `properties`, `filterTestAccounts`, `breakdown_filter`, `compareFilter`, `metricFilters`, `metricVariables`. Other keys (for example `display`, `dateRange`, `filter_test_accounts` or `compare`) are rejected. Ignored when accessed via a sharing token.",
"anyOf": [
{
"type": "string"
diff --git a/services/mcp/src/api/client.ts b/services/mcp/src/api/client.ts
index e7e655557dff..bed7048e117d 100644
--- a/services/mcp/src/api/client.ts
+++ b/services/mcp/src/api/client.ts
@@ -1322,9 +1322,17 @@ export class ApiClient {
return retrieveById()
}
+ // A 404 keeps a wrong or stale short_id out of the internal error bucket and error tracking.
return {
success: false,
- error: new Error(`No insight found with short_id: ${insightId}`),
+ error: new PostHogApiError({
+ status: 404,
+ statusText: 'Not Found',
+ body: '',
+ url: `${this.baseUrl}/api/projects/${projectId}/insights/?short_id=${insightId}`,
+ method: 'GET',
+ message: `No insight found with short_id: ${insightId} in project ${projectId}. Use insights-list to find the insight. If the insight is in a different project, use switch-project.`,
+ }),
}
},
diff --git a/services/mcp/src/schema/tool-inputs.ts b/services/mcp/src/schema/tool-inputs.ts
index e5a28e025b86..9f3dbe0f8922 100644
--- a/services/mcp/src/schema/tool-inputs.ts
+++ b/services/mcp/src/schema/tool-inputs.ts
@@ -534,7 +534,7 @@ export const InsightQueryInputSchema = z.preprocess(
.union([z.string(), z.record(z.string(), z.unknown())])
.optional()
.describe(
- "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
+ "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepted keys: `date_from`, `date_to`, `explicitDate`, `interval`, `properties`, `filterTestAccounts`, `breakdown_filter`, `compareFilter`, `metricFilters`, `metricVariables`. Other keys (for example `display`, `dateRange`, `filter_test_accounts` or `compare`) are rejected. Ignored when accessed via a sharing token."
),
})
)
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-query.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-query.json
index fa2c1abc5a43..4342a312d480 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-query.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-query.json
@@ -14,7 +14,7 @@
"type": "object"
}
],
- "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
+ "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepted keys: `date_from`, `date_to`, `explicitDate`, `interval`, `properties`, `filterTestAccounts`, `breakdown_filter`, `compareFilter`, `metricFilters`, `metricVariables`. Other keys (for example `display`, `dateRange`, `filter_test_accounts` or `compare`) are rejected. Ignored when accessed via a sharing token."
},
"insightId": {
"anyOf": [
diff --git a/services/mcp/tests/unit/api-client.test.ts b/services/mcp/tests/unit/api-client.test.ts
index 2500392f06fe..710874bc763b 100644
--- a/services/mcp/tests/unit/api-client.test.ts
+++ b/services/mcp/tests/unit/api-client.test.ts
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ApiClient } from '@/api/client'
import { MCP_CLIENT_IP_HEADERS, verifySignedClientIp } from '@/lib/client-ip-signature'
import { USER_AGENT, getUserAgent } from '@/lib/constants'
-import { PostHogTransportError } from '@/lib/errors'
+import { PostHogApiError, PostHogTransportError } from '@/lib/errors'
import { getToolByName } from '@/shared/test-utils'
import { GENERATED_TOOLS as PROXY_TOOLS } from '@/tools/generated/proxy-records'
import { GENERATED_TOOLS } from '@/tools/generated/skills'
@@ -501,6 +501,9 @@ describe('ApiClient', () => {
expect(mockFetch).toHaveBeenCalledTimes(1)
expect(result.success).toBe(false)
+ const error = result.success ? undefined : result.error
+ expect(error).toBeInstanceOf(PostHogApiError)
+ expect((error as PostHogApiError).status).toBe(404)
vi.unstubAllGlobals()
})