Skip to content

fix(cdp): treat malformed transactional email recipients as unreachable - #125908

Merged
trunk-io[bot] merged 1 commit into
masterfrom
dmarchuk/cdp-transactional-email-bad-address-unreachable
Oct 29, 2026
Merged

trunk-io[bot] merged 1 commit into
masterfrom
dmarchuk/cdp-transactional-email-bad-address-unreachable

Conversation

@dmarchuk

@dmarchuk dmarchuk commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  • An email address with a look-alike dot or a hidden character (user@ph。example.com) passes Django's validation but fails cdp-api's.
  • The sender then gets a 503 "email pipeline is unavailable, try again shortly", although no retry can ever send it.
  • Our own app emails retry it six times over about 19 minutes, and every attempt counts against the transactional email refusal alert.

Changes

  • cdp-api now answers such an address the same way as an address with no mail server: 422 recipient_unreachable.
  • Django already drops those emails without retrying, and the alert already ignores that outcome.
  • Any other invalid request still gets 400 invalid_request, and cdp-api now logs which fields failed. It logs field names and error codes, never the values.

How did you test this code?

Locally, through Django's send endpoint, then cdp-api, the email worker, maildev and the dev Postgres. Ran on master first, then on this branch.

Send to master this branch
A normal address 202, email arrives in maildev same
user@ph。example.com, plus U+FF0E, U+FE52 and U+2024 dots 503 delivery_unavailable, metric invalid_request +4 422 recipient_unreachable, metric recipient_unreachable +4
The same address through posthog/email.py retryable WorkflowsEmailError, no send record returns cleanly, send record kept unsent
A body with variables as a list no reason logged cdp-api logs issues:[{path:"variables",code:"invalid_type"}]

Not checked: the six Celery retries and the alert itself. Both follow from code this PR doesn't change.

Test rationale: one parameterized case list in the existing transactional email block of cdp-api.serial.test.ts. It fails if a bad address goes back to a 400, if another invalid field gets reported as unreachable, or if the log starts including the address.

Release status

  • No feature flag controls this change

Docs update

  • None.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Agent: Claude Code, claude-opus-5-5

Skills: /posthog-local-e2e-qa, /writing-tests, /writing-pr-descriptions, /writing-plainly

More context for AI agents
  • Decisions: split from the Django logging change in fix(cdp): log the refusal code when a transactional email is rejected #125918 to keep one lane per PR.
  • Follow-ups: cdp-api also refuses legit international domains such as ivan@пример.рф; converting the domain to punycode before the check would fix that.
  • Read closely: the issues.every(...) condition in cdp-api.ts, which decides between 422 and 400.

🤖 Generated with Claude Code

@trunk-io

trunk-io Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

😎 Merged successfully - details.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🤖 CI report

✅ Trunk lane — non-backend lane (node:cdp)

This PR is assigned to the non-backend lane (node:cdp). It does not run backend Python tests and may merge in parallel with PRs in other lanes.

✅ Duplication (Python) — clean

New Python code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

✅ Duplication (TypeScript) — clean

New TypeScript code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

✅ Comment density — 5% of added code lines are comments (2 of 44)

This section warns when comments are more than 3% of the code lines a PR adds, and alerts above 6%. Before agent-assisted PRs, the typical share was about 2%. Only full-line comments count. Docstrings, generated files, snapshots, migrations, and workflow files are left out.

Comments that restate the code, record how the change came about, or narrate the next line add noise for the next reader. Keep the comments that explain a reason the code cannot show, and remove the rest. See .agents/skills/writing-code-comments/SKILL.md for the house rules.

Files with the most added comment lines:

File Comment lines Added lines
nodejs/src/cdp/cdp-api.ts 2 11

This check does not block merging. It updates on every push and clears when the share drops.

@hosthog

hosthog Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

HostHog preview — posthog-desktop-web

The previews for this PR have been torn down and no longer serve.

@dmarchuk
dmarchuk force-pushed the dmarchuk/cdp-transactional-email-bad-address-unreachable branch from 0686d7f to f049ca2 Compare October 29, 2026 21:32
@dmarchuk
dmarchuk marked this pull request as ready for review October 29, 2026 22:03
@parameterai

parameterai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Risk: No findings

This PR changes cdp-api's transactional-email send endpoint to answer a syntactically malformed recipient address with 422 recipient_unreachable (the same outcome as its existing no-mail-server check) instead of 400 invalid_request, and adds a warn log of validation issue paths/codes — never values — for the remaining 400 cases. I traced the new branch end to end (zod schema, classifier condition, check ordering, Django-side error mapping in posthog/email.py) and found no security issue: the 422 fires before any queueing, idempotency, or quota action, only the to field's format failure can reach it, and the log fields are all token-pinned or value-free.

Sentinel reviewed f049ca2 · Review settings

@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team October 29, 2026 22:03
@dmarchuk dmarchuk added the stamphog Request AI approval (no full review) label Oct 29, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

The risky email-send behavior has owning-team authorship assurance and focused regression coverage. No unresolved review concerns or other policy refusal grounds were found.

  • Author wrote 0% of the modified lines and has 38 merged PRs in these paths (familiarity MODERATE).
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
size ✓ 11L, 1F substantive, 47L/2F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1b-small (47L, 2F, single-area, fix)
stamphog 2.6.1 .stamphog/policy.yml @ f049ca2 · reviewed head f049ca2

@trunk-io
trunk-io Bot merged commit 6bdb2d3 into master Oct 29, 2026
356 of 361 checks passed
@trunk-io
trunk-io Bot deleted the dmarchuk/cdp-transactional-email-bad-address-unreachable branch October 29, 2026 23:44
@deployment-status-posthog

deployment-status-posthog Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-10-30 00:04 UTC Run
prod-us ✅ Deployed 2026-10-30 00:15 UTC Run
prod-eu ✅ Deployed 2026-10-30 00:19 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stamphog Request AI approval (no full review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant