High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: retained bodies must be byte-exact prefixes, not display-truncated with a fabricated ellipsis (PIN-040)
Luigi Agosti3d06e42fe02d
diff --git a/internal/bridge/observe/network.go b/internal/bridge/observe/network.goindex b6dbec61..be498f29 100644--- a/internal/bridge/observe/network.go+++ b/internal/bridge/observe/network.go@@ -291,20 +291,34 @@ func skipRetainedBody(buf *NetworkBuffer, requestID, reason string) {})}-// 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) {+// Operator-facing names of the two budgets a retained body is cut against, used+// to build the skip reason so a dropped body says which one dropped it.+const (+ retentionLimitScope = "retention limit"+ retentionBudgetScope = "retention budget"+)++// clampRetainedBody applies a byte budget to a retained response body. One rule:+// a retained body is a byte-exact prefix of the response, or there is no retained+// body. Text is cut on a rune boundary with the suffix-free helper — the display+// variant appends "..." inside the budget, and a machine-read body must not carry+// characters the response never contained when bodyTruncated already says it was+// cut. 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), and a budget smaller+// than the body's first character leaves no whole rune to keep; both are dropped+// with a reason rather than returned corrupt or returned empty-but-retained.+func clampRetainedBody(body string, base64Encoded bool, limit int, scope string) (clamped string, truncated bool, dropReason string) {if limit <= 0 || len(body) <= limit {- return body, false, false+ return body, false, ""}if base64Encoded {- return "", false, true+ return "", false, "base64 body exceeds " + scope+ }+ prefix := sanitize.PrefixUTF8Bytes(body, limit)+ if prefix == "" {+ return "", false, scope + " is smaller than the body's first character"}- return sanitize.TruncateUTF8Bytes(body, limit), true, false+ return prefix, true, ""}func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBuffer, requestID string) {@@ -341,9 +355,9 @@ func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBu})return}- body, truncated, dropped := clampRetainedBody(body, base64Encoded, maxBytes)- if dropped {- skipRetainedBody(buf, requestID, "base64 body exceeds retention limit")+ body, truncated, dropReason := clampRetainedBody(body, base64Encoded, maxBytes, retentionLimitScope)+ if dropReason != "" {+ skipRetainedBody(buf, requestID, dropReason)return}remainingBudget := int(nm.retainBodyMaxPerTab - buf.RetainedBytes())@@ -351,9 +365,9 @@ func (nm *NetworkMonitor) maybeRetainBody(tabCtx context.Context, buf *NetworkBuskipRetainedBody(buf, requestID, "retention budget exceeded")return}- body, cutForBudget, dropped := clampRetainedBody(body, base64Encoded, remainingBudget)- if dropped {- skipRetainedBody(buf, requestID, "base64 body exceeds retention budget")+ body, cutForBudget, dropReason := clampRetainedBody(body, base64Encoded, remainingBudget, retentionBudgetScope)+ if dropReason != "" {+ skipRetainedBody(buf, requestID, dropReason)return}truncated = truncated || cutForBudget@@ -435,7 +449,9 @@ func normalizeNetworkEntry(entry NetworkEntry) NetworkEntry {entry.StatusText = sanitize.TruncateUTF8Bytes(entry.StatusText, maxNetworkStatusTextBytes)entry.MimeType = sanitize.TruncateUTF8Bytes(entry.MimeType, maxNetworkMimeTypeBytes)entry.Error = sanitize.TruncateUTF8Bytes(entry.Error, maxNetworkErrorBytes)- entry.PostData = sanitize.TruncateUTF8Bytes(entry.PostData, maxNetworkPostDataBytes)+ // PostData is the request body: machine-read like the response body, so it is+ // cut to a byte-exact prefix rather than display-truncated with an ellipsis.+ entry.PostData = sanitize.PrefixUTF8Bytes(entry.PostData, maxNetworkPostDataBytes)entry.RequestHeaders = normalizeNetworkHeaders(entry.RequestHeaders)entry.ResponseHeaders = normalizeNetworkHeaders(entry.ResponseHeaders)return entrydiff --git a/internal/bridge/observe/network_body_clamp_test.go b/internal/bridge/observe/network_body_clamp_test.goindex 2bf7dbdd..04cfaf43 100644--- a/internal/bridge/observe/network_body_clamp_test.go+++ b/internal/bridge/observe/network_body_clamp_test.go@@ -3,9 +3,12 @@ package observeimport ("context""encoding/base64"+ "os""strings""testing""unicode/utf8"++ "github.com/pinchtab/pinchtab/internal/sanitize")// multiByteBody's cut offsets fall inside a 3-byte and a 4-byte character, so a@@ -44,6 +47,21 @@ func assertBodyIsCleanUTF8(t *testing.T, source, got string) {if strings.Count(got, "�") > strings.Count(source, "�") {t.Fatalf("retained body gained replacement characters: %q", got)}+ assertRetainedIsPrefix(t, source, got)+}++// The clamp exists to keep characters the response never contained out of the+// payload, and valid-UTF-8-plus-no-new-U+FFFD does not say that: a retained body+// of ".." passes both. A retained body is machine-read and bodyTruncated already+// carries the signal, so the only honest shape is a byte-exact prefix.+func assertRetainedIsPrefix(t *testing.T, source, got string) {+ t.Helper()+ if len(got) > len(source) {+ t.Fatalf("retained body is longer than the source: %d > %d bytes (%q)", len(got), len(source), got)+ }+ if got != source[:len(got)] {+ t.Fatalf("retained body is not a byte-exact prefix of the response:\n got %q\nwant %q", got, source[:len(got)])+ }}// The two cut sites are the opt-in retainBodyMaxBytes limit and the always-on@@ -149,3 +167,118 @@ func TestRetainBodiesUnderBudgetAreUnchanged(t *testing.T) {})}}++// firstRuneWideBody's first character is 4 bytes, so budgets 1..3 leave no whole+// rune to keep. multiByteBody cannot reach that case — it opens with "{" — which+// is why the fabricated-suffix bug survived a loop over every offset of it.+const firstRuneWideBody = `🎯 target reached`++func TestRetainTextBodyIsDroppedWhenNoWholeRuneFits(t *testing.T) {+ for _, site := range []struct {+ name string+ limitFor func(int) (int, int64)+ wantReason string+ }{+ {+ name: "retainBodyMaxBytes limit",+ limitFor: func(limit int) (int, int64) { return limit, 1 << 20 },+ wantReason: "retention limit is smaller than the body's first character",+ },+ {+ name: "per-tab remaining budget",+ limitFor: func(limit int) (int, int64) { return 0, int64(limit) },+ wantReason: "retention budget is smaller than the body's first character",+ },+ } {+ t.Run(site.name, func(t *testing.T) {+ for limit := 1; limit < 4; limit++ {+ maxBytes, perTab := site.limitFor(limit)+ entry := retainWithBody(t, firstRuneWideBody, false, maxBytes, perTab)++ if entry.ResponseBody != "" {+ t.Errorf("limit=%d: kept %q of a body whose first character does not fit", limit, entry.ResponseBody)+ }+ if entry.BodyRetained {+ t.Errorf("limit=%d: reported bodyRetained with nothing retained: %+v", limit, entry)+ }+ if entry.BodyTruncated {+ t.Errorf("limit=%d: reported bodyTruncated for a body that was dropped, not cut", limit)+ }+ if !entry.BodySkipped {+ t.Errorf("limit=%d: a dropped body must report bodySkipped: %+v", limit, entry)+ }+ if entry.BodySkipReason != site.wantReason {+ t.Errorf("limit=%d: skip reason = %q, want %q", limit, entry.BodySkipReason, site.wantReason)+ }+ }++ // One byte more and the whole first rune fits, so the body comes back as a+ // prefix — the drop above is the budget being too small, not a dead branch.+ maxBytes, perTab := site.limitFor(4)+ entry := retainWithBody(t, firstRuneWideBody, false, maxBytes, perTab)+ if entry.ResponseBody != "🎯" {+ t.Fatalf("limit=4: body = %q, want the first character alone", entry.ResponseBody)+ }+ if !entry.BodyRetained || !entry.BodyTruncated || entry.BodySkipped {+ t.Fatalf("limit=4: want retained+truncated and not skipped: %+v", entry)+ }+ assertRetainedIsPrefix(t, firstRuneWideBody, entry.ResponseBody)+ })+ }+}++// PostData is the request body — the same field class as the response body, and it+// went through the same display helper. It has no truncated flag of its own, so a+// fabricated ellipsis there is indistinguishable from content the client sent.+func TestPostDataIsCutToAByteExactPrefix(t *testing.T) {+ long := strings.Repeat("a", maxNetworkPostDataBytes-2) + "🎯" + "tail"++ got := normalizeNetworkEntry(NetworkEntry{PostData: long}).PostData++ if len(got) > maxNetworkPostDataBytes {+ t.Fatalf("PostData = %d bytes, want at most %d", len(got), maxNetworkPostDataBytes)+ }+ if got != long[:len(got)] {+ t.Fatalf("PostData is not a byte-exact prefix of what was sent: tail = %q", got[max(0, len(got)-8):])+ }+ if strings.Contains(got, sanitize.TruncationSuffix) && !strings.Contains(long, sanitize.TruncationSuffix) {+ t.Fatalf("PostData gained the display truncation suffix: tail = %q", got[max(0, len(got)-8):])+ }+}++// The display fields are the case the ellipsis is right for: a human reads a URL+// or an error string and the marker is the point. This pins the split both ways —+// the two body fields must not reach the ellipsis variant, and the metadata fields+// must not lose it.+func TestBodyClampsAndDisplayFieldsUseTheirOwnHelper(t *testing.T) {+ raw, err := os.ReadFile("network.go")+ if err != nil {+ t.Fatal(err)+ }+ src := string(raw)++ clamp := src[strings.Index(src, "func clampRetainedBody("):]+ if end := strings.Index(clamp, "\nfunc "); end >= 0 {+ clamp = clamp[:end]+ }+ if !strings.Contains(clamp, "sanitize.PrefixUTF8Bytes(") {+ t.Error("clampRetainedBody no longer cuts with the suffix-free helper")+ }+ if strings.Contains(clamp, "sanitize.TruncateUTF8Bytes(") {+ t.Error("clampRetainedBody reaches the ellipsis variant again: the retained body would carry characters the response never sent")+ }+ if !strings.Contains(src, "entry.PostData = sanitize.PrefixUTF8Bytes(") {+ t.Error("PostData no longer cuts with the suffix-free helper")+ }++ for _, field := range []string{"entry.URL", "entry.Method", "entry.ResourceType", "entry.StatusText", "entry.MimeType", "entry.Error"} {+ if !strings.Contains(src, field+" = sanitize.TruncateUTF8Bytes(") {+ t.Errorf("%s no longer uses the ellipsis variant; a human reads it and the marker is the signal", field)+ }+ }+ for _, headerClamp := range []string{"key = sanitize.TruncateUTF8Bytes(", "value = sanitize.TruncateUTF8Bytes("} {+ if !strings.Contains(src, headerClamp) {+ t.Errorf("header clamp %q no longer uses the ellipsis variant", headerClamp)+ }+ }+}