Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Discard empty upload_name before persisting file #7905

Merged
merged 14 commits into from
Sep 29, 2020
1 change: 1 addition & 0 deletions changelog.d/7905.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix synapse storing a media file with an invalid upload_name.
clokep marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,24 @@ def mark_recently_accessed(self, server_name, media_id):
async def create_content(
self,
media_type: str,
upload_name: str,
upload_name: Optional[str],
content: IO,
content_length: int,
auth_user: str,
) -> str:
"""Store uploaded content for a local user and return the mxc URL

Args:
media_type: The content type of the file
upload_name: The name of the file
media_type: The content type of the file.
upload_name: The name of the file, if provided.
content: A file like object that is the content to store
content_length: The length of the content
auth_user: The user_id of the uploader

Returns:
The mxc url of the stored content
"""

media_id = random_string(24)

file_info = FileInfo(server_name=None, file_id=media_id)
Expand Down
4 changes: 4 additions & 0 deletions synapse/rest/media/v1/upload_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ async def _async_render_POST(self, request):
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
)

# If the name is falsey (e.g. an empty byte string) ensure it is None.
else:
upload_name = None

headers = request.requestHeaders

if headers.hasHeader(b"Content-Type"):
Expand Down