Skip to content

Commit 26ee788

Browse files
[3.11] gh-87720 Fix additional header refolding-quoting edge case (GH-134271) (#159065)
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 35dd324 commit 26ee788

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

@@ -922,6 +930,9 @@ def pop_trailing_ws(self):
922930
def comments(self):
923931
return []
924932

933+
def contains_token_type(self, *token_types):
934+
return self.token_type in token_types
935+
925936
def __getnewargs__(self):
926937
return(str(self), self.token_type)
927938

@@ -2798,7 +2809,7 @@ def _refold_parse_tree(parse_tree, *, policy):
27982809
continue
27992810
tstr = str(part)
28002811
if not want_encoding:
2801-
if part.token_type in ('ptext', 'vtext'):
2812+
if part.token_type == 'ptext' or part.contains_token_type('vtext'):
28022813
# Encode if tstr contains special characters.
28032814
want_encoding = not SPECIALSNL.isdisjoint(tstr)
28042815
else:

‎Lib/test/test_email/test__header_value_parser.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,11 @@ def test_address_list_with_specials_in_encoded_word(self):
30053005
'A =?utf-8?q?v=C3=A9ry?= long name\n'
30063006
' containing =?utf-8?q?a=2C?= comma\n'
30073007
' <to@example.com>\n'),
3008+
('=?utf-8?Q?a=2C=20123456789012345678901234567890123456?='
3009+
' <abc@example.com>',
3010+
'=?utf-8?q?a=2C?=\n'
3011+
' 123456789012345678901234567890123456\n'
3012+
' <abc@example.com>\n'),
30083013
]
30093014
for (to, folded) in cases:
30103015
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)