diff --git a/superset/dashboards/api.py b/superset/dashboards/api.py index bf77d372d6d8..8f62d3f9af2d 100644 --- a/superset/dashboards/api.py +++ b/superset/dashboards/api.py @@ -1342,10 +1342,32 @@ def thumbnail(self, pk: int, digest: str, **kwargs: Any) -> WerkzeugResponse: self.incr_stats("from_cache", self.thumbnail.__name__) try: image = cache_payload.get_image() + # Validate the BytesIO object is properly initialized if not image or not hasattr(image, "read"): + logger.warning( + "Thumbnail image object is invalid for dashboard %s", + str(dashboard.id), + ) + return self.response_404() + # Additional validation: ensure the BytesIO has content + if image.getbuffer().nbytes == 0: + logger.warning( + "Thumbnail image is empty for dashboard %s", + str(dashboard.id), + ) return self.response_404() + # Reset position to ensure reading from start + image.seek(0) except ScreenshotImageNotAvailableException: return self.response_404() + except Exception as ex: # pylint: disable=broad-except + logger.error( + "Error retrieving thumbnail for dashboard %s: %s", + str(dashboard.id), + str(ex), + exc_info=True, + ) + return self.response_404() return Response( FileWrapper(image), mimetype="image/png",