diff --git a/backend/internal/service/json_escape_probe.go b/backend/internal/service/json_escape_probe.go index 317bbbed7c44..226b37dc8b43 100644 --- a/backend/internal/service/json_escape_probe.go +++ b/backend/internal/service/json_escape_probe.go @@ -18,28 +18,41 @@ import "bytes" // Escapes are consumed pairwise, so an escaped backslash followed by a literal // u (Windows paths, regexes, code samples) is plain text, not an escape. func JSONMayEscapePlainASCII(body []byte) bool { - for i := 0; ; { - j := bytes.IndexByte(body[i:], '\\') - if j < 0 { - return false - } - next := i + j + 1 - if next >= len(body) { + gap := bytes.IndexByte(body, '\\') + for gap >= 0 { + body = body[gap+1:] + if len(body) == 0 { return true } - if body[next] != 'u' { - i = next + 1 - continue - } - if next+5 > len(body) { - return true + width := 1 + if body[0] == 'u' { + if len(body) < 5 { + return true + } + r, ok := parseJSONHex4(body[1:5]) + if !ok || jsonEscapeHidesPlainASCII(r) { + return true + } + width = 5 } - r, ok := parseJSONHex4(body[next+1 : next+5]) - if !ok || jsonEscapeHidesPlainASCII(r) { - return true + body = body[width:] + gap = nextJSONBackslash(body, gap) + } + return false +} + +// Escapes in quoted code and tool output often cluster. After a nearby hit, +// check the next few bytes inline before searching the rest of the body. +func nextJSONBackslash(body []byte, previousGap int) int { + const nearby = 16 + if previousGap < nearby && len(body) >= nearby { + for i, c := range body[:nearby] { + if c == '\\' { + return i + } } - i = next + 5 } + return bytes.IndexByte(body, '\\') } func parseJSONHex4(b []byte) (rune, bool) { diff --git a/backend/internal/service/json_escape_probe_test.go b/backend/internal/service/json_escape_probe_test.go index 46b1f1d6d7bd..bf7c5b60734f 100644 --- a/backend/internal/service/json_escape_probe_test.go +++ b/backend/internal/service/json_escape_probe_test.go @@ -3,6 +3,7 @@ package service import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -36,3 +37,45 @@ func TestJSONMayEscapePlainASCII(t *testing.T) { }) } } + +func TestJSONMayEscapePlainASCIIBackslashRuns(t *testing.T) { + for count := 0; count <= 9; count++ { + run := strings.Repeat("\\", count) + odd := count%2 != 0 + for _, prefix := range []string{"", "\\u003c", "\\n" + strings.Repeat("x", 15), "\\n" + strings.Repeat("x", 16), strings.Repeat("code\\n", 100)} { + require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run)), "trailing run %d", count) + require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run+"u0061")), "keyword run %d", count) + require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run+"u0x")), "malformed run %d", count) + } + } +} + +func BenchmarkJSONMayEscapePlainASCII(b *testing.B) { + code := strings.Repeat("func f() { return \\\"sample\\\" }\\n", 1<<15) + for _, tc := range []struct { + name string + body string + want bool + }{ + {"short", "{\"input\":\"ok\"}", false}, + {"short-newline", "{\"input\":\"a\\nb\"}", false}, + {"plain", strings.Repeat("function sample ", 1<<16), false}, + {"quoted-code", code, false}, + {"source-file", strings.Repeat("package app\\nfunc sample() string { return \\\"Example 测试\\\" }\\n", 1<<14), false}, + {"sparse-newlines", strings.Repeat(strings.Repeat("x", 126)+"\\n", 1<<13), false}, + {"late-keyword-escape", code + "\\u0061", true}, + {"html-escapes", strings.Repeat("\\u003c\\u003e", 1<<16), false}, + {"escaped-backslashes", strings.Repeat("\\\\", 1<<19), false}, + } { + b.Run(tc.name, func(b *testing.B) { + body := []byte(tc.body) + b.ReportAllocs() + b.SetBytes(int64(len(body))) + for b.Loop() { + if JSONMayEscapePlainASCII(body) != tc.want { + b.Fatal("unexpected escape classification") + } + } + }) + } +}