Skip to content

Commit 6926474

Browse files
committed
Address review comments
1 parent c268499 commit 6926474

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Lib/email/_header_value_parser.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@
9696
SPECIALSNL = SPECIALS | NLSET
9797

9898

99-
def escape_for_quotes(value):
99+
def make_quoted_pairs(value):
100100
"""Escape dquote and backslash for use within a quoted-string."""
101101
return str(value).replace('\\', '\\\\').replace('"', '\\"')
102102

103103

104104
def quote_string(value):
105-
escaped = escape_for_quotes(value)
105+
escaped = make_quoted_pairs(value)
106106
return f'"{escaped}"'
107107

108108

@@ -2914,13 +2914,14 @@ def _refold_parse_tree(parse_tree, *, policy):
29142914
# It's not a terminal, try folding the subparts.
29152915
newparts = list(part)
29162916
if part.token_type == 'bare-quoted-string':
2917-
# Restore the quotes and escape contents.
2918-
dquote = ValueTerminal('"', 'ptext')
2917+
# To fold a quoted string we need to create a list of terminal
2918+
# tokens that will render the leading and trailing quotes
2919+
# and use quoted pairs in the value as appropriate.
29192920
newparts = (
2920-
[dquote] +
2921-
[ValueTerminal(escape_for_quotes(p), 'ptext')
2921+
[ValueTerminal('"', 'ptext')] +
2922+
[ValueTerminal(make_quoted_pairs(p), 'ptext')
29222923
for p in newparts] +
2923-
[dquote])
2924+
[ValueTerminal('"', 'ptext')])
29242925
if not part.as_ew_allowed:
29252926
wrap_as_ew_blocked += 1
29262927
newparts.append(end_ew_not_allowed)
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
Fix a problem where email.policy.default header refolding could incorrectly
2-
omit quotes from structured email headers, enabling sender or recipient
3-
spoofing via a carefully crafted display-name.
1+
Fix bug in the folding of quoted strings when flattening an email message using
2+
a modern email policy. Previously when a quoted string was folded so that
3+
it spanned more than one line, the surrounding quotes and internal escapes
4+
would be omitted. This could theoretically be used to spoof header lines
5+
using a carefully constructed quoted string if the resulting rendered email
6+
was transmitted or re-parsed.

0 commit comments

Comments
 (0)