Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion 15 Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def make_quoted_pairs(value):
return str(value).replace('\\', '\\\\').replace('"', '\\"')


def make_parenthesis_pairs(value):
"""Escape parenthesis and backslash for use within a comment."""
return str(value).replace('\\', '\\\\') \
.replace('(', '\\(').replace(')', '\\)')


def quote_string(value):
escaped = make_quoted_pairs(value)
return f'"{escaped}"'
Expand Down Expand Up @@ -927,7 +933,7 @@ def value(self):
return ' '

def startswith_fws(self):
return True
return self and self[0] in WSP


class ValueTerminal(Terminal):
Expand Down Expand Up @@ -2865,6 +2871,13 @@ def _refold_parse_tree(parse_tree, *, policy):
[ValueTerminal(make_quoted_pairs(p), 'ptext')
for p in newparts] +
[ValueTerminal('"', 'ptext')])
if part.token_type == 'comment':
newparts = (
[ValueTerminal('(', 'ptext')] +
[ValueTerminal(make_parenthesis_pairs(p), 'ptext')
if p.token_type == 'ptext' else p
for p in newparts] +
[ValueTerminal(')', 'ptext')])
if not part.as_ew_allowed:
wrap_as_ew_blocked += 1
newparts.append(end_ew_not_allowed)
Expand Down
23 changes: 23 additions & 0 deletions 23 Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,29 @@ def test_address_list_with_specials_in_long_quoted_string(self):
with self.subTest(to=to):
self._test(parser.get_address_list(to)[0], folded, policy=policy)

def test_address_list_with_long_unwrapable_comment(self):
policy = self.policy.clone(max_line_length=40)
cases = [
# (to, folded)
('(loremipsumdolorsitametconsecteturadipi)<spy@example.org>',
'(loremipsumdolorsitametconsecteturadipi)<spy@example.org>\n'),
('<spy@example.org>(loremipsumdolorsitametconsecteturadipi)',
'<spy@example.org>(loremipsumdolorsitametconsecteturadipi)\n'),
('(loremipsum dolorsitametconsecteturadipi)<spy@example.org>',
'(loremipsum dolorsitametconsecteturadipi)<spy@example.org>\n'),
('<spy@example.org>(loremipsum dolorsitametconsecteturadipi)',
'<spy@example.org>(loremipsum\n dolorsitametconsecteturadipi)\n'),
('(Escaped \\( \\) chars \\\\ in comments stay escaped)<spy@example.org>',
'(Escaped \\( \\) chars \\\\ in comments stay\n escaped)<spy@example.org>\n'),
('((loremipsum)(loremipsum)(loremipsum)(loremipsum))<spy@example.org>',
'((loremipsum)(loremipsum)(loremipsum)(loremipsum))<spy@example.org>\n'),
('((loremipsum)(loremipsum)(loremipsum) (loremipsum))<spy@example.org>',
'((loremipsum)(loremipsum)(loremipsum)\n (loremipsum))<spy@example.org>\n'),
]
for (to, folded) in cases:
with self.subTest(to=to):
self._test(parser.get_address_list(to)[0], folded, policy=policy)

def test_address_list_with_specials_in_encoded_word(self):
# An encoded-word parsed from a structured header must remain
# encoded when it contains specials. Regression for gh-121284.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed a bug in the folding of comments when flattening an email message
using a modern email policy. Comments consisting of a very long sequence of
non-foldable characters could trigger a forced line wrap that omitted the
required leading space on the continuation line, causing the remainder of
the comment to be interpreted as a new header field. This enabled header
injection with carefully crafted inputs.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.