Skip to content
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

Handle lower-case http headers in _Mulitpart_Parser_Protocol #17545

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions changelog.d/17545.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle lower-case http headers in _Mulitpart_Parser_Protocol.
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 3 additions & 3 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,11 @@ def dataReceived(self, incoming_data: bytes) -> None:
if not self.parser:

def on_header_field(data: bytes, start: int, end: int) -> None:
if data[start:end] == b"Location":
if data[start:end].lower() == b"location":
self.has_redirect = True
if data[start:end] == b"Content-Disposition":
if data[start:end].lower() == b"content-disposition":
self.in_disposition = True
if data[start:end] == b"Content-Type":
if data[start:end].lower() == b"content-type":
self.in_content_type = True

def on_header_value(data: bytes, start: int, end: int) -> None:
Expand Down
24 changes: 24 additions & 0 deletions tests/http/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
class ReadMultipartResponseTests(TestCase):
data1 = b"\r\n\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\nContent-Type: application/json\r\n\r\n{}\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\nContent-Type: text/plain\r\nContent-Disposition: inline; filename=test_upload\r\n\r\nfile_"
data2 = b"to_stream\r\n--6067d4698f8d40a0a794ea7d7379d53a--\r\n\r\n"
data3 = b"\r\n\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\ncontent-type: application/json\r\n\r\n{}\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\ncontent-type: text/plain\r\ncontent-disposition: inline; filename=test_upload\r\n\r\nfile_"
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved

redirect_data = b"\r\n\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\nContent-Type: application/json\r\n\r\n{}\r\n--6067d4698f8d40a0a794ea7d7379d53a\r\nLocation: https://cdn.example.org/ab/c1/2345.txt\r\n\r\n--6067d4698f8d40a0a794ea7d7379d53a--\r\n\r\n"

Expand Down Expand Up @@ -118,6 +119,29 @@ def test_parse_file(self) -> None:
multipart_response.disposition, b"inline; filename=test_upload"
)

def test_parse_file_lowercase_headers(self) -> None:
"""
Check that a multipart response containing a file is properly parsed
into the json/file parts, and the json and file are properly captured if the http headers are lowercased
"""
result, deferred, protocol = self._build_multipart_response(249, 250)

# Start sending data.
protocol.dataReceived(self.data3)
protocol.dataReceived(self.data2)
# Close the connection.
protocol.connectionLost(Failure(ResponseDone()))

multipart_response: MultipartResponse = deferred.result # type: ignore[assignment]

self.assertEqual(multipart_response.json, b"{}")
self.assertEqual(result.getvalue(), b"file_to_stream")
self.assertEqual(multipart_response.length, len(b"file_to_stream"))
self.assertEqual(multipart_response.content_type, b"text/plain")
self.assertEqual(
multipart_response.disposition, b"inline; filename=test_upload"
)

def test_parse_redirect(self) -> None:
"""
check that a multipart response containing a redirect is properly parsed and redirect url is
Expand Down
Loading