-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.9] gh-121650: Encode newlines in headers, and verify headers are s…
…ound (GH-122233) (#122610) Per RFC 2047: > [...] these encoding schemes allow the > encoding of arbitrary octet values, mail readers that implement this > decoding should also ensure that display of the decoded data on the > recipient's terminal will not cause unwanted side-effects It seems that the "quoted-word" scheme is a valid way to include a newline character in a header value, just like we already allow undecodable bytes or control characters. They do need to be properly quoted when serialized to text, though. This should fail for custom fold() implementations that aren't careful about newlines. (cherry picked from commit 0976339) Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Bas Bloemsaat <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
- Loading branch information
1 parent
3f5d9d1
commit f7be505
Showing
10 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from email.generator import Generator, BytesGenerator | ||
from email.headerregistry import Address | ||
from email import policy | ||
import email.errors | ||
from test.test_email import TestEmailBase, parameterize | ||
|
||
|
||
|
@@ -216,6 +217,44 @@ def test_rfc2231_wrapping_switches_to_default_len_if_too_narrow(self): | |
g.flatten(msg) | ||
self.assertEqual(s.getvalue(), self.typ(expected)) | ||
|
||
def test_keep_encoded_newlines(self): | ||
msg = self.msgmaker(self.typ(textwrap.dedent("""\ | ||
To: nobody | ||
Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected] | ||
None | ||
"""))) | ||
expected = textwrap.dedent("""\ | ||
To: nobody | ||
Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected] | ||
None | ||
""") | ||
s = self.ioclass() | ||
g = self.genclass(s, policy=self.policy.clone(max_line_length=80)) | ||
g.flatten(msg) | ||
self.assertEqual(s.getvalue(), self.typ(expected)) | ||
|
||
def test_keep_long_encoded_newlines(self): | ||
msg = self.msgmaker(self.typ(textwrap.dedent("""\ | ||
To: nobody | ||
Subject: Bad subject=?UTF-8?Q?=0A?=Bcc: [email protected] | ||
None | ||
"""))) | ||
expected = textwrap.dedent("""\ | ||
To: nobody | ||
Subject: Bad subject | ||
=?utf-8?q?=0A?=Bcc: | ||
[email protected] | ||
None | ||
""") | ||
s = self.ioclass() | ||
g = self.genclass(s, policy=self.policy.clone(max_line_length=30)) | ||
g.flatten(msg) | ||
self.assertEqual(s.getvalue(), self.typ(expected)) | ||
|
||
|
||
class TestGenerator(TestGeneratorBase, TestEmailBase): | ||
|
||
|
@@ -224,6 +263,29 @@ class TestGenerator(TestGeneratorBase, TestEmailBase): | |
ioclass = io.StringIO | ||
typ = str | ||
|
||
def test_verify_generated_headers(self): | ||
"""gh-121650: by default the generator prevents header injection""" | ||
class LiteralHeader(str): | ||
name = 'Header' | ||
def fold(self, **kwargs): | ||
return self | ||
|
||
for text in ( | ||
'Value\r\nBad Injection\r\n', | ||
'NoNewLine' | ||
): | ||
with self.subTest(text=text): | ||
message = message_from_string( | ||
"Header: Value\r\n\r\nBody", | ||
policy=self.policy, | ||
) | ||
|
||
del message['Header'] | ||
message['Header'] = LiteralHeader(text) | ||
|
||
with self.assertRaises(email.errors.HeaderWriteError): | ||
message.as_string() | ||
|
||
|
||
class TestBytesGenerator(TestGeneratorBase, TestEmailBase): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/Library/2024-07-27-16-10-41.gh-issue-121650.nf6oc9.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:mod:`email` headers with embedded newlines are now quoted on output. The | ||
:mod:`~email.generator` will now refuse to serialize (write) headers that | ||
are unsafely folded or delimited; see | ||
:attr:`~email.policy.Policy.verify_generated_headers`. (Contributed by Bas | ||
Bloemsaat and Petr Viktorin in :gh:`121650`.) |