Skip to content

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

fix: health error messages cut a non-ASCII response body mid-rune (PIN-060)

Luigi Agostiae240c57ecbf
diff --git a/internal/orchestrator/health_test.go b/internal/orchestrator/health_test.go
index 82022c2c..435ab9ff 100644
--- a/internal/orchestrator/health_test.go
+++ b/internal/orchestrator/health_test.go
@@ -11,9 +11,11 @@ import (
"sync/atomic"
"testing"
"time"
+ "unicode/utf8"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/sanitize"
)
type exitingMockCmd struct {
@@ -642,3 +644,62 @@ func TestMonitor_ChildExitAfterRunningTransitionsToStopped(t *testing.T) {
t.Fatalf("status after child exit = %q, want stopped", status)
}
}
+
+// compactBody cuts arbitrary remote bytes into an operator-facing error message. It used
+// to slice at a raw byte offset, so any non-ASCII body landed mid-rune and the message
+// carried U+FFFD where the server's own words should be. The fixture puts a multi-byte
+// rune ACROSS the old cut offset on purpose: an ASCII body cannot tell the two
+// implementations apart, which is how this survived a census.
+func TestCompactBodyCutsOnARuneBoundary(t *testing.T) {
+ // An all-euro body puts a rune boundary only every three bytes, so a naive cut lands
+ // mid-rune unless the budget happens to be a multiple of three. Both cut points are
+ // checked, because the marked form cuts at budget-minus-marker, not at the budget.
+ const runeWidth = len("€")
+ for _, cut := range []int{maxCompactBodyBytes, maxCompactBodyBytes - len(sanitize.TruncationSuffix)} {
+ if cut%runeWidth == 0 {
+ t.Fatalf("budget %d puts cut %d on a rune boundary, so this fixture cannot tell a naive cut from a rune-safe one; widen the fixture rune",
+ maxCompactBodyBytes, cut)
+ }
+ }
+ body := strings.Repeat("€", maxCompactBodyBytes)
+
+ got := compactBody([]byte(body))
+
+ if !utf8.ValidString(got) {
+ t.Errorf("compactBody returned invalid UTF-8: %q", got)
+ }
+ if strings.ContainsRune(got, utf8.RuneError) {
+ t.Errorf("compactBody returned U+FFFD, so the cut split a rune: %q", got)
+ }
+ if !strings.HasSuffix(got, sanitize.TruncationSuffix) {
+ t.Errorf("compactBody returned %q without the truncation marker; an operator cannot tell a cut body from a short one", got)
+ }
+ // A rune-safe cut honours the budget as a CEILING, so bound both sides: over means
+ // the marker is not counted inside the budget, far under means the cut fires early.
+ if len(got) > maxCompactBodyBytes {
+ t.Errorf("compactBody returned %d bytes, over the %d budget", len(got), maxCompactBodyBytes)
+ }
+ if len(got) < maxCompactBodyBytes-utf8.UTFMax {
+ t.Errorf("compactBody returned only %d bytes for a %d budget; the cut fires too early", len(got), maxCompactBodyBytes)
+ }
+}
+
+func TestCompactBodyLeavesShortAndEmptyBodiesAlone(t *testing.T) {
+ short := "instance refused: profile is locked"
+ if got := compactBody([]byte(short)); got != short {
+ t.Errorf("compactBody(%q) = %q, want it unchanged", short, got)
+ }
+ if got := compactBody([]byte(" " + short + "\n")); got != short {
+ t.Errorf("compactBody did not trim surrounding space: %q", got)
+ }
+ // Exactly at the budget is not truncation, so no marker.
+ atCap := strings.Repeat("c", maxCompactBodyBytes)
+ if got := compactBody([]byte(atCap)); got != atCap {
+ t.Errorf("a body exactly at the budget was altered: %d bytes in, %d out", len(atCap), len(got))
+ }
+ for _, empty := range []string{"", " ", "\n\t "} {
+ if got := compactBody([]byte(empty)); got != "<empty>" {
+ t.Errorf("compactBody(%q) = %q, want <empty>", empty, got)
+ }
+ }
+}
diff --git a/internal/orchestrator/health_transport.go b/internal/orchestrator/health_transport.go
index 64c33df5..9d30754f 100644
--- a/internal/orchestrator/health_transport.go
+++ b/internal/orchestrator/health_transport.go
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/sanitize"
)
const orchestratorActivitySource = "orchestrator"
@@ -101,14 +102,19 @@ func isInstanceHealthyStatus(code int) bool {
return code > 0 && code < http.StatusInternalServerError
}
+// maxCompactBodyBytes bounds the slice of a remote response body that reaches an
+// operator-facing error message.
+const maxCompactBodyBytes = 220
+
+// compactBody is the marked form on purpose: every caller embeds the result in an
+// fmt.Errorf, and an operator reading one needs to tell a short body from a cut one.
+// The body is arbitrary remote bytes, so the cut goes through sanitize rather than a
+// byte slice — a raw trimmed[:220] lands mid-rune on any non-ASCII body and puts U+FFFD
+// in the message where the server's own words should be.
func compactBody(body []byte) string {
trimmed := strings.TrimSpace(string(body))
if trimmed == "" {
return "<empty>"
}
- const max = 220
- if len(trimmed) > max {
- return trimmed[:max]
- }
- return trimmed
+ return sanitize.TruncateUTF8Bytes(trimmed, maxCompactBodyBytes)
}
diff --git a/internal/sanitize/text_test.go b/internal/sanitize/text_test.go
index 2f25c67f..194b1360 100644
--- a/internal/sanitize/text_test.go
+++ b/internal/sanitize/text_test.go
@@ -130,3 +130,43 @@ func TestPrefixUTF8BytesCutsOnRuneBoundaryWithoutMarker(t *testing.T) {
})
}
}
+
+// The helper's own small-budget branch returns TruncationSuffix[:maxBytes] — a raw byte
+// slice of a constant, which is the very rule this package exists to enforce. It is safe
+// only while TruncationSuffix is ASCII: spell it as the single-rune "…" and a budget of
+// 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) {
+ inputs := []string{
+ "",
+ "ascii only",
+ "héllo wörld",
+ "日本語のテキストです",
+ "🙂🙃🙂 emoji run",
+ strings.Repeat("é", 40),
+ }
+
+ var checked int
+ for _, in := range inputs {
+ for maxBytes := -2; maxBytes <= len(in)+4; maxBytes++ {
+ got := TruncateUTF8Bytes(in, maxBytes)
+ checked++
+ if !utf8.ValidString(got) {
+ t.Errorf("TruncateUTF8Bytes(%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))
+ }
+ }
+ }
+ if checked == 0 {
+ t.Fatal("no budgets exercised; this sweep would pass vacuously")
+ }
+
+ // The floor for the branch above: it is only reachable while the suffix is short
+ // enough for a budget to fall inside it.
+ if len(TruncationSuffix) == 0 {
+ t.Error("TruncationSuffix is empty, so the small-budget branch this sweep guards is unreachable")
+ }
+}