Skip to content

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

fix: the ghost-chrome accessible name is cut mid-rune (PIN-107)

Luigi Agosti6735ddd86980
diff --git a/internal/browsers/ghostchrome/staticfetch/semantics.go b/internal/browsers/ghostchrome/staticfetch/semantics.go
index 230985fb..ae39e53c 100644
--- a/internal/browsers/ghostchrome/staticfetch/semantics.go
+++ b/internal/browsers/ghostchrome/staticfetch/semantics.go
@@ -3,6 +3,8 @@ package staticfetch
import (
"strings"
+ "github.com/pinchtab/pinchtab/internal/sanitize"
+
"github.com/gost-dom/browser/dom"
"github.com/gost-dom/browser/html"
)
@@ -99,6 +101,13 @@ func getRole(el dom.Element) string {
return "generic"
}
+// accessibleNameMaxBytes is the TOTAL byte budget for an accessible name,
+// marker included — sanitize.TruncateUTF8Bytes spends len(TruncationSuffix) of it
+// rather than adding to it. The name comes from arbitrary page text, so the cut
+// must land on a rune boundary: an invalid-UTF-8 name is the identity an agent
+// matches on to decide what to click, and it ends up in a JSON response.
+const accessibleNameMaxBytes = 100
+
func getAccessibleName(el dom.Element) string {
if label, ok := el.GetAttribute("aria-label"); ok {
return label
@@ -118,11 +127,7 @@ func getAccessibleName(el dom.Element) string {
}
}
if isInteractive(el) {
- text := strings.TrimSpace(el.TextContent())
- if len(text) > 100 {
- text = text[:100] + "..."
- }
- return text
+ return sanitize.TruncateUTF8Bytes(strings.TrimSpace(el.TextContent()), accessibleNameMaxBytes)
}
return ""
}
diff --git a/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go b/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go
new file mode 100644
index 00000000..04c2fc74
--- /dev/null
+++ b/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go
@@ -0,0 +1,60 @@
+package staticfetch
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "github.com/pinchtab/pinchtab/internal/sanitize"
+)
+
+// The accessible name is the identity an agent matches on to decide what to
+// click, so a cut that lands inside a rune does not merely look wrong — it puts
+// invalid UTF-8 into a JSON response and renders as U+FFFD.
+//
+// The label is 99 ASCII bytes followed by a two-byte rune, so the byte cap falls
+// between that rune's two bytes. A hand-rolled text[:100] keeps the lead byte
+// alone; a rune-aware cut cannot.
+func TestAccessibleNameIsTruncatedOnARuneBoundary(t *testing.T) {
+ label := strings.Repeat("A", accessibleNameMaxBytes-1) + "é"
+ html := `<!doctype html><html><body><button>` + label + `</button></body></html>`
+
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ _, _ = w.Write([]byte(html))
+ }))
+ defer ts.Close()
+
+ lite := NewBrowser()
+ defer func() { _ = lite.Close() }()
+ if _, err := lite.Navigate(context.Background(), ts.URL); err != nil {
+ t.Fatalf("Navigate: %v", err)
+ }
+
+ var name string
+ for _, node := range snapshotNodes(t, lite, "all") {
+ if strings.HasPrefix(node.Name, "AAAA") {
+ name = node.Name
+ break
+ }
+ }
+ if name == "" {
+ t.Fatal("no snapshot node carries the long button label — the fixture no longer reaches getAccessibleName")
+ }
+
+ if !utf8.ValidString(name) {
+ t.Errorf("accessible name is not valid UTF-8: %q", name)
+ }
+ if strings.ContainsRune(name, utf8.RuneError) {
+ t.Errorf("accessible name carries the replacement character: %q", name)
+ }
+ if !strings.HasSuffix(name, sanitize.TruncationSuffix) {
+ t.Errorf("accessible name %q does not carry the truncation marker %q", name, sanitize.TruncationSuffix)
+ }
+ if len(name) > accessibleNameMaxBytes {
+ t.Errorf("accessible name is %d bytes, over the %d-byte total budget: %q", len(name), accessibleNameMaxBytes, name)
+ }
+}
diff --git a/internal/cli/actions/actions_network.go b/internal/cli/actions/actions_network.go
index dd5a189f..82b7dce0 100644
--- a/internal/cli/actions/actions_network.go
+++ b/internal/cli/actions/actions_network.go
@@ -10,6 +10,7 @@ import (
"strings"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
+ "github.com/pinchtab/pinchtab/internal/sanitize"
"github.com/spf13/cobra"
)
@@ -78,14 +79,15 @@ func Network(client *http.Client, base, token string, cmd *cobra.Command, args [
return
}
for _, e := range resp.Entries {
- truncURL := e.URL
- if len(truncURL) > 80 {
- truncURL = truncURL[:77] + "..."
- }
- fmt.Printf("%-6s %3d %s\n", e.Method, e.Status, truncURL)
+ fmt.Printf("%-6s %3d %s\n", e.Method, e.Status, sanitize.TruncateUTF8Bytes(e.URL, networkURLDisplayMaxBytes))
}
}
+// networkURLDisplayMaxBytes is the TOTAL byte budget for a URL in the terminal
+// listing, marker included — the same total the hand-rolled cut produced (77
+// bytes plus a 3-byte marker), so the column width is unchanged.
+const networkURLDisplayMaxBytes = 80
+
// NetworkDetail shows full details for a specific request.
func NetworkDetail(client *http.Client, base, token string, cmd *cobra.Command, requestID string) {
params := url.Values{}
diff --git a/internal/cli/actions/actions_network_display_test.go b/internal/cli/actions/actions_network_display_test.go
new file mode 100644
index 00000000..c2201821
--- /dev/null
+++ b/internal/cli/actions/actions_network_display_test.go
@@ -0,0 +1,84 @@
+package actions
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "github.com/pinchtab/pinchtab/internal/sanitize"
+ "github.com/spf13/cobra"
+)
+
+func newNetworkTestCmd() *cobra.Command {
+ cmd := &cobra.Command{}
+ cmd.Flags().Bool("clear", false, "")
+ cmd.Flags().Bool("stream", false, "")
+ cmd.Flags().Bool("json", false, "")
+ for _, name := range []string{"tab", "filter", "method", "status", "type", "limit", "buffer-size"} {
+ cmd.Flags().String(name, "", "")
+ }
+ return cmd
+}
+
+// The URL column is a byte budget, so a percent-decoded non-ASCII URL longer than
+// the cap must still be cut on a rune boundary — a terminal shows U+FFFD for a
+// half rune, and the listing is what an operator reads to pick a requestId.
+func TestNetworkListingTruncatesURLsOnARuneBoundary(t *testing.T) {
+ longURL := "https://example.com/" + strings.Repeat("a", networkURLDisplayMaxBytes) + "é/tail"
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "count": 1,
+ "entries": []map[string]any{
+ {"method": "GET", "status": 200, "url": longURL},
+ },
+ })
+ }))
+ defer srv.Close()
+
+ out := captureStdout(t, func() {
+ Network(http.DefaultClient, srv.URL, "", newNetworkTestCmd(), nil)
+ })
+
+ if !utf8.ValidString(out) {
+ t.Errorf("network listing is not valid UTF-8: %q", out)
+ }
+ if strings.ContainsRune(out, utf8.RuneError) {
+ t.Errorf("network listing carries the replacement character: %q", out)
+ }
+
+ line := strings.TrimSpace(out)
+ shown := line[strings.LastIndex(line, " ")+2:]
+ if !strings.HasSuffix(shown, sanitize.TruncationSuffix) {
+ t.Errorf("displayed URL %q does not carry the truncation marker", shown)
+ }
+ if len(shown) != networkURLDisplayMaxBytes {
+ t.Errorf("displayed URL is %d bytes, want the %d-byte total budget: %q", len(shown), networkURLDisplayMaxBytes, shown)
+ }
+}
+
+// A URL inside the budget must be printed whole, or the conversion would have
+// traded a mid-rune cut for a truncation that fires too early.
+func TestNetworkListingLeavesAShortURLIntact(t *testing.T) {
+ const shortURL = "https://example.com/ok"
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "count": 1,
+ "entries": []map[string]any{{"method": "GET", "status": 200, "url": shortURL}},
+ })
+ }))
+ defer srv.Close()
+
+ out := captureStdout(t, func() {
+ Network(http.DefaultClient, srv.URL, "", newNetworkTestCmd(), nil)
+ })
+ if !strings.Contains(out, shortURL) {
+ t.Errorf("listing %q does not contain the untruncated URL %q", out, shortURL)
+ }
+}