: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
fix(email): keep email text checks linear on long input (#127392)
Yasen4beb761f6cd5
diff --git a/frontend/src/generated/core/api.schemas.ts b/frontend/src/generated/core/api.schemas.tsindex 55427278f789..ac00d414a4dc 100644--- a/frontend/src/generated/core/api.schemas.ts+++ b/frontend/src/generated/core/api.schemas.ts@@ -502,7 +502,11 @@ export interface OrganizationInviteApi {readonly created_by: UserBasicApireadonly created_at: stringreadonly updated_at: string- /** @nullable */+ /**+ * Optional personal message in the invite email.+ * @maxLength 1000+ * @nullable+ */message?: string | null/** List of team IDs and corresponding access levels to private projects. */private_project_access?: unknowndiff --git a/frontend/src/generated/core/api.zod.ts b/frontend/src/generated/core/api.zod.tsindex 2447131fe4ff..ba01c7784d11 100644--- a/frontend/src/generated/core/api.zod.ts+++ b/frontend/src/generated/core/api.zod.ts@@ -434,6 +434,8 @@ export const invitesCreateBodyTargetEmailMax = 254export const invitesCreateBodyFirstNameMax = 30+export const invitesCreateBodyMessageMax = 1000+export const invitesCreateBodySendEmailDefault = trueexport const invitesCreateBodyCombinePendingInvitesDefault = false@@ -444,7 +446,11 @@ export const InvitesCreateBody = /* @__PURE__ */ zod.object({.union([zod.literal(1), zod.literal(8), zod.literal(15)]).optional().describe('\* `1` - member\n\* `8` - administrator\n\* `15` - owner'),- message: zod.string().nullish(),+ message: zod+ .string()+ .max(invitesCreateBodyMessageMax)+ .nullish()+ .describe('Optional personal message in the invite email.'),private_project_access: zod.unknown().optional()@@ -457,6 +463,8 @@ export const invitesBulkCreateBodyTargetEmailMax = 254export const invitesBulkCreateBodyFirstNameMax = 30+export const invitesBulkCreateBodyMessageMax = 1000+export const invitesBulkCreateBodySendEmailDefault = trueexport const invitesBulkCreateBodyCombinePendingInvitesDefault = false@@ -467,7 +475,11 @@ export const InvitesBulkCreateBody = /* @__PURE__ */ zod.object({.union([zod.literal(1), zod.literal(8), zod.literal(15)]).optional().describe('\* `1` - member\n\* `8` - administrator\n\* `15` - owner'),- message: zod.string().nullish(),+ message: zod+ .string()+ .max(invitesBulkCreateBodyMessageMax)+ .nullish()+ .describe('Optional personal message in the invite email.'),private_project_access: zod.unknown().optional()diff --git a/posthog/api/organization_invite.py b/posthog/api/organization_invite.pyindex 35a5ddd4c2c1..991b61459264 100644--- a/posthog/api/organization_invite.py+++ b/posthog/api/organization_invite.py@@ -192,7 +192,10 @@ class OrganizationInviteSerializer(serializers.ModelSerializer):"created_at","updated_at",]- extra_kwargs = {"target_email": {"required": True, "allow_null": False}}+ extra_kwargs = {+ "target_email": {"required": True, "allow_null": False},+ "message": {"max_length": 1000, "help_text": "Optional personal message in the invite email."},+ }def validate_target_email(self, email: str):email = EmailNormalizer.normalize(email)diff --git a/posthog/api/test/test_organization_invites.py b/posthog/api/test/test_organization_invites.pyindex f4dad5b3ae77..596fe7bd92a9 100644--- a/posthog/api/test/test_organization_invites.py+++ b/posthog/api/test/test_organization_invites.py@@ -621,6 +621,17 @@ class TestOrganizationInvitesAPI(APIBaseTest):self.assertEqual(OrganizationInvite.objects.count(), count)+ def test_invite_creation_rejects_message_over_length_limit(self):+ count = OrganizationInvite.objects.count()+ response = self.client.post(+ f"/api/organizations/{self.organization.id}/invites/",+ {"target_email": "[email protected]", "message": "a" * 1001},+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["attr"], "message")+ self.assertEqual(response.json()["code"], "max_length")+ self.assertEqual(OrganizationInvite.objects.count(), count)+@parameterized.expand([("plus_addressed", "[email protected]", "plus_addressing_not_allowed"),diff --git a/posthog/helpers/email_utils.py b/posthog/helpers/email_utils.pyindex 9d3cc2a01950..49fb856215a7 100644--- a/posthog/helpers/email_utils.py+++ b/posthog/helpers/email_utils.py@@ -21,6 +21,7 @@ from django.core.exceptions import MultipleObjectsReturnedfrom django.db.models import CharField, F, Func, Q, QuerySet, Valuefrom django.db.models.functions import Lower, Replace+import re2import requestsimport structlogimport posthoganalytics@@ -32,20 +33,26 @@ if TYPE_CHECKING:logger = structlog.get_logger(__name__)+# The lookbehind and the possessive repeats scan each run of scheme characters once, which keeps the scan linear._URL_SCHEME_RE = re.compile(- r"[a-z][a-z0-9+.\-]*://"+ r"(?<![a-z0-9+.\-])[0-9+.\-]*+[a-z][a-z0-9+.\-]*+://"r"|\b(?:javascript|data|vbscript|file|ftp|mailto|tel|sms):"r"|www\.",re.IGNORECASE,)-_BARE_DOMAIN_RE = re.compile(- r"\b(?:[a-z0-9](?:[a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,24}\b",- re.IGNORECASE,+# re2 keeps the scan linear. Do not limit the label count to get this with `re`, because a match+# would then stop inside a long chain and leave the domain at its end linkable.+# re2 case folding excludes `ı` and `İ`, so the pattern names them. Its `\b` treats them as non-word+# characters, so the pattern has no `\b`. Without `\b`, some text gets an extra invisible defang.+_BARE_DOMAIN_RE = re2.compile(+ r"(?i)(?:[a-z0-9\x{130}\x{131}](?:[a-z0-9\x{130}\x{131}\-]{0,61}[a-z0-9\x{130}\x{131}])?\.)+"+ r"[a-z\x{130}\x{131}]{2,24}")_CONTROL_CHAR_RE = re.compile(r"[\x00-\x1f\x7f\u0085\u2028\u2029]")_NON_NEWLINE_CONTROL_RE = re.compile(r"[\x00-\x08\x0b-\x1f\x7f\u0085\u2028\u2029]")_BRACKET_RE = re.compile(r"[<>]")-_INVISIBLE_CHAR_RE = re.compile(r"[\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]")+# Includes lone surrogates, because re2 raises UnicodeEncodeError on them.+_INVISIBLE_CHAR_RE = re.compile(r"[\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff\ud800-\udfff]")_URL_ERROR = "URLs are not allowed in this field."_CONTROL_ERROR = "Line breaks and control characters are not allowed in this field."@@ -112,18 +119,6 @@ def validate_message_body(value: str | None) -> str | None:return value-def contains_bare_domain(value: str | None) -> bool:- """- True if `value` contains a bare domain (e.g. `example.com`) after NFKC- normalization. The `validate_*` helpers above intentionally *allow* bare- domains; callers that want to reject domains entirely (e.g. so mail clients- can't auto-link them in a branded email) layer this check on top.- """- if not value:- return False- return bool(_BARE_DOMAIN_RE.search(unicodedata.normalize("NFKC", value)))--def _extract_error_code(err: serializers.ValidationError) -> str:"""Pull the first `code` off a DRF ValidationError. `err.detail` is a uniondiff --git a/posthog/helpers/tests/test_email_utils.py b/posthog/helpers/tests/test_email_utils.pyindex 7168b205b946..43c849349f06 100644--- a/posthog/helpers/tests/test_email_utils.py+++ b/posthog/helpers/tests/test_email_utils.py@@ -1,6 +1,7 @@from datetime import timedeltafrom typing import Optional, cast+import pytestfrom unittest.mock import MagicMock, patchfrom django.db import connection@@ -654,6 +655,8 @@ class TestValidateMessageBody(SimpleTestCase):("bracket", "hello <there>", "invalid_bracket"),("invisible", "foo\u200bbar", "invalid_invisible_char"),("rtl_override", "foo\u202ebar", "invalid_invisible_char"),+ ("lone_surrogate", "foo\ud83dbar", "invalid_invisible_char"),+ ("long_scheme_of_digits", "a" + "1" * 40 + "://x", "invalid_url"),("non_newline_control", "foo\x01bar", "invalid_control_char"),("carriage_return", "foo\rbar", "invalid_control_char"),("del", "foo\x7fbar", "invalid_control_char"),@@ -801,6 +804,16 @@ class TestSanitizeEmailString(SimpleTestCase):'<img src="x" onerror="javascript:alert(1)">',"<img src="x" onerror="javascript:alert(1)">",),+ ("uppercase_domain", "join EVIL.COM now", "join EVIL.\u200bCOM now"),+ ("long_domain_chain", "pad." * 200 + "evil.com", "pad.\u200b" * 200 + "evil.\u200bcom"),+ ("long_scheme", "a" * 100 + "://x", "a" * 100 + ":\u200b//x"),+ ("long_scheme_of_digits", "a" + "1" * 40 + "://x", "a" + "1" * 40 + ":\u200b//x"),+ ("dotless_i_in_label", "paypa\u0131.com", "paypa\u0131.\u200bcom"),+ ("dotless_i_only_label", "\u0131\u0131.com", "\u0131\u0131.\u200bcom"),+ ("dotted_capital_i_in_tld", "evil.\u0130\u0130", "evil.\u200b\u0130\u0130"),+ ("cjk_before_keyword", "\u65e5data:x", "\u65e5data:x"),+ ("accented_letter_before_domain", "\u00e9vil.com", "\u00e9vil.\u200bcom"),+ ("lone_surrogate", "evil.com \ud83d", "evil.\u200bcom "),])def test_sanitizes(self, _name: str, value: str, expected: str) -> None:@@ -814,3 +827,17 @@ class TestSanitizeEmailString(SimpleTestCase):# the entity, which is the existing trade-off of running sanitize_email_string# over already-sanitized data. Documented as a guardrail.self.assertEqual(sanitize_email_string("&"), "&amp;")+++# A regex that backtracks quadratically cannot finish these inputs inside this limit.+class TestEmailTextChecksOnLongInput(SimpleTestCase):+ @parameterized.expand(+ [+ ("dotted_run", "a." * 500_000),+ ("letter_run", "a" * 1_000_000 + "."),+ ]+ )+ def test_returns_long_input_unchanged(self, _name: str, value: str) -> None:+ self.assertTrue(sanitize_email_string(value) == value)+ self.assertTrue(validate_message_body(value) == value)diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex bf92dc030d7f..5d0f329c5294 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -76957,7 +76957,11 @@ export namespace Schemas {readonly created_by: UserBasic;readonly created_at: string;readonly updated_at: string;- /** @nullable */+ /**+ * Optional personal message in the invite email.+ * @maxLength 1000+ * @nullable+ */message?: string | null;/** List of team IDs and corresponding access levels to private projects. */private_project_access?: unknown;