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

Commit 1e94989

Browse filesBrowse files
committed
#17369: Improve handling of broken RFC2231 values in get_filename.
This fixes a regression relative to python2.
1 parent bd3a11b commit 1e94989
Copy full SHA for 1e94989

3 files changed

+28Lines changed: 28 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/email/utils.py‎

Copy file name to clipboardExpand all lines: Lib/email/utils.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ def collapse_rfc2231_value(value, errors='replace',
337337
# object. We do not want bytes() normal utf-8 decoder, we want a straight
338338
# interpretation of the string as character bytes.
339339
charset, language, text = value
340+
if charset is None:
341+
# Issue 17369: if charset/lang is None, decode_rfc2231 couldn't parse
342+
# the value, so use the fallback_charset.
343+
charset = fallback_charset
340344
rawbytes = bytes(text, 'raw-unicode-escape')
341345
try:
342346
return str(rawbytes, charset, errors)
Collapse file

‎Lib/test/test_email/test_email.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_email/test_email.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5018,6 +5018,26 @@ def test_rfc2231_single_tick_in_filename(self):
50185018
self.assertNotIsInstance(param, tuple)
50195019
self.assertEqual(param, "Frank's Document")
50205020

5021+
def test_rfc2231_missing_tick(self):
5022+
m = '''\
5023+
Content-Disposition: inline;
5024+
\tfilename*0*="'This%20is%20broken";
5025+
'''
5026+
msg = email.message_from_string(m)
5027+
self.assertEqual(
5028+
msg.get_filename(),
5029+
"'This is broken")
5030+
5031+
def test_rfc2231_missing_tick_with_encoded_non_ascii(self):
5032+
m = '''\
5033+
Content-Disposition: inline;
5034+
\tfilename*0*="'This%20is%E2broken";
5035+
'''
5036+
msg = email.message_from_string(m)
5037+
self.assertEqual(
5038+
msg.get_filename(),
5039+
"'This is\ufffdbroken")
5040+
50215041
# test_headerregistry.TestContentTypeHeader.rfc2231_single_quote_in_value_with_charset_and_lang
50225042
def test_rfc2231_tick_attack_extended(self):
50235043
eq = self.assertEqual
Collapse file

‎Misc/NEWS‎

Copy file name to clipboardExpand all lines: Misc/NEWS
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Core and Builtins
4545
Library
4646
-------
4747

48+
- Issue #17369: get_filename was raising an exception if the filename
49+
parameter's RFC2231 encoding was broken in certain ways. This was
50+
a regression relative to python2.
51+
4852
- Issue #20013: Some imap servers disconnect if the current mailbox is
4953
deleted, and imaplib did not handle that case gracefully. Now it
5054
handles the 'bye' correctly.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.