Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-44637: Fix DBQuote mail header refold
When a header content is too long, the RFC demands to fold it over
multiple lines. Each line starting with a space to denote folded-lines
from regular ones.

Folding a line requires splitint it, there are only a few sweet spots
where it is possible to do so (e.g. between two words). Words are pretty
deep in the parse-tree thus multiple parts must be unwrap to reveal
them. One of those parts can be a quoted-string, printing a
quoted-string as a whole correctly wraps its content with double-quotes
but printing every child never quotes them.

When a quoted-string must be unwrap to find a sweet-splot to split the
line, we now inject double-quotes literals before and after its
children.
  • Loading branch information
Julien00859 committed Dec 1, 2021
commit 622b932af468340bfe3fe3befba7d5fe593e98d7
9 changes: 8 additions & 1 deletion Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2841,7 +2841,14 @@ def _refold_parse_tree(parse_tree, *, policy):
continue
if not hasattr(part, 'encode'):
# It's not a terminal, try folding the subparts.
newparts = list(part)
if part.token_type == 'bare-quoted-string':
newparts = [
ValueTerminal('"', 'DQUOTE'),
*list(part),
ValueTerminal('"', 'DQUOTE'),
]
else:
newparts = list(part)
if not part.as_ew_allowed:
wrap_as_ew_blocked += 1
newparts.append(end_ew_not_allowed)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,13 @@ def test_long_rfc2047_header_with_embedded_fws(self):
=?utf-8?q?_to_see_if_line_wrapping_with_encoded_words_and_embedded?=
=?utf-8?q?_folding_white_space_works?=""")+'\n')

def test_long_quoted_string_header(self):
msg = Message(policy=email.policy.default)
msg['To'] = '"John Doe Example Inc. Houtesiplou Belgium" <john.doe@example.com>'
self.assertEqual(
msg.as_string(maxheaderlen=40),
'To: "John Doe Example Inc. Houtesiplou\n Belgium" <john.doe@example.com>\n\n',
)


# Test mangling of "From " lines in the body of a message
Expand Down