From d67787ca0f073a5682bd9296aed6899661c5c8d0 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Thu, 18 Jan 2024 19:46:18 +0100 Subject: [PATCH] Filter invalid UTF8 from message body. Closes #134 --- xmpp.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/xmpp.go b/xmpp.go index 4dd161a..30673c9 100644 --- a/xmpp.go +++ b/xmpp.go @@ -35,6 +35,7 @@ import ( "net/http" "net/url" "os" + "regexp" "slices" "strconv" "strings" @@ -1153,6 +1154,7 @@ func (c *Client) Send(chat Chat) (n int, err error) { stanza := "" + subtext + "%s" + oobtext + thdtext + "\n" + chat.Text = validUTF8(chat.Text) return fmt.Fprintf(c.stanzaWriter, stanza, xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce(), xmlEscape(chat.Text)) } @@ -1534,3 +1536,12 @@ func (t tee) Read(p []byte) (n int, err error) { } return } + +func validUTF8(s string) string { + // Remove invalid code points. + s = strings.ToValidUTF8(s, "�") + reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`) + s = reg.ReplaceAllString(s, "�") + + return s +}