fix(llmrec): cap captured provider response bodies

TeeResponse mirrored every byte of every attempt into memory with
no limit, so a large or retrying stream could exhaust RAM.
This commit is contained in:
Coff0xc
2026-11-19 14:26:30 +01:00
committed by BaaRai
parent ae068f29cf
commit 53f764babf
3 changed files with 48 additions and 2 deletions
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **LLM response capture bound**: each recorded provider attempt keeps at most 1 MiB, and a call keeps at most 8 attempts, so a large or retrying stream cannot grow the recorder without bound. The live stream is unchanged.
- **Intercept audit ring**: `preToolUse` records allow only after intercept and the target gate decide, so a blocked call no longer leaves a leftover allow row in `Guard.Audit()`.
- **DSN password redaction**: `config.Redact` now masks libpq `password=` keywords and `?password=` query values as well as URL userinfo, so a ping-failure log cannot print the database password.
- **Asset DSL nesting**: `ParseDSL` refuses queries longer than 4 KiB or nested more than 32 parentheses deep, so a crafted `dsl` cannot grow the goroutine stack without bound.
+23 -2
View File
@@ -35,8 +35,17 @@ type Capture struct {
type attempt struct {
status int
body strings.Builder
full bool // true once body hit maxAttemptBody; further reads are not stored
}
// maxAttemptBody is the captured size of one HTTP attempt. The stream still
// reaches the caller in full; only the in-memory mirror stops.
const maxAttemptBody = 1 << 20 // 1 MiB
// maxCaptureAttempts is how many HTTP attempts one Capture keeps. Further
// attempts pass through unread into the mirror.
const maxCaptureAttempts = 8
// NewCapture returns a context carrying a fresh Capture, plus the Capture itself.
func NewCapture(ctx context.Context) (context.Context, *Capture) {
c := &Capture{}
@@ -74,8 +83,12 @@ func (c *Capture) TeeResponse(status int, rc io.ReadCloser) io.ReadCloser {
if c == nil || rc == nil {
return rc
}
a := &attempt{status: status}
c.mu.Lock()
if len(c.attempts) >= maxCaptureAttempts {
c.mu.Unlock()
return rc
}
a := &attempt{status: status}
c.attempts = append(c.attempts, a)
c.mu.Unlock()
return &teeBody{rc: rc, c: c, a: a}
@@ -153,7 +166,15 @@ func (t *teeBody) Read(p []byte) (int, error) {
n, err := t.rc.Read(p)
if n > 0 {
t.c.mu.Lock()
t.a.body.Write(p[:n])
if !t.a.full {
remain := maxAttemptBody - t.a.body.Len()
if n > remain {
t.a.body.Write(p[:remain])
t.a.full = true
} else {
t.a.body.Write(p[:n])
}
}
t.c.mu.Unlock()
}
return n, err
+24
View File
@@ -95,6 +95,30 @@ func TestCaptureRetriedAttemptsAreAllKept(t *testing.T) {
}
}
func TestCaptureCapsAttemptBodyAndCount(t *testing.T) {
_, c := NewCapture(context.Background())
payload := strings.Repeat("a", maxAttemptBody+4096)
body := c.TeeResponse(200, io.NopCloser(strings.NewReader(payload)))
got, err := io.ReadAll(body)
if err != nil {
t.Fatalf("read: %v", err)
}
if len(got) != len(payload) {
t.Fatalf("tee truncated the caller stream: %d want %d", len(got), len(payload))
}
if n := len(c.RawResponse()); n != maxAttemptBody {
t.Fatalf("captured %d bytes, want cap %d", n, maxAttemptBody)
}
for i := 1; i < maxCaptureAttempts+3; i++ {
rc := c.TeeResponse(200, io.NopCloser(strings.NewReader("x")))
_, _ = io.ReadAll(rc)
}
if n := len(c.Attempts()); n != maxCaptureAttempts {
t.Fatalf("attempts %d, want %d", n, maxCaptureAttempts)
}
}
func TestCaptureEmptyWhenNoHTTPHappened(t *testing.T) {
_, c := NewCapture(context.Background())
if c.RawRequest() != "" || c.RawResponse() != "" {