Skip to content

fix(llm): reject oversized Bedrock event-stream frames - #3

Open
dajiaohuang wants to merge 119 commits into
devfrom
bedrock-frame-bounds
Open

dajiaohuang wants to merge 119 commits into
devfrom
bedrock-frame-bounds

Conversation

@dajiaohuang

Copy link
Copy Markdown
Owner

Summary

AWS documents a 16 MiB maximum event-stream message. The frame prelude declares a 32-bit length, so a malformed response can declare up to 4 GiB. Nothing bounded the wait before this change, causing appendChunk to keep allocating and copying the accumulated buffer.

Problem

In packages/llm/src/protocols/bedrock-event-stream.ts, consumeFrames reads totalLength from the 4-byte frame prelude and loops until that many bytes have arrived. A malformed response can declare 0xFFFFFFFF (4 GiB). Because the buffer is only compacted once a frame completes, appendChunk re-copies the whole accumulated window on every network chunk. Cost is quadratic in bytes received.

AWS documents a 16 MiB maximum event-stream message; @smithy/eventstream-codec does not enforce it, and neither did we.

Fix

Add MAX_EVENT_STREAM_MESSAGE_LENGTH = 16 * 1024 * 1024 and reject frames that declare a larger length with a typed InvalidProviderOutput error instead of buffering toward a frame that can never complete.

Testing

sent wall (before) RSS (before)
8 MiB 1.2 s 134 MiB
16 MiB 4.1 s 166 MiB
32 MiB 10.9 s 262 MiB
64 MiB 47.2 s 650 MiB

After the fix, a frame declaring > 16 MiB immediately emits an error and stops buffering.

Fixes anomalyco#44630

opencode-agent Bot and others added 30 commits August 14, 2026 13:23
joelstucki-taulia and others added 27 commits August 21, 2026 22:19
AWS documents a 16 MiB maximum event-stream message. The frame prelude
declares a 32-bit length, so a malformed response can declare up to 4 GiB.
Nothing bounded the wait before this change, causing appendChunk to keep
allocating and copying the accumulated buffer.

Add MAX_EVENT_STREAM_MESSAGE_LENGTH and reject frames that declare a
larger length with a typed error instead of buffering toward a frame
that can never complete.

Fixes anomalyco#44630
@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock event-stream framing buffers without bound when a frame prelude declares an oversized length