Skip to content

Commit 7cff053

Browse files
miss-islingtonmedmundsencukou
authored
[3.11] gh-80222: Fix email address header folding with long quoted-string (GH-122753) (#129009)
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. (cherry picked from commit 5aaf416) Co-authored-by: Mike Edmunds <medmunds@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 9d727fe commit 7cff053

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Lib/email/_header_value_parser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@
9595
NLSET = {'\n', '\r'}
9696
SPECIALSNL = SPECIALS | NLSET
9797

98+
99+
def make_quoted_pairs(value):
100+
"""Escape dquote and backslash for use within a quoted-string."""
101+
return str(value).replace('\\', '\\\\').replace('"', '\\"')
102+
103+
98104
def quote_string(value):
99-
return '"'+str(value).replace('\\', '\\\\').replace('"', r'\"')+'"'
105+
escaped = make_quoted_pairs(value)
106+
return f'"{escaped}"'
107+
100108

101109
# Match a RFC 2047 word, looks like =?utf-8?q?someword?=
102110
rfc2047_matcher = re.compile(r'''
@@ -2866,6 +2874,15 @@ def _refold_parse_tree(parse_tree, *, policy):
28662874
if not hasattr(part, 'encode'):
28672875
# It's not a terminal, try folding the subparts.
28682876
newparts = list(part)
2877+
if part.token_type == 'bare-quoted-string':
2878+
# To fold a quoted string we need to create a list of terminal
2879+
# tokens that will render the leading and trailing quotes
2880+
# and use quoted pairs in the value as appropriate.
2881+
newparts = (
2882+
[ValueTerminal('"', 'ptext')] +
2883+
[ValueTerminal(make_quoted_pairs(p), 'ptext')
2884+
for p in newparts] +
2885+
[ValueTerminal('"', 'ptext')])
28692886
if not part.as_ew_allowed:
28702887
wrap_as_ew_blocked += 1
28712888
newparts.append(end_ew_not_allowed)

Lib/test/test_email/test__header_value_parser.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,13 +2991,40 @@ def test_address_list_with_list_separator_after_fold(self):
29912991
self._test(parser.get_address_list(to)[0],
29922992
f'{a},\n =?utf-8?q?H=C3=BCbsch?= Kaktus <beautiful@example.com>\n')
29932993

2994-
a = '.' * 79
2994+
a = '.' * 79 # ('.' is a special, so must be in quoted-string.)
29952995
to = f'"{a}" <xyz@example.com>, "Hübsch Kaktus" <beautiful@example.com>'
29962996
self._test(parser.get_address_list(to)[0],
2997-
f'{a}\n'
2997+
f'"{a}"\n'
29982998
' <xyz@example.com>, =?utf-8?q?H=C3=BCbsch?= Kaktus '
29992999
'<beautiful@example.com>\n')
30003000

3001+
def test_address_list_with_specials_in_long_quoted_string(self):
3002+
# Regression for gh-80222.
3003+
policy = self.policy.clone(max_line_length=40)
3004+
cases = [
3005+
# (to, folded)
3006+
('"Exfiltrator <spy@example.org> (unclosed comment?" <to@example.com>',
3007+
'"Exfiltrator <spy@example.org> (unclosed\n'
3008+
' comment?" <to@example.com>\n'),
3009+
('"Escaped \\" chars \\\\ in quoted-string stay escaped" <to@example.com>',
3010+
'"Escaped \\" chars \\\\ in quoted-string\n'
3011+
' stay escaped" <to@example.com>\n'),
3012+
('This long display name does not need quotes <to@example.com>',
3013+
'This long display name does not need\n'
3014+
' quotes <to@example.com>\n'),
3015+
('"Quotes are not required but are retained here" <to@example.com>',
3016+
'"Quotes are not required but are\n'
3017+
' retained here" <to@example.com>\n'),
3018+
('"A quoted-string, it can be a valid local-part"@example.com',
3019+
'"A quoted-string, it can be a valid\n'
3020+
' local-part"@example.com\n'),
3021+
('"local-part-with-specials@but-no-fws.cannot-fold"@example.com',
3022+
'"local-part-with-specials@but-no-fws.cannot-fold"@example.com\n'),
3023+
]
3024+
for (to, folded) in cases:
3025+
with self.subTest(to=to):
3026+
self._test(parser.get_address_list(to)[0], folded, policy=policy)
3027+
30013028
# XXX Need tests with comments on various sides of a unicode token,
30023029
# and with unicode tokens in the comments. Spaces inside the quotes
30033030
# currently don't do the right thing.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fix bug in the folding of quoted strings when flattening an email message using
2+
a modern email policy. Previously when a quoted string was folded so that
3+
it spanned more than one line, the surrounding quotes and internal escapes
4+
would be omitted. This could theoretically be used to spoof header lines
5+
using a carefully constructed quoted string if the resulting rendered email
6+
was transmitted or re-parsed.

0 commit comments

Comments
 (0)