Skip to content

Commit d6476c2

Browse files
committed
Remove trust_x_headers
Removes the trust_x_headers option for create_form_parser and associated tests.
1 parent e6013ae commit d6476c2

File tree

2 files changed

+2
-29
lines changed

2 files changed

+2
-29
lines changed

python_multipart/multipart.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
12411241
elif state == MultipartState.HEADER_VALUE_ALMOST_DONE:
12421242
# The last character should be a LF. If not, it's an error.
12431243
if c != LF:
1244-
msg = "Did not find LF character at end of header " "(found %r)" % (c,)
1244+
msg = "Did not find LF character at end of header (found %r)" % (c,)
12451245
self.logger.warning(msg)
12461246
e = MultipartParseError(msg)
12471247
e.offset = i
@@ -1783,7 +1783,6 @@ def create_form_parser(
17831783
headers: dict[str, bytes],
17841784
on_field: OnFieldCallback | None,
17851785
on_file: OnFileCallback | None,
1786-
trust_x_headers: bool = False,
17871786
config: dict[Any, Any] = {},
17881787
) -> FormParser:
17891788
"""This function is a helper function to aid in creating a FormParser
@@ -1796,8 +1795,6 @@ def create_form_parser(
17961795
headers: A dictionary-like object of HTTP headers. The only required header is Content-Type.
17971796
on_field: Callback to call with each parsed field.
17981797
on_file: Callback to call with each parsed file.
1799-
trust_x_headers: Whether or not to trust information received from certain X-Headers - for example, the file
1800-
name from X-File-Name.
18011798
config: Configuration variables to pass to the FormParser.
18021799
"""
18031800
content_type: str | bytes | None = headers.get("Content-Type")
@@ -1813,14 +1810,8 @@ def create_form_parser(
18131810
# We need content_type to be a string, not a bytes object.
18141811
content_type = content_type.decode("latin-1")
18151812

1816-
# File names are optional.
1817-
if trust_x_headers:
1818-
file_name = headers.get("X-File-Name")
1819-
else:
1820-
file_name = None
1821-
18221813
# Instantiate a form parser.
1823-
form_parser = FormParser(content_type, on_field, on_file, boundary=boundary, file_name=file_name, config=config)
1814+
form_parser = FormParser(content_type, on_field, on_file, boundary=boundary, config=config)
18241815

18251816
# Return our parser.
18261817
return form_parser

tests/test_multipart.py

-18
Original file line numberDiff line numberDiff line change
@@ -1391,24 +1391,6 @@ def test_parse_form(self) -> None:
13911391
# 15 - i.e. all data is written.
13921392
self.assertEqual(on_file.call_args[0][0].size, 15)
13931393

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-
14121394
def test_parse_form_content_length(self) -> None:
14131395
files: list[FileProtocol] = []
14141396

0 commit comments

Comments
 (0)