Add SSRF-safe aiohttp downloader with size limits and use it in Matrix/Mattermost adapters#691
Add SSRF-safe aiohttp downloader with size limits and use it in Matrix/Mattermost adapters#691badMade wants to merge 7 commits into
Conversation
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-import |
1 |
First entries
gateway/platforms/matrix.py:1089: [unresolved-import] unresolved-import: Cannot resolve imported module `httpx`
Unchanged: 4356 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
There was a problem hiding this comment.
Code Review
This pull request introduces a centralized, SSRF-safe utility function download_public_url_bytes_aiohttp in gateway/platforms/base.py to handle public URL downloads with size limits and redirect protection. It refactors the Matrix and Mattermost platform adapters to use this new utility. A review comment suggests catching ValueError explicitly in Mattermost's retry loop to avoid retrying permanent validation failures like SSRF blocks or size limit violations.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
Adds a shared, SSRF-aware media downloader to the gateway platform base layer and migrates Matrix/Mattermost adapters to use it, aiming to centralize redirect validation, enforce hard download size limits, and unify error handling for public URL media fetches.
Changes:
- Introduces
download_public_url_bytes_aiohttp(with redirect SSRF protection + max-bytes enforcement) andPublicUrlDownloadHTTPErroringateway/platforms/base.py. - Updates Matrix
send_imageand Mattermost URL/image download paths to use the shared downloader and derive filenames from the resolved final URL. - Adds unit tests for unsafe redirect blocking and streaming size limit enforcement.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
gateway/platforms/base.py |
Adds SSRF-safe aiohttp downloader with redirect handling and byte caps. |
gateway/platforms/matrix.py |
Switches image download to shared aiohttp downloader (proxy-aware), uses final URL for filename. |
gateway/platforms/mattermost.py |
Replaces ad-hoc downloads with shared downloader; adds HTTP-status-based retry behavior. |
tests/gateway/test_platform_base.py |
Adds tests covering unsafe redirects and streaming byte-limit enforcement for the new downloader. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Auto-merge: checks failingThe following checks did not pass:
Please fix the failing checks before this PR can be merged. |
Motivation
Description
download_public_url_bytes_aiohttptogateway/platforms/base.pywith redirect-based SSRF protection, configurablemax_bytes, redirect handling, streaming-aware size enforcement, and aPublicUrlDownloadHTTPErrortype; introduceMAX_GATEWAY_MEDIA_DOWNLOAD_BYTESconstant and importurljoin.download_public_url_bytes_aiohttpforsend_image, including proxy kwargs and using the final resolved URL for filenames and sensiblecontent-typefallback.download_public_url_bytes_aiohttpin_send_url_as_fileand image batch download paths, add handling forPublicUrlDownloadHTTPErrorto implement retry behavior on 429/5xx, normalizecontent-typefallback toimage/png, and derive filenames from the final URL.Testing
tests/gateway/test_platform_base.pycoveringdownload_public_url_bytes_aiohttpbehavior (unsafe redirect blocking and streaming size limit); the tests passed when run withpytest tests/gateway/test_platform_base.py -q.Codex Task