The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(email): keep two From-parsing invariants from the #124322 salvage
kshitijk4poor4556de4128ce
diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.pyindex 787118da58d..a528f43dc7a 100644--- a/tests/gateway/test_email.py+++ b/tests/gateway/test_email.py@@ -79,13 +79,6 @@ class TestHelperFunctions(unittest.TestCase):)- def test_extract_email_address_still_reads_a_normal_address(self):- """The fail-closed path must not break the ordinary forms."""- from plugins.platforms.email.adapter import _extract_email_address- self.assertEqual(_extract_email_address("Plain <[email protected]>"), "[email protected]")- self.assertEqual(_extract_email_address("[email protected]"), "[email protected]")- self.assertEqual(_extract_email_address("JOHN DOE <[email protected]>"), "[email protected]")-def test_extract_email_address_unfolds_display_name_before_parsing(self):from plugins.platforms.email.adapter import _extract_email_addressself.assertEqual(@@ -94,15 +87,10 @@ class TestHelperFunctions(unittest.TestCase):),)-- def test_extract_email_address_unfolds_spoof_before_parsing(self):- from plugins.platforms.email.adapter import _extract_email_address- self.assertEqual(- _extract_email_address(- '"Victim\r\n <[email protected]>" <[email protected]>'- ),- "[email protected]",- )+ # Ordinary forms must keep resolving the same way.+ self.assertEqual(_extract_email_address("Plain <[email protected]>"), "[email protected]")+ self.assertEqual(_extract_email_address("[email protected]"), "[email protected]")+ self.assertEqual(_extract_email_address("JOHN DOE <[email protected]>"), "[email protected]")def test_strip_html_basic(self):@@ -405,68 +393,6 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):"auth_reason": "dmarc=pass" if authenticated else "no Authentication-Results header"}))return captured- def test_authenticated_attacker_cannot_impersonate_allowlisted_display_name(self):- """End-to-end: parse -> authenticate -> dispatch, for the spoofed From.-- The attacker's domain passes DMARC truthfully, so the only thing- standing between their mail and a MessageEvent carrying the victim's- identity is that the parser reads the right address.- """- import asyncio- from gateway.config import PlatformConfig- from plugins.platforms.email.adapter import EmailAdapter-- raw = (b'From: "Victim <[email protected]>" <[email protected]>\r\n'- b'Authentication-Results: mx.example.com; dmarc=pass header.from=evil.test\r\n'- b'Subject: run task\r\nMessage-ID: <[email protected]>\r\n\r\nhello')- with patch.dict(os.environ, {- "EMAIL_ADDRESS": "[email protected]", "EMAIL_PASSWORD": "secret",- "EMAIL_IMAP_HOST": "imap.example.com", "EMAIL_SMTP_HOST": "smtp.example.com",- "EMAIL_ALLOWED_USERS": "[email protected]",- }):- adapter = EmailAdapter(PlatformConfig(enabled=True))- captured = []-- async def capture(event):- captured.append(event)-- adapter.handle_message = capture- parsed = adapter._parse_fetched_message(b"1", raw)- self.assertEqual(parsed["sender_addr"], "[email protected]")- self.assertTrue(parsed["sender_authenticated"])- asyncio.run(adapter._dispatch_message(parsed))- self.assertEqual(captured, [])-- def test_folded_allowlisted_sender_still_dispatches(self):- """A folded display name must not turn a legitimate sender into a non-address."""- import asyncio- from gateway.config import PlatformConfig- from plugins.platforms.email.adapter import EmailAdapter-- raw = (- b'From: "Some Very Long Display Name That Exceeds The Line\r\n'- b' Limit" <[email protected]>\r\n'- b'Authentication-Results: mx.example.com; dmarc=pass header.from=example.com\r\n'- b'Subject: hello\r\nMessage-ID: <[email protected]>\r\n\r\nhello'- )- with patch.dict(os.environ, {- "EMAIL_ADDRESS": "[email protected]", "EMAIL_PASSWORD": "secret",- "EMAIL_IMAP_HOST": "imap.example.com", "EMAIL_SMTP_HOST": "smtp.example.com",- "EMAIL_ALLOWED_USERS": "[email protected]",- }):- adapter = EmailAdapter(PlatformConfig(enabled=True))- captured = []-- async def capture(event):- captured.append(event)-- adapter.handle_message = capture- parsed = adapter._parse_fetched_message(b"2", raw)- self.assertEqual(parsed["sender_addr"], "[email protected]")- self.assertTrue(parsed["sender_authenticated"])- asyncio.run(adapter._dispatch_message(parsed))- self.assertEqual(len(captured), 1)-def test_mail_the_gateway_admits_or_answers_reaches_it(self):cases = {"pair opt-in": {"extra": {"unauthorized_dm_behavior": "pair"}},@@ -1271,25 +1197,6 @@ class TestSenderAuthentication(unittest.TestCase):)self.assertTrue(ok, reason)- def test_spoofed_from_is_judged_as_the_attacker_not_the_victim(self):- """A spoofed From: reaches the allowlist as the ATTACKER, not the victim.-- ``"Victim <[email protected]>" <[email protected]>`` puts the victim's- address inside the QUOTED display name, so a first-pair regex reads the- From: as ``[email protected]`` and every downstream check -- the- allowlist, the pairing decision, the session identity -- is made against- the victim. parseaddr takes the real addr-spec instead.-- Note ``dmarc=pass`` is accepted for either domain by design: the- receiving MTA already enforced From alignment, so- ``_verify_sender_authentication`` trusts that verdict rather than- re-deriving it. The allowlist is the gate this bug defeated.- """- from plugins.platforms.email.adapter import _extract_email_address- spoofed = '"Victim <[email protected]>" <[email protected]>'- self.assertEqual(_extract_email_address(spoofed), "[email protected]")- self.assertNotEqual(_extract_email_address(spoofed), "[email protected]")-def test_dkim_pass_aligned_authenticates(self):ok, reason = self._verify(