Filter invalid UTF8 from message body.

Closes #134
This commit is contained in:
Martin Dosch 2024-01-18 19:46:18 +01:00
parent f8a24505f4
commit d67787ca0f

11
xmpp.go
View File

@ -35,6 +35,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"regexp"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
@ -1153,6 +1154,7 @@ func (c *Client) Send(chat Chat) (n int, err error) {
stanza := "<message to='%s' type='%s' id='%s' xml:lang='en'>" + subtext + "<body>%s</body>" + oobtext + thdtext + "</message>\n" stanza := "<message to='%s' type='%s' id='%s' xml:lang='en'>" + subtext + "<body>%s</body>" + oobtext + thdtext + "</message>\n"
chat.Text = validUTF8(chat.Text)
return fmt.Fprintf(c.stanzaWriter, stanza, return fmt.Fprintf(c.stanzaWriter, stanza,
xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce(), xmlEscape(chat.Text)) 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 return
} }
func validUTF8(s string) string {
// Remove invalid code points.
s = strings.ToValidUTF8(s, "<22>")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
s = reg.ReplaceAllString(s, "<22>")
return s
}