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
5 changes: 5 additions & 0 deletions python_multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,11 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No
i += 1
continue

# Skip leading non-boundary characters
if c != boundary[2]:
i += 1
continue

# index is used as in index into our boundary. Set to 0.
index = 0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ingore this line


------WebKitFormBoundaryTkr3kCBQlBe1nrhc
Content-Disposition: form-data; name="field"

This is a test.
------WebKitFormBoundaryTkr3kCBQlBe1nrhc--
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
boundary: ----WebKitFormBoundaryTkr3kCBQlBe1nrhc
expected:
- name: field
type: field
data: !!binary |
VGhpcyBpcyBhIHRlc3Qu
18 changes: 18 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,24 @@ def on_file(f: FileProtocol) -> None:
f = FormParser("multipart/form-data", on_field=Mock(), on_file=on_file, boundary="boundary")
f.write(data.encode("latin-1"))

def test_multipart_parser_data_before_first_boundary(self) -> None:
"""This test makes sure that the parser does not handle when there is junk data before the first boundary."""
data = (
"EXtra" + "\r\n" * 2 + "--boundary\r\n"
'Content-Disposition: form-data; name="file"; filename="filename.txt"\r\n'
"Content-Type: text/plain\r\n\r\n"
"hello\r\n"
"--boundary--"
)

files: list[File] = []

def on_file(f: FileProtocol) -> None:
files.append(cast(File, f))

f = FormParser("multipart/form-data", on_field=Mock(), on_file=on_file, boundary="boundary")
f.write(data.encode("latin-1"))

@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog: pytest.LogCaptureFixture) -> None:
self._caplog = caplog
Expand Down