: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
feat(conversations): preview pasted ticket links in internal channels (#102151)
Alex V25e4f0de25ec
diff --git a/products/conversations/backend/api/slack_events.py b/products/conversations/backend/api/slack_events.pyindex 10b73e577e8b..ac153d11aa3b 100644--- a/products/conversations/backend/api/slack_events.py+++ b/products/conversations/backend/api/slack_events.py@@ -27,6 +27,7 @@ logger = structlog.get_logger(__name__)# Event types we handle for support ticketsSUPPORT_EVENT_TYPES = ["app_mention",+ "link_shared","message","reaction_added","member_joined_channel",diff --git a/products/conversations/backend/api/slack_oauth.py b/products/conversations/backend/api/slack_oauth.pyindex 589c9eaa9acd..cf206e354fdd 100644--- a/products/conversations/backend/api/slack_oauth.py+++ b/products/conversations/backend/api/slack_oauth.py@@ -38,6 +38,11 @@ SUPPORTHOG_SLACK_SCOPES = ["files:write","groups:history","groups:read",+ # links:read delivers link_shared, links:write posts the unfurl. Slack only fires+ # link_shared for domains registered on the app config, so an install that predates+ # these scopes simply never unfurls until an admin reconnects.+ "links:read",+ "links:write","reactions:read","users:read","users:read.email",diff --git a/products/conversations/backend/api/tests/test_slack_message_routing.py b/products/conversations/backend/api/tests/test_slack_message_routing.pyindex bdae874f7595..6c3d1f8e813b 100644--- a/products/conversations/backend/api/tests/test_slack_message_routing.py+++ b/products/conversations/backend/api/tests/test_slack_message_routing.py@@ -2,8 +2,9 @@ import jsonfrom datetime import UTC, datetimefrom posthog.test.base import BaseTest-from unittest.mock import Mock, patch+from unittest.mock import MagicMock, Mock, patch+from django.conf import settingsfrom django.core.cache import cachefrom django.test import override_settings@@ -18,11 +19,13 @@ from products.conversations.backend.cache import is_nudge_suppressedfrom products.conversations.backend.models import TeamConversationsSlackConfig, Ticketfrom products.conversations.backend.models.constants import Channel, ChannelDetailfrom products.conversations.backend.slack import (+ MAX_UNFURLS_PER_MESSAGE,TICKET_CONFIRM_ACTION_DISMISS,TICKET_CONFIRM_ACTION_OPEN,TICKET_VIEW_ACTION,SlackConfirmationNeedsRetry,create_ticket_from_confirmation,+ handle_link_shared,handle_member_joined_channel,handle_member_left_channel,handle_support_mention,@@ -31,8 +34,9 @@ from products.conversations.backend.slack import (my_tickets_link,ticket_created_blocks,ticket_deep_link,+ ticket_number_from_url,)-from products.conversations.backend.tasks.slack import process_supporthog_interactivity+from products.conversations.backend.tasks.slack import process_supporthog_event, process_supporthog_interactivityfrom products.customer_analytics.backend.facade import api as customer_analyticsfrom products.customer_analytics.backend.facade.testing import create_account@@ -1478,3 +1482,250 @@ class TestTicketConfirmationBlocks(BaseTest):# The requester scene addresses a ticket by UUID, unlike the Support scene's number.assert my_tickets_link(ticket).endswith(f"/my-tickets?ticket={ticket.id}")+++UNFURL_TEAM = "T_WORKSPACE"+# What conversations.info returns for an ordinary internal channel.+INTERNAL_CHANNEL = {"id": "C_INTERNAL", "name": "team-support", "is_channel": True}+++class TestTicketLinkUnfurl(BaseTest):+ """An unfurl renders for the whole channel and cannot be made partial, so it is posted+ only where both the channel and the person pasting the link are internal."""++ def setUp(self):+ super().setUp()+ cache.clear()+ self.team.conversations_settings = {"slack_enabled": True}+ self.team.save()+ self.ticket = _create_slack_ticket(self.team)+ self.url = ticket_deep_link(self.ticket, self.team)+ capture_patcher = patch(f"{MODULE}.capture_support_event")+ self.mock_capture = capture_patcher.start()+ self.addCleanup(capture_patcher.stop)++ def _event(self, *, urls: list[str] | None = None, user: str = "U_TEAMMATE", **extra) -> dict:+ return {+ "type": "link_shared",+ "channel": "C_INTERNAL",+ "user": user,+ "message_ts": MESSAGE_TS,+ "links": [{"domain": "posthog.com", "url": url} for url in (urls or [self.url])],+ **extra,+ }++ def _run(+ self,+ event: dict,+ *,+ channel_info: dict = INTERNAL_CHANNEL,+ sharer: dict | None = None,+ channel_error: Exception | None = None,+ unfurl_error: Exception | None = None,+ ) -> MagicMock:+ client = MagicMock()+ client.conversations_info.return_value = {"channel": channel_info}+ if channel_error is not None:+ client.conversations_info.side_effect = channel_error+ if unfurl_error is not None:+ client.chat_unfurl.side_effect = unfurl_error+ resolved = sharer if sharer is not None else {"email": self.user.email, "team_id": UNFURL_TEAM}+ with (+ patch(f"{MODULE}.get_slack_client", return_value=client),+ patch(f"{MODULE}.resolve_slack_user", return_value=resolved),+ ):+ handle_link_shared(event, self.team, UNFURL_TEAM)+ return client++ def test_internal_channel_gets_a_status_card(self):+ client = self._run(self._event())++ client.chat_unfurl.assert_called_once()+ kwargs = client.chat_unfurl.call_args.kwargs+ assert kwargs["channel"] == "C_INTERNAL"+ assert kwargs["ts"] == MESSAGE_TS+ rendered = json.dumps(kwargs["unfurls"][self.url])+ assert "Status" in rendered and "Last updated" in rendered+ assert f"Ticket #{self.ticket.ticket_number}" in rendered+ _team, event_name, props = self.mock_capture.call_args.args+ assert event_name == "support slack ticket unfurled"+ assert props["ticket_count"] == 1++ def test_card_carries_no_customer_words(self):+ self.ticket.last_message_text = "my api key leaked, here it is"+ self.ticket.email_subject = "urgent from acme"+ self.ticket.save()++ client = self._run(self._event())++ rendered = json.dumps(client.chat_unfurl.call_args.kwargs["unfurls"][self.url])+ assert "leaked" not in rendered+ assert "acme" not in rendered++ @parameterized.expand(+ [+ # Slack Connect, and an invitation to it that has not been accepted yet.+ ("shared_with_another_org", {"is_channel": True, "is_ext_shared": True}),+ ("invited_to_be_shared", {"is_channel": True, "is_pending_ext_shared": True}),+ # Another workspace in the same Enterprise Grid org is still not this workspace.+ ("shared_across_a_grid_org", {"is_channel": True, "is_org_shared": True}),+ ("shared_flag_set", {"is_channel": True, "is_shared": True}),+ # No flag marks these as external, so only rejecting them by type works.+ ("direct_message", {"is_im": True}),+ ("group_direct_message", {"is_mpim": True}),+ # An ok-but-empty or unfamiliar payload must not read as internal.+ ("unrecognized_payload", {}),+ ]+ )+ def test_only_an_ordinary_channel_of_this_workspace_gets_a_card(self, _name, channel_info):+ client = self._run(self._event(), channel_info=channel_info)++ client.chat_unfurl.assert_not_called()++ def test_a_private_channel_of_this_workspace_gets_a_card(self):+ client = self._run(self._event(), channel_info={"id": "C_PRIVATE", "is_group": True})++ client.chat_unfurl.assert_called_once()++ def test_unreadable_channel_fails_closed(self):+ # Cannot prove the channel is internal, and an unfurl cannot be taken back.+ client = self._run(self._event(), channel_error=RuntimeError("not_in_channel"))++ client.chat_unfurl.assert_not_called()++ def test_a_slack_failure_is_not_reported_as_a_success(self):+ self._run(self._event(), unfurl_error=RuntimeError("slack down"))++ self.mock_capture.assert_not_called()++ @parameterized.expand(+ [+ ("outsider", {"email": "[email protected]", "team_id": UNFURL_TEAM}),+ # A guest's profile email is set by their own workspace, so it can claim ours.+ ("external_workspace", {"email": USE_TEAMMATE_EMAIL, "team_id": "T_OTHER"}),+ ]+ )+ def test_non_member_sharer_gets_nothing(self, _name, sharer):+ if sharer["email"] == USE_TEAMMATE_EMAIL:+ sharer = {**sharer, "email": self.user.email}++ client = self._run(self._event(), sharer=sharer)++ client.chat_unfurl.assert_not_called()++ def test_a_ticket_the_sharer_cannot_read_is_not_unfurled(self):+ # Organization membership is not access to the ticket, and nothing downstream of this+ # card applies that rule, so the lookup has to go through access control.+ with patch(f"{MODULE}.UserAccessControl") as mock_access:+ mock_access.return_value.filter_queryset_by_access_level.return_value = Ticket.objects.none()+ client = self._run(self._event())++ assert mock_access.call_args.args[0].id == self.user.id+ assert mock_access.return_value.filter_queryset_by_access_level.call_args.kwargs["resource"] == "ticket"+ client.chat_unfurl.assert_not_called()++ def test_unknown_ticket_number_is_not_unfurled(self):+ client = self._run(self._event(urls=[f"{settings.SITE_URL}/project/{self.team.id}/support/tickets/424242"]))++ client.chat_unfurl.assert_not_called()++ def _assert_no_slack_call(self, event: dict) -> None:+ with patch(f"{MODULE}.get_slack_client") as mock_get_client:+ handle_link_shared(event, self.team, UNFURL_TEAM)++ mock_get_client.assert_not_called()++ def test_a_link_that_is_not_a_ticket_costs_no_slack_calls(self):+ # The app's whole host is registered for unfurling, so every pasted PostHog link lands+ # here. Looking the channel up first would spend an uncached, rate-limited+ # conversations_info call on every insight and dashboard link in the workspace.+ self._assert_no_slack_call(self._event(urls=[f"{settings.SITE_URL}/project/{self.team.id}/insights/abc123"]))++ def test_a_disabled_team_is_a_noop(self):+ self.team.conversations_settings = {"slack_enabled": False}+ self.team.save()++ self._assert_no_slack_call(self._event())++ def test_an_event_without_a_channel_is_ignored(self):+ self._assert_no_slack_call(self._event() | {"channel": ""})++ def test_at_most_five_cards_for_one_message(self):+ tickets = [self.ticket, *(_create_slack_ticket(self.team) for _ in range(MAX_UNFURLS_PER_MESSAGE))]++ client = self._run(self._event(urls=[ticket_deep_link(t, self.team) for t in tickets]))++ assert len(client.chat_unfurl.call_args.kwargs["unfurls"]) == MAX_UNFURLS_PER_MESSAGE++ def test_the_same_link_twice_gets_one_card(self):+ client = self._run(self._event(urls=[self.url, self.url]))++ assert list(client.chat_unfurl.call_args.kwargs["unfurls"]) == [self.url]++ def test_a_link_shared_event_reaches_the_handler(self):+ # The dispatch itself: a wrong event-type string would disable the whole feature with+ # every other test in this class still passing.+ config = get_or_create_team_extension(self.team, TeamConversationsSlackConfig)+ config.slack_team_id = UNFURL_TEAM+ config.slack_bot_token = "xoxb-test"+ config.save(update_fields=["slack_team_id", "slack_bot_token"])++ with patch(f"{TASKS_MODULE}.handle_link_shared") as mock_handler:+ process_supporthog_event(self._event(), UNFURL_TEAM)++ mock_handler.assert_called_once()++ def test_each_link_gets_the_card_for_its_own_ticket(self):+ other = _create_slack_ticket(self.team)+ other_url = ticket_deep_link(other, self.team)++ client = self._run(self._event(urls=[self.url, other_url]))++ unfurls = client.chat_unfurl.call_args.kwargs["unfurls"]+ assert set(unfurls) == {self.url, other_url}+ assert f"Ticket #{self.ticket.ticket_number}" in json.dumps(unfurls[self.url])+ assert f"Ticket #{other.ticket_number}" in json.dumps(unfurls[other_url])++ def test_unfurl_id_is_preferred_when_slack_sends_one(self):+ client = self._run(self._event(unfurl_id="abc123", source="conversations_history"))++ kwargs = client.chat_unfurl.call_args.kwargs+ assert kwargs["unfurl_id"] == "abc123"+ assert kwargs["source"] == "conversations_history"+ assert "channel" not in kwargs++ @parameterized.expand(+ [+ ("other_host", "https://evil.example.com/project/{team}/support/tickets/{number}"),+ ("other_project", "{site}/project/999999/support/tickets/{number}"),+ ("not_a_ticket", "{site}/project/{team}/support/tickets"),+ ("trailing_segment", "{site}/project/{team}/support/tickets/{number}/edit"),+ ]+ )+ def test_only_this_installs_own_ticket_urls_resolve(self, _name, template):+ # A number that matched a foreign link would resolve against this team to a real but+ # unrelated ticket, so host and project are both part of the match.+ url = template.format(site=settings.SITE_URL, team=self.team.id, number=self.ticket.ticket_number)++ assert ticket_number_from_url(url, self.team) is None++ def test_our_own_ticket_url_resolves(self):+ assert ticket_number_from_url(self.url, self.team) == self.ticket.ticket_number++ def test_a_malformed_url_is_not_a_ticket(self):+ # urlparse raises on an unterminated IPv6 host rather than returning something usable.+ assert ticket_number_from_url("http://[", self.team) is None++ def test_an_absurdly_long_number_is_not_a_ticket(self):+ # int() raises above 4300 digits, and this runs ahead of the gates, so a crafted link+ # has to resolve to nothing rather than raise and leave a retried event behind.+ url = f"{settings.SITE_URL}/project/{self.team.id}/support/tickets/{'9' * 5000}"++ assert ticket_number_from_url(url, self.team) is None++ def test_host_match_ignores_case(self):+ # Hosts are case-insensitive, so a typed-out host is still our own link.+ scheme, _, rest = self.url.partition("://")+ host, _, path = rest.partition("/")++ assert ticket_number_from_url(f"{scheme}://{host.upper()}/{path}", self.team) == self.ticket.ticket_numberdiff --git a/products/conversations/backend/slack.py b/products/conversations/backend/slack.pyindex 2aaf0c1ffcd3..347cbedca01c 100644--- a/products/conversations/backend/slack.py+++ b/products/conversations/backend/slack.py@@ -37,6 +37,8 @@ from posthog.models.team.team import Teamfrom posthog.models.user import Userfrom posthog.ph_client import ph_scoped_capture+from products.access_control.backend.facade.user_access_control import UserAccessControl+from .cache import (NUDGE_COOLDOWN_TTL,get_cached_bot_user_id,@@ -1536,6 +1538,184 @@ def _backfill_thread_replies()+# Must stay in step with ticket_deep_link, which writes the URLs this matches. The digit bounds+# are load-bearing: this runs on any pasted link before the gates below, and int() raises above+# 4300 digits, so \d+ would let a crafted URL raise and leave a retried event behind. Ten digits+# covers everything the ticket_number column can hold.+_TICKET_URL_PATH_RE = re.compile(r"^/project/(?P<project_id>\d{1,10})/support/tickets/(?P<ticket_number>\d{1,10})/?$")++# Anything that puts someone outside this workspace in the room: Slack Connect, an invitation+# to it, Enterprise Grid cross-workspace sharing, and direct messages. A DM cannot reach us+# today (link_shared needs im:history/mpim:history, which SupportHog does not request), but+# the rule belongs in this gate rather than in the scope list, where a later feature could+# widen it by accident.+_NON_INTERNAL_CHANNEL_FLAGS = (+ "is_ext_shared",+ "is_pending_ext_shared",+ "is_org_shared",+ "is_shared",+ "is_im",+ "is_mpim",+)++MAX_UNFURLS_PER_MESSAGE = 5+++def ticket_number_from_url(url: str, team: Team) -> int | None:+ """The ticket number in one of this install's own ticket URLs, else None.++ Host and project both have to match. A link to another region or another project is a+ different ticket, and its number would otherwise resolve against this team to a real but+ unrelated ticket.+ """+ try:+ parsed = urlparse(url)+ except ValueError:+ return None+ if parsed.netloc.lower() != urlparse(settings.SITE_URL).netloc.lower():+ return None+ match = _TICKET_URL_PATH_RE.match(parsed.path)+ if not match:+ return None+ if int(match.group("project_id")) != _get_team_id(team):+ return None+ return int(match.group("ticket_number"))+++def _slack_date(value: datetime) -> str:+ """Render a timestamp in each reader's own timezone, with an ISO fallback."""+ return f"<!date^{int(value.timestamp())}^{{date_short_pretty}} at {{time}}|{value.isoformat()}>"+++def ticket_unfurl(ticket: "Ticket", team: Team) -> dict:+ """The preview card for a pasted ticket link.++ Status and timings only. No message text, requester or email: the reader is whoever is in+ the channel, which is a wider audience than the people working the ticket, and a preview+ nobody asked for is the wrong place to widen who sees a customer's words.+ """+ fields = [+ {"type": "mrkdwn", "text": f"*Status*\n{ticket.get_status_display()}"},+ {"type": "mrkdwn", "text": f"*Priority*\n{ticket.get_priority_display() if ticket.priority else 'Not set'}"},+ {"type": "mrkdwn", "text": f"*Created*\n{_slack_date(ticket.created_at)}"},+ {"type": "mrkdwn", "text": f"*Last updated*\n{_slack_date(ticket.updated_at)}"},+ ]+ return {+ "blocks": [+ {+ "type": "section",+ "text": {+ "type": "mrkdwn",+ "text": f"*<{ticket_deep_link(ticket, team)}|Ticket #{ticket.ticket_number}>*",+ },+ },+ {"type": "section", "fields": fields},+ ]+ }+++def _is_internal_channel(client: WebClient, channel: str) -> bool:+ """Whether `channel` is an ordinary channel of this workspace alone.++ Fails closed, twice over: a channel we cannot read is one we cannot prove is internal, and+ only a payload that positively identifies itself as a channel or private group passes, so+ an unfamiliar shape is rejected rather than read as "no external flags, so internal". An+ unfurl is irreversible once it renders for someone outside the organization.++ Known residual: a Slack guest invited straight into the workspace (single- or+ multi-channel) is a workspace member as far as these flags go, so a channel holding one+ still counts as internal. Slack offers no per-channel guest signal, and proving every+ member belongs to the organization would mean paginating the member list on every pasted+ link. The card carries metadata only and the link itself was already posted by a member,+ so the incremental exposure is status and timings rather than a ticket.+ """+ try:+ response = client.conversations_info(channel=channel)+ except Exception:+ logger.warning("slack_support_unfurl_channel_lookup_failed", slack_channel_id=channel)+ return False+ info = response.get("channel") or {}+ if any(info.get(flag) for flag in _NON_INTERNAL_CHANNEL_FLAGS):+ return False+ return bool(info.get("is_channel") or info.get("is_group"))+++def handle_link_shared(event: dict, team: Team, slack_team_id: str) -> None:+ """Preview a pasted ticket link, in internal channels only.++ An unfurl is visible to the whole channel and cannot be made partial, so both gates have+ to hold: the channel is not shared with another organization, and the person who pasted+ the link is a member of this team's organization. Ticket status is our operational view,+ not the customer's view of their own request.+ """+ settings_dict = team.conversations_settings or {}+ if not settings_dict.get("slack_enabled"):+ return++ channel = event.get("channel") or ""+ if not channel:+ return++ # Match the URLs before calling Slack. The app's whole host is registered for unfurling, so+ # every PostHog link anyone pastes arrives here — an insight, a dashboard, a replay — and+ # conversations_info below is uncached and rate limited per workspace. The cap counts ticket+ # links, so one pasted after five unrelated links is still previewed.+ candidates: dict[str, int] = {}+ for link in event.get("links") or []:+ if len(candidates) >= MAX_UNFURLS_PER_MESSAGE:+ break+ url = link.get("url", "")+ if url in candidates:+ continue+ ticket_number = ticket_number_from_url(url, team)+ if ticket_number is not None:+ candidates[url] = ticket_number+ if not candidates:+ return++ client = get_slack_client(team)+ if not _is_internal_channel(client, channel):+ return++ sharer = resolve_slack_user(client, event.get("user") or "", workspace=slack_team_id)+ if sharer.get("team_id") != slack_team_id:+ return+ sharer_user = resolve_posthog_user_for_slack(sharer.get("email"), team)+ if sharer_user is None:+ return++ # Belonging to the organization is not access to the ticket: "ticket" is an access-controlled+ # resource, so a member can be denied the resource or a single ticket. The card is rendered+ # here instead of in the app, so nothing downstream would apply that rule, and pasting guessed+ # URLs would otherwise read out status for tickets the person cannot open.+ readable = UserAccessControl(sharer_user, team=team).filter_queryset_by_access_level(+ Ticket.objects.filter(team=team, ticket_number__in=set(candidates.values())), resource="ticket"+ )+ tickets = {ticket.ticket_number: ticket for ticket in readable}+ unfurls = {url: ticket_unfurl(tickets[number], team) for url, number in candidates.items() if number in tickets}+ if not unfurls:+ return++ # unfurl_id/source works whether or not the bot is in the channel; channel/ts is the+ # fallback for a payload that predates it.+ target = (+ {"unfurl_id": event["unfurl_id"], "source": event["source"]}+ if event.get("unfurl_id") and event.get("source")+ else {"channel": channel, "ts": event.get("message_ts", "")}+ )+ try:+ client.chat_unfurl(unfurls=unfurls, **target)+ except Exception:+ logger.warning("slack_support_unfurl_failed", slack_channel_id=channel, count=len(unfurls))+ return++ capture_support_event(+ team,+ "support slack ticket unfurled",+ {"slack_team_id": slack_team_id, "slack_channel_id": channel, "ticket_count": len(unfurls)},+ )++def handle_support_reaction(event: dict, team: Team, slack_team_id: str) -> None:"""Handle a Slack 'reaction_added' event to create a ticket from a reacted message.diff --git a/products/conversations/backend/tasks/slack.py b/products/conversations/backend/tasks/slack.pyindex c50cdb457208..0f106b99f484 100644--- a/products/conversations/backend/tasks/slack.py+++ b/products/conversations/backend/tasks/slack.py@@ -63,6 +63,7 @@ from products.conversations.backend.slack import (get_bot_user_id,get_safe_ticket_emoji,get_slack_client,+ handle_link_shared,handle_member_joined_channel,handle_member_left_channel,handle_support_mention,@@ -125,6 +126,8 @@ def _handle_supporthog_event(event: dict[str, Any], team: Team, slack_team_id: shandle_support_message(event, team, slack_team_id)elif event_type == "app_mention":handle_support_mention(event, team, slack_team_id)+ elif event_type == "link_shared":+ handle_link_shared(event, team, slack_team_id)elif event_type == "reaction_added":handle_support_reaction(event, team, slack_team_id)elif event_type == "member_joined_channel":