: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(exports): render exports whose cache keys contain a backslash (#127485)
Matt Puab198ed9bf10a
diff --git a/products/exports/backend/tasks/image_exporter.py b/products/exports/backend/tasks/image_exporter.pyindex cf03e7d990c7..6ca614412633 100644--- a/products/exports/backend/tasks/image_exporter.py+++ b/products/exports/backend/tasks/image_exporter.py@@ -68,6 +68,11 @@ def _build_cache_keys_param(insight_cache_keys: Optional[dict[int, str]]) -> strreturn f"&cache_keys={quote(json.dumps(insight_cache_keys))}"+def _exporter_url(query_string: str) -> str:+ # absolute_uri rejects a `%5c` anywhere in its input, but cache keys embed path-cleaning regexes with backslashes+ return f"{absolute_uri('/exporter')}?{query_string}"++TMP_DIR = "/tmp" # NOTE: Externalise this to ENV varMAX_WIDTH_PIXELS = 4000 # Max width for wide content like funnels with many steps@@ -241,18 +246,18 @@ def _export_to_png(show_legend = exported_asset.insight.show_legendlegend_param = "&legend=true" if show_legend else ""cache_keys_param = _build_cache_keys_param(insight_cache_keys)- url_to_render = absolute_uri(f"/exporter?token={access_token}{legend_param}{cache_keys_param}")+ url_to_render = _exporter_url(f"token={access_token}{legend_param}{cache_keys_param}")wait_for_css_selector = ".ExportedInsight"screenshot_width = _insight_query_screenshot_width(exported_asset.insight.query or {})elif exported_asset.dashboard is not None:cache_keys_param = _build_cache_keys_param(insight_cache_keys)- url_to_render = absolute_uri(f"/exporter?token={access_token}{cache_keys_param}")+ url_to_render = _exporter_url(f"token={access_token}{cache_keys_param}")wait_for_css_selector = ".InsightCard"screenshot_width = 1920elif exported_asset.export_context and exported_asset.export_context.get("session_recording_id"):# Handle replay export using /exporter route (same as insights/dashboards)- url_to_render = absolute_uri(- f"/exporter?token={access_token}&t={exported_asset.export_context.get('timestamp') or 0}&fullscreen=true"+ url_to_render = _exporter_url(+ f"token={access_token}&t={exported_asset.export_context.get('timestamp') or 0}&fullscreen=true")wait_for_css_selector = exported_asset.export_context.get("css_selector", REPLAY_WRAPPER_SELECTOR)screenshot_width = exported_asset.export_context.get("width", 1400)@@ -277,9 +282,7 @@ def _export_to_png(# the `/exporter` query string.encoded_page_url = quote(heatmap_url, safe="")encoded_data_url = quote(exported_asset.export_context.get("heatmap_data_url") or "", safe="")- url_to_render = absolute_uri(- f"/exporter?token={access_token}&pageURL={encoded_page_url}&dataURL={encoded_data_url}"- )+ url_to_render = _exporter_url(f"token={access_token}&pageURL={encoded_page_url}&dataURL={encoded_data_url}")wait_for_css_selector = exported_asset.export_context.get("css_selector", ".heatmaps-ready")screenshot_width = exported_asset.export_context.get("width", 1400)screenshot_height = exported_asset.export_context.get("height", 600)@@ -302,7 +305,7 @@ def _export_to_png(legend_param = ("&legend=true" if _insight_query_wants_legend(exported_asset.export_context["source"]) else "")- url_to_render = absolute_uri(f"/exporter?token={access_token}{legend_param}")+ url_to_render = _exporter_url(f"token={access_token}{legend_param}")wait_for_css_selector = ".ExportedInsight"screenshot_width = _insight_query_screenshot_width(exported_asset.export_context["source"])page_load_timeout = 100diff --git a/products/exports/backend/tasks/test/test_image_exporter.py b/products/exports/backend/tasks/test/test_image_exporter.pyindex dfc5aa755a4b..b4f05188c0e1 100644--- a/products/exports/backend/tasks/test/test_image_exporter.py+++ b/products/exports/backend/tasks/test/test_image_exporter.py@@ -1,3 +1,4 @@+import jsonfrom datetime import datetimefrom typing import Anyfrom urllib.parse import parse_qs, urlparse@@ -292,9 +293,17 @@ class TestImageExporter(APIBaseTest):f"Call {i + 1} has unexpected insight {called_insight.id}")+ @parameterized.expand(+ [+ ("plain_key", "test_cache_key_123"),+ ("key_with_backslash", "cache_[{'regex': '/project/\\\\d+'}]"),+ ]+ )@patch("products.exports.backend.tasks.image_exporter.calculate_for_query_based_insight")def test_export_captures_cache_keys_and_passes_to_url(self,+ _name: str,+ cache_key: str,mock_calculate: Any,mock_remove: Any,mock_open: Any,@@ -312,7 +321,7 @@ class TestImageExporter(APIBaseTest):insight=insight,)- mock_calculate.return_value = make_insight_result("test_cache_key_123")+ mock_calculate.return_value = make_insight_result(cache_key)with self.settings(OBJECT_STORAGE_ENABLED=False):image_exporter.export_image(exported_asset)@@ -323,11 +332,20 @@ class TestImageExporter(APIBaseTest):url_to_render = call_args[0][1] # Second positional arg is the URLassert "cache_keys=" in url_to_render, f"URL should contain cache_keys parameter: {url_to_render}"- assert "test_cache_key_123" in url_to_render, f"URL should contain the cache key: {url_to_render}"+ exported_cache_keys = json.loads(parse_qs(urlparse(url_to_render).query)["cache_keys"][0])+ assert exported_cache_keys == {str(insight.id): cache_key}+ @parameterized.expand(+ [+ ("plain_keys", ""),+ ("keys_with_backslash", "\\d+"),+ ]+ )@patch("products.exports.backend.tasks.image_exporter.calculate_for_query_based_insight")def test_dashboard_export_captures_all_cache_keys(self,+ _name: str,+ cache_key_suffix: str,mock_calculate: Any,mock_remove: Any,mock_open: Any,@@ -353,7 +371,7 @@ class TestImageExporter(APIBaseTest):)def mock_calc(insight: Any, **kwargs: Any) -> InsightResult:- return make_insight_result(f"cache_key_for_insight_{insight.id}")+ return make_insight_result(f"cache_key_for_insight_{insight.id}{cache_key_suffix}")mock_calculate.side_effect = mock_calc@@ -364,11 +382,10 @@ class TestImageExporter(APIBaseTest):assert mock_screenshot_asset.calledurl_to_render = mock_screenshot_asset.call_args[0][1]- # URL should contain cache_keys for all insights- for insight in insights:- assert f"cache_key_for_insight_{insight.id}" in url_to_render, (- f"URL should contain cache key for insight {insight.id}"- )+ exported_cache_keys = json.loads(parse_qs(urlparse(url_to_render).query)["cache_keys"][0])+ assert exported_cache_keys == {+ str(insight.id): f"cache_key_for_insight_{insight.id}{cache_key_suffix}" for insight in insights+ }@patch("products.exports.backend.tasks.image_exporter._screenshot_asset_browserless")@patch("products.exports.backend.tasks.image_exporter.open", new_callable=mock_open, read_data=b"image_data")diff --git a/products/subscriptions/backend/logic/slack_subscriptions.py b/products/subscriptions/backend/logic/slack_subscriptions.pyindex 4bc573a08a25..cfc6ac7432a8 100644--- a/products/subscriptions/backend/logic/slack_subscriptions.py+++ b/products/subscriptions/backend/logic/slack_subscriptions.py@@ -50,7 +50,9 @@ _RETRYABLE_SLACK_ERRORS = frozenset(})-MAX_SLACK_UPLOAD_BYTES = 1 * 1024 * 1024+# Slack accepts uploads up to 1 GB. This lower cap guards the delivery activity, which holds+# every chart in memory at once, not the Slack API. Keep it well above a normal chart render.+MAX_SLACK_UPLOAD_BYTES = 25 * 1024 * 1024@dataclass@@ -64,19 +66,24 @@ class SlackMessage:unfurl: bool = True-@dataclass+@frozenclass SlackDeliveryResult:main_message_sent: booltotal_thread_messages: intfailed_thread_message_indices: list[int]+ undelivered_asset_count: int = 0@propertydef is_partial_failure(self) -> bool:- return self.main_message_sent and len(self.failed_thread_message_indices) > 0+ return self.main_message_sent and not self.is_complete_success@propertydef is_complete_success(self) -> bool:- return self.main_message_sent and len(self.failed_thread_message_indices) == 0+ return (+ self.main_message_sent+ and len(self.failed_thread_message_indices) == 0+ and self.undelivered_asset_count == 0+ )@frozen@@ -85,6 +92,7 @@ class SlackGallery:initial_comment: strfile_uploads: list[dict[str, Any]] = field(default_factory=list)blocks: list[dict[str, Any]] = field(default_factory=list)+ undelivered_asset_count: int = 0def _ai_data_processing_approved(subscription: Subscription, integration: IntegrationRecord | None) -> bool:@@ -320,6 +328,7 @@ def _prepare_slack_gallery(initial_comment="\n\n".join(lines),file_uploads=file_uploads,blocks=blocks,+ undelivered_asset_count=len(unattached_assets),)@@ -652,6 +661,7 @@ async def deliver_slack_gallery(main_message_sent=True,total_thread_messages=0,failed_thread_message_indices=[],+ undelivered_asset_count=gallery.undelivered_asset_count,)for attempt in range(3):@@ -678,7 +688,12 @@ async def deliver_slack_gallery(subscription_id=subscription.id,file_count=len(gallery.file_uploads),)- return SlackDeliveryResult(main_message_sent=True, total_thread_messages=0, failed_thread_message_indices=[])+ return SlackDeliveryResult(+ main_message_sent=True,+ total_thread_messages=0,+ failed_thread_message_indices=[],+ undelivered_asset_count=gallery.undelivered_asset_count,+ )async def send_slack_message_with_integration_async(diff --git a/products/subscriptions/backend/temporal/activities.py b/products/subscriptions/backend/temporal/activities.pyindex 6afc266db986..cacb74663f2b 100644--- a/products/subscriptions/backend/temporal/activities.py+++ b/products/subscriptions/backend/temporal/activities.py@@ -13,6 +13,7 @@ from structlog import get_loggerfrom temporalio.exceptions import ApplicationErrorfrom posthog.sync import database_sync_to_async+from posthog.utils import pluralizefrom products.dashboards.backend.models.dashboard_tile import DashboardTilefrom products.exports.backend.models.exported_asset import ExportedAsset@@ -30,7 +31,11 @@ from products.subscriptions.backend.logic.failure_notifications import (send_subscription_delivery_failure_email,)from products.subscriptions.backend.logic.slack_subscriptions import send_slack_message_with_integration_async-from products.subscriptions.backend.logic.subscription_utils import MAX_INSIGHTS+from products.subscriptions.backend.logic.subscription_utils import (+ ASSET_GENERATION_FAILED_MESSAGE,+ MAX_INSIGHTS,+ _has_asset_failed,+)from products.subscriptions.backend.logic.teams_subscriptions import build_teams_subscription_cardfrom products.subscriptions.backend.models.subscription import Subscription, SubscriptionDeliveryfrom products.subscriptions.backend.models.subscription_context import SubscriptionContext@@ -58,6 +63,7 @@ from products.subscriptions.backend.temporal.types import (NoExportableInsightsContext,NoExportableInsightsReason,RecipientResult,+ RecipientResultStatus,UpdateDeliveryRecordInputs,)@@ -398,6 +404,30 @@ async def create_export_assets(inputs: CreateExportAssetsInputs) -> CreateExport)+def _downgrade_for_unrendered_assets(+ result: DeliverSubscriptionResult, unrendered_count: int, total_count: int+) -> DeliverSubscriptionResult:+ """A destination that accepts the message is not a delivered chart. Without this, a+ subscription whose insights all failed to render is still recorded as a success."""+ message = f"{unrendered_count} of {pluralize(total_count, 'insight')} could not be generated"+ all_unrendered = unrendered_count == total_count+ status: RecipientResultStatus = "failed" if all_unrendered else "partial"+ # With no chart rendered, a partial result (such as a failed Slack thread message) delivered nothing either.+ downgradable: set[RecipientResultStatus] = {"success", "partial"} if all_unrendered else {"success"}+ downgraded = [+ RecipientResult(+ recipient=recipient_result.recipient,+ status=status,+ error={"message": message, "type": "unrendered_assets"},+ human_readable_error=message,+ )+ if recipient_result.status in downgradable+ else recipient_result+ for recipient_result in result.recipient_results+ ]+ return dataclasses.replace(result, recipient_results=downgraded)++@temporalio.activity.defnasync def deliver_subscription(inputs: DeliverSubscriptionInputs) -> DeliverSubscriptionResult:# TODO(2026-07-31): After workflows started before 2026-07-31 have drained, remove this v1 activity,@@ -524,10 +554,22 @@ async def _deliver_insight_dashboard_subscription(subscription, disable_reason, notify_owner=False)+ unrendered_assets = [asset for asset in assets if _has_asset_failed(asset)]+ if unrendered_assets:+ downgraded = _downgrade_for_unrendered_assets(result, len(unrendered_assets), len(assets))+ # Only a result this step turned to failed needs the event. Paths that failed earlier already captured one.+ if any(+ before.status != "failed" and after.status == "failed"+ for before, after in zip(result.recipient_results, downgraded.recipient_results)+ ):+ _capture_delivery_failed_event(subscription, Exception(ASSET_GENERATION_FAILED_MESSAGE))+ result = downgraded+await LOGGER.ainfo("deliver_subscription.completed",subscription_id=inputs.subscription_id,target_type=subscription.target_type,+ unrendered_asset_count=len(unrendered_assets),)return resultdiff --git a/products/subscriptions/backend/temporal/delivery_common.py b/products/subscriptions/backend/temporal/delivery_common.pyindex 9824e3988648..0548d4f11418 100644--- a/products/subscriptions/backend/temporal/delivery_common.py+++ b/products/subscriptions/backend/temporal/delivery_common.py@@ -9,6 +9,7 @@ from posthog.email import EmailDeliveryErrorfrom posthog.exceptions_capture import capture_exceptionfrom posthog.integrations import IntegrationRecordfrom posthog.sync import database_sync_to_async+from posthog.utils import pluralizefrom products.subscriptions.backend.logic import SLACK_USER_CONFIG_ERRORS, _capture_delivery_failed_eventfrom products.subscriptions.backend.logic.auto_disable import (@@ -218,14 +219,24 @@ async def deliver_slack(subscription_id=subscription.id,failed_thread_count=len(result.failed_thread_message_indices),total_thread_count=result.total_thread_messages,+ undelivered_asset_count=result.undelivered_asset_count,)+ parts: list[str] = []+ failure_types: list[str] = []failed_count = len(result.failed_thread_message_indices)- partial_message = f"{failed_count} thread message{'s' if failed_count != 1 else ''} failed"+ if failed_count:+ parts.append(f"{pluralize(failed_count, 'thread message')} failed")+ failure_types.append("partial_thread_failure")+ undelivered_count = result.undelivered_asset_count+ if undelivered_count:+ parts.append(f"{pluralize(undelivered_count, 'chart')} could not be attached")+ failure_types.append("undelivered_assets")+ partial_message = "; ".join(parts)recipient_results.append(RecipientResult(recipient=subscription.recipient_label,status="partial",- error={"message": partial_message, "type": "partial_thread_failure"},+ error={"message": partial_message, "type": ",".join(failure_types)},human_readable_error=partial_message,))diff --git a/products/subscriptions/backend/temporal/test_delivery_activities.py b/products/subscriptions/backend/temporal/test_delivery_activities.pyindex 2fe94ef85268..7d9cae385258 100644--- a/products/subscriptions/backend/temporal/test_delivery_activities.py+++ b/products/subscriptions/backend/temporal/test_delivery_activities.py@@ -18,6 +18,7 @@ from posthog.temporal.exports.types import ExportAssetResultfrom products.exports.backend.models.exported_asset import ExportedAssetfrom products.product_analytics.backend.facade.models import Insightfrom products.subscriptions.backend.facade.contracts import SubscriptionTriggerType+from products.subscriptions.backend.logic.slack_subscriptions import SlackDeliveryResultfrom products.subscriptions.backend.temporal.activities import (advance_next_delivery_date,create_delivery_record,@@ -78,6 +79,68 @@ async def test_deliver_slack_explains_missing_file_upload_scope(team, user) -> N}+async def test_deliver_slack_records_partial_when_an_asset_cannot_be_attached(team, user) -> None:+ integration = await sync_to_async(Integration.objects.create)(team=team, kind="slack", config={})+ subscription = await sync_to_async(create_subscription)(+ team=team,+ created_by=user,+ target_type="slack",+ target_value="C123|#general",+ integration=integration,+ )+ send = AsyncMock(+ return_value=SlackDeliveryResult(+ main_message_sent=True,+ total_thread_messages=0,+ failed_thread_message_indices=[],+ undelivered_asset_count=1,+ )+ )++ result = await deliver_slack(subscription, [], send)++ assert result.recipient_results[0].status == "partial"+ assert result.recipient_results[0].error == {+ "message": "1 chart could not be attached",+ "type": "undelivered_assets",+ }+++ "rendered_count,expected_status,expected_message",+ [+ (0, "failed", "1 of 1 insight could not be generated"),+ (1, "partial", "1 of 2 insights could not be generated"),+ ],+ ids=["all_unrendered", "some_unrendered"],+)+async def test_deliver_subscription_downgrades_unrendered_assets(+ team, user, rendered_count, expected_status, expected_message+) -> None:+ insight = await sync_to_async(Insight.objects.create)(team=team, short_id="unrnd", name="Never rendered")+ unrendered = await sync_to_async(ExportedAsset.objects.create)(+ team=team, insight=insight, export_format="image/png", exception="render timed out"+ )+ asset_ids = [unrendered.id]+ for _ in range(rendered_count):+ rendered = await sync_to_async(ExportedAsset.objects.create)(+ team=team, insight=insight, export_format="image/png", content_location="s3://bucket/ok.png"+ )+ asset_ids.append(rendered.id)+ subscription = await sync_to_async(create_subscription)(team=team, insight=insight, created_by=user)+ inputs = DeliverSubscriptionInputs(+ subscription_id=subscription.id,+ exported_asset_ids=asset_ids,+ total_insight_count=len(asset_ids),+ )++ with patch("products.subscriptions.backend.temporal.activities.send_email_subscription_report"):+ result = await ActivityEnvironment().run(deliver_subscription_v2, inputs)++ assert result.recipient_results[0].status == expected_status+ assert result.recipient_results[0].human_readable_error == expected_message++# v1 only exists as a Temporal history-compat shim for pre-patch workflows. Both# entry points must hit the shared delivery + EmailDeliveryError -> non-retryable# ApplicationError mapping, so the delivery record keeps per-recipient results.diff --git a/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py b/products/subscriptions/backend/tests/logic/test_slack_subscriptions.pyindex a9e1bf61b56d..a971dd94c462 100644--- a/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py+++ b/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py@@ -19,7 +19,6 @@ from products.product_analytics.backend.facade.models import Insightfrom products.slack_app.backend.facade.testing import REQUIRED_SLACK_SCOPESfrom products.subscriptions.backend.logic import SLACK_USER_CONFIG_ERRORSfrom products.subscriptions.backend.logic.slack_subscriptions import (- MAX_SLACK_UPLOAD_BYTES,SlackGallery,_blocks_for_asset,_prepare_slack_gallery,@@ -1053,7 +1052,7 @@ class TestSlackPostAllInMainMessage(APIBaseTest):team=self.team,insight_id=self.insight.id,export_format="image/png",- content=b"x" * (MAX_SLACK_UPLOAD_BYTES + 1),+ content=b"x" * 16,)successful = ExportedAsset.objects.create(team=self.team,@@ -1062,7 +1061,8 @@ class TestSlackPostAllInMainMessage(APIBaseTest):content=b"PNG",)assets = list(ExportedAsset.objects.filter(id__in=[oversized.id, successful.id]).select_related("insight"))- gallery = _prepare_slack_gallery(subscription, assets, total_asset_count=1)+ with patch("products.subscriptions.backend.logic.slack_subscriptions.MAX_SLACK_UPLOAD_BYTES", 8):+ gallery = _prepare_slack_gallery(subscription, assets, total_asset_count=1)assert len(gallery.file_uploads) == 1assert gallery.file_uploads[0]["content"] == b"PNG"attachment_errors = [@@ -1071,6 +1071,7 @@ class TestSlackPostAllInMainMessage(APIBaseTest):if block["type"] == "section" and block["text"].get("text") == "Could not attach My Test subscription"]assert len(attachment_errors) == 1+ assert gallery.undelivered_asset_count == 1assert "Could not generate" in gallery.initial_commentassert "My Test subscription" in gallery.initial_commentdiff --git a/products/subscriptions/backend/tests/temporal/test_subscriptions_workflows.py b/products/subscriptions/backend/tests/temporal/test_subscriptions_workflows.pyindex fe3f698d2273..9a999adb017b 100644--- a/products/subscriptions/backend/tests/temporal/test_subscriptions_workflows.py+++ b/products/subscriptions/backend/tests/temporal/test_subscriptions_workflows.py@@ -800,18 +800,27 @@ async def test_deliver_subscription_auto_disables_invalid_subscriptions(@pytest.mark.asyncio@pytest.mark.parametrize(- "label, exception_types, send_error, expected_raise, expected_enabled",+ "label, exception_types, send_error, expected_raise, expected_status, expected_enabled",[- ("owner_left", ["CreatorLeftOrganizationError"], None, None, False),- ("owner_lost_access_to_every_tile", ["TableAccessDeniedError", "TableAccessDeniedError"], None, None, False),+ # No chart rendered, so the delivery is recorded as failed even though the email carried the reason.+ ("owner_left", ["CreatorLeftOrganizationError"], None, None, "failed", False),+ (+ "owner_lost_access_to_every_tile",+ ["TableAccessDeniedError", "TableAccessDeniedError"],+ None,+ None,+ "failed",+ False,+ ),# One denied tile still delivers the dashboard, with that tile showing why.- ("owner_lost_access_to_one_tile", ["TableAccessDeniedError", None], None, None, True),+ ("owner_lost_access_to_one_tile", ["TableAccessDeniedError", None], None, None, "partial", True),# A permanent bounce raises out of the send, and the disable must still happen.("owner_left_and_email_bounces",["CreatorLeftOrganizationError"],EmailDeliveryError("permanent rejection"),ApplicationError,+ None,False,),# A transient failure is retried by Temporal, so the retry delivers the reason before any disable.@@ -820,12 +829,13 @@ async def test_deliver_subscription_auto_disables_invalid_subscriptions(["CreatorLeftOrganizationError"],ConnectionError("smtp unreachable"),ConnectionError,+ None,True,),],)async def test_deliver_subscription_sends_the_reason_then_disables_when_the_owner_cannot_run_it(- team, user, label, exception_types, send_error, expected_raise, expected_enabled+ team, user, label, exception_types, send_error, expected_raise, expected_status, expected_enabled):insight = await sync_to_async(Insight.objects.create)(team=team, short_id=f"own-{label[:5]}", name="owner")asset_ids = []@@ -869,7 +879,7 @@ async def test_deliver_subscription_sends_the_reason_then_disables_when_the_owneawait runelse:result = await run- assert result.recipient_results[0].status == "success"+ assert result.recipient_results[0].status == expected_statussend_report.assert_called_once()send_disabled_email.assert_not_called()