Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

redact: a masked body is held at its size, not in a buffer grown by doubling (#3383)

yetone2dfaf755397e
diff --git a/internal/redact/heldsize_test.go b/internal/redact/heldsize_test.go
new file mode 100644
index 000000000..098ec7d28
--- /dev/null
+++ b/internal/redact/heldsize_test.go
@@ -0,0 +1,37 @@
+package redact
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+)
+
+// A masked body is held as the request's body while it streams (#3383),
+// so it is held at about its size: a buffer grown by doubling was up to
+// twice that, with few values masked or with many placeholders longer
+// than what they stand for.
+func TestMaskedBodyHeldAtItsSize(t *testing.T) {
+ for _, every := range []int{1, 400} {
+ var sb strings.Builder
+ sb.WriteString(`{"messages":[`)
+ for i := 0; i < 4000; i++ {
+ if i > 0 {
+ sb.WriteString(",")
+ }
+ text := strings.Repeat("some words about the build, nothing to mask in them. ", 20)
+ if i%every == 0 {
+ text += fmt.Sprintf("mail me at user%[email protected] or call 1380013%04d", i, i)
+ }
+ fmt.Fprintf(&sb, `{"role":"user","content":%q}`, text)
+ }
+ sb.WriteString(`]}`)
+ body := []byte(sb.String())
+ out, n := MaskJSON(body, Options{Personal: true})
+ if n == 0 {
+ t.Fatalf("every %d: nothing masked", every)
+ }
+ if slack := cap(out) - len(out); slack > len(out)/8+len(body)/16+64 {
+ t.Errorf("every %d: the masked body of %d bytes is held in %d", every, len(out), cap(out))
+ }
+ }
+}
diff --git a/internal/redact/json.go b/internal/redact/json.go
index f64ca8d3e..09671f124 100644
--- a/internal/redact/json.go
+++ b/internal/redact/json.go
@@ -214,7 +214,13 @@ func walk(b []byte, fn func(path, key, s string) string) (out []byte, ok bool) {
return b, true
}
w.out.Write(b[w.last:])
- return w.out.Bytes(), true
+ out = w.out.Bytes()
+ if cap(out)-len(out) > len(out)/8 {
+ // placeholders longer than what they stand for grew it past the
+ // room made for it: held as it is, a doubled buffer (#3383)
+ out = bytes.Clone(out)
+ }
+ return out, true
}
type walker struct {
@@ -324,6 +330,12 @@ func (w *walker) value(i int, path, key string) (int, bool) {
return i, false
}
if t := w.fn(path, key, s); t != s {
+ if w.out.Cap() == 0 {
+ // the masked body is about the body's size: grown by
+ // doubling, its buffer was up to twice that, held as the
+ // request's body while it streams (#3383)
+ w.out.Grow(len(w.in) + len(w.in)/16 + 64)
+ }
w.out.Write(w.in[w.last:i])
w.out.WriteByte('"')
w.out.WriteString(jsonEscape(t))