forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-80222: Fix email address header folding with long quoted-str…
…ing (python#122753) Email generators using email.policy.default could incorrectly omit the quote ('"') characters from a quoted-string during header refolding, leading to invalid address headers and enabling header spoofing. This change restores the quote characters on a bare-quoted-string as the header is refolded, and escapes backslash and quote chars in the string.
- Loading branch information
Showing
3 changed files
with
53 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3082,13 +3082,40 @@ def test_address_list_with_list_separator_after_fold(self): | |
self._test(parser.get_address_list(to)[0], | ||
f'{a},\n =?utf-8?q?H=C3=BCbsch?= Kaktus <[email protected]>\n') | ||
|
||
a = '.' * 79 | ||
a = '.' * 79 # ('.' is a special, so must be in quoted-string.) | ||
to = f'"{a}" <[email protected]>, "Hübsch Kaktus" <[email protected]>' | ||
self._test(parser.get_address_list(to)[0], | ||
f'{a}\n' | ||
f'"{a}"\n' | ||
' <[email protected]>, =?utf-8?q?H=C3=BCbsch?= Kaktus ' | ||
'<[email protected]>\n') | ||
|
||
def test_address_list_with_specials_in_long_quoted_string(self): | ||
# Regression for gh-80222. | ||
policy = self.policy.clone(max_line_length=40) | ||
cases = [ | ||
# (to, folded) | ||
('"Exfiltrator <[email protected]> (unclosed comment?" <[email protected]>', | ||
'"Exfiltrator <[email protected]> (unclosed\n' | ||
' comment?" <[email protected]>\n'), | ||
('"Escaped \\" chars \\\\ in quoted-string stay escaped" <[email protected]>', | ||
'"Escaped \\" chars \\\\ in quoted-string\n' | ||
' stay escaped" <[email protected]>\n'), | ||
('This long display name does not need quotes <[email protected]>', | ||
'This long display name does not need\n' | ||
' quotes <[email protected]>\n'), | ||
('"Quotes are not required but are retained here" <[email protected]>', | ||
'"Quotes are not required but are\n' | ||
' retained here" <[email protected]>\n'), | ||
('"A quoted-string, it can be a valid local-part"@example.com', | ||
'"A quoted-string, it can be a valid\n' | ||
' local-part"@example.com\n'), | ||
('"[email protected]"@example.com', | ||
'"[email protected]"@example.com\n'), | ||
] | ||
for (to, folded) in cases: | ||
with self.subTest(to=to): | ||
self._test(parser.get_address_list(to)[0], folded, policy=policy) | ||
|
||
# XXX Need tests with comments on various sides of a unicode token, | ||
# and with unicode tokens in the comments. Spaces inside the quotes | ||
# currently don't do the right thing. | ||
|
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/Security/2024-08-06-11-43-08.gh-issue-80222.wfR4BU.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,6 @@ | ||
Fix bug in the folding of quoted strings when flattening an email message using | ||
a modern email policy. Previously when a quoted string was folded so that | ||
it spanned more than one line, the surrounding quotes and internal escapes | ||
would be omitted. This could theoretically be used to spoof header lines | ||
using a carefully constructed quoted string if the resulting rendered email | ||
was transmitted or re-parsed. |