diff --git a/routes_passthrough.go b/routes_passthrough.go index 4fc311d..0d2f4d8 100644 --- a/routes_passthrough.go +++ b/routes_passthrough.go @@ -1788,6 +1788,15 @@ func (g *Gateway) proxyUpstream(w http.ResponseWriter, r *http.Request, upstream reader = io.TeeReader(resp.Body, sseTee) } + // The caller releases tee only after this function returns; a panic in + // the copy never returns, so release here and keep unwinding. + defer func() { + if p := recover(); p != nil { + tee.release() + panic(p) + } + }() + if _, err := io.Copy(fw, reader); err != nil { // errors.Is(err, context.Canceled) here means the CLIENT went away // mid-copy — reader wraps resp.Body, which is now a watchdogBody diff --git a/security_fixes_test.go b/security_fixes_test.go index ad0738c..b07bbf4 100644 --- a/security_fixes_test.go +++ b/security_fixes_test.go @@ -383,3 +383,43 @@ func TestTeeBudget_ReserveNeverExceedsLimit(t *testing.T) { assert.EqualValues(t, limit, granted.Load(), "exactly the budget is granted") assert.EqualValues(t, limit, atomic.LoadInt64(&b.used)) } + +// firstWritePanics is a ResponseWriter whose first Write panics, to +// simulate a failure inside the proxy copy loop. +type firstWritePanics struct { + http.ResponseWriter + writes atomic.Int32 +} + +func (w *firstWritePanics) Write(p []byte) (int, error) { + if w.writes.Add(1) == 1 { + panic("simulated copy failure") + } + return w.ResponseWriter.Write(p) +} + +// TestPassthrough_PanicInCopyReleasesTeeBudget pins that a panic while a +// response is teed returns the tee's reserved bytes to the shared budget. +func TestPassthrough_PanicInCopyReleasesTeeBudget(t *testing.T) { + t.Parallel() + tests := []struct { + name string + body string + }{ + {name: "small json body", body: `{"model":"gpt-known","usage":{"prompt_tokens":4,"completion_tokens":2}}`}, + {name: "larger json body", body: `{"model":"gpt-known","pad":"` + strings.Repeat("x", 4096) + `","usage":{"prompt_tokens":4,"completion_tokens":2}}`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + gw := newPassthroughUsageGateway(t, tt.body, []string{"gpt-known"}, &LimitsConfig{}, nil) + req := httptest.NewRequest(http.MethodPost, "/openai/v1/native-endpoint", strings.NewReader(`{"model":"gpt-1"}`)) + req.Header.Set("Authorization", "Bearer sk-alice") + w := &firstWritePanics{ResponseWriter: httptest.NewRecorder()} + + gw.ServeHTTP(w, req) + + assert.Zero(t, atomic.LoadInt64(&gw.teeBudget.used), "a panic must not leak the tee reservation") + }) + } +}