mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-05 18:12:58 +00:00
Bound decompressed WebSocket messages before buffering
Enforce the existing inbound ceiling on decoded WebSocket messages before buffering and JSON handling while preserving normal compression.
This commit is contained in:
@@ -766,6 +766,17 @@ transport failure. Normal closure, navigation/going-away, and abnormal closure
|
||||
codes used by disconnected clients are informational; policy violations and
|
||||
other unexpected codes remain error-classified for operator visibility.
|
||||
|
||||
Browser WebSocket ingress is bounded to 64 KiB both on the wire and after
|
||||
permessage-deflate decompression. The hub reads at most the decoded limit plus
|
||||
one byte before accepting a message or closing with code 1009. Oversized
|
||||
compressed messages must be rejected before JSON decoding and without waiting
|
||||
for a final frame, so an unfinished compressed message cannot retain an
|
||||
unbounded expanded buffer. Messages at the decoded limit remain valid when
|
||||
the wire limit is also met, and normal inbound and outbound compression remain
|
||||
supported. The transport proof
|
||||
is `internal/websocket/hub_inbound_limit_test.go`, covering plain, compressed,
|
||||
fragmented and unfinished messages with at-limit controls.
|
||||
|
||||
Successful quick security setup is durable before its success response is
|
||||
emitted: Pulse writes the canonical auth environment file, activates the same
|
||||
hashed credentials in memory, and for root systemd installs reports both the
|
||||
|
||||
@@ -3136,6 +3136,7 @@
|
||||
"internal/api/security_tokens_test.go",
|
||||
"internal/api/system_settings_telemetry_test.go",
|
||||
"internal/api/websocket_origin_security_test.go",
|
||||
"internal/websocket/hub_inbound_limit_test.go",
|
||||
"internal/websocket/hub_test.go"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -24,7 +25,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// maxWebSocketInboundMessageSize bounds client->server websocket message size.
|
||||
// maxWebSocketInboundMessageSize bounds both encoded and decoded inbound messages.
|
||||
maxWebSocketInboundMessageSize = 64 * 1024
|
||||
// maxWebSocketOrgIDLength keeps org IDs bounded to prevent oversized header/query abuse.
|
||||
maxWebSocketOrgIDLength = 64
|
||||
@@ -1850,6 +1851,29 @@ func isUnexpectedClientClose(err error) bool {
|
||||
)
|
||||
}
|
||||
|
||||
// Gorilla's read limit counts encoded frame bytes. Bound the decompressed
|
||||
// reader as well, before buffering or JSON decoding, including messages whose
|
||||
// sender has not finished the final frame.
|
||||
func (c *Client) readInboundMessage() ([]byte, error) {
|
||||
_, reader, err := c.conn.NextReader()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
message, err := io.ReadAll(io.LimitReader(reader, maxWebSocketInboundMessageSize+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(message) > maxWebSocketInboundMessageSize {
|
||||
if err := c.conn.WriteControl(websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseMessageTooBig, ""),
|
||||
time.Now().Add(time.Second)); err != nil {
|
||||
log.Debug().Err(err).Str("client", c.id).Msg("Failed to send WebSocket size-limit close")
|
||||
}
|
||||
return nil, websocket.ErrReadLimit
|
||||
}
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// readPump handles incoming messages from the client
|
||||
func (c *Client) readPump() {
|
||||
defer func() {
|
||||
@@ -1875,7 +1899,7 @@ func (c *Client) readPump() {
|
||||
log.Info().Str("client", c.id).Msg("readPump started")
|
||||
|
||||
for {
|
||||
_, message, err := c.conn.ReadMessage()
|
||||
message, err := c.readInboundMessage()
|
||||
if err != nil {
|
||||
if isUnexpectedClientClose(err) {
|
||||
log.Error().Err(err).Str("client", c.id).Msg("webSocket read error")
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func TestHandleWebSocket_DecompressedInboundLimit(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
compressed bool
|
||||
bytes int
|
||||
fragmented bool
|
||||
unfinished bool
|
||||
wantTooBig bool
|
||||
}{
|
||||
{name: "plain at limit", bytes: maxWebSocketInboundMessageSize},
|
||||
{name: "compressed at limit", compressed: true, bytes: maxWebSocketInboundMessageSize},
|
||||
{name: "plain above limit", bytes: maxWebSocketInboundMessageSize + 1, wantTooBig: true},
|
||||
{name: "compressed above limit", compressed: true, bytes: maxWebSocketInboundMessageSize + 1, wantTooBig: true},
|
||||
{name: "compressed expansion", compressed: true, bytes: 4 * maxWebSocketInboundMessageSize, wantTooBig: true},
|
||||
{name: "compressed fragmented expansion", compressed: true, bytes: 4 * maxWebSocketInboundMessageSize, fragmented: true, wantTooBig: true},
|
||||
{name: "compressed unfinished expansion", compressed: true, bytes: 4 * maxWebSocketInboundMessageSize, unfinished: true, wantTooBig: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
hub := NewHub(nil)
|
||||
go hub.Run()
|
||||
t.Cleanup(hub.Stop)
|
||||
server := httptest.NewServer(http.HandlerFunc(hub.HandleWebSocket))
|
||||
t.Cleanup(server.Close)
|
||||
dialer := websocket.Dialer{EnableCompression: tc.compressed}
|
||||
conn, response, err := dialer.Dial("ws"+strings.TrimPrefix(server.URL, "http")+"?org_id=default", wsHeadersForHTTP(t, server.URL))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { conn.Close() })
|
||||
if tc.compressed && !strings.Contains(response.Header.Get("Sec-WebSocket-Extensions"), "permessage-deflate") {
|
||||
t.Fatal("compression was not negotiated")
|
||||
}
|
||||
const prefix = `{"type":"ping","data":"`
|
||||
const suffix = `"}`
|
||||
payload := []byte(prefix + strings.Repeat("x", tc.bytes-len(prefix)-len(suffix)) + suffix)
|
||||
if tc.fragmented || tc.unfinished {
|
||||
compressed := deflateInboundFixture(t, payload)
|
||||
var frames []byte
|
||||
if tc.fragmented {
|
||||
middle := len(compressed) / 2
|
||||
frames = appendInboundFixtureFrame(frames, 1, false, true, compressed[:middle])
|
||||
frames = appendInboundFixtureFrame(frames, 0, true, false, compressed[middle:])
|
||||
} else {
|
||||
frames = appendInboundFixtureFrame(frames, 1, false, true, compressed)
|
||||
}
|
||||
_, err = io.Copy(conn.UnderlyingConn(), bytes.NewReader(frames))
|
||||
} else {
|
||||
err = conn.WriteMessage(websocket.TextMessage, payload)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
if err := conn.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, reply, err := conn.ReadMessage()
|
||||
if tc.wantTooBig {
|
||||
if !websocket.IsCloseError(err, websocket.CloseMessageTooBig) {
|
||||
t.Fatalf("oversized message was not closed with 1009: reply=%s err=%v", reply, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("at-limit message was rejected: %v", err)
|
||||
}
|
||||
var message Message
|
||||
if err := json.Unmarshal(reply, &message); err != nil || message.Type != "pong" {
|
||||
t.Fatalf("at-limit ping did not receive pong: reply=%s err=%v", reply, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A sync-flushed raw DEFLATE stream without its final four bytes is the
|
||||
// permessage-deflate payload. The largest fixture expands to only 256 KiB.
|
||||
func deflateInboundFixture(t *testing.T, payload []byte) []byte {
|
||||
t.Helper()
|
||||
var compressed bytes.Buffer
|
||||
writer, err := flate.NewWriter(&compressed, flate.BestCompression)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := writer.Write(payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result := bytes.Clone(compressed.Bytes())
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result) < 4 || !bytes.Equal(result[len(result)-4:], []byte{0, 0, 255, 255}) {
|
||||
t.Fatal("fixture lacks the DEFLATE sync-flush trailer")
|
||||
}
|
||||
result = result[:len(result)-4]
|
||||
if len(result) >= maxWebSocketInboundMessageSize {
|
||||
t.Fatal("fixture must fit below the compressed wire limit")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Build masked client frames explicitly so the test can omit FIN. Rejection
|
||||
// must occur before a sender finishes a message, rather than after ReadAll.
|
||||
func appendInboundFixtureFrame(dst []byte, opcode byte, final, compressed bool, payload []byte) []byte {
|
||||
first := opcode
|
||||
if final {
|
||||
first |= 0x80
|
||||
}
|
||||
if compressed {
|
||||
first |= 0x40
|
||||
}
|
||||
dst = append(dst, first)
|
||||
switch {
|
||||
case len(payload) < 126:
|
||||
dst = append(dst, 0x80|byte(len(payload)))
|
||||
case len(payload) <= 65535:
|
||||
dst = append(dst, 0x80|126)
|
||||
dst = binary.BigEndian.AppendUint16(dst, uint16(len(payload)))
|
||||
default:
|
||||
dst = append(dst, 0x80|127)
|
||||
dst = binary.BigEndian.AppendUint64(dst, uint64(len(payload)))
|
||||
}
|
||||
mask := [4]byte{17, 34, 51, 68}
|
||||
dst = append(dst, mask[:]...)
|
||||
for i, value := range payload {
|
||||
dst = append(dst, value^mask[i%len(mask)])
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -2257,6 +2257,7 @@ class CanonicalCompletionGuardTest(unittest.TestCase):
|
||||
"internal/api/security_tokens_test.go",
|
||||
"internal/api/system_settings_telemetry_test.go",
|
||||
"internal/api/websocket_origin_security_test.go",
|
||||
"internal/websocket/hub_inbound_limit_test.go",
|
||||
"internal/websocket/hub_test.go",
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user