]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39073: validate Address parts to disallow CRLF (#19007)
authorAshwin Ramaswami <aramaswamis@gmail.com>
Mon, 30 Mar 2020 00:38:41 +0000 (20:38 -0400)
committerGitHub <noreply@github.com>
Mon, 30 Mar 2020 00:38:41 +0000 (20:38 -0400)
 Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks.

Lib/email/headerregistry.py
Lib/test/test_email/test_headerregistry.py
Misc/NEWS.d/next/Security/2020-03-15-01-28-36.bpo-39073.6Szd3i.rst [new file with mode: 0644]

index cc1d1912918149e400ce3d4fbcec4eb77c1ee7c6..5d84fc0d82d0b04b86c83241c051720f7b586737 100644 (file)
@@ -31,6 +31,11 @@ class Address:
         without any Content Transfer Encoding.
 
         """
+
+        inputs = ''.join(filter(None, (display_name, username, domain, addr_spec)))
+        if '\r' in inputs or '\n' in inputs:
+            raise ValueError("invalid arguments; address parts cannot contain CR or LF")
+
         # This clause with its potential 'raise' may only happen when an
         # application program creates an Address object using an addr_spec
         # keyword.  The email library code itself must always supply username
index 38f7ddbf06d5cde84b6ce076ae1361fa1f15f527..82e121350ffbf5f3d165457c9ee7fb05a1206b32 100644 (file)
@@ -1437,6 +1437,25 @@ class TestAddressAndGroup(TestEmailBase):
     #    with self.assertRaises(ValueError):
     #        Address('foo', 'wők', 'example.com')
 
+    def test_crlf_in_constructor_args_raises(self):
+        cases = (
+            dict(display_name='foo\r'),
+            dict(display_name='foo\n'),
+            dict(display_name='foo\r\n'),
+            dict(domain='example.com\r'),
+            dict(domain='example.com\n'),
+            dict(domain='example.com\r\n'),
+            dict(username='wok\r'),
+            dict(username='wok\n'),
+            dict(username='wok\r\n'),
+            dict(addr_spec='wok@example.com\r'),
+            dict(addr_spec='wok@example.com\n'),
+            dict(addr_spec='wok@example.com\r\n')
+        )
+        for kwargs in cases:
+            with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid arguments"):
+                Address(**kwargs)
+
     def test_non_ascii_username_in_addr_spec_raises(self):
         with self.assertRaises(ValueError):
             Address('foo', addr_spec='wők@example.com')
diff --git a/Misc/NEWS.d/next/Security/2020-03-15-01-28-36.bpo-39073.6Szd3i.rst b/Misc/NEWS.d/next/Security/2020-03-15-01-28-36.bpo-39073.6Szd3i.rst
new file mode 100644 (file)
index 0000000..6c9447b
--- /dev/null
@@ -0,0 +1 @@
+Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks.