mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
fix: ensure websocket close messages are truncated to 123 bytes (#779)
It's possible for websocket close messages to be too long, which cause them to silently fail without a proper close message. See error below: ``` 2022-03-31 17:08:34.862 [INFO] (stdlib) <close_notjs.go:72> "2022/03/31 17:08:34 websocket: failed to marshal close frame: reason string max is 123 but got \"insert provisioner daemon:Cannot encode []database.ProvisionerType into oid 19098 - []database.ProvisionerType must implement Encoder or be converted to a string\" with length 161" ```
This commit is contained in:
@ -5,8 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
@ -142,3 +144,23 @@ func TestReadUsername(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WebsocketCloseMsg(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("TruncateSingleByteCharacters", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
msg := strings.Repeat("d", 255)
|
||||
trunc := httpapi.WebsocketCloseSprintf(msg)
|
||||
assert.LessOrEqual(t, len(trunc), 123)
|
||||
})
|
||||
|
||||
t.Run("TruncateMultiByteCharacters", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
msg := strings.Repeat("こんにちは", 10)
|
||||
trunc := httpapi.WebsocketCloseSprintf(msg)
|
||||
assert.LessOrEqual(t, len(trunc), 123)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user