Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01e46ee

Browse files
committed
Merge: #16983: Apply postel's law to encoded words inside quoted strings.
2 parents ff9616b + 0400d33 commit 01e46ee

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/email/_header_value_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,13 @@ def get_bare_quoted_string(value):
15561556
while value and value[0] != '"':
15571557
if value[0] in WSP:
15581558
token, value = get_fws(value)
1559+
elif value[:2] == '=?':
1560+
try:
1561+
token, value = get_encoded_word(value)
1562+
bare_quoted_string.defects.append(errors.InvalidHeaderDefect(
1563+
"encoded word inside quoted string"))
1564+
except errors.HeaderParseError:
1565+
token, value = get_qcontent(value)
15591566
else:
15601567
token, value = get_qcontent(value)
15611568
bare_quoted_string.append(token)

Lib/test/test_email/test__header_value_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ def test_get_bare_quoted_string_empty_quotes(self):
540540
self._test_get_x(parser.get_bare_quoted_string,
541541
'""', '""', '', [], '')
542542

543+
# Issue 16983: apply postel's law to some bad encoding.
544+
def test_encoded_word_inside_quotes(self):
545+
self._test_get_x(parser.get_bare_quoted_string,
546+
'"=?utf-8?Q?not_really_valid?="',
547+
'"not really valid"',
548+
'not really valid',
549+
[errors.InvalidHeaderDefect],
550+
'')
551+
543552
# get_comment
544553

545554
def test_get_comment_only(self):

Lib/test/test_email/test_headerregistry.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,16 @@ class TestAddressHeader(TestHeaderBase):
11551155
'example.com',
11561156
None),
11571157

1158+
'rfc2047_atom_in_quoted_string_is_decoded':
1159+
('"=?utf-8?q?=C3=89ric?=" <[email protected]>',
1160+
[errors.InvalidHeaderDefect],
1161+
'Éric <[email protected]>',
1162+
'Éric',
1163+
1164+
'foo',
1165+
'example.com',
1166+
None),
1167+
11581168
}
11591169

11601170
# XXX: Need many more examples, and in particular some with names in

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #16983: the new email header parsing code will now decode encoded words
31+
that are (incorrectly) surrounded by quotes, and register a defect.
32+
3033
- Issue #19772: email.generator no longer mutates the message object when
3134
doing a down-transform from 8bit to 7bit CTEs.
3235

0 commit comments

Comments
 (0)