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

Thumbnailer fails with requested width=0 or height=0 #14579

Open
dklimpel opened this issue Nov 29, 2022 · 4 comments
Open

Thumbnailer fails with requested width=0 or height=0 #14579

dklimpel opened this issue Nov 29, 2022 · 4 comments
Labels
A-Media-Repository Uploading, downloading images and video, thumbnailing A-URL-Preview Issues related to generating server-side previews of remote URLs A-Validation 500 (mostly) errors due to lack of event/parameter validation O-Uncommon Most users are unlikely to come across this or unexpected workflow S-Tolerable Minor significance, cosmetic issues, low or no impact to users. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.

Comments

@dklimpel
Copy link
Contributor

dklimpel commented Nov 29, 2022

Description

If you request a thumbnail with width=0&height=0 (for both scale and crop)

  • container image returns HTTP 500
  • Ubuntu package returns the image

I cannot explain why the platforms behave differently.
In any case, validation of the rest API is missing (#13147).

Steps to reproduce

Ubuntu package deployment

container deployment

2022-11-29 09:40:59,762 - synapse.http.server - 124 - ERROR - GET-13- Failed handle request via 'ThumbnailResource': <XForwardedForRequest at 0x7f46681b7220 method='GET' uri='/_matrix/media/r0/thumbnail/k8s.azure.kird.net/qQlsITBVkgBeIrbvLpbByOOy?width=0&height=0&method=scale' clientproto='HTTP/1.1' site='8083'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/synapse/http/server.py", line 307, in _async_render_wrapper
    callback_return = await self._async_render(request)
  File "/usr/local/lib/python3.9/site-packages/synapse/http/server.py", line 337, in _async_render
    callback_return = await raw_callback_return
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 76, in _async_render_GET
    await self._select_or_generate_local_thumbnail(
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 178, in _select_or_generate_local_thumbnail
    file_path = await self.media_repo.generate_local_exact_thumbnail(
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/media_repository.py", line 592, in generate_local_exact_thumbnail
    t_byte_source = await defer_to_thread(-packages/synapse/rest/media/v1/thumbnail_resource.py", line 178, in _select_or_generate_local_thumbnail
    file_path = await self.media_repo.generate_local_exact_thumbnail(
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/media_repository.py", line 592, in generate_local_exact_thumbnail
    t_byte_source = await defer_to_thread(
  File "/usr/local/lib/python3.9/site-packages/twisted/python/threadpool.py", line 244, in inContext
    result = inContext.theWork()  # type: ignore[attr-de
  File "/usr/local/lib/python3.9/site-packages/twisted/python/threadpool.py", line 244, in inContext
    result = inContext.theWork()  # type: ignore[attr-defined]
  File "/usr/local/lib/python3.9/site-packages/twisted/python/threadpool.py", line 260, in <lambda>
    inContext.theWork = lambda: context.call(  # type: ignore[attr-defined]
  File "/usr/local/lib/python3.9/site-packages/twisted/python/context.py", line 117, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python3.9/site-packages/twisted/python/context.py", line 82, in callWithContext
    return func(*args, **kw)
  File "/usr/local/lib/python3.9/site-packages/synapse/logging/context.py", line 970, in g
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/media_repository.py", line 562, in _generate_thumbnail
    return thumbnailer.scale(t_width, t_height, t_type)
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnailer.py", line 143, in scale
    with self._resize(width, height) as scaled:
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnailer.py", line 135, in _resize
    return self.image.resize((width, height), Image.ANTIALIAS)
  File "/usr/local/lib/python3.9/site-packages/PIL/Image.py", line 2115, in resize
    return self._new(self.im.resize(size, resample, box))
ValueError: height and width must be > 0

I am did the same image for both plattforms / servers.

Homeserver

another homeserver

Synapse Version

various

Installation Method

Other (please mention below)

Database

single PostgreSQL server

Workers

Multiple workers

Platform

  • Container / K8s platform:
    • Synapse v1.64.0 and 1.72.0 (matrixdotorg/synapse:latest)
  • Debian/Ubuntu platform

Configuration

No response

Relevant log output

see above

Anything else that would be useful to know?

container version:

cat /usr/local/lib/python3.9/site-packages/PIL/_version.py 
# Master version for Pillow
__version__ = "9.3.0

package version:

cat /opt/venvs/matrix-synapse/lib/python3.8/site-packages/PIL/_version.py 
# Master version for Pillow
__version__ = "9.2.0"
@clokep
Copy link
Member

clokep commented Nov 29, 2022

Similar error stack to #10721, but slightly different input. See also the fix for that: #11288.

@DMRobertson DMRobertson added A-Media-Repository Uploading, downloading images and video, thumbnailing A-Validation 500 (mostly) errors due to lack of event/parameter validation S-Tolerable Minor significance, cosmetic issues, low or no impact to users. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. O-Uncommon Most users are unlikely to come across this or unexpected workflow A-URL-Preview Issues related to generating server-side previews of remote URLs labels Nov 29, 2022
@feroom
Copy link

feroom commented Dec 3, 2022

Similar error 💯

Debian/Ubuntu platform
Synapse v1.72.0 (matrix.org package)

I think this error is caused by the Element IOS client. I'm not sure, but an error appeared in the logs when an avatar was added from such clients.

@FlyveHest
Copy link

I've got many IOS clients connected to my homeserver, and the log is full of these errors. (And have been for a long time)

Also running Synapse in a container.

@FlyveHest
Copy link

Did a little more digging, this example line from my reverse proxy

x.x.x.x - - [15/Dec/2022:09:34:16 +0100] "GET /_matrix/media/r0/thumbnail/matrix-server.example/rAgGyDiZgIGXzumIIYcQhWPB?width=0&height=0&method=scale HTTP/1.1" 500 5701 "-" "Element/1.9.13 (iPhone 14 Pro Max; iOS 16.2; Scale/3.00)"

results in this in the synapse log

x/media/r0/thumbnail/matrix.gladblad.dk/rAgGyDiZgIGXzumIIYcQhWPB?width=0&height=0&method=scale' clientproto='HTTP/1.1' site='8008'>
Dec 15 09:34:16 matrix docker/synapse[1641]: Traceback (most recent call last):
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/http/server.py", line 307, in _async_render_wrapper
Dec 15 09:34:16 matrix docker/synapse[1641]:     callback_return = await self._async_render(request)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/http/server.py", line 337, in _async_render
Dec 15 09:34:16 matrix docker/synapse[1641]:     callback_return = await raw_callback_return
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 76, in _async_render_GET
Dec 15 09:34:16 matrix docker/synapse[1641]:     await self._select_or_generate_local_thumbnail(
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 178, in _select_or_generate_local_thumbnail
Dec 15 09:34:16 matrix docker/synapse[1641]:     file_path = await self.media_repo.generate_local_exact_thumbnail(
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/media_repository.py", line 592, in generate_local_exact_thumbnail
Dec 15 09:34:16 matrix docker/synapse[1641]:     t_byte_source = await defer_to_thread(
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/twisted/python/threadpool.py", line 244, in inContext
Dec 15 09:34:16 matrix docker/synapse[1641]:     result = inContext.theWork()  # type: ignore[attr-defined]
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/twisted/python/threadpool.py", line 260, in <lambda>
Dec 15 09:34:16 matrix docker/synapse[1641]:     inContext.theWork = lambda: context.call(  # type: ignore[attr-defined]
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/twisted/python/context.py", line 117, in callWithContext
Dec 15 09:34:16 matrix docker/synapse[1641]:     return self.currentContext().callWithContext(ctx, func, *args, **kw)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/twisted/python/context.py", line 82, in callWithContext
Dec 15 09:34:16 matrix docker/synapse[1641]:     return func(*args, **kw)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/logging/context.py", line 969, in g
Dec 15 09:34:16 matrix docker/synapse[1641]:     return f(*args, **kwargs)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/media_repository.py", line 562, in _generate_thumbnail
Dec 15 09:34:16 matrix docker/synapse[1641]:     return thumbnailer.scale(t_width, t_height, t_type)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnailer.py", line 143, in scale
Dec 15 09:34:16 matrix docker/synapse[1641]:     with self._resize(width, height) as scaled:
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/synapse/rest/media/v1/thumbnailer.py", line 135, in _resize
Dec 15 09:34:16 matrix docker/synapse[1641]:     return self.image.resize((width, height), Image.ANTIALIAS)
Dec 15 09:34:16 matrix docker/synapse[1641]:   File "/usr/local/lib/python3.9/site-packages/PIL/Image.py", line 2115, in resize
Dec 15 09:34:16 matrix docker/synapse[1641]:     return self._new(self.im.resize(size, resample, box))
Dec 15 09:34:16 matrix docker/synapse[1641]: ValueError: height and width must be > 0

There are many many lines like this, and they all come from a various combination of IOS element clients, so maybe it would make sense to make a bugreport there as well.

But, this should probably be handled in Synapse as well.

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 A-URL-Preview Issues related to generating server-side previews of remote URLs A-Validation 500 (mostly) errors due to lack of event/parameter validation O-Uncommon Most users are unlikely to come across this or unexpected workflow S-Tolerable Minor significance, cosmetic issues, low or no impact to users. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues.
Projects
None yet
Development

No branches or pull requests

5 participants