Skip to content

Commit

Permalink
Check for field name and raise if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnstrk committed Dec 16, 2024
1 parent b1c2d3b commit 954df08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python_multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ def __init__(

def on_start() -> None:
nonlocal file
file = FileClass(file_name, config=cast("FileConfig", self.config))
file = FileClass(file_name, b"", config=cast("FileConfig", self.config))

def on_data(data: bytes, start: int, end: int) -> None:
nonlocal file
Expand Down Expand Up @@ -1692,9 +1692,11 @@ def on_headers_finished() -> None:
disp, options = parse_options_header(content_disp)

# Get the field and filename.
field_name = options.get(b"name", b"")
field_name = options.get(b"name")
file_name = options.get(b"filename")
# TODO: check for errors
if field_name is None:
raise FormParserError('Field name not found in Content-Disposition: "{!r}"'.format(content_disp))
# TODO: check for other errors

# Create the proper class.
content_type_b = headers.get("content-type")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,23 @@ def on_file(f: FileProtocol) -> None:
f.finalize()
self.assert_file_data(files[0], b"Test")

def test_bad_content_disposition(self) -> None:
# Field name is required.
data = (
b"----boundary\r\nContent-Disposition: form-data;\r\n"
b"Content-Type: text/plain\r\n"
b"Test\r\n----boundary--\r\n"
)

on_field = Mock()
on_file = Mock()

f = FormParser("multipart/form-data", on_field, on_file, boundary="--boundary")

with self.assertRaises(FormParserError):
f.write(data)
f.finalize()

def test_handles_None_fields(self) -> None:
fields: list[Field] = []

Expand Down

0 comments on commit 954df08

Please sign in to comment.