Skip to content
Closed
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
2 changes: 1 addition & 1 deletion multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _is_valid_charset(charset):
# A token or quoted-string (simple qs | token | slow qs)
_value = r'"[^\\"]*"|%s|"(?:\\.|[^\\"])*"' % _token
# A "; key=value" pair from content-disposition header
_option = r"; *(%s) *= *(%s)" % (_hname, _value)
_option = r";[ \t]*(%s) *= *(%s)" % (_hname, _value)
_re_option = re.compile(_option)


Expand Down
29 changes: 29 additions & 0 deletions test/test_header_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ def test_options_parser(self):
parse = functools.partial(multipart.parse_options_header, unquote=multipart.content_disposition_unquote)
self.assertEqual(parse(head+'FileName="Te%22s\\\\t.txt"')[1]['filename'], 'Te"s\\\\t.txt')

def test_options_parser_parameter_whitespace(self):
for whitespace in ('', ' ', '\t', ' \t '):
for boundary in ('demo', '"demo"'):
with self.subTest(whitespace=whitespace, boundary=boundary):
header = 'multipart/form-data;' + whitespace + 'boundary=' + boundary
self.assertEqual(
multipart.parse_options_header(header),
('multipart/form-data', {'boundary': 'demo'}),
)

def test_options_parser_non_ows_whitespace(self):
for whitespace in ('\r', '\n', '\r\n', '\v', '\f', '\u00a0'):
with self.subTest(whitespace=whitespace):
header = 'multipart/form-data;' + whitespace + 'boundary=demo'
self.assertEqual(multipart.parse_options_header(header), ('multipart/form-data', {}))

def test_content_disposition_parameter_whitespace(self):
for whitespace in ('', ' ', '\t', ' \t '):
with self.subTest(whitespace=whitespace):
header = 'form-data;' + whitespace + 'name="field";' + whitespace + 'filename="test.txt"'
self.assertEqual(
multipart.parse_options_header(header, unquote=multipart.content_disposition_unquote),
('form-data', {'name': 'field', 'filename': 'test.txt'}),
)
self.assertEqual(
multipart.parse_content_disposition(header),
('form-data', 'field', 'test.txt'),
)

def test_content_disposition_parser(self):
parse = multipart.parse_content_disposition

Expand Down
32 changes: 32 additions & 0 deletions test/test_wsgi_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ def test_multipart(self):
self.assertEqual(files['file1'].name, 'file1')
self.assertEqual(files['file1'].content_type, 'image/png')

def test_multipart_boundary_whitespace(self):
self.boundary = 'demo'
self.write_field('field', 'ok')
self.write_end()
for whitespace in ('', ' ', '\t', ' \t '):
with self.subTest(whitespace=whitespace):
self.environ['CONTENT_TYPE'] = 'multipart/form-data;' + whitespace + 'boundary=demo'
forms, files = self.parse_form_data(strict=True)
self.assertEqual(dict(forms), {'field': 'ok'})
self.assertEqual(dict(files), {})

def test_multipart_disposition_whitespace(self):
self.boundary = 'demo'
self.environ['CONTENT_TYPE'] = 'multipart/form-data; boundary=demo'
for whitespace in ('', ' ', '\t', ' \t '):
with self.subTest(whitespace=whitespace):
self.reset().write_boundary()
self.write('Content-Disposition: form-data;' + whitespace + 'name="field"\r\n\r\nok')
self.write_boundary()
self.write(
'Content-Disposition: form-data;' + whitespace + 'name="upload";'
+ whitespace + 'filename="test.txt"\r\n',
'Content-Type: text/plain\r\n\r\nfile contents',
)
self.write_end()
forms, files = self.parse_form_data(strict=True)
self.assertEqual(dict(forms), {'field': 'ok'})
self.assertEqual(len(files), 1)
self.assertEqual(files['upload'].name, 'upload')
self.assertEqual(files['upload'].filename, 'test.txt')
self.assertEqual(files['upload'].raw, b'file contents')

def test_multipart_strict_passed_to_parser(self):
self.write(
"--foo\r\n",
Expand Down