fix(gateway): validate image downloads before caching (cross-platform)#7125
Merged
Conversation
Slack may return an HTML sign-in/redirect page instead of actual media bytes (e.g. expired token, restricted file access). This adds two layers of defense: 1. Content-Type check in slack.py rejects text/html responses early 2. Magic-byte validation in base.py's cache_image_from_bytes() rejects non-image data regardless of source platform Also adds ValueError guards in wecom.py and email.py so the new validation doesn't crash those adapters. Closes #6829
This was referenced Apr 10, 2026
12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Slack may return HTML sign-in/redirect pages instead of actual media bytes. This adds two layers of defense:
text/htmlresponses earlycache_image_from_bytes()rejects non-image data regardless of source platform (protects Slack, WeCom, Email, and future adapters)Also adds ValueError guards in wecom.py and email.py so the new validation doesn't crash those adapters.
Changes
gateway/platforms/base.py:_looks_like_image()+ validation incache_image_from_bytes()gateway/platforms/slack.py: Content-Type check before cachinggateway/platforms/email.py: ValueError guardgateway/platforms/wecom.py: ValueError guard (2 call sites)tests/gateway/test_media_download_retry.py: 6 new tests + existing tests updatedTest results
30 passed
Salvaged from #6971 (@Tranquil-Flow). Closes #6829.