Skip to content

Commit 03c8b49

Browse files
[3.14] gh-87720 Fix additional header refolding-quoting edge case (GH-134271) (#159067)
In this case, the higher level syntactic unit fit on the remainder of the line in un-encoded format, and that higher level unit was not a ptext or vtext, so the encoding check failed. To fix this, we look inside the current unit to see if has anything that was originally encoded. (cherry picked from commit 5a22a62) Co-authored-by: R. David Murray <rdmurray@bitdance.com>
1 parent 58ed025 commit 03c8b49

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎Lib/email/_header_value_parser.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def comments(self):
169169
comments.extend(token.comments)
170170
return comments
171171

172+
def contains_token_type(self, *token_types):
173+
if self.token_type in token_types:
174+
return True
175+
for t in self:
176+
if t.contains_token_type(*token_types):
177+
return True
178+
return False
179+
172180
def fold(self, *, policy):
173181
return _refold_parse_tree(self, policy=policy)
174182

@@ -935,6 +943,9 @@ def pop_trailing_ws(self):
935943
def comments(self):
936944
return []
937945

946+
def contains_token_type(self, *token_types):
947+
return self.token_type in token_types
948+
938949
def __getnewargs__(self):
939950
return(str(self), self.token_type)
940951

@@ -2863,7 +2874,7 @@ def _refold_parse_tree(parse_tree, *, policy):
28632874
continue
28642875
tstr = str(part)
28652876
if not want_encoding:
2866-
if part.token_type in ('ptext', 'vtext'):
2877+
if part.token_type == 'ptext' or part.contains_token_type('vtext'):
28672878
# Encode if tstr contains special characters.
28682879
want_encoding = not SPECIALSNL.isdisjoint(tstr)
28692880
else:

‎Lib/test/test_email/test__header_value_parser.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,6 +3347,11 @@ def test_address_list_with_specials_in_encoded_word(self):
33473347
'A =?utf-8?q?v=C3=A9ry?= long name\n'
33483348
' containing =?utf-8?q?a=2C?= comma\n'
33493349
' <to@example.com>\n'),
3350+
('=?utf-8?Q?a=2C=20123456789012345678901234567890123456?='
3351+
' <abc@example.com>',
3352+
'=?utf-8?q?a=2C?=\n'
3353+
' 123456789012345678901234567890123456\n'
3354+
' <abc@example.com>\n'),
33503355
]
33513356
for (to, folded) in cases:
33523357
with self.subTest(to=to):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed error in :mod:`email` new policies where header folding failed to
2+
detect the need to re-quote text obtained from encoded words in certain
3+
cases.

0 commit comments

Comments
 (0)