Skip to content

Commit 7dc2f33

Browse files
[3.15] gh-87720 Fix additional header refolding-quoting edge case (GH-134271) (#159069)
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 00cf7d9 commit 7dc2f33

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
@@ -166,6 +166,14 @@ def comments(self):
166166
comments.extend(token.comments)
167167
return comments
168168

169+
def contains_token_type(self, *token_types):
170+
if self.token_type in token_types:
171+
return True
172+
for t in self:
173+
if t.contains_token_type(*token_types):
174+
return True
175+
return False
176+
169177
def fold(self, *, policy):
170178
return _refold_parse_tree(self, policy=policy)
171179

@@ -933,6 +941,9 @@ def pop_trailing_ws(self):
933941
def comments(self):
934942
return []
935943

944+
def contains_token_type(self, *token_types):
945+
return self.token_type in token_types
946+
936947
def __getnewargs__(self):
937948
return(str(self), self.token_type)
938949

@@ -2916,7 +2927,7 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
29162927
continue
29172928
tstr = str(part)
29182929
if not want_encoding:
2919-
if part.token_type in ('ptext', 'vtext'):
2930+
if part.token_type == 'ptext' or part.contains_token_type('vtext'):
29202931
# Encode if tstr contains special characters.
29212932
want_encoding = not SPECIALSNL.isdisjoint(tstr)
29222933
else:

‎Lib/test/test_email/test__header_value_parser.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,11 @@ def test_address_list_with_specials_in_encoded_word(self):
33363336
'A =?utf-8?q?v=C3=A9ry?= long name\n'
33373337
' containing =?utf-8?q?a=2C?= comma\n'
33383338
' <to@example.com>\n'),
3339+
('=?utf-8?Q?a=2C=20123456789012345678901234567890123456?='
3340+
' <abc@example.com>',
3341+
'=?utf-8?q?a=2C?=\n'
3342+
' 123456789012345678901234567890123456\n'
3343+
' <abc@example.com>\n'),
33393344
]
33403345
for (to, folded) in cases:
33413346
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)