Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
bpo-39040: Fix parsing of email headers with whitespace between encod…
…ed-words.

In certain malformed content-disposition headers, parameter values are quoted
and split as encoded words on two lines with extra whitespaces. This fixes the
issue by removing the extra whitespace between the two encoded words.
  • Loading branch information
maxking committed Dec 16, 2019
commit 9da6ddfd98454f2c287d46396e3b1a2563ffd7a1
11 changes: 11 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,12 +1218,23 @@ def get_bare_quoted_string(value):
if value[0] in WSP:
token, value = get_fws(value)
elif value[:2] == '=?':
valid_ew = False
try:
token, value = get_encoded_word(value)
bare_quoted_string.defects.append(errors.InvalidHeaderDefect(
"encoded word inside quoted string"))
valid_ew = True
except errors.HeaderParseError:
token, value = get_qcontent(value)

Comment thread
maxking marked this conversation as resolved.
Outdated
# Collapse the whitespace between two encoded words that occur in a
# bare-quoted-string.
if valid_ew and len(bare_quoted_string) > 1:
if (bare_quoted_string[-1].token_type == 'fws' and
bare_quoted_string[-2].token_type == 'encoded-word'):

Comment thread
maxking marked this conversation as resolved.
Outdated
bare_quoted_string[-1] = EWWhiteSpaceTerminal(
bare_quoted_string[-1], 'fws')
else:
token, value = get_qcontent(value)
bare_quoted_string.append(token)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,16 @@ def content_disp_as_value(self,
{'filename': 'foo'},
[errors.InvalidHeaderDefect]),

'invalid_value_with_fws_bw_ew': (
Comment thread
maxking marked this conversation as resolved.
Outdated
'attachment; filename="=?UTF-8?Q?Schulbesuchsbest=C3=A4ttigung=2E?='
' =?UTF-8?Q?pdf?="',
'attachment',
{'filename': 'Schulbesuchsbestättigung.pdf'},
[errors.InvalidHeaderDefect]*3,
('attachment; filename="Schulbesuchsbestättigung.pdf"'),
('Content-Disposition: attachment;\n'
' filename*=utf-8\'\'Schulbesuchsbest%C3%A4ttigung.pdf\n'),
)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix parsing of invalid Content-Disposition email headers by collapsing
whitespace between encoded words in a bare-quote-string.
Comment thread
maxking marked this conversation as resolved.
Outdated