Skip to content

Commit e6013ae

Browse files
committed
Use trust_x_header and test it.
1 parent 3e909f5 commit e6013ae

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

python_multipart/multipart.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,10 @@ def create_form_parser(
18141814
content_type = content_type.decode("latin-1")
18151815

18161816
# File names are optional.
1817-
file_name = headers.get("X-File-Name")
1817+
if trust_x_headers:
1818+
file_name = headers.get("X-File-Name")
1819+
else:
1820+
file_name = None
18181821

18191822
# Instantiate a form parser.
18201823
form_parser = FormParser(content_type, on_field, on_file, boundary=boundary, file_name=file_name, config=config)

tests/test_multipart.py

+19
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ def on_header_begin() -> None:
13681368
self.assertEqual(calls, 3)
13691369

13701370

1371+
@parametrize_class
13711372
class TestHelperFunctions(unittest.TestCase):
13721373
def test_create_form_parser(self) -> None:
13731374
r = create_form_parser({"Content-Type": b"application/octet-stream"}, None, None)
@@ -1390,6 +1391,24 @@ def test_parse_form(self) -> None:
13901391
# 15 - i.e. all data is written.
13911392
self.assertEqual(on_file.call_args[0][0].size, 15)
13921393

1394+
@parametrize("trust_x_headers", [True, False])
1395+
def test_parse_form_trust_x_false(self, trust_x_headers: bool) -> None:
1396+
on_field = Mock()
1397+
on_file = Mock()
1398+
1399+
headers = {"Content-Type": b"application/octet-stream", "X-File-Name": b"foo.txt"}
1400+
parser = create_form_parser(headers, on_field, on_file, trust_x_headers=trust_x_headers)
1401+
parser.write(b"123456789012345")
1402+
parser.finalize()
1403+
1404+
assert on_file.call_count == 1
1405+
1406+
# The first argument (a File Object) name should come from the X header only if allowed.
1407+
if trust_x_headers:
1408+
self.assertEqual(on_file.call_args[0][0].file_name, b"foo.txt")
1409+
else:
1410+
self.assertEqual(on_file.call_args[0][0].file_name, None)
1411+
13931412
def test_parse_form_content_length(self) -> None:
13941413
files: list[FileProtocol] = []
13951414

0 commit comments

Comments
 (0)