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

Prevent exception when remote media Content-Type header value is None #17864

Open
wants to merge 2 commits into
base: develop
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
1 change: 1 addition & 0 deletions changelog.d/17864.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent exception from receiving an invalid `Content-Type` response header upon fetching remote media.
6 changes: 5 additions & 1 deletion synapse/media/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,11 @@ async def _federation_download_remote_file(
)
raise SynapseError(502, "Failed to fetch remote media")

if b"Content-Type" in headers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do the same thing in the other places we do if b"Content-Type" in headers:?

What about other headers?

if (
b"Content-Type" in headers
and len(headers[b"Content-Type"]) > 0
and headers[b"Content-Type"][0] is not None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could apply extra scrutiny via

Suggested change
and headers[b"Content-Type"][0] is not None
and isinstance(headers[b"Content-Type"][0], str)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I believe headers[b"Content-Type"][0] is bytes, but the point of isinstance still stands.

):
media_type = headers[b"Content-Type"][0].decode("ascii")
else:
media_type = "application/octet-stream"
Expand Down
Loading