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

Media-Upload returns 500 for images with width or height of 1px #10721

Closed
Barry3D opened this issue Aug 31, 2021 · 3 comments · Fixed by #11288
Closed

Media-Upload returns 500 for images with width or height of 1px #10721

Barry3D opened this issue Aug 31, 2021 · 3 comments · Fixed by #11288
Labels
A-Media-Repository Uploading, downloading images and video, thumbnailing good first issue Good for newcomers S-Minor Blocks non-critical functionality, workarounds exist. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.

Comments

@Barry3D
Copy link

Barry3D commented Aug 31, 2021

Description

We've recently noticed that uploads to _matrix/media/r0/upload always cause an error when one of the images dimensions is only at one pixel.

Steps to reproduce

  • create a file that has one dimension of one pixel
  • upload it to synapse (_matrix/media/r0/upload)
2021-08-31 08:49:51,833 - synapse.http.server - 97 - ERROR - POST-66 - Failed handle request via 'UploadResource': <XForwardedForRequest at 0x7f9a6b2284a8 method='POST' uri='/_matrix/media/r0/upload?filename=bla.png&access_token=<redacted>' clientproto='HTTP/1.0' site='8008'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/synapse/http/server.py", line 258, in _async_render_wrapper
    callback_return = await self._async_render(request)
  File "/usr/local/lib/python3.7/dist-packages/synapse/http/server.py", line 286, in _async_render
    callback_return = await raw_callback_return
  File "/usr/local/lib/python3.7/dist-packages/synapse/rest/media/v1/upload_resource.py", line 94, in _async_render_POST
    media_type, upload_name, content, content_length, requester.user
  File "/usr/local/lib/python3.7/dist-packages/synapse/rest/media/v1/media_repository.py", line 182, in create_content
    await self._generate_thumbnails(None, media_id, media_id, media_type)
  File "/usr/local/lib/python3.7/dist-packages/synapse/rest/media/v1/media_repository.py", line 733, in _generate_thumbnails
    self.hs.get_reactor(), thumbnailer.scale, t_width, t_height, t_type
  File "/usr/local/lib/python3.7/dist-packages/twisted/python/threadpool.py", line 238, in inContext
    result = inContext.theWork()  # type: ignore[attr-defined]
  File "/usr/local/lib/python3.7/dist-packages/twisted/python/threadpool.py", line 255, in <lambda>
    ctx, func, *args, **kw
  File "/usr/local/lib/python3.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python3.7/dist-packages/twisted/python/context.py", line 83, in callWithContext
    return func(*args, **kw)
  File "/usr/local/lib/python3.7/dist-packages/synapse/logging/context.py", line 902, in g
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/synapse/rest/media/v1/thumbnailer.py", line 123, in scale
    scaled = self._resize(width, height)
  File "/usr/local/lib/python3.7/dist-packages/synapse/rest/media/v1/thumbnailer.py", line 115, in _resize
    return self.image.resize((width, height), Image.ANTIALIAS)
  File "/usr/local/lib/python3.7/dist-packages/PIL/Image.py", line 2001, in resize
    return self._new(self.im.resize(size, resample, box))
ValueError: height and width must be > 0

Version information

We are currently using version 1.41.0 in a docker container running debian:10-slim.

The installation ist done via pip.

@reivilibre reivilibre added S-Minor Blocks non-critical functionality, workarounds exist. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. good first issue Good for newcomers labels Aug 31, 2021
@reivilibre
Copy link
Contributor

This seems to be the code that generates the thumbnails.

I was not able to reproduce this issue myself with a 1x1 image.

However, I guess if you did something like a 5000x1 image, it would rescale whilst preserving the aspect ratio to something like 320x0 (say).

(Now reproduced myself with a 1000x1 image on v1.41.1.)

This does block the upload of the image altogether, so not sure if S-Minor is the best, but equally these are pretty unusual images?

A simple solution seems like it would be: ensure the dimension is always at least 1 pixel -- math.max(1, width) etc.

@Barry3D
Copy link
Author

Barry3D commented Aug 31, 2021

It is an uncommon issue and we only found it by testing our own code.

Your assumption might be correct. The images we used where all much bigger on the other side. The minimum I tested was 1px by 1000px iirc.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Media-Repository Uploading, downloading images and video, thumbnailing good first issue Good for newcomers S-Minor Blocks non-critical functionality, workarounds exist. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants