Repository navigation
fix(llm): reject oversized Bedrock event-stream frames - #3
Open
dajiaohuang wants to merge 119 commits into
Open
dajiaohuang wants to merge 119 commits into
dajiaohuang wants to merge 119 commits into
Conversation
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: Filip <[email protected]>
…re AI Gateway (anomalyco#42634) Co-authored-by: Keefe Tang <[email protected]>
Co-authored-by: Victor Navarro <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: Luke Parker <[email protected]>
Co-authored-by: Frank <[email protected]>
Co-authored-by: rekram1-node <[email protected]>
…tible providers (anomalyco#43915) Co-authored-by: Joel Stucki <[email protected]>
Co-authored-by: MrMushrooooom <[email protected]> Co-authored-by: Frank <[email protected]>
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
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
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. |
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
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
appendChunkto keep allocating and copying the accumulated buffer.Problem
In
packages/llm/src/protocols/bedrock-event-stream.ts,consumeFramesreadstotalLengthfrom the 4-byte frame prelude and loops until that many bytes have arrived. A malformed response can declare0xFFFFFFFF(4 GiB). Because the buffer is only compacted once a frame completes,appendChunkre-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-codecdoes not enforce it, and neither did we.Fix
Add
MAX_EVENT_STREAM_MESSAGE_LENGTH = 16 * 1024 * 1024and reject frames that declare a larger length with a typedInvalidProviderOutputerror instead of buffering toward a frame that can never complete.Testing
After the fix, a frame declaring > 16 MiB immediately emits an error and stops buffering.
Fixes anomalyco#44630