: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(workflows): keep an existing dmarc record when auto-configuring email dns (#109695)
Michael Reichenbach77f6f1d40436
diff --git a/posthog/api/integration.py b/posthog/api/integration.pyindex 8e95d3af37b1..f27fb4c1319b 100644--- a/posthog/api/integration.py+++ b/posthog/api/integration.py@@ -2529,6 +2529,7 @@ class IntegrationViewSet(host=resolved.host,provider_endpoint=provider_endpoint,redirect_uri=redirect_uri,+ group_ids=resolved.group_ids,)except DomainConnectSigningKeyMissing as e:capture_exception(diff --git a/posthog/domain_connect.py b/posthog/domain_connect.pyindex deb967b4afde..5a18c7390b17 100644--- a/posthog/domain_connect.py+++ b/posthog/domain_connect.py@@ -20,6 +20,7 @@ import dns.resolverfrom cryptography.hazmat.primitives import hashes, serializationfrom cryptography.hazmat.primitives.asymmetric import paddingfrom cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey+from dns.rdtypes.txtbase import TXTBasefrom posthog.schema import DomainConnectProviderName@@ -102,6 +103,7 @@ def build_sync_apply_url(redirect_uri: str | None = None,private_key: RSAPrivateKey | None = None,key_id: str | None = None,+ group_ids: tuple[str, ...] = (),) -> str:"""Build a Domain Connect synchronous apply URL.@@ -112,6 +114,8 @@ def build_sync_apply_url(prefixes it to the host of each template record, which scopes the template to asubdomain. Templates that set hostRequired must get one.+ If group_ids is set, the provider applies only those template groups instead of all of them.+If private_key is provided, the query string is signed with RS256 andsig= / key= parameters are appended (required by providers like Cloudflare)."""@@ -121,6 +125,8 @@ def build_sync_apply_url(if host:params["host"] = hostparams.update(variables)+ if group_ids:+ params["groupId"] = ",".join(group_ids)if redirect_uri:params["redirect_uri"] = redirect_uri@@ -221,6 +227,11 @@ class DomainConnectContext:host: strservice_id: strvariables: dict[str, str]+ group_ids: tuple[str, ...] = ()+++# Cloudflare ignores TXT conflict matching and would publish a second, invalidating DMARC record.+EMAIL_TEMPLATE_GROUPS_WITHOUT_DMARC: tuple[str, ...] = ("verification", "dkim", "spf", "mailfrom")def resolve_email_context(integration_id: int, team_id: int) -> DomainConnectContext:@@ -268,6 +279,9 @@ def resolve_email_context(integration_id: int, team_id: int) -> DomainConnectCon"mailFromSub": mail_from_subdomain,"sesRegion": ses_region,}+ group_ids = (+ () if _dmarc_absence_confirmed(instance.config.get("domain", "")) else EMAIL_TEMPLATE_GROUPS_WITHOUT_DMARC+ )# The template variables are bare SES tokens and a subdomain label, so they stay# the same when the records move from the full sender domain to root plus host.return DomainConnectContext(@@ -275,6 +289,7 @@ def resolve_email_context(integration_id: int, team_id: int) -> DomainConnectConhost=domain_parts.host,service_id=service_id,variables=variables,+ group_ids=group_ids,)@@ -307,6 +322,7 @@ def generate_apply_url(host: str | None = None,provider_endpoint: str | None = None,redirect_uri: str | None = None,+ group_ids: tuple[str, ...] = (),) -> str:"""Generate a Domain Connect apply URL, either via auto-discovery or a specific provider.@@ -349,9 +365,25 @@ def generate_apply_url(redirect_uri=redirect_uri,private_key=signing_key,key_id=key_id,+ group_ids=group_ids,)+def _txt_value(rdata: TXTBase) -> str:+ return "".join(s.decode("utf-8") if isinstance(s, bytes) else s for s in rdata.strings).strip()+++def _dmarc_absence_confirmed(domain: str) -> bool:+ try:+ answers = dns.resolver.resolve(f"_dmarc.{domain}", "TXT", lifetime=5)+ return not any(_txt_value(rdata).lower().startswith("v=dmarc1") for rdata in answers)+ except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):+ return True+ except Exception:+ logger.warning("DMARC lookup failed for %s", domain, exc_info=True)+ return False++def _lookup_domain_connect_endpoint(domain: str) -> str | None:"""DNS TXT lookup for _domainconnect.{domain}.@@ -360,10 +392,9 @@ def _lookup_domain_connect_endpoint(domain: str) -> str | None:try:answers = dns.resolver.resolve(f"_domainconnect.{domain}", "TXT")for rdata in answers:- # TXT records come as a list of strings; join them- txt_value = "".join(s.decode("utf-8") if isinstance(s, bytes) else s for s in rdata.strings)+ txt_value = _txt_value(rdata)if txt_value:- return txt_value.strip()+ return txt_valueexcept (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers, dns.resolver.Timeout):passexcept Exception:diff --git a/posthog/test/test_domain_connect.py b/posthog/test/test_domain_connect.pyindex 284ef8a7778f..ae726bdeba9e 100644--- a/posthog/test/test_domain_connect.py+++ b/posthog/test/test_domain_connect.py@@ -2,12 +2,14 @@ import reimport jsonimport base64from pathlib import Path+from urllib.parse import unquotefrom posthog.test.base import BaseTestfrom unittest.mock import MagicMock, patchfrom django.core.cache import cache+import dns.resolverfrom cryptography.hazmat.primitives import hashes as crypto_hashesfrom cryptography.hazmat.primitives.asymmetric import (padding as asym_padding,@@ -17,6 +19,7 @@ from parameterized import parameterizedfrom posthog.domain_connect import (DOMAIN_CONNECT_PROVIDERS,+ EMAIL_TEMPLATE_GROUPS_WITHOUT_DMARC,DomainConnectSigningKeyMissing,build_sync_apply_url,discover_domain_connect,@@ -138,10 +141,19 @@ class TestBuildSyncApplyUrl(BaseTest):host="ph",private_key=private_key,key_id="_dcpubkeyv1",+ group_ids=("dkim", "spf"),)- self.assertIn("sig=", url)- self.assertIn("key=_dcpubkeyv1", url)+ signed_query, signature_params = url.split("?", 1)[1].split("&sig=", 1)+ self.assertIn("groupId=dkim%2Cspf", signed_query)+ self.assertIn("key=_dcpubkeyv1", signature_params)+ signature = unquote(signature_params.split("&key=", 1)[0])+ private_key.public_key().verify(+ base64.b64decode(signature),+ signed_query.encode("utf-8"),+ asym_padding.PKCS1v15(),+ crypto_hashes.SHA256(),+ )def test_url_without_signing_key_has_no_sig(self) -> None:url = build_sync_apply_url(@@ -335,8 +347,51 @@ class TestTemplateResolverAlignment(BaseTest):@parameterized.expand([- ("subdomain sender", "posthog.com.email-verification-us.json", "US", "news.example.com", "news"),- ("root domain sender", "posthog.com.email-verification-eu.json", "EU", "example.com", ""),+ (+ "subdomain sender without a dmarc record",+ "posthog.com.email-verification-us.json",+ "US",+ "news.example.com",+ "news",+ dns.resolver.NXDOMAIN(),+ True,+ ),+ (+ "root sender without a dmarc record",+ "posthog.com.email-verification-eu.json",+ "EU",+ "example.com",+ "",+ dns.resolver.NoAnswer(),+ True,+ ),+ (+ "sender with an existing dmarc record",+ "posthog.com.email-verification-us.json",+ "US",+ "news.example.com",+ "news",+ [MagicMock(strings=[b"v=DMARC1; p=reject;"])],+ False,+ ),+ (+ "sender whose dmarc lookup times out",+ "posthog.com.email-verification-eu.json",+ "EU",+ "example.com",+ "",+ dns.resolver.Timeout(),+ False,+ ),+ (+ "sender whose dmarc record is not utf-8",+ "posthog.com.email-verification-us.json",+ "US",+ "news.example.com",+ "news",+ [MagicMock(strings=[b"\xff"])],+ False,+ ),])@patch("posthog.models.integration.EmailIntegration")@@ -348,6 +403,8 @@ class TestTemplateResolverAlignment(BaseTest):region: str,sender_domain: str,expected_host: str,+ dmarc_lookup: Exception | list[MagicMock],+ applies_dmarc: bool,mock_integration_cls: MagicMock,mock_email_cls: MagicMock,) -> None:@@ -375,9 +432,18 @@ class TestTemplateResolverAlignment(BaseTest):}mock_email_cls.return_value = mock_email- with self.settings(CLOUD_DEPLOYMENT=region, SES_REGION="us-east-1"):+ with (+ self.settings(CLOUD_DEPLOYMENT=region, SES_REGION="us-east-1"),+ patch("posthog.domain_connect.dns.resolver.resolve") as mock_resolve,+ ):+ if isinstance(dmarc_lookup, Exception):+ mock_resolve.side_effect = dmarc_lookup+ else:+ mock_resolve.return_value = dmarc_lookupresolved = resolve_email_context(1, 1)+ mock_resolve.assert_called_once_with(f"_dmarc.{sender_domain}", "TXT", lifetime=5)+self.assertEqual(set(resolved.variables.keys()), expected_vars)self.assertEqual(resolved.service_id, template["serviceId"])self.assertEqual(resolved.root_domain, "example.com")@@ -385,3 +451,10 @@ class TestTemplateResolverAlignment(BaseTest):self.assertEqual(resolved.variables["verifyToken"], "verify-token-123")self.assertEqual(resolved.variables["dkim1"], "aaa")self.assertEqual(resolved.variables["mailFromSub"], "feedback")++ template_groups = {record["groupId"] for record in template["records"]}+ if applies_dmarc:+ self.assertEqual(resolved.group_ids, ())+ else:+ self.assertEqual(resolved.group_ids, EMAIL_TEMPLATE_GROUPS_WITHOUT_DMARC)+ self.assertEqual(template_groups - set(resolved.group_ids), {"dmarc"})