fix(bedrock): validate EventStream frame lengths before allocating

The decoder allocated total_length bytes straight from the upstream prelude
(up to ~4 GiB) and sliced headers by an unchecked headers_length, which could
panic in the decode goroutine outside gin's recovery and crash the process.

Reject frames whose headers exceed 128 KiB or the frame, or whose payload
exceeds 16 MiB (the AWS EventStream protocol limits), before allocating.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
harukaandClaude Opus 5.5 committed 2026-11-06 03:00:03 -05:00
1 parent 0853d16036
commit a0b058a9e9
2 files changed
+52

No files matched your search

@@ -249,6 +249,13 @@ type bedrockEventStreamDecoder struct {
reader *bufio.Reader
}
// AWS EventStream 协议上限(与 AWS SDK 的 eventstream 解码器一致)。帧长度来自
// 上游,必须在分配前校验,否则单个伪造的 prelude 即可申请约 4 GiB。
const (
bedrockEventStreamMaxHeadersLength = 128 << 10
bedrockEventStreamMaxPayloadLength = 16 << 20
)
func newBedrockEventStreamDecoder(r io.Reader) *bedrockEventStreamDecoder {
return &bedrockEventStreamDecoder{
reader: bufio.NewReaderSize(r, 64*1024),
@@ -276,6 +283,12 @@ func (d *bedrockEventStreamDecoder) Decode() ([]byte, error) {
if totalLength < 16 { // minimum: 12 prelude + 4 message_crc
return nil, fmt.Errorf("invalid eventstream frame: total_length=%d", totalLength)
}
if headersLength > bedrockEventStreamMaxHeadersLength || headersLength > totalLength-16 {
return nil, fmt.Errorf("invalid eventstream frame: headers_length=%d total_length=%d", headersLength, totalLength)
}
if totalLength-16-headersLength > bedrockEventStreamMaxPayloadLength {
return nil, fmt.Errorf("invalid eventstream frame: payload too large, total_length=%d", totalLength)
}
// 读取 headers + payload + message_crc
remaining := int(totalLength) - 12
@@ -243,6 +243,45 @@ func TestBedrockEventStreamDecoder(t *testing.T) {
})
}
func TestBedrockEventStreamDecoderRejectsUntrustedLengths(t *testing.T) {
// Only the prelude is sent: the decoder must reject the declared lengths
// before allocating or reading the frame body.
prelude := func(totalLen, headersLen uint32) []byte {
var buf bytes.Buffer
_ = binary.Write(&buf, binary.BigEndian, totalLen)
_ = binary.Write(&buf, binary.BigEndian, headersLen)
_ = binary.Write(&buf, binary.BigEndian, crc32.Checksum(buf.Bytes(), crc32.MakeTable(crc32.IEEE)))
return buf.Bytes()
}
for _, tc := range []struct {
name string
totalLen uint32
headersLen uint32
}{
{name: "payload over protocol limit", totalLen: 16 + bedrockEventStreamMaxPayloadLength + 1},
{name: "maximum total length", totalLen: ^uint32(0)},
{name: "headers beyond frame", totalLen: 64, headersLen: 49},
{name: "headers over protocol limit", totalLen: 16 + bedrockEventStreamMaxHeadersLength + 1, headersLen: bedrockEventStreamMaxHeadersLength + 1},
} {
t.Run(tc.name, func(t *testing.T) {
decoder := newBedrockEventStreamDecoder(bytes.NewReader(prelude(tc.totalLen, tc.headersLen)))
_, err := decoder.Decode()
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid eventstream frame")
})
}
t.Run("headers filling the frame are accepted", func(t *testing.T) {
var frame bytes.Buffer
_, _ = frame.Write(prelude(16+4, 4))
_, _ = frame.Write([]byte{0, 0, 0, 0}) // empty-name, unknown-type header bytes
_ = binary.Write(&frame, binary.BigEndian, crc32.Checksum(frame.Bytes(), crc32.MakeTable(crc32.IEEE)))
decoder := newBedrockEventStreamDecoder(bytes.NewReader(frame.Bytes()))
_, err := decoder.Decode()
assert.Equal(t, io.EOF, err)
})
}
func TestBuildBedrockURL(t *testing.T) {
t.Run("stream URL with colon in model ID", func(t *testing.T) {
url := BuildBedrockURL("us-east-1", "us.anthropic.claude-opus-4-5-20251101-v1:0", true)