(cherry picked from commit 17939826efa20a97f087b3d390ec5136df350bae) Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
26f09017e1
Коммит
384635216f
@@ -469,6 +469,12 @@ func (wc *WebConn) readPump() {
|
||||
return
|
||||
}
|
||||
|
||||
// Reject binary frames from unauthenticated connections. See MM-68222.
|
||||
if msgType != websocket.TextMessage && !wc.IsAuthenticated() {
|
||||
wc.logSocketErr("websocket.UnauthBinary", errors.New("binary frames require authentication"))
|
||||
return
|
||||
}
|
||||
|
||||
var decoder interface {
|
||||
Decode(v any) error
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -241,3 +242,48 @@ func TestWebConnDrainDeadQueue(t *testing.T) {
|
||||
t.Run("Overwritten First", func(t *testing.T) { run(int64(128), deadQueueSize+10) })
|
||||
})
|
||||
}
|
||||
|
||||
func TestWebConnRejectBinaryFrameUnauthenticated(t *testing.T) {
|
||||
th := Setup(t)
|
||||
|
||||
readPumpDone := make(chan struct{})
|
||||
upgradeErrCh := make(chan error, 1)
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
upgrader := &websocket.Upgrader{}
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
upgradeErrCh <- err
|
||||
return
|
||||
}
|
||||
upgradeErrCh <- nil
|
||||
|
||||
wc := th.Service.NewWebConn(&WebConnConfig{
|
||||
WebSocket: conn,
|
||||
}, th.Suite, &hookRunner{})
|
||||
|
||||
require.False(t, wc.IsAuthenticated())
|
||||
|
||||
go func() {
|
||||
wc.readPump()
|
||||
close(readPumpDone)
|
||||
}()
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
d := websocket.Dialer{}
|
||||
clientConn, _, err := d.Dial("ws://"+s.Listener.Addr().String()+"/ws", nil)
|
||||
require.NoError(t, err)
|
||||
defer clientConn.Close()
|
||||
|
||||
require.NoError(t, <-upgradeErrCh)
|
||||
|
||||
err = clientConn.WriteMessage(websocket.BinaryMessage, []byte{0x01, 0x02, 0x03})
|
||||
require.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-readPumpDone:
|
||||
case <-time.After(5 * time.Second):
|
||||
require.Fail(t, "readPump did not exit after receiving binary frame")
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user