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): limit custom sender address length (#127081)

Daniel Marchukaa4c970ebc86
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.ts
index 390512f5959d..825423616ecc 100644
--- a/nodejs/src/cdp/services/messaging/email.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts
@@ -347,6 +347,8 @@ describe('EmailService', () => {
})
})
describe('from overrides', () => {
+ const maxLengthEmail = `${'a'.repeat(242)}@posthog.com`
+
it.each<[string, { email?: string; name?: string }, string, string]>([
[
'address on the verified domain',
@@ -354,6 +356,12 @@ describe('EmailService', () => {
'"Test User" <[email protected]>',
],
+ [
+ 'address at the maximum length',
+ { email: maxLengthEmail },
+ `"Test User" <${maxLengthEmail}>`,
+ maxLengthEmail,
+ ],
[
'address with different domain casing',
{ email: '[email protected]' },
@@ -390,13 +398,19 @@ describe('EmailService', () => {
expect(sentCommand.input.FeedbackForwardingEmailAddress).toBe(expectedFeedback)
})
- it.each([
+ it.each<[string, string, string?]>([
['an address on an unverified domain', '[email protected]'],
['an address on a subdomain of the verified domain', '[email protected]'],
['a list of addresses', '[email protected], [email protected]'],
['an RFC-822 formatted address', '"Name" <[email protected]>'],
['a value that is not an email address', 'not-an-email'],
- ])('discards %s and sends from the integration sender', async (_desc, email) => {
+ [
+ 'an address longer than the maximum length',
+ `${'a'.repeat(243)}@posthog.com`,
+ '254 characters or fewer',
+ ],
+ ['an overlong value that is not an email address', 'a'.repeat(255), '254 characters or fewer'],
+ ])('discards %s and sends from the integration sender', async (_desc, email, expectedWarning = email) => {
invocation.queueParameters = createEmailParams({
from: { integrationId: 1, email },
})
@@ -407,7 +421,9 @@ describe('EmailService', () => {
expect(result.error).toBeUndefined()
const sentCommand = sendEmailSpy.mock.calls[0][0] as { input: any }
expect(sentCommand.input.FromEmailAddress).toBe('"Test User" <[email protected]>')
- expect(result.logs.some((log) => log.level === 'warn' && log.message.includes(email))).toBe(true)
+ expect(result.logs.some((log) => log.level === 'warn' && log.message.includes(expectedWarning))).toBe(
+ true
+ )
})
})
describe('email sending', () => {
diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.ts
index d99cf20fa3a8..27ad77432e00 100644
--- a/nodejs/src/cdp/services/messaging/email.service.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.ts
@@ -379,6 +379,7 @@ export function extractEmailsFromAddressList(value: string | undefined): string[
// characters that would let a templated value smuggle a second address or break out of the
// RFC-822 `"Name" <email>` framing (whitespace, quotes, angle brackets, list separators).
const FROM_OVERRIDE_EMAIL_REGEX = /^[^\s@"<>,;]+@[^\s@"<>,;]+\.[^\s@"<>,;]+$/
+const MAX_FROM_OVERRIDE_EMAIL_LENGTH = 254
// The display name is embedded as `"${name}" <email>`, so strip the characters that would
// terminate the quoted phrase or start the address part, plus control characters.
@@ -1086,6 +1087,14 @@ export class EmailService {
return integration.config.email
}
+ if (overrideEmail.length > MAX_FROM_OVERRIDE_EMAIL_LENGTH) {
+ addLog(
+ 'warn',
+ `Ignoring the custom sender address because it is too long. Sending from ${integration.config.email} instead. Use a From address with ${MAX_FROM_OVERRIDE_EMAIL_LENGTH} characters or fewer.`
+ )
+ return integration.config.email
+ }
+
if (!FROM_OVERRIDE_EMAIL_REGEX.test(overrideEmail)) {
addLog(
'warn',
diff --git a/products/cdp/backend/test/test_validation.py b/products/cdp/backend/test/test_validation.py
index 71f041a5908b..6b8c3cfa1ee1 100644
--- a/products/cdp/backend/test/test_validation.py
+++ b/products/cdp/backend/test/test_validation.py
@@ -642,6 +642,8 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
("planted_placeholder", "[email protected]", 'is not on the verified domain "posthog.com"'),
("not_an_email", "not-an-email", "is not a valid email address"),
("address_list", "[email protected], [email protected]", "is not a valid email address"),
+ ("overlong_address", f"{'a' * 243}@posthog.com", "254 characters or fewer"),
+ ("overlong_non_address", "a" * 255, "254 characters or fewer"),
]
)
def test_email_literal_sender_override_rejected_at_save(self, _name, address, expected):
@@ -656,6 +658,7 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
@parameterized.expand(
[
("on_domain", "[email protected]"),
+ ("max_length", f"{'a' * 242}@posthog.com"),
("case_insensitive", "[email protected]"),
("liquid_template", "{{ event.properties.sender }}"),
("hog_template", "{event.properties.sender}"),
diff --git a/products/cdp/backend/validation.py b/products/cdp/backend/validation.py
index eadfba5a5f2c..521fe23f5325 100644
--- a/products/cdp/backend/validation.py
+++ b/products/cdp/backend/validation.py
@@ -81,6 +81,7 @@ def masked_secret_input_keys(stored_inputs: object) -> list[str]:
# Mirrors FROM_OVERRIDE_EMAIL_REGEX in nodejs/src/cdp/services/messaging/email.service.ts, which
# is what the send path enforces after rendering. Keep the two in sync.
FROM_OVERRIDE_EMAIL_REGEX = re.compile(r'^[^\s@"<>,;]+@[^\s@"<>,;]+\.[^\s@"<>,;]+$')
+MAX_FROM_OVERRIDE_EMAIL_LENGTH = 254
def _sender_integration_ids(from_value: dict) -> set[int]:
@@ -136,6 +137,14 @@ def _validate_email_sender_override(from_value: dict, context: dict) -> None:
# enforces the domain.
return
+ if len(override) > MAX_FROM_OVERRIDE_EMAIL_LENGTH:
+ raise serializers.ValidationError(
+ {
+ "input": "The custom sender address is too long. "
+ f"Use an address with {MAX_FROM_OVERRIDE_EMAIL_LENGTH} characters or fewer."
+ }
+ )
+
if not FROM_OVERRIDE_EMAIL_REGEX.match(override):
raise serializers.ValidationError(
{
diff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.py
index 421539358547..390d926a9b34 100644
--- a/products/workflows/backend/tests/api/test_hog_flow.py
+++ b/products/workflows/backend/tests/api/test_hog_flow.py
@@ -994,7 +994,14 @@ class TestHogFlowAPI(APIBaseTest):
for fragment in fragments:
assert fragment in detail, (fragment, detail)
- def test_literal_off_domain_sender_override_is_rejected_at_save(self):
+ @parameterized.expand(
+ [
+ ("off_domain", "[email protected]", 'is not on the verified domain "posthog.com"'),
+ ("overlong_address", f"{'a' * 243}@posthog.com", "254 characters or fewer"),
+ ("overlong_non_address", "a" * 255, "254 characters or fewer"),
+ ]
+ )
+ def test_invalid_literal_sender_override_is_rejected_at_save(self, _name: str, address: str, expected: str) -> None:
# The domain rule lived only in the send path, so a bad literal address saved fine and
# every send then fell back to the integration's sender with a run-log warning the
# author never saw. The save is where the author can still act on it.
@@ -1005,14 +1012,14 @@ class TestHogFlowAPI(APIBaseTest):
config={"email": "[email protected]", "name": "Sender", "domain": "posthog.com", "verified": True},
)
inputs = _valid_email_inputs()
- inputs["email"]["value"]["from"] = {"integrationId": integration.id, "email": "[email protected]"}
+ inputs["email"]["value"]["from"] = {"integrationId": integration.id, "email": address}
hog_flow, action = self._create_hog_flow_with_action({"inputs": inputs})
action["type"] = "function_email"
response = self.client.post(f"/api/projects/{self.team.id}/hog_flows", hog_flow, HTTP_X_POSTHOG_CLIENT="mcp")
assert response.status_code == 400, response.json()
- assert 'is not on the verified domain "posthog.com"' in response.json()["detail"]
+ assert expected in response.json()["detail"]
def test_stored_off_domain_sender_override_survives_a_resave(self):
# Workflows written before June 2026 carry a placeholder address the author never typed.