package chat import ( "strings" "testing" ) func TestSanitizeHTML(t *testing.T) { tests := []struct { name string in string want string }{ {"plain text escaped", `hello & "friends"`, "hello & "friends""}, {"allowed formatting kept", "bold it u s", "bold it u s"}, {"paragraph and lists", "

a

", "

a

"}, {"code and pre", "
x := 1
", "
x := 1
"}, {"blockquote", "
q
", "
q
"}, {"br void", "a
b
c", "a
b
c"}, {"script stripped", `hi`, "alert(1)hi"}, {"img dropped", `safe`, "safe"}, {"event handlers stripped from allowed tag", `x`, "x"}, {"style attr stripped", `

x

`, "

x

"}, {"https link kept with rel", `l`, `l`}, {"http link kept", `l`, `l`}, {"javascript href removed", `l`, "l"}, {"data href removed", `l`, "l"}, {"single-quoted js href removed", `l`, "l"}, {"unquoted href", `l`, `l`}, {"unclosed tags balanced", "bold both", "bold both"}, {"stray close ignored", "text", "text"}, {"mismatched nesting closed in order", "x", "x"}, {"case insensitive tags", "x", "xy"}, {"nested quotes attack", `">x`, `alert(1)">x`}, {"incomplete tag escaped", "a alert(1)`, ``, ``, ``, `x`, `x`, `x`, `</script>`, `

`, `
`, } for _, v := range vectors { got := strings.ToLower(SanitizeHTML(v)) for _, bad := range []string{"x & `); got != "x & y" { t.Errorf("SanitizePlain = %q", got) } }