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(cdp): treat malformed transactional email recipients as unreachable (#125908)

Daniel Marchuk6bdb2d311b1d
diff --git a/nodejs/src/cdp/cdp-api.serial.test.ts b/nodejs/src/cdp/cdp-api.serial.test.ts
index 5366677985c9..288a88ffb517 100644
--- a/nodejs/src/cdp/cdp-api.serial.test.ts
+++ b/nodejs/src/cdp/cdp-api.serial.test.ts
@@ -16,6 +16,7 @@ import { createRedisV2PoolFromConfig } from '~/common/redis/redis-v2'
import { closeHub, createHub } from '~/common/utils/db/hub'
import { PostgresUse } from '~/common/utils/db/postgres'
import { parseJSON } from '~/common/utils/json-parse'
+import { logger } from '~/common/utils/logger'
import { UUIDT } from '~/common/utils/utils'
import { waitForExpect } from '~/tests/helpers/expectations'
@@ -2446,6 +2447,41 @@ describe('CDP API', () => {
expect(mockQueueInvocations).toHaveBeenCalledTimes(1)
})
+ it.each([
+ ['a valid address', { to: '[email protected]' }, 202, 'queued'],
+ ['a domain with a fullwidth dot', { to: 'user@ph\uFF0Eexample.com' }, 422, 'recipient_unreachable'],
+ ['an ideographic full stop', { to: 'user@ph\u3002example.com' }, 422, 'recipient_unreachable'],
+ ['a zero-width space', { to: 'user\[email protected]' }, 422, 'recipient_unreachable'],
+ ['no recipient', { to: undefined }, 400, 'invalid_request'],
+ [
+ 'a bad address and no distinct_id',
+ { to: 'user@ph\u3002example.com', distinct_id: '' },
+ 400,
+ 'invalid_request',
+ ],
+ ] as const)('answers a send to %s', async (_desc, overrides, expectedStatus, outcome) => {
+ const warn = jest.spyOn(logger, 'warn')
+ const before = await outcomeCount(outcome)
+
+ const res = await send(
+ hogFlow.id,
+ { Authorization: `Bearer ${mintToken(team.id, hogFlow.id)}` },
+ { ...body, ...overrides }
+ )
+
+ expect(res.status).toEqual(expectedStatus)
+ expect(await outcomeCount(outcome)).toEqual(before + 1)
+ expect(mockQueueInvocations).toHaveBeenCalledTimes(expectedStatus === 202 ? 1 : 0)
+ if (expectedStatus === 422) {
+ expect(res.body).toEqual({ error: 'recipient_unreachable' })
+ }
+ const validationLogs = warn.mock.calls.filter((args) =>
+ args.includes('Transactional email request failed validation')
+ )
+ expect(validationLogs).toHaveLength(expectedStatus === 400 ? 1 : 0)
+ expect(JSON.stringify(validationLogs)).not.toContain('example.com')
+ })
+
it('sends to a recipient whose only suppression is a spam complaint', async () => {
await hub.postgres.query(
PostgresUse.COMMON_WRITE,
diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.ts
index bbde6e02e343..ccf4d277cf6d 100644
--- a/nodejs/src/cdp/cdp-api.ts
+++ b/nodejs/src/cdp/cdp-api.ts
@@ -1178,6 +1178,17 @@ export class CdpApi {
stages.start('validate')
const parsed = TransactionalEmailRequestSchema.safeParse(req.body)
if (!parsed.success) {
+ const issues = parsed.error.issues
+ // Signup accepts some addresses this check refuses, and no retry can fix an address.
+ if (issues.every((issue) => issue.path[0] === 'to' && issue.code === 'invalid_format')) {
+ return reply('recipient_unreachable', 422, { error: 'recipient_unreachable' })
+ }
+ // Paths and codes only: the values include the recipient's address.
+ logger.warn('⚠️', 'Transactional email request failed validation', {
+ team_id: req.params.team_id,
+ hog_flow_id: req.params.id,
+ issues: issues.map((issue) => ({ path: issue.path.join('.'), code: issue.code })),
+ })
return reply('invalid_request', 400, { error: parsed.error.message })
}
const { to, distinct_id: distinctId, variables, idempotency_key: idempotencyKey } = parsed.data