Merge pull request #180 from mdosch/fix-134

Filter invalid UTF8 from message body.
This commit is contained in:
Martin 2024-01-18 20:04:09 +01:00 committed by GitHub
commit 62928b3483
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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
}