fix(gateway): cap inbound media download size to prevent memory exhaustion - #50321
Merged
Conversation
…stion Inbound image/audio/video payloads were buffered fully into process memory before being written to the cache, with no size limit. A large upload (Discord Nitro allows 500 MB) or a remote media URL in an inbound message pointing at a huge file could spike RAM and OOM-kill the gateway. Enforce a configurable cap in the shared cache helpers (gateway/platforms/ base.py) so the protection holds across every platform adapter, not one: - cache_image/audio/video_from_bytes reject oversized payloads before writing (video was the gap in the original report — now covered). - cache_image/audio_from_url stream the body, rejecting on an oversized Content-Length header and re-checking the running total per chunk so an absent/lying header can't smuggle an unbounded body past the cap. - Discord's _read_attachment_bytes checks att.size up front, so an oversized attachment is rejected before any bytes are pulled into memory. Configurable via gateway.max_inbound_media_bytes in config.yaml (default 128 MiB; 0 disables). No new env var — non-secret config lives in config.yaml. Salvaged and extended from @sgaofen's PR #13341 (the original report and the shared-helper approach). Reapplied onto current main (Discord adapter has since moved to plugins/platforms/discord/), the configurable knob moved from an env var to config.yaml, and the video cache helper added. Co-authored-by: Hermes Agent <noreply@nousresearch.com>
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
not-subscriptable |
1 |
First entries
tests/gateway/test_media_download_retry.py:74: [not-subscriptable] not-subscriptable: Cannot subscript object of type `None` with no `__getitem__` method
✅ Fixed issues: none
Unchanged: 5929 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
This was referenced Jun 21, 2026
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
Inbound image/audio/video can no longer OOM the gateway — payloads are now size-capped before they're buffered into memory. Fixes #13145.
Inbound media was read fully into process RAM before being written to the cache, with no limit. A large upload (Discord Nitro allows 500 MB) or a remote media URL pointing at a huge file could spike memory and OOM-kill the gateway on constrained deployments.
Changes
gateway/platforms/base.py:validate_inbound_media_size()+get_inbound_media_max_bytes(); guardcache_image/audio/video_from_bytes; streamcache_image/audio_from_url(reject on oversizedContent-Length, re-check running total per chunk).plugins/platforms/discord/adapter.py:_read_attachment_byteschecksatt.sizeup front — oversized attachments rejected before any bytes hit memory.hermes_cli/config.py: newgateway.max_inbound_media_bytes(default 128 MiB;0disables). No new env var — non-secret config stays inconfig.yaml.TestInboundMediaSizeCap+ migrated the existing retry/SSRF tests to the streaming API.Validation
cache_*_from_bytesover cap (image/audio/video)ValueError, nothing writtencache_*_from_urloversized (Content-Length or chunked)att.read()never calledcap=0tests/gateway/test_platform_base.py157✓,test_media_download_retry.py36✓,test_discord_*34✓. E2E verified against a live local HTTP server (streaming + Content-Length paths) on a tempHERMES_HOME.Credit
Salvaged and extended from @sgaofen's #13341 (original report + shared-helper approach). Reapplied onto current
main(Discord adapter has since moved toplugins/), moved the knob from an env var toconfig.yaml, and added the video cache guard #13341 missed. Closes the cluster: also supersedes #42931 (@youngstar-eth), #29430 (@rayasa07), #35297 (@zapabob).Infographic