Skip to content

Limit network image buffering to prevent OOM on oversized responses#2509

Open
XavLimSG wants to merge 1 commit intosquare:masterfrom
XavLimSG:master
Open

Limit network image buffering to prevent OOM on oversized responses#2509
XavLimSG wants to merge 1 commit intosquare:masterfrom
XavLimSG:master

Conversation

@XavLimSG
Copy link
Copy Markdown

I found that the network decode path could buffer an arbitrarily large response body into memory on byte-array decode flows, which can
lead to OOM risk with oversized payloads.

I kept this change focused and minimal: I added a hard size guard for network responses and enforced the same limit in the decode path.

Root cause

On network requests, byte-array decode paths (readByteArray()) did not enforce a maximum response size before loading data into
memory.

What I changed

  • I added a max response size guard (50MB) in NetworkRequestHandler before decode.
  • I now pass that max size into BitmapUtils.decodeStream(...) for network decodes.
  • I added a bounded reader helper in BitmapUtils (readByteArrayWithLimit) and used it for:
    • API 28+ decode path
    • pre-P WebP byte-array decode path
  • I added a regression unit test:
    • oversizedNetworkResponseThrowsAndClosesBody
    • Verifies oversized responses fail with ContentLengthException and response body is closed.

Before / After

  • Before: oversized network payloads could be fully buffered in memory by byte-array decode paths.
  • After: oversized payloads are rejected early and decode paths enforce a byte limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant