Skip to content
Closed
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
Prev Previous commit
Fix email bug 36041: don't unescape bare quoted strings during refold
Since the _refold_parse_tree is context unaware, the function is unable
to determine if we can directly encode the text into the header. In the
language of RFC 5322, _refold_parse_tree is unaware if it is folding a
structured header or an unstructured header.

Since a quoted string will always be acceptable for both header types,
when a quoted string needs to be refolded (because it would make the
line overflow) prepare quoted children instead of the usual semantic
children of the BareQuotedString.
  • Loading branch information
Aaryn Tonita committed Feb 26, 2019
commit 686a713a84c267d3172950bf0d64ebe33619243b
18 changes: 18 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,24 @@ def _refold_parse_tree(parse_tree, *, policy):
if newline or part.startswith_fws():
lines.append(newline + tstr)
continue
# Do not strip the quotes from BareQuotedString's by iterating over the
# children. Doing so might break a structured header and this function
# is context unaware. Instead prepare quoted children.
if isinstance(part, BareQuotedString):
subparts = list(part)
quoted_subparts = []
dquote = ValueTerminal('"', 'ptext')
quoted_subparts.append(dquote)
for subpart in subparts:
quoted_without_quotes = quote_string(subpart)[1:-1]
quoted_terminal = ValueTerminal(quoted_without_quotes, 'ptext')
quoted_subparts.append(quoted_terminal)
quoted_subparts.append(dquote)
if not part.as_ew_allowed:
wrap_as_ew_blocked += 1
quoted_subparts.append(end_ew_not_allowed)
parts = quoted_subparts + parts
continue
if not hasattr(part, 'encode'):
# It's not a terminal, try folding the subparts.
newparts = list(part)
Expand Down