Skip to content

Use trust_x_header and test it (fix #195) #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions python_multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
elif state == MultipartState.HEADER_VALUE_ALMOST_DONE:
# The last character should be a LF. If not, it's an error.
if c != LF:
msg = "Did not find LF character at end of header " "(found %r)" % (c,)
msg = "Did not find LF character at end of header (found %r)" % (c,)
self.logger.warning(msg)
e = MultipartParseError(msg)
e.offset = i
Expand Down Expand Up @@ -1783,7 +1783,6 @@ def create_form_parser(
headers: dict[str, bytes],
on_field: OnFieldCallback | None,
on_file: OnFileCallback | None,
trust_x_headers: bool = False,
config: dict[Any, Any] = {},
) -> FormParser:
"""This function is a helper function to aid in creating a FormParser
Expand All @@ -1796,8 +1795,6 @@ def create_form_parser(
headers: A dictionary-like object of HTTP headers. The only required header is Content-Type.
on_field: Callback to call with each parsed field.
on_file: Callback to call with each parsed file.
trust_x_headers: Whether or not to trust information received from certain X-Headers - for example, the file
name from X-File-Name.
config: Configuration variables to pass to the FormParser.
"""
content_type: str | bytes | None = headers.get("Content-Type")
Expand All @@ -1813,11 +1810,8 @@ def create_form_parser(
# We need content_type to be a string, not a bytes object.
content_type = content_type.decode("latin-1")

# File names are optional.
file_name = headers.get("X-File-Name")

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

# Return our parser.
return form_parser
Expand Down
1 change: 1 addition & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ def on_header_begin() -> None:
self.assertEqual(calls, 3)


@parametrize_class
class TestHelperFunctions(unittest.TestCase):
def test_create_form_parser(self) -> None:
r = create_form_parser({"Content-Type": b"application/octet-stream"}, None, None)
Expand Down
Loading