Merge pull request #334 from Coff0xc/fix/blobrange-overflow

fix(traffic): bound BlobRange without overflowing int64
This commit is contained in:
BaaRai
2026-11-18 17:32:13 +01:00
committed by GitHub
3 changed files with 55 additions and 7 deletions
+1
View File
@@ -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.
+35
View File
@@ -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)
}
})
}
}
+19 -7
View File
@@ -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"`