3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-11 06:29:29 +01:00

Merge pull request #1484 from slingamn/utf8only

initial work on #1483
This commit is contained in:
Shivaram Lingamneni 2021-01-15 06:21:53 -05:00 committed by GitHub
commit 7b300a802f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 12 deletions

View File

@ -686,9 +686,12 @@ func (client *Client) run(session *Session) {
if err == errInvalidUtf8 {
invalidUtf8 = true // handle as normal, including labeling
} else if err != nil {
quitMessage := "connection closed"
if err == errReadQ {
quitMessage = "readQ exceeded"
var quitMessage string
switch err {
case errReadQ, errWSBinaryMessage:
quitMessage = err.Error()
default:
quitMessage = "connection closed"
}
client.Quit(quitMessage, session)
// since the client did not actually send us a QUIT,

View File

@ -837,6 +837,9 @@ func (conf *Config) prepareListeners() (err error) {
}
lconf.RequireProxy = block.TLS.Proxy || block.Proxy
lconf.WebSocket = block.WebSocket
if lconf.WebSocket && !conf.Server.EnforceUtf8 {
return fmt.Errorf("enabling a websocket listener requires the use of server.enforce-utf8")
}
lconf.HideSTS = block.HideSTS
conf.Server.trueListeners[addr] = lconf
}
@ -1446,6 +1449,9 @@ func (config *Config) generateISupport() (err error) {
if config.Server.Casemapping == CasemappingPRECIS {
isupport.Add("UTF8MAPPING", precisUTF8MappingToken)
}
if config.Server.EnforceUtf8 {
isupport.Add("UTF8ONLY", "")
}
isupport.Add("WHOX", "")
err = isupport.RegenerateCachedReply()

View File

@ -19,8 +19,9 @@ const (
)
var (
crlf = []byte{'\r', '\n'}
errReadQ = errors.New("ReadQ Exceeded")
crlf = []byte{'\r', '\n'}
errReadQ = errors.New("ReadQ Exceeded")
errWSBinaryMessage = errors.New("WebSocket binary messages are unsupported")
)
// IRCConn abstracts away the distinction between a regular
@ -148,11 +149,7 @@ func (wc IRCWSConn) UnderlyingConn() *utils.WrappedConn {
func (wc IRCWSConn) WriteLine(buf []byte) (err error) {
buf = bytes.TrimSuffix(buf, crlf)
if !globalUtf8EnforcementSetting && !utf8.Valid(buf) {
// there's not much we can do about this;
// silently drop the message
return nil
}
// #1483: if we have websockets at all, then we're enforcing utf8
return wc.conn.WriteMessage(websocket.TextMessage, buf)
}
@ -172,8 +169,7 @@ func (wc IRCWSConn) ReadLine() (line []byte, err error) {
if messageType == websocket.TextMessage {
return line, nil
} else {
// for purposes of fakelag, treat non-text message as an empty line
return nil, nil
return nil, errWSBinaryMessage
}
} else if err == websocket.ErrReadLimit {
return line, errReadQ

View File

@ -166,6 +166,7 @@ func (wl *WSListener) handle(w http.ResponseWriter, r *http.Request) {
}
return false
},
Subprotocols: []string{"text.ircv3.net"},
}
conn, err := wsUpgrader.Upgrade(w, r, nil)