42828d68e9c8d2ed88688898d62f6727f787ac0a — Hubert Hirtz 2 months ago 046175f
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:
1 files changed, 3 insertions(+), 1 deletions(-)

M conn.go
M conn.go => conn.go +3 -1
@@ 5,8 5,10 @@ import (


@@ 62,7 64,7 @@ func (wic websocketIRCConn) ReadMessage() (*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()
	if !wic.writeDeadline.IsZero() {
		var cancel context.CancelFunc