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(conversations): improve staff login lookup for widget tickets (#116384)

Tom Piccirelloe845cb5d07d8
diff --git a/frontend/src/layout/navigation/ImpersonationNotice/AdminLoginButtons.tsx b/frontend/src/layout/navigation/ImpersonationNotice/AdminLoginButtons.tsx
index 4a910cd909ab..15202298d1a7 100644
--- a/frontend/src/layout/navigation/ImpersonationNotice/AdminLoginButtons.tsx
+++ b/frontend/src/layout/navigation/ImpersonationNotice/AdminLoginButtons.tsx
@@ -9,7 +9,9 @@ export function AdminLoginButtons({
ticketContext: ImpersonationTicketContext | null
adminLoginUrls: AdminLoginUrl[]
}): JSX.Element {
- const disabledReason = !ticketContext?.email ? 'This ticket has no associated email' : undefined
+ const disabledReason = adminLoginUrls.length === 0 ? 'This ticket has no associated email' : undefined
+ const loginTarget =
+ ticketContext?.verifiedDistinctId && ticketContext.isEmailUnverified ? 'verified account' : ticketContext?.email
// Region is ambiguous when we couldn't infer it, so we offer one button per
// region and label each so staff know which admin page they're opening.
@@ -30,7 +32,7 @@ export function AdminLoginButtons({
tooltip="This currently redirects to the admin login page, but in future will log you in directly."
onClick={() => window.open(url, '_blank')}
>
- Login as {ticketContext?.email}
+ Login as {loginTarget}
{showRegionLabel ? ` (${region})` : ''}
</LemonButton>
))
diff --git a/frontend/src/layout/navigation/ImpersonationNotice/ImpersonationNotice.tsx b/frontend/src/layout/navigation/ImpersonationNotice/ImpersonationNotice.tsx
index fd55c88f2756..b8f7b345e8dc 100644
--- a/frontend/src/layout/navigation/ImpersonationNotice/ImpersonationNotice.tsx
+++ b/frontend/src/layout/navigation/ImpersonationNotice/ImpersonationNotice.tsx
@@ -94,7 +94,11 @@ function LoginAsContent({
<p className="ImpersonationNotice__message">
{ticketContext.email ? (
<>
- Customer: <span className="text-success">{ticketContext.email}</span>
+ Customer:{' '}
+ <span className={ticketContext.isEmailUnverified ? undefined : 'text-success'}>
+ {ticketContext.email}
+ </span>
+ {ticketContext.isEmailUnverified && ' (not verified)'}
</>
) : (
'No customer email on this ticket'
diff --git a/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.test.ts b/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.test.ts
index 5c5807c974b9..6d8ac24712bb 100644
--- a/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.test.ts
+++ b/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.test.ts
@@ -203,6 +203,25 @@ describe('impersonationNoticeLogic', () => {
],
})
})
+
+ it('looks up a verified widget ticket by its signed distinct ID, not by the typed email', async () => {
+ logic.actions.setTicketContext({
+ ticketId: '1',
+ email: '[email protected]',
+ region: Region.US,
+ isEmailUnverified: true,
+ verifiedDistinctId: 'signed/distinct id',
+ })
+
+ await expectLogic(logic).toMatchValues({
+ adminLoginUrls: [
+ {
+ region: Region.US,
+ url: 'https://us.posthog.com/admin/posthog/user/?q=signed%2Fdistinct%20id',
+ },
+ ],
+ })
+ })
})
})
diff --git a/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.ts b/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.ts
index d6be717b1f5d..57dc5eda5964 100644
--- a/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.ts
+++ b/frontend/src/layout/navigation/ImpersonationNotice/impersonationNoticeLogic.ts
@@ -30,6 +30,10 @@ export interface ImpersonationTicketContext {
ticketId: string
email: string
region?: Region
+ /** True when no server check attests the email. */
+ isEmailUnverified?: boolean
+ /** The ticket identity a server check attested. The staff lookup uses it in place of the email. */
+ verifiedDistinctId?: string
}
export interface AdminLoginUrl {
@@ -42,9 +46,9 @@ export interface AdminLoginUrl {
// production cloud region rather than no link at all.
const ADMIN_LOOKUP_REGIONS: Region[] = [Region.US, Region.EU]
-function adminLoginUrlForRegion(region: Region, email: string): string {
+function adminLoginUrlForRegion(region: Region, userQuery: string): string {
const domain = CLOUD_HOSTNAMES[region]
- return `https://${domain}/admin/posthog/user/?q=${encodeURIComponent(email)}`
+ return `https://${domain}/admin/posthog/user/?q=${encodeURIComponent(userQuery)}`
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -271,13 +275,15 @@ export const impersonationNoticeLogic = kea<impersonationNoticeLogicType>([
adminLoginUrls: [
(s) => [s.ticketContext],
(ticketContext: ImpersonationTicketContext | null): AdminLoginUrl[] => {
- if (!ticketContext?.email) {
+ // The admin user search matches a distinct ID as well as an email.
+ const userQuery = ticketContext?.verifiedDistinctId || ticketContext?.email
+ if (!ticketContext || !userQuery) {
return []
}
const regions = ticketContext.region ? [ticketContext.region] : ADMIN_LOOKUP_REGIONS
return regions.map((region) => ({
region,
- url: adminLoginUrlForRegion(region, ticketContext.email),
+ url: adminLoginUrlForRegion(region, userQuery),
}))
},
],
diff --git a/products/conversations/backend/api/tests/test_widget.py b/products/conversations/backend/api/tests/test_widget.py
index 85e5a8301034..82fb9f51400b 100644
--- a/products/conversations/backend/api/tests/test_widget.py
+++ b/products/conversations/backend/api/tests/test_widget.py
@@ -1299,16 +1299,18 @@ class TestWidgetIdentityVerification(BaseTest):
@parameterized.expand(
[
- ("zendesk", None, Channel.EMAIL, 123),
- ("slack", True, Channel.SLACK, None),
+ ("zendesk", None, Channel.EMAIL, 123, False),
+ ("slack", True, Channel.SLACK, None, False),
+ ("outbound_email_owned_by_distinct_id", None, Channel.EMAIL, None, True),
]
)
def test_send_message_to_bridged_ticket_preserves_channel_identity(
- self, _name, identity_verified, channel_source, zendesk_ticket_id
+ self, _name, identity_verified, channel_source, zendesk_ticket_id, owned_by_distinct_id
):
+ ticket_distinct_id = self.distinct_id if owned_by_distinct_id else email
ticket = self._create_email_ticket(
- email,
+ ticket_distinct_id,
identity_verified=identity_verified,
channel_source=channel_source,
zendesk_ticket_id=zendesk_ticket_id,
@@ -1321,14 +1323,16 @@ class TestWidgetIdentityVerification(BaseTest):
**self._email_claim(email),
"message": "Follow-up",
"ticket_id": str(ticket.id),
+ "traits": {"email": "[email protected]"},
},
**self._get_headers(),
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
ticket.refresh_from_db()
- self.assertEqual(ticket.distinct_id, email)
+ self.assertEqual(ticket.distinct_id, ticket_distinct_id)
self.assertEqual(ticket.identity_verified, identity_verified)
+ self.assertEqual(ticket.anonymous_traits, {})
def test_mark_read_email_bridge_allows_verified_ticket(self):
diff --git a/products/conversations/backend/api/widget.py b/products/conversations/backend/api/widget.py
index dc483f7e95d6..87786ba5499a 100644
--- a/products/conversations/backend/api/widget.py
+++ b/products/conversations/backend/api/widget.py
@@ -56,7 +56,7 @@ from products.conversations.backend.cache import (
set_cached_tickets,
)
from products.conversations.backend.models import SigningSecret, Ticket
-from products.conversations.backend.models.constants import WORKFLOW_AUTHOR_TYPE, ChannelDetail
+from products.conversations.backend.models.constants import WORKFLOW_AUTHOR_TYPE, Channel, ChannelDetail
from products.conversations.backend.services.identity import (
canonicalize_claim_value,
identity_claim_has_expired,
@@ -397,8 +397,9 @@ class WidgetMessageView(APIView):
if ownership_match == "distinct_id" and ticket.distinct_id != distinct_id:
ticket.distinct_id = distinct_id
- # Update traits if provided
- if traits:
+ # Other channels set traits from the attested sender, and the email bridge lets a
+ # viewer post to those tickets, so only a widget ticket takes traits from the request.
+ if traits and ticket.channel_source == Channel.WIDGET:
ticket.anonymous_traits.update(traits)
# Update session data if provided
@@ -407,8 +408,9 @@ class WidgetMessageView(APIView):
if session_context:
ticket.session_context.update(session_context)
- # HMAC-verified requests are server-attested — mark the identity trusted.
- if ownership_match == "distinct_id":
+ # The HMAC attests only the distinct ID. On other channels identity_verified attests the
+ # sender's address or platform account, and this request proves neither.
+ if ownership_match == "distinct_id" and ticket.channel_source == Channel.WIDGET:
ticket.identity_verified = True
# Increment unread count for team (customer sent a message)
diff --git a/products/conversations/frontend/components/IdentityBadge/IdentityBadge.tsx b/products/conversations/frontend/components/IdentityBadge/IdentityBadge.tsx
index 275fdbcdb91d..d09edb7003e2 100644
--- a/products/conversations/frontend/components/IdentityBadge/IdentityBadge.tsx
+++ b/products/conversations/frontend/components/IdentityBadge/IdentityBadge.tsx
@@ -4,7 +4,7 @@ import { LemonTag, Tooltip } from '@posthog/lemon-ui'
const DOCS_URL = 'https://posthog.com/docs/support/widget#identity-verification'
const VERIFIED_TEXT =
- "Verified identity — PostHog confirmed this person controls the identity on the ticket (a signed widget request, an authenticated email, or a validated Slack, Teams, or GitHub event). You can trust who you're talking to."
+ "Verified identity — PostHog confirmed this person controls the identity on the ticket (a signed widget request, an authenticated email, or a validated Slack, Teams, or GitHub event). For a widget ticket, the check covers the signed-in account. The name and email on the ticket come from the form and aren't checked."
const UNVERIFIED_TEXT =
"Unverified identity — this ticket was raised anonymously, so the claimed identity may not belong to this person. Confirm who you're talking to before sharing account details."
const UNKNOWN_TEXT =
diff --git a/products/conversations/frontend/scenes/ticket/StaffActionsPanel.tsx b/products/conversations/frontend/scenes/ticket/StaffActionsPanel.tsx
index 3851aa84e5a5..8d004397148f 100644
--- a/products/conversations/frontend/scenes/ticket/StaffActionsPanel.tsx
+++ b/products/conversations/frontend/scenes/ticket/StaffActionsPanel.tsx
@@ -22,7 +22,13 @@ export function StaffActionsPanel(): JSX.Element {
<span className="text-xs text-muted-alt break-all">
{ticketContext?.email ? (
<>
- Customer: <span className="text-success">{ticketContext.email}</span>
+ Customer:{' '}
+ <span
+ className={ticketContext.isEmailUnverified ? undefined : 'text-success'}
+ >
+ {ticketContext.email}
+ </span>
+ {ticketContext.isEmailUnverified && ' (not verified)'}
</>
) : (
'No customer email on this ticket'
diff --git a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
index 57856cff6691..bf8d8e5d32d3 100644
--- a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
+++ b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.test.ts
@@ -18,7 +18,12 @@ import { ActivityScope } from '~/types'
import type { TicketAssignee } from '../../components/Assignee'
import type { Ticket, TicketStatus } from '../../types'
-import { EmailReplyBlockedReason, getEmailReplyBlockedReason, supportTicketSceneLogic } from './supportTicketSceneLogic'
+import {
+ EmailReplyBlockedReason,
+ getEmailReplyBlockedReason,
+ getImpersonationTicketContext,
+ supportTicketSceneLogic,
+} from './supportTicketSceneLogic'
const FEEDBACK_STORAGE_KEY = 'conversations_ai_reply_feedback'
@@ -461,6 +466,112 @@ describe('getEmailReplyBlockedReason', () => {
})
})
+describe('getImpersonationTicketContext', () => {
+ type ContextTicket = Parameters<typeof getImpersonationTicketContext>[0]
+
+ const widgetTicket = (overrides: Partial<ContextTicket> = {}): ContextTicket => ({
+ id: 'ticket-1',
+ channel_source: 'widget',
+ distinct_id: 'user-distinct-id',
+ identity_verified: true,
+ anonymous_traits: { email: '[email protected]' },
+ ...overrides,
+ })
+
+ // A server check attests the distinct ID of a verified ticket, but a form can supply the traits
+ // email. Using that email, or the distinct ID of a ticket nothing attested, points the staff
+ // lookup at an account the sender picked.
+ test.each<[string, ContextTicket, ReturnType<typeof getImpersonationTicketContext>]>([
+ [
+ 'verified widget ticket carries its signed distinct ID',
+ widgetTicket(),
+ {
+ ticketId: 'ticket-1',
+ email: '[email protected]',
+ isEmailUnverified: true,
+ verifiedDistinctId: 'user-distinct-id',
+ },
+ ],
+ [
+ 'unverified widget ticket has no signed distinct ID',
+ widgetTicket({ identity_verified: false }),
+ { ticketId: 'ticket-1', email: '[email protected]', isEmailUnverified: true },
+ ],
+ [
+ 'verified email ticket keeps the email lookup',
+ widgetTicket({
+ channel_source: 'email',
+ distinct_id: '[email protected]',
+ anonymous_traits: {},
+ email_from: '[email protected]',
+ }),
+ {
+ ticketId: 'ticket-1',
+ email: '[email protected]',
+ isEmailUnverified: false,
+ verifiedDistinctId: '[email protected]',
+ },
+ ],
+ [
+ 'Slack ticket with a changed email keeps the sender lookup',
+ widgetTicket({
+ channel_source: 'slack',
+ distinct_id: '[email protected]',
+ anonymous_traits: { email: '[email protected]' },
+ }),
+ {
+ ticketId: 'ticket-1',
+ email: '[email protected]',
+ isEmailUnverified: true,
+ verifiedDistinctId: '[email protected]',
+ },
+ ],
+ [
+ 'unauthenticated email ticket has no verified sender',
+ widgetTicket({
+ channel_source: 'email',
+ distinct_id: '[email protected]',
+ identity_verified: false,
+ anonymous_traits: { email: '[email protected]' },
+ email_from: '[email protected]',
+ }),
+ { ticketId: 'ticket-1', email: '[email protected]', isEmailUnverified: true },
+ ],
+ [
+ 'outbound email ticket verified by a reply looks up its sender',
+ widgetTicket({
+ channel_source: 'email',
+ distinct_id: 'recipient-analytics-id',
+ anonymous_traits: { email: '[email protected]' },
+ email_from: '[email protected]',
+ }),
+ {
+ ticketId: 'ticket-1',
+ email: '[email protected]',
+ isEmailUnverified: false,
+ verifiedDistinctId: '[email protected]',
+ },
+ ],
+ [
+ 'non-string traits email falls back to email_from',
+ widgetTicket({
+ channel_source: 'email',
+ distinct_id: '[email protected]',
+ anonymous_traits: { email: 42 },
+ email_from: '[email protected]',
+ }),
+ {
+ ticketId: 'ticket-1',
+ email: '[email protected]',
+ isEmailUnverified: false,
+ verifiedDistinctId: '[email protected]',
+ },
+ ],
+ ])('%s', (_name, ticket, expected) => {
+ expect(getImpersonationTicketContext(ticket)).toEqual(expected)
+ })
+})
+
describe('supportTicketSceneLogic replyRecipientDescription', () => {
let logic: ReturnType<typeof supportTicketSceneLogic.build>
diff --git a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
index 864be8ce4986..c39b85fa48b6 100644
--- a/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
+++ b/products/conversations/frontend/scenes/ticket/supportTicketSceneLogic.ts
@@ -36,7 +36,10 @@ import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
-import { impersonationNoticeLogic } from '~/layout/navigation/ImpersonationNotice/impersonationNoticeLogic'
+import {
+ ImpersonationTicketContext,
+ impersonationNoticeLogic,
+} from '~/layout/navigation/ImpersonationNotice/impersonationNoticeLogic'
import api, { ApiConfig } from '~/lib/api'
import { PERSON_DISPLAY_NAME_COLUMN_NAME } from '~/lib/constants'
import { CLOUD_HOSTNAMES } from '~/lib/constants'
@@ -81,6 +84,7 @@ import type {
ChatMessage,
MessageDeliveryStatus,
Ticket,
+ TicketChannel,
TicketPriority,
TicketStatus,
} from '../../types'
@@ -137,6 +141,47 @@ function regionFromUrl(url?: string): Region | undefined {
return undefined
}
+// A verified ticket on these channels holds an attested identity that the admin user search matches:
+// the signed distinct ID of a widget request, or the sender's address. A GitHub ticket holds a GitHub
+// login in distinct_id, which matches no PostHog user.
+const ATTESTED_LOOKUP_CHANNELS: TicketChannel[] = ['widget', 'email', 'slack', 'teams']
+
+export function getImpersonationTicketContext(
+ ticket: Pick<
+ Ticket,
+ | 'id'
+ | 'channel_source'
+ | 'distinct_id'
+ | 'identity_verified'
+ | 'anonymous_traits'
+ | 'email_from'
+ | 'session_context'
+ >
+): ImpersonationTicketContext {
+ // The ticket update API stores traits as any JSON value, so the email trait can be a non-string.
+ const traitEmail = ticket.anonymous_traits?.email
+ // email_from is the customer's address on email tickets, and it's the only
+ // place it lives on tickets whose traits were never populated.
+ const email = (typeof traitEmail === 'string' && traitEmail) || ticket.email_from || ''
+ // An outbound email ticket keeps the recipient's analytics ID in distinct_id. A later authenticated
+ // reply from email_from verifies the ticket, so email_from is the attested identity on email tickets.
+ const attestedIdentity =
+ ticket.channel_source === 'email' ? ticket.email_from || ticket.distinct_id : ticket.distinct_id
+ const verifiedDistinctId =
+ ticket.identity_verified === true && ATTESTED_LOOKUP_CHANNELS.includes(ticket.channel_source)
+ ? attestedIdentity || undefined
+ : undefined
+ return {
+ ticketId: ticket.id,
+ email,
+ region: regionFromUrl(ticket.session_context?.current_url),
+ // The widget form supplies the traits email, so the sender can enter the address of another
+ // account. The email counts as verified only when it is the attested identity.
+ isEmailUnverified: !verifiedDistinctId || email.toLowerCase() !== verifiedDistinctId.toLowerCase(),
+ verifiedDistinctId,
+ }
+}
+
function createEventsQuery(personId: string, sessionId?: string, ticketCreatedAt?: string): DataTableNode {
// Show events around ticket creation time (5 min before/after) or last 24h if no timestamp
const after = ticketCreatedAt ? new Date(new Date(ticketCreatedAt).getTime() - 5 * 60 * 1000).toISOString() : '-24h'
@@ -1301,13 +1346,7 @@ export const supportTicketSceneLogic = kea<supportTicketSceneLogicType>([
actions.setTicket(ticket)
actions.loadMessages()
- impersonationNoticeLogic.findMounted()?.actions.setTicketContext({
- ticketId: ticket.id,
- // email_from is the customer's address on email tickets, and it's the only
- // place it lives on tickets whose traits were never populated.
- email: ticket.anonymous_traits?.email || ticket.email_from || '',
- region: regionFromUrl(ticket.session_context?.current_url),
- })
+ impersonationNoticeLogic.findMounted()?.actions.setTicketContext(getImpersonationTicketContext(ticket))
// Load session context data
actions.loadPerson()