Skip to content

Commit a5d1e33

Browse files
[3.12] gh-87720 Fix additional header refolding-quoting edge case (GH-134271) (#159066)
gh-87720 Fix additional header refolding-quoting edge case (GH-134271) 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 284c40b commit a5d1e33

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

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

@@ -928,6 +936,9 @@ def pop_trailing_ws(self):
928936
def comments(self):
929937
return []
930938

939+
def contains_token_type(self, *token_types):
940+
return self.token_type in token_types
941+
931942
def __getnewargs__(self):
932943
return(str(self), self.token_type)
933944

@@ -2819,7 +2830,7 @@ def _refold_parse_tree(parse_tree, *, policy):
28192830
continue
28202831
tstr = str(part)
28212832
if not want_encoding:
2822-
if part.token_type in ('ptext', 'vtext'):
2833+
if part.token_type == 'ptext' or part.contains_token_type('vtext'):
28232834
# Encode if tstr contains special characters.
28242835
want_encoding = not SPECIALSNL.isdisjoint(tstr)
28252836
else:

‎Lib/test/test_email/test__header_value_parser.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,11 @@ def test_address_list_with_specials_in_encoded_word(self):
30963096
'A =?utf-8?q?v=C3=A9ry?= long name\n'
30973097
' containing =?utf-8?q?a=2C?= comma\n'
30983098
' <to@example.com>\n'),
3099+
('=?utf-8?Q?a=2C=20123456789012345678901234567890123456?='
3100+
' <abc@example.com>',
3101+
'=?utf-8?q?a=2C?=\n'
3102+
' 123456789012345678901234567890123456\n'
3103+
' <abc@example.com>\n'),
30993104
]
31003105
for (to, folded) in cases:
31013106
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)