Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor: name both truncation helpers for their policy, so a machine-read field cannot pick the ellipsis by default (PIN-113)

Luigi Agosti70bfdfbd63c3
diff --git a/internal/activity/url_sanitize.go b/internal/activity/url_sanitize.go
index 3bb73dfc..234840a5 100644
--- a/internal/activity/url_sanitize.go
+++ b/internal/activity/url_sanitize.go
@@ -45,5 +45,5 @@ func sanitizeActivityURL(raw string) string {
}
}
- return sanitize.TruncateUTF8Bytes(parsed.String(), maxActivityURLBytes)
+ return sanitize.TruncateUTF8BytesWithEllipsis(parsed.String(), maxActivityURLBytes)
}
diff --git a/internal/bridge/console_logs.go b/internal/bridge/console_logs.go
index 12a6a831..113d5983 100644
--- a/internal/bridge/console_logs.go
+++ b/internal/bridge/console_logs.go
@@ -183,16 +183,16 @@ func (s *ConsoleLogStore) RemoveTab(tabID string) {
}
func normalizeConsoleLogEntry(entry LogEntry) LogEntry {
- entry.Level = sanitize.TruncateUTF8Bytes(entry.Level, maxConsoleLevelBytes)
- entry.Message = sanitize.TruncateUTF8Bytes(entry.Message, maxConsoleMessageBytes)
- entry.Source = sanitize.TruncateUTF8Bytes(entry.Source, maxConsoleSourceBytes)
+ entry.Level = sanitize.TruncateUTF8BytesWithEllipsis(entry.Level, maxConsoleLevelBytes)
+ entry.Message = sanitize.TruncateUTF8BytesWithEllipsis(entry.Message, maxConsoleMessageBytes)
+ entry.Source = sanitize.TruncateUTF8BytesWithEllipsis(entry.Source, maxConsoleSourceBytes)
return entry
}
func normalizeErrorLogEntry(entry ErrorEntry) ErrorEntry {
- entry.Message = sanitize.TruncateUTF8Bytes(entry.Message, maxErrorMessageBytes)
- entry.Type = sanitize.TruncateUTF8Bytes(entry.Type, maxErrorTypeBytes)
- entry.URL = sanitize.TruncateUTF8Bytes(entry.URL, maxErrorURLBytes)
- entry.Stack = sanitize.TruncateUTF8Bytes(entry.Stack, maxErrorStackBytes)
+ entry.Message = sanitize.TruncateUTF8BytesWithEllipsis(entry.Message, maxErrorMessageBytes)
+ entry.Type = sanitize.TruncateUTF8BytesWithEllipsis(entry.Type, maxErrorTypeBytes)
+ entry.URL = sanitize.TruncateUTF8BytesWithEllipsis(entry.URL, maxErrorURLBytes)
+ entry.Stack = sanitize.TruncateUTF8BytesWithEllipsis(entry.Stack, maxErrorStackBytes)
return entry
}
diff --git a/internal/bridge/observe/network.go b/internal/bridge/observe/network.go
index 483e31d3..b1449dcf 100644
--- a/internal/bridge/observe/network.go
+++ b/internal/bridge/observe/network.go
@@ -293,7 +293,7 @@ func clampRetainedBody(body string, base64Encoded bool, limit int, scope string)
if base64Encoded {
return "", false, "base64 body exceeds " + scope
}
- prefix := sanitize.PrefixUTF8Bytes(body, limit)
+ prefix := sanitize.TruncateUTF8BytesExact(body, limit)
if prefix == "" {
return "", false, scope + " is smaller than the body's first character"
}
@@ -470,12 +470,12 @@ func requestEntryFromEvent(e *network.EventRequestWillBeSent) NetworkEntry {
}
func normalizeNetworkEntry(entry NetworkEntry) NetworkEntry {
- entry.URL = sanitize.TruncateUTF8Bytes(entry.URL, maxNetworkURLBytes)
- entry.Method = sanitize.TruncateUTF8Bytes(entry.Method, maxNetworkMethodBytes)
- entry.ResourceType = sanitize.TruncateUTF8Bytes(entry.ResourceType, maxNetworkResourceTypeBytes)
- entry.StatusText = sanitize.TruncateUTF8Bytes(entry.StatusText, maxNetworkStatusTextBytes)
- entry.MimeType = sanitize.TruncateUTF8Bytes(entry.MimeType, maxNetworkMimeTypeBytes)
- entry.Error = sanitize.TruncateUTF8Bytes(entry.Error, maxNetworkErrorBytes)
+ entry.URL = sanitize.TruncateUTF8BytesWithEllipsis(entry.URL, maxNetworkURLBytes)
+ entry.Method = sanitize.TruncateUTF8BytesWithEllipsis(entry.Method, maxNetworkMethodBytes)
+ entry.ResourceType = sanitize.TruncateUTF8BytesWithEllipsis(entry.ResourceType, maxNetworkResourceTypeBytes)
+ entry.StatusText = sanitize.TruncateUTF8BytesWithEllipsis(entry.StatusText, maxNetworkStatusTextBytes)
+ entry.MimeType = sanitize.TruncateUTF8BytesWithEllipsis(entry.MimeType, maxNetworkMimeTypeBytes)
+ entry.Error = sanitize.TruncateUTF8BytesWithEllipsis(entry.Error, maxNetworkErrorBytes)
// The request body is clamped by the one owner of that rule, so it inherits the
// prefix-or-nothing policy the response body already states. The budget measures the
// decoded body, which is what PostData holds, so the constant describes the request
@@ -508,7 +508,7 @@ func normalizeNetworkHeaders(headers map[string]string) map[string]string {
break
}
- key = sanitize.TruncateUTF8Bytes(key, maxNetworkHeaderKeyBytes)
+ key = sanitize.TruncateUTF8BytesWithEllipsis(key, maxNetworkHeaderKeyBytes)
if key == "" {
continue
}
@@ -521,7 +521,7 @@ func normalizeNetworkHeaders(headers map[string]string) map[string]string {
break
}
- value = sanitize.TruncateUTF8Bytes(value, valueLimit)
+ value = sanitize.TruncateUTF8BytesWithEllipsis(value, valueLimit)
entryBytes := len(key) + len(value)
if entryBytes <= 0 {
continue
diff --git a/internal/bridge/observe/network_body_clamp_test.go b/internal/bridge/observe/network_body_clamp_test.go
index 9154a9e1..3ee6f8a8 100644
--- a/internal/bridge/observe/network_body_clamp_test.go
+++ b/internal/bridge/observe/network_body_clamp_test.go
@@ -261,10 +261,10 @@ func TestBodyClampsAndDisplayFieldsUseTheirOwnHelper(t *testing.T) {
if end := strings.Index(clamp, "\nfunc "); end >= 0 {
clamp = clamp[:end]
}
- if !strings.Contains(clamp, "sanitize.PrefixUTF8Bytes(") {
+ if !strings.Contains(clamp, "sanitize.TruncateUTF8BytesExact(") {
t.Error("clampRetainedBody no longer cuts with the suffix-free helper")
}
- if strings.Contains(clamp, "sanitize.TruncateUTF8Bytes(") {
+ if strings.Contains(clamp, "sanitize.TruncateUTF8BytesWithEllipsis(") {
t.Error("clampRetainedBody reaches the ellipsis variant again: the retained body would carry characters the response never sent")
}
if !strings.Contains(src, "clampRetainedBody(entry.PostData,") {
@@ -284,11 +284,11 @@ func TestBodyClampsAndDisplayFieldsUseTheirOwnHelper(t *testing.T) {
displayFields := []string{"entry.URL", "entry.Method", "entry.ResourceType", "entry.StatusText", "entry.MimeType", "entry.Error"}
for _, field := range displayFields {
- if !strings.Contains(src, field+" = sanitize.TruncateUTF8Bytes(") {
+ if !strings.Contains(src, field+" = sanitize.TruncateUTF8BytesWithEllipsis(") {
t.Errorf("%s no longer uses the ellipsis variant; a human reads it and the marker is the signal", field)
}
}
- headerClamps := []string{"key = sanitize.TruncateUTF8Bytes(", "value = sanitize.TruncateUTF8Bytes("}
+ headerClamps := []string{"key = sanitize.TruncateUTF8BytesWithEllipsis(", "value = sanitize.TruncateUTF8BytesWithEllipsis("}
for _, headerClamp := range headerClamps {
if !strings.Contains(src, headerClamp) {
t.Errorf("header clamp %q no longer uses the ellipsis variant", headerClamp)
@@ -299,7 +299,7 @@ func TestBodyClampsAndDisplayFieldsUseTheirOwnHelper(t *testing.T) {
// helper, which cannot see a site it does not know about — and an unlisted field
// taking the ellipsis by default is how the defect this file exists for arrived.
accounted := len(displayFields) + len(headerClamps) + 1 // the one payload clamp, which both the retained body and PostData go through
- if sites := strings.Count(src, "sanitize.TruncateUTF8Bytes(") + strings.Count(src, "sanitize.PrefixUTF8Bytes("); sites != accounted {
+ if sites := strings.Count(src, "sanitize.TruncateUTF8BytesWithEllipsis(") + strings.Count(src, "sanitize.TruncateUTF8BytesExact("); sites != accounted {
t.Errorf("network.go cuts a field at %d sites but this test classifies %d — a new field is picking a truncation policy nobody reviewed. Add it above: the ellipsis variant if a human reads it, the suffix-free one if it is machine-read and a marker would be fabricated content",
sites, accounted)
}
diff --git a/internal/bridge/tabs/dialog.go b/internal/bridge/tabs/dialog.go
index 60a3399e..71776f5b 100644
--- a/internal/bridge/tabs/dialog.go
+++ b/internal/bridge/tabs/dialog.go
@@ -223,8 +223,8 @@ func normalizeDialogState(state *DialogState) *DialogState {
}
copyState := *state
- copyState.Type = sanitize.TruncateUTF8Bytes(copyState.Type, 32)
- copyState.Message = sanitize.TruncateUTF8Bytes(copyState.Message, maxDialogTextBytes)
- copyState.DefaultPrompt = sanitize.TruncateUTF8Bytes(copyState.DefaultPrompt, maxDialogTextBytes)
+ copyState.Type = sanitize.TruncateUTF8BytesWithEllipsis(copyState.Type, 32)
+ copyState.Message = sanitize.TruncateUTF8BytesWithEllipsis(copyState.Message, maxDialogTextBytes)
+ copyState.DefaultPrompt = sanitize.TruncateUTF8BytesWithEllipsis(copyState.DefaultPrompt, maxDialogTextBytes)
return &copyState
}
diff --git a/internal/cli/actions/actions_network.go b/internal/cli/actions/actions_network.go
index 82b7dce0..8a08380e 100644
--- a/internal/cli/actions/actions_network.go
+++ b/internal/cli/actions/actions_network.go
@@ -79,7 +79,7 @@ func Network(client *http.Client, base, token string, cmd *cobra.Command, args [
return
}
for _, e := range resp.Entries {
- fmt.Printf("%-6s %3d %s\n", e.Method, e.Status, sanitize.TruncateUTF8Bytes(e.URL, networkURLDisplayMaxBytes))
+ fmt.Printf("%-6s %3d %s\n", e.Method, e.Status, sanitize.TruncateUTF8BytesWithEllipsis(e.URL, networkURLDisplayMaxBytes))
}
}
diff --git a/internal/handlers/network_export_stream.go b/internal/handlers/network_export_stream.go
index dc4cb005..bb177b5f 100644
--- a/internal/handlers/network_export_stream.go
+++ b/internal/handlers/network_export_stream.go
@@ -274,7 +274,7 @@ func (s *exportStreamSession) exportEntry(entry bridge.NetworkEntry) (stop bool)
return true
}
s.count++
- data, _ := json.Marshal(map[string]any{"entries": s.count, "url": sanitize.TruncateUTF8Bytes(entry.URL, maxProgressURLBytes)})
+ data, _ := json.Marshal(map[string]any{"entries": s.count, "url": sanitize.TruncateUTF8BytesWithEllipsis(entry.URL, maxProgressURLBytes)})
_, _ = fmt.Fprintf(s.w, "event: export\ndata: %s\n\n", data)
s.flusher.Flush()
return false
diff --git a/internal/htmltrim/trim.go b/internal/htmltrim/trim.go
index 3bedd7e0..939fba83 100644
--- a/internal/htmltrim/trim.go
+++ b/internal/htmltrim/trim.go
@@ -33,7 +33,7 @@ func TrimHTML(html string) string {
}
}
- return sanitize.PrefixUTF8Bytes(strings.Join(trimmed, "\n"), maxTrimmedBytes)
+ return sanitize.TruncateUTF8BytesExact(strings.Join(trimmed, "\n"), maxTrimmedBytes)
}
var (
diff --git a/internal/orchestrator/health_transport.go b/internal/orchestrator/health_transport.go
index 9d30754f..a6596f61 100644
--- a/internal/orchestrator/health_transport.go
+++ b/internal/orchestrator/health_transport.go
@@ -116,5 +116,5 @@ func compactBody(body []byte) string {
if trimmed == "" {
return "<empty>"
}
- return sanitize.TruncateUTF8Bytes(trimmed, maxCompactBodyBytes)
+ return sanitize.TruncateUTF8BytesWithEllipsis(trimmed, maxCompactBodyBytes)
}
diff --git a/internal/sanitize/text.go b/internal/sanitize/text.go
index 2e3505b3..56a4cb28 100644
--- a/internal/sanitize/text.go
+++ b/internal/sanitize/text.go
@@ -14,10 +14,10 @@ var (
winAbs = regexp.MustCompile(`(^|[\s"'(=:])([A-Za-z]:\\(?:[^\s"'():;<>{}\[\]]+\\?)+)`)
)
-// PrefixUTF8Bytes returns the longest prefix of s that fits in maxBytes bytes
+// TruncateUTF8BytesExact returns the longest prefix of s that fits in maxBytes bytes
// without splitting a rune. Nothing is appended: callers that want a
-// truncation marker use TruncateUTF8Bytes.
-func PrefixUTF8Bytes(s string, maxBytes int) string {
+// truncation marker use TruncateUTF8BytesWithEllipsis.
+func TruncateUTF8BytesExact(s string, maxBytes int) string {
if maxBytes <= 0 {
return ""
}
@@ -34,7 +34,7 @@ func PrefixUTF8Bytes(s string, maxBytes int) string {
return s[:cut]
}
-func TruncateUTF8Bytes(s string, maxBytes int) string {
+func TruncateUTF8BytesWithEllipsis(s string, maxBytes int) string {
if maxBytes <= 0 {
return ""
}
@@ -44,7 +44,7 @@ func TruncateUTF8Bytes(s string, maxBytes int) string {
if maxBytes <= len(TruncationSuffix) {
return TruncationSuffix[:maxBytes]
}
- return PrefixUTF8Bytes(s, maxBytes-len(TruncationSuffix)) + TruncationSuffix
+ return TruncateUTF8BytesExact(s, maxBytes-len(TruncationSuffix)) + TruncationSuffix
}
func StripANSI(s string) string {
@@ -113,9 +113,9 @@ func isTextSpoofingRune(r rune) bool {
}
func CleanForLog(s string, maxBytes int) string {
- return TruncateUTF8Bytes(StripTextSpoofingRunes(StripControlChars(StripANSI(s))), maxBytes)
+ return TruncateUTF8BytesWithEllipsis(StripTextSpoofingRunes(StripControlChars(StripANSI(s))), maxBytes)
}
func CleanError(s string, maxBytes int) string {
- return TruncateUTF8Bytes(RedactAbsolutePaths(StripTextSpoofingRunes(StripControlChars(StripANSI(s)))), maxBytes)
+ return TruncateUTF8BytesWithEllipsis(RedactAbsolutePaths(StripTextSpoofingRunes(StripControlChars(StripANSI(s)))), maxBytes)
}
diff --git a/internal/sanitize/text_test.go b/internal/sanitize/text_test.go
index 194b1360..407cf100 100644
--- a/internal/sanitize/text_test.go
+++ b/internal/sanitize/text_test.go
@@ -57,7 +57,7 @@ func TestCleanErrorRedactsAbsolutePaths(t *testing.T) {
// that separates these two exported helpers, and it is the reason callers pick
// one over the other, so it is asserted where the rule is owned rather than left
// to whichever downstream package happens to notice.
-func TestTruncateUTF8BytesCutsOnRuneBoundaryWithMarker(t *testing.T) {
+func TestTruncateUTF8BytesWithEllipsisCutsOnRuneBoundaryWithMarker(t *testing.T) {
const s = "héllo"
tests := []struct {
@@ -79,26 +79,26 @@ func TestTruncateUTF8BytesCutsOnRuneBoundaryWithMarker(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got := TruncateUTF8Bytes(tt.input, tt.maxBytes)
+ got := TruncateUTF8BytesWithEllipsis(tt.input, tt.maxBytes)
if got != tt.want {
- t.Fatalf("TruncateUTF8Bytes(%q, %d) = %q, want %q", tt.input, tt.maxBytes, got, tt.want)
+ t.Fatalf("TruncateUTF8BytesWithEllipsis(%q, %d) = %q, want %q", tt.input, tt.maxBytes, got, tt.want)
}
if !utf8.ValidString(got) {
- t.Fatalf("TruncateUTF8Bytes(%q, %d) is not valid UTF-8", tt.input, tt.maxBytes)
+ t.Fatalf("TruncateUTF8BytesWithEllipsis(%q, %d) is not valid UTF-8", tt.input, tt.maxBytes)
}
if len(got) > tt.maxBytes && tt.maxBytes > 0 {
- t.Fatalf("TruncateUTF8Bytes(%q, %d) = %q exceeds the byte budget", tt.input, tt.maxBytes, got)
+ t.Fatalf("TruncateUTF8BytesWithEllipsis(%q, %d) = %q exceeds the byte budget", tt.input, tt.maxBytes, got)
}
// Below the marker's own length there is no room for it, so the
// output is a prefix of the marker rather than content plus marker.
if len(tt.input) > tt.maxBytes && tt.maxBytes > len(TruncationSuffix) && !strings.Contains(got, TruncationSuffix) {
- t.Fatalf("TruncateUTF8Bytes(%q, %d) = %q cut silently; the marker is what distinguishes it from PrefixUTF8Bytes", tt.input, tt.maxBytes, got)
+ t.Fatalf("TruncateUTF8BytesWithEllipsis(%q, %d) = %q cut silently; the marker is what distinguishes it from TruncateUTF8BytesExact", tt.input, tt.maxBytes, got)
}
})
}
}
-func TestPrefixUTF8BytesCutsOnRuneBoundaryWithoutMarker(t *testing.T) {
+func TestTruncateUTF8BytesExactCutsOnRuneBoundaryWithoutMarker(t *testing.T) {
const s = "héllo"
tests := []struct {
@@ -117,15 +117,15 @@ func TestPrefixUTF8BytesCutsOnRuneBoundaryWithoutMarker(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got := PrefixUTF8Bytes(s, tt.maxBytes)
+ got := TruncateUTF8BytesExact(s, tt.maxBytes)
if got != tt.want {
- t.Fatalf("PrefixUTF8Bytes(%q, %d) = %q, want %q", s, tt.maxBytes, got, tt.want)
+ t.Fatalf("TruncateUTF8BytesExact(%q, %d) = %q, want %q", s, tt.maxBytes, got, tt.want)
}
if !utf8.ValidString(got) {
- t.Fatalf("PrefixUTF8Bytes(%q, %d) is not valid UTF-8", s, tt.maxBytes)
+ t.Fatalf("TruncateUTF8BytesExact(%q, %d) is not valid UTF-8", s, tt.maxBytes)
}
if strings.Contains(got, TruncationSuffix) {
- t.Fatalf("PrefixUTF8Bytes appended a truncation marker: %q", got)
+ t.Fatalf("TruncateUTF8BytesExact appended a truncation marker: %q", got)
}
})
}
@@ -137,7 +137,7 @@ func TestPrefixUTF8BytesCutsOnRuneBoundaryWithoutMarker(t *testing.T) {
// 1 or 2 emits a broken rune from inside the helper every caller trusts. Swept across
// every budget rather than at hand-picked ones, because the defect lives at exactly the
// budgets nobody writes a case for.
-func TestTruncateUTF8BytesNeverEmitsABrokenRuneAtAnyBudget(t *testing.T) {
+func TestTruncateUTF8BytesWithEllipsisNeverEmitsABrokenRuneAtAnyBudget(t *testing.T) {
inputs := []string{
"",
"ascii only",
@@ -150,13 +150,13 @@ func TestTruncateUTF8BytesNeverEmitsABrokenRuneAtAnyBudget(t *testing.T) {
var checked int
for _, in := range inputs {
for maxBytes := -2; maxBytes <= len(in)+4; maxBytes++ {
- got := TruncateUTF8Bytes(in, maxBytes)
+ got := TruncateUTF8BytesWithEllipsis(in, maxBytes)
checked++
if !utf8.ValidString(got) {
- t.Errorf("TruncateUTF8Bytes(%q, %d) = %q, which is not valid UTF-8", in, maxBytes, got)
+ t.Errorf("TruncateUTF8BytesWithEllipsis(%q, %d) = %q, which is not valid UTF-8", in, maxBytes, got)
}
if maxBytes > 0 && len(got) > maxBytes {
- t.Errorf("TruncateUTF8Bytes(%q, %d) returned %d bytes, over budget", in, maxBytes, len(got))
+ t.Errorf("TruncateUTF8BytesWithEllipsis(%q, %d) returned %d bytes, over budget", in, maxBytes, len(got))
}
}
}
diff --git a/internal/urls/log_redact.go b/internal/urls/log_redact.go
index ee25c9d3..163c76a9 100644
--- a/internal/urls/log_redact.go
+++ b/internal/urls/log_redact.go
@@ -47,5 +47,5 @@ func RedactForLog(raw string) string {
}
}
- return sanitize.TruncateUTF8Bytes(parsed.String(), maxLogURLBytes)
+ return sanitize.TruncateUTF8BytesWithEllipsis(parsed.String(), maxLogURLBytes)
}
diff --git a/internal/urls/urls_test.go b/internal/urls/urls_test.go
index 736f6040..1577af4c 100644
--- a/internal/urls/urls_test.go
+++ b/internal/urls/urls_test.go
@@ -154,7 +154,7 @@ func TestRedactForLog(t *testing.T) {
}
// The byte cap on a redacted log URL moved from a package-private copy to
-// sanitize.TruncateUTF8Bytes. These pin the empty, under-limit, at-limit and
+// sanitize.TruncateUTF8BytesWithEllipsis. These pin the empty, under-limit, at-limit and
// over-limit cases at a converted call site.
//
// Rune-boundary splitting is deliberately NOT pinned here and cannot be: