High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: network response bodies are cut at a raw byte offset, returning U+FFFD and undecodable base64 PIN-035
Luigi Agostid4f4a8076dfd
diff --git a/internal/bridge/observe/network.go b/internal/bridge/observe/network.goindex bba4f885..b6dbec61 100644--- a/internal/bridge/observe/network.go+++ b/internal/bridge/observe/network.go@@ -278,6 +278,35 @@ func (nm *NetworkMonitor) bodyRetentionEnabled() bool {return nm.retainBodies}+// fetchResponseBody reads the body from the browser. Indirected so retention+// policy can be exercised without a live CDP target.+var fetchResponseBody = GetResponseBodyDirect++func skipRetainedBody(buf *NetworkBuffer, requestID, reason string) {+ buf.Update(requestID, func(entry *NetworkEntry) {+ entry.BodyPending = false+ entry.BodySkipped = true+ entry.BodySkipReason = reason+ entry.BodyError = ""+ })+}++// clampRetainedBody applies a byte budget to a retained response body. Text is+// cut on a rune boundary: a raw byte cut orphans UTF-8 continuation bytes and+// the JSON encoder then emits U+FFFD the response never contained. A base64+// body cannot be cut at all — the encoding's length and padding make a fragment+// undecodable in whole, not only at the tail — so it is dropped for the caller+// to report, rather than returned corrupt.+func clampRetainedBody(body string, base64Encoded bool, limit int) (clamped string, truncated, dropped bool) {+ if limit <= 0 || len(body) <= limit {+ return body, false, false+ }+ if base64Encoded {+ return "", false, true+ }+ return sanitize.TruncateUTF8Bytes(body, limit), true, false+}+func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBuffer, requestID string) {// Every return path below resolves BodyPending via buf.Update; signal once on// exit so retained-body readers wake instead of polling.@@ -288,36 +317,21 @@ func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBumaxBytes := nm.retainBodyMaxBytesnm.mu.RUnlock()if !enabled {- buf.Update(requestID, func(entry *NetworkEntry) {- entry.BodyPending = false- entry.BodySkipped = true- entry.BodySkipReason = "retention disabled"- entry.BodyError = ""- })+ skipRetainedBody(buf, requestID, "retention disabled")return}if buf.RetainedBytes() >= nm.retainBodyMaxPerTab {- buf.Update(requestID, func(entry *NetworkEntry) {- entry.BodyPending = false- entry.BodySkipped = true- entry.BodySkipReason = "retention budget exceeded"- entry.BodyError = ""- })+ skipRetainedBody(buf, requestID, "retention budget exceeded")return}select {case nm.retainBodySemaphore <- struct{}{}:defer func() { <-nm.retainBodySemaphore }()default:- buf.Update(requestID, func(entry *NetworkEntry) {- entry.BodyPending = false- entry.BodySkipped = true- entry.BodySkipReason = "retention concurrency limit reached"- entry.BodyError = ""- })+ skipRetainedBody(buf, requestID, "retention concurrency limit reached")return}- body, base64Encoded, err := GetResponseBodyDirect(tabCtx, requestID)+ body, base64Encoded, err := fetchResponseBody(tabCtx, requestID)if err != nil {buf.Update(requestID, func(entry *NetworkEntry) {entry.BodyPending = false@@ -327,25 +341,22 @@ func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBu})return}- truncated := false- if maxBytes > 0 && len(body) > maxBytes {- body = body[:maxBytes]- truncated = true+ body, truncated, dropped := clampRetainedBody(body, base64Encoded, maxBytes)+ if dropped {+ skipRetainedBody(buf, requestID, "base64 body exceeds retention limit")+ return}remainingBudget := int(nm.retainBodyMaxPerTab - buf.RetainedBytes())if remainingBudget <= 0 {- buf.Update(requestID, func(entry *NetworkEntry) {- entry.BodyPending = false- entry.BodySkipped = true- entry.BodySkipReason = "retention budget exceeded"- entry.BodyError = ""- })+ skipRetainedBody(buf, requestID, "retention budget exceeded")return}- if len(body) > remainingBudget {- body = body[:remainingBudget]- truncated = true+ body, cutForBudget, dropped := clampRetainedBody(body, base64Encoded, remainingBudget)+ if dropped {+ skipRetainedBody(buf, requestID, "base64 body exceeds retention budget")+ return}+ truncated = truncated || cutForBudgetbuf.Update(requestID, func(entry *NetworkEntry) {entry.ResponseBody = bodyentry.Base64Encoded = base64Encodeddiff --git a/internal/bridge/observe/network_body_clamp_test.go b/internal/bridge/observe/network_body_clamp_test.gonew file mode 100644index 00000000..2bf7dbdd--- /dev/null+++ b/internal/bridge/observe/network_body_clamp_test.go@@ -0,0 +1,151 @@+package observe++import (+ "context"+ "encoding/base64"+ "strings"+ "testing"+ "unicode/utf8"+)++// multiByteBody's cut offsets fall inside a 3-byte and a 4-byte character, so a+// raw byte cut orphans continuation bytes and the JSON encoder emits U+FFFD.+const multiByteBody = `{"note":"Docs — guide 🎯 done","tail":"padding padding padding"}`++func retainWithBody(t *testing.T, body string, base64Encoded bool, maxBytes int, perTab int64) NetworkEntry {+ t.Helper()++ orig := fetchResponseBody+ fetchResponseBody = func(context.Context, string) (string, bool, error) {+ return body, base64Encoded, nil+ }+ t.Cleanup(func() { fetchResponseBody = orig })++ nm := NewNetworkMonitor(16)+ nm.ConfigureBodyRetention(true, maxBytes)+ nm.retainBodyMaxPerTab = perTab++ buf := NewNetworkBuffer(16)+ buf.Add(NetworkEntry{RequestID: "req-1", URL: "https://example.test/", BodyPending: true})+ nm.maybeRetainBody(context.Background(), buf, "req-1")++ entry, ok := buf.Get("req-1")+ if !ok {+ t.Fatal("entry disappeared from the buffer")+ }+ return entry+}++func assertBodyIsCleanUTF8(t *testing.T, source, got string) {+ t.Helper()+ if !utf8.ValidString(got) {+ t.Fatalf("retained body is not valid UTF-8: %q", got)+ }+ if strings.Count(got, "�") > strings.Count(source, "�") {+ t.Fatalf("retained body gained replacement characters: %q", got)+ }+}++// The two cut sites are the opt-in retainBodyMaxBytes limit and the always-on+// per-tab budget; the second is reachable without opting in at all.+func TestRetainTextBodyCutsOnRuneBoundariesAtBothSites(t *testing.T) {+ for offset := 1; offset < len(multiByteBody); offset++ {+ byMaxBytes := retainWithBody(t, multiByteBody, false, offset, 1<<20)+ assertBodyIsCleanUTF8(t, multiByteBody, byMaxBytes.ResponseBody)+ if !byMaxBytes.BodyRetained {+ t.Fatalf("maxBytes=%d: text body must still be retained", offset)+ }++ byBudget := retainWithBody(t, multiByteBody, false, 0, int64(offset))+ assertBodyIsCleanUTF8(t, multiByteBody, byBudget.ResponseBody)+ if !byBudget.BodyRetained {+ t.Fatalf("budget=%d: text body must still be retained", offset)+ }+ }+}++func TestRetainTextBodyMarksTruncatedNotSkipped(t *testing.T) {+ for _, tc := range []struct {+ name string+ limit int+ perTab int64+ }{+ {"retainBodyMaxBytes limit", 16, 1 << 20},+ {"per-tab remaining budget", 0, 16},+ } {+ t.Run(tc.name, func(t *testing.T) {+ entry := retainWithBody(t, multiByteBody, false, tc.limit, tc.perTab)++ if !entry.BodyTruncated {+ t.Fatalf("a cut-but-usable text body must report bodyTruncated: %+v", entry)+ }+ if entry.BodySkipped || entry.BodySkipReason != "" {+ t.Fatalf("a truncated text body must not also report bodySkipped: skipped=%v reason=%q", entry.BodySkipped, entry.BodySkipReason)+ }+ if entry.ResponseBody == "" {+ t.Fatal("a truncated text body must still carry the part that fit")+ }+ })+ }+}++func TestRetainBase64BodyOverBudgetIsSkippedNotCorrupted(t *testing.T) {+ raw := strings.Repeat("binary-payload-", 40)+ encoded := base64.StdEncoding.EncodeToString([]byte(raw))++ for _, tc := range []struct {+ name string+ limit int+ perTab int64+ wantReason string+ }{+ {"retainBodyMaxBytes limit", 32, 1 << 20, "base64 body exceeds retention limit"},+ {"per-tab remaining budget", 0, 32, "base64 body exceeds retention budget"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ entry := retainWithBody(t, encoded, true, tc.limit, tc.perTab)++ if entry.ResponseBody != "" {+ if _, err := base64.StdEncoding.DecodeString(entry.ResponseBody); err != nil {+ t.Fatalf("returned an undecodable base64 fragment: %v (%q)", err, entry.ResponseBody)+ }+ }+ if !entry.BodySkipped {+ t.Fatalf("an over-budget base64 body must report bodySkipped: %+v", entry)+ }+ if entry.BodySkipReason != tc.wantReason {+ t.Fatalf("skip reason = %q, want %q", entry.BodySkipReason, tc.wantReason)+ }+ if entry.BodyTruncated {+ t.Fatal("a dropped body must not be reported as truncated: the client would parse a body that is not there")+ }+ })+ }+}++func TestRetainBodiesUnderBudgetAreUnchanged(t *testing.T) {+ encoded := base64.StdEncoding.EncodeToString([]byte("small binary payload"))++ for _, tc := range []struct {+ name string+ body string+ base64Encoded bool+ }{+ {"text", multiByteBody, false},+ {"base64", encoded, true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ entry := retainWithBody(t, tc.body, tc.base64Encoded, len(tc.body)+1, 1<<20)++ if entry.ResponseBody != tc.body {+ t.Fatalf("body under the budget was altered:\n got %q\nwant %q", entry.ResponseBody, tc.body)+ }+ if entry.BodyTruncated || entry.BodySkipped {+ t.Fatalf("body under the budget must be neither truncated nor skipped: %+v", entry)+ }+ if entry.Base64Encoded != tc.base64Encoded {+ t.Fatalf("base64Encoded = %v, want %v", entry.Base64Encoded, tc.base64Encoded)+ }+ })+ }+}