Make sure that WebSocket messages are valid UTF-8

... by replacing invalid bytes with the REPLACEMENT CHARACTER U+FFFD

This is better than:
- discarding the whole message, since the user would not see it...
- removing invalid bytes, since the user would not see their presence,
- converting the encoding (this is actually not possible).

Contrary to its documentation, strings.ToValidUTF8 doesn't copy the
string if it's valid UTF-8:
<https://golang.org/src/strings/strings.go?s=15815:15861#L623>
This commit is contained in:
Hubert Hirtz 2020-09-02 17:06:17 +02:00 committed by Simon Ser
parent 046175f564
commit 42828d68e9
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -5,8 +5,10 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"strings"
"sync" "sync"
"time" "time"
"unicode"
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
"nhooyr.io/websocket" "nhooyr.io/websocket"
@ -62,7 +64,7 @@ func (wic websocketIRCConn) ReadMessage() (*irc.Message, error) {
} }
func (wic websocketIRCConn) WriteMessage(msg *irc.Message) error { func (wic websocketIRCConn) WriteMessage(msg *irc.Message) error {
b := []byte(msg.String()) b := []byte(strings.ToValidUTF8(msg.String(), string(unicode.ReplacementChar)))
ctx := context.Background() ctx := context.Background()
if !wic.writeDeadline.IsZero() { if !wic.writeDeadline.IsZero() {
var cancel context.CancelFunc var cancel context.CancelFunc