From 74c9c17626f402c369b8297c6f9e029730ee7bae Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:46:18 +0000 Subject: [PATCH] fix(traffic): bound BlobRange without overflowing int64 offset+length > size wraps when length is near MaxInt64, so the cap is skipped and make([]byte, length) can exhaust memory. Compare length against size-offset instead. --- CHANGELOG.md | 1 + traffic/blobrange_test.go | 35 +++++++++++++++++++++++++++++++++++ traffic/traffic.go | 26 +++++++++++++++++++------- 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 traffic/blobrange_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 3558aec..13ecdf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`. - **PostgreSQL text[] literals escape backslashes**: `marshalStringArray` quoted double quotes but not backslashes, so a value ending in `\"` could close the current array element and inject another. It now doubles backslashes before quotes, matching `marshalJSONBArray`. - **Browser MCP env null no longer crashes startup**: a stored MCP `env` of JSON `null` unmarshaled into a nil map, and `syncBrowserMCPProxy` then assigned `NODE_EXTRA_CA_CERTS` when traffic capture was on, panicking the process on every start. `decodeStrMap` now always returns a writable map. - **Provider refusals reported as refusals**: a turn the provider's safety system declined (Anthropic `stop_reason: "refusal"`, OpenAI-compatible `finish_reason: "content_filter"`) has no text and no tool calls, so it ended as `completed` with "the model ended this turn normally", and the idle-turn rerun resent the same conversation up to the empty-response retry count, where it is expected to be refused again. The LLM recorder now hands each call's stop reason to the run (`llmrec.WithStopSink`, `agent.WithStopTracker`). After a refusal the idle-turn rerun is skipped, and the run summary says the provider refused the turn and the intent was not completed. diff --git a/traffic/blobrange_test.go b/traffic/blobrange_test.go new file mode 100644 index 0000000..73627b8 --- /dev/null +++ b/traffic/blobrange_test.go @@ -0,0 +1,35 @@ +package traffic + +import ( + "math" + "testing" +) + +func TestClampBlobRange(t *testing.T) { + cases := []struct { + name string + offset, length int64 + size int64 + wantOff, wantN int64 + wantEmpty bool + }{ + {"zero length means rest", 2, 0, 10, 2, 8, false}, + {"negative length means rest", 2, -1, 10, 2, 8, false}, + {"exact rest", 2, 8, 10, 2, 8, false}, + {"past end", 2, 100, 10, 2, 8, false}, + {"offset at end", 10, 4, 10, 10, 0, true}, + {"offset past end", 11, 4, 10, 11, 0, true}, + {"negative offset", -3, 4, 10, 0, 4, false}, + {"empty blob", 0, 10, 0, 0, 0, true}, + {"overflow length", 1, math.MaxInt64, 10, 1, 9, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + off, n, empty := clampBlobRange(tc.offset, tc.length, tc.size) + if off != tc.wantOff || n != tc.wantN || empty != tc.wantEmpty { + t.Fatalf("got off=%d n=%d empty=%v, want off=%d n=%d empty=%v", + off, n, empty, tc.wantOff, tc.wantN, tc.wantEmpty) + } + }) + } +} diff --git a/traffic/traffic.go b/traffic/traffic.go index 3ace802..b865de5 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -942,15 +942,10 @@ func (t *Traffic) BlobRange(hash string, offset, length int64) (data []byte, tot return nil, 0, err } defer f.Close() - if offset < 0 { - offset = 0 - } - if offset >= size { + offset, length, empty := clampBlobRange(offset, length, size) + if empty { return nil, size, nil } - if length <= 0 || offset+length > size { - length = size - offset - } buf := make([]byte, length) n, err := f.ReadAt(buf, offset) if err != nil && !errors.Is(err, io.EOF) { @@ -959,6 +954,23 @@ func (t *Traffic) BlobRange(hash string, offset, length int64) (data []byte, tot return buf[:n], size, nil } +// clampBlobRange returns a safe (offset, length) for a blob of size bytes. +// length > size-offset is used instead of offset+length > size so a huge +// length cannot wrap int64 and skip the cap. +func clampBlobRange(offset, length, size int64) (off, n int64, empty bool) { + if offset < 0 { + offset = 0 + } + if size <= 0 || offset >= size { + return offset, 0, true + } + remain := size - offset + if length <= 0 || length > remain { + length = remain + } + return offset, length, false +} + // ExchangeMeta is one row of the index (returned by Search). type ExchangeMeta struct { ID string `json:"id"`