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

Conversation

anoadragon453
Copy link
Member

Fixes the following exception, which we were seeing around 30 times/day on matrix.org:

  File "synapse/http/server.py", line 332, in _async_render_wrapper
    callback_return = await self._async_render(request)
  File "synapse/http/server.py", line 544, in _async_render
    callback_return = await raw_callback_return
  File "synapse/rest/client/media.py", line 260, in on_GET
    await self.media_repo.get_remote_media(
  File "synapse/media/media_repository.py", line 531, in get_remote_media
    responder, media_info = await self._get_remote_media_impl(
  File "synapse/media/media_repository.py", line 691, in _get_remote_media_impl
    raise e
  File "synapse/media/media_repository.py", line 676, in _get_remote_media_impl
    media_info = await self._federation_download_remote_file(
  File "synapse/media/media_repository.py", line 916, in _federation_download_remote_file
    media_type = headers[b"Content-Type"][0].decode("ascii")
AttributeError: 'NoneType' object has no attribute 'decode'

@anoadragon453 anoadragon453 marked this pull request as ready for review October 23, 2024 09:46
@anoadragon453 anoadragon453 requested a review from a team as a code owner October 23, 2024 09:46
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants