Changeset 415 in code for trunk


Ignore:
Timestamp:
Sep 2, 2020, 3:09:32 PM (5 years ago)
Author:
hubert
Message:

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>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/conn.go

    r402 r415  
    66        "io"
    77        "net"
     8        "strings"
    89        "sync"
    910        "time"
     11        "unicode"
    1012
    1113        "gopkg.in/irc.v3"
     
    6365
    6466func (wic websocketIRCConn) WriteMessage(msg *irc.Message) error {
    65         b := []byte(msg.String())
     67        b := []byte(strings.ToValidUTF8(msg.String(), string(unicode.ReplacementChar)))
    6668        ctx := context.Background()
    6769        if !wic.writeDeadline.IsZero() {
Note: See TracChangeset for help on using the changeset viewer.