High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: strip bidi overrides and zero-width blanks that render text as something else
Luigi Agostiecd2630e727b
diff --git a/internal/sanitize/format_test.go b/internal/sanitize/format_test.gonew file mode 100644index 00000000..daafd574--- /dev/null+++ b/internal/sanitize/format_test.go@@ -0,0 +1,51 @@+package sanitize++import (+ "strings"+ "testing"+)++// StripControlChars relies on unicode.IsControl, which only covers category Cc.+// Bidirectional overrides and zero-width blanks are category Cf, so they passed+// through the sanitizer that exists precisely to stop untrusted text rendering+// as something other than its contents.+func TestCleanErrorStripsTextSpoofingFormatRunes(t *testing.T) {+ tests := []struct {+ name string+ in string+ gone string+ }{+ {"right-to-left override", "Error: \u202Egnp.exe\u202C failed", "\u202E"},+ {"pop directional formatting", "Error: \u202Egnp.exe\u202C failed", "\u202C"},+ {"left-to-right mark", "admin\u200E", "\u200E"},+ {"first strong isolate", "\u2068spoofed\u2069", "\u2068"},+ {"zero width space", "ad\u200Bmin", "\u200B"},+ {"byte order mark", "tok\uFEFFen", "\uFEFF"},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := CleanError(tt.in, 200); strings.Contains(got, tt.gone) {+ t.Errorf("CleanError(%q) = %q, still contains %q", tt.in, got, tt.gone)+ }+ if got := CleanForLog(tt.in, 200); strings.Contains(got, tt.gone) {+ t.Errorf("CleanForLog(%q) = %q, still contains %q", tt.in, got, tt.gone)+ }+ })+ }+}++// Zero-width joiners are required to render emoji sequences and Indic/Arabic+// script correctly, so stripping every format character would corrupt+// legitimate text. They must survive.+func TestCleanErrorKeepsJoinersAndOrdinaryText(t *testing.T) {+ for _, in := range []string{+ "family \U0001F468\u200D\U0001F469\u200D\U0001F467 emoji",+ "\u0915\u094D\u200Cष",+ "plain ascii message",+ "unicode: café — naïve",+ } {+ if got := CleanError(in, 200); got != in {+ t.Errorf("CleanError(%q) = %q, want unchanged", in, got)+ }+ }+}diff --git a/internal/sanitize/text.go b/internal/sanitize/text.goindex 407be39a..ce8ced38 100644--- a/internal/sanitize/text.go+++ b/internal/sanitize/text.go@@ -68,10 +68,46 @@ func RedactAbsolutePaths(s string) string {return s}+// StripTextSpoofingRunes removes the Unicode format characters that let text+// render as something other than its contents: bidirectional overrides and+// isolates (the Trojan Source class, where a right-to-left override makes+// "gnp.exe" display as "exe.png"), and zero-width blanks that hide word+// boundaries.+//+// StripControlChars cannot catch these — unicode.IsControl only covers category+// Cc, and these are Cf. The list is deliberate rather than "every Cf rune":+// zero-width joiner and non-joiner are required to render emoji sequences and+// Indic/Arabic script, so removing them would corrupt legitimate text.+func StripTextSpoofingRunes(s string) string {+ if !strings.ContainsFunc(s, isTextSpoofingRune) {+ return s+ }+ var b strings.Builder+ b.Grow(len(s))+ for _, r := range s {+ if isTextSpoofingRune(r) {+ continue+ }+ b.WriteRune(r)+ }+ return b.String()+}++func isTextSpoofingRune(r rune) bool {+ switch r {+ case '\u200B', '\uFEFF', // zero-width space, byte order mark+ '\u200E', '\u200F', // left-to-right / right-to-left mark+ '\u202A', '\u202B', '\u202C', '\u202D', '\u202E', // embeddings and overrides+ '\u2066', '\u2067', '\u2068', '\u2069': // isolates+ return true+ }+ return false+}+func CleanForLog(s string, maxBytes int) string {- return TruncateUTF8Bytes(StripControlChars(StripANSI(s)), maxBytes)+ return TruncateUTF8Bytes(StripTextSpoofingRunes(StripControlChars(StripANSI(s))), maxBytes)}func CleanError(s string, maxBytes int) string {- return TruncateUTF8Bytes(RedactAbsolutePaths(StripControlChars(StripANSI(s))), maxBytes)+ return TruncateUTF8Bytes(RedactAbsolutePaths(StripTextSpoofingRunes(StripControlChars(StripANSI(s)))), maxBytes)}