Harden StripTags against stray angle brackets
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

This commit is contained in:
Snider 2026-04-15 02:14:45 +01:00
parent b1ff334c85
commit 4bbdb10080
2 changed files with 18 additions and 1 deletions

View file

@ -32,7 +32,7 @@ func StripTags(html string) string {
switch r {
case '<':
if i+1 < len(runes) && isTagStartRune(runes[i+1]) {
if i+1 < len(runes) && isTagStartRune(runes[i+1]) && hasTagCloser(runes, i+2) {
inTag = true
continue
}
@ -67,6 +67,15 @@ func isTagStartRune(r rune) bool {
}
}
func hasTagCloser(runes []rune, start int) bool {
for i := start; i < len(runes); i++ {
if runes[i] == '>' {
return true
}
}
return false
}
// Imprint renders a node tree to HTML, strips tags, tokenises the text,
// and returns a GrammarImprint — the full render-reverse pipeline.
// Usage example: imp := Imprint(Text("welcome"), NewContext())

View file

@ -54,6 +54,14 @@ func TestStripTags_PreservesComparisonOperators_Good(t *testing.T) {
}
}
func TestStripTags_LiteralAngleBracket_Good(t *testing.T) {
got := StripTags(`a<b`)
want := `a<b`
if got != want {
t.Errorf("StripTags(literal angle) = %q, want %q", got, want)
}
}
func TestStripTags_Entities_Good(t *testing.T) {
got := StripTags(`&lt;script&gt;`)
want := "&lt;script&gt;"