Skip to content

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

fix: the widened data-URI pattern matched 'data:' inside a word, so 'Metadata:' became 'Meta' (PIN-061)

Luigi Agostiea4edf159e50
diff --git a/internal/htmltrim/trim.go b/internal/htmltrim/trim.go
index bb6bcaa2..1987b836 100644
--- a/internal/htmltrim/trim.go
+++ b/internal/htmltrim/trim.go
@@ -21,7 +21,7 @@ func TrimHTML(html string) string {
html = reStyle.ReplaceAllString(html, "")
html = reComment.ReplaceAllString(html, "")
html = stripSVG(html)
- html = reDataURI.ReplaceAllString(html, "")
+ html = reDataURI.ReplaceAllString(html, "$1")
html = reWhitespace.ReplaceAllString(html, " ")
html = reNewlines.ReplaceAllString(html, "\n")
@@ -50,7 +50,12 @@ var (
// attribute, the quote or paren of a CSS url(), and whitespace for a srcset
// candidate. A comma is NOT a terminator — it separates a data URI's own metadata
// from its payload, so excluding it would leave every base64 blob behind.
- reDataURI = regexp.MustCompile(`(?i)data:[^"'()<>\s]*`)
+ //
+ // A URI only begins where one of those delimiters (or the start) precedes it,
+ // captured in group 1 and re-emitted on replace. Without that, "data:" matched
+ // inside a word — "Metadata:" became "Meta" — stripping the page text this helper
+ // exists to keep. A "data-" attribute is safe either way: it has a hyphen, not a colon.
+ reDataURI = regexp.MustCompile(`(?i)(\A|["'(,=\s])data:[^"'()<>\s]*`)
reWhitespace = regexp.MustCompile(`[ \t]+`)
reNewlines = regexp.MustCompile(`\n{3,}`)
)
diff --git a/internal/htmltrim/trim_test.go b/internal/htmltrim/trim_test.go
index 6adfc910..a2c1d571 100644
--- a/internal/htmltrim/trim_test.go
+++ b/internal/htmltrim/trim_test.go
@@ -154,6 +154,25 @@ func TestTrimHTMLStillStripsWholeValueDataURIs(t *testing.T) {
}
}
+// The data-URI pattern strips a URI, not the letters "data:" wherever they fall. Widening
+// it to match anywhere inside a value also let it match inside a word — "Metadata:" in page
+// text became "Meta", eating the interactive markup this helper exists to carry. A real URI
+// is preceded by a quote, paren, comma, '=' or whitespace, so those are what license the
+// strip. Reverting the delimiter guard corrupts every one of these.
+func TestTrimHTMLLeavesDataColonInsideAWordAlone(t *testing.T) {
+ for _, tc := range []struct{ name, html, want string }{
+ {"prose label", `<p>Metadata: 2024</p>`, `<p>Metadata: 2024</p>`},
+ {"word with a mime tail", `<td>metadata:image/png here</td>`, `<td>metadata:image/png here</td>`},
+ {"data- attribute is unaffected", `<div data-src="keep">x</div>`, `<div data-src="keep">x</div>`},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := TrimHTML(tc.html); got != tc.want {
+ t.Errorf("TrimHTML(%q) = %q, want it unchanged — 'data:' inside a word is not a URI", tc.html, got)
+ }
+ })
+ }
+}
+
// SVG is foreign content and nests legally, so a nested <svg> reaches this helper through
// DOM serialisation. The non-greedy regex stopped at the FIRST </svg>, leaving the outer
// element's path data and a stray closing tag in the prompt.