Harden the chat hub, evict idle rate-limit keys, isolate Dozzle
Test and deploy / test (push) Successful in 16s
Test and deploy / deploy (push) Successful in 30s

- Chat: cap frame, message and file URL sizes, put a write deadline on
  every send so a stalled client can't hold the hub lock, and check the
  save, prune and history errors instead of broadcasting an unsaved
  message. ChatPanel's maxlength stays under the server cap.
- RateLimiter: sweep keys idle past the window, at most once a window.
- Dozzle: move to an internal dozzle-net shared only with Caddy, so the
  admin gate is the only way to reach the Docker socket.
- Drop the GraphQL WebSocket transport: the route is POST-only and the
  schema has no subscriptions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-26 15:15:31 +00:00
co-authored by Claude Opus 5.5
parent 223c8ae35a
commit 18265871e1
7 changed files with 78 additions and 15 deletions
+39 -7
View File
@@ -1,6 +1,7 @@
package services
import (
"log"
"net/http"
"net/url"
"sync"
@@ -54,6 +55,24 @@ const (
rateLimitMaxMsgs = 10
)
// The route is public, so frame and field sizes are capped. Writes happen
// under mu, so a stalled client may hold it for at most writeWait.
// ChatPanel's maxlength of 1000 UTF-16 units is at most 3000 bytes, under
// maxContentBytes; over-limit messages are dropped silently.
const (
maxFrameBytes = 16 << 10
maxContentBytes = 4000
maxFileURLBytes = 512
writeWait = 5 * time.Second
)
func writeJSON(conn *websocket.Conn, v any) error {
if err := conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil {
return err
}
return conn.WriteJSON(v)
}
type wsIncoming struct {
Action string `json:"action,omitempty"`
ID uint `json:"id,omitempty"`
@@ -81,6 +100,7 @@ func InitWebSocket(database *gorm.DB, domain string) {
// isAdmin allows seeing, sending and deleting private messages.
func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
defer conn.Close()
conn.SetReadLimit(maxFrameBytes)
// Register and snapshot under one lock, so a concurrent broadcast is
// either in the history or delivered after it, never lost or reordered.
@@ -94,9 +114,11 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
if !isAdmin {
historyQuery = historyQuery.Where("private = ?", false)
}
historyQuery.Find(&history)
if err := historyQuery.Find(&history).Error; err != nil {
log.Printf("[ws] loading history: %v", err)
}
if err := conn.WriteJSON(wsHistoryEvent{Action: "history", Messages: history}); err != nil {
if err := writeJSON(conn, wsHistoryEvent{Action: "history", Messages: history}); err != nil {
delete(clients, conn)
mu.Unlock()
return
@@ -130,6 +152,10 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
continue
}
if len(incoming.Text) > maxContentBytes || len(incoming.FileURL) > maxFileURLBytes {
continue
}
msg := models.Message{
Content: incoming.Text,
FileURL: incoming.FileURL,
@@ -138,17 +164,23 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
}
mu.Lock()
wsDB.Create(&msg)
if err := wsDB.Create(&msg).Error; err != nil {
mu.Unlock()
log.Printf("[ws] saving message: %v", err)
continue
}
// Soft delete, so the table still grows; the chat does not.
wsDB.Where("id NOT IN (?)",
if err := wsDB.Where("id NOT IN (?)",
wsDB.Model(&models.Message{}).Select("id").Order("created_at DESC").Limit(maxMessages),
).Delete(&models.Message{})
).Delete(&models.Message{}).Error; err != nil {
log.Printf("[ws] pruning messages: %v", err)
}
for client, clientAdmin := range clients {
if msg.Private && !clientAdmin {
continue
}
if err := client.WriteJSON(msg); err != nil {
if err := writeJSON(client, msg); err != nil {
client.Close()
delete(clients, client)
}
@@ -173,7 +205,7 @@ func deleteMessage(id uint) {
event := wsDeleteEvent{Action: "delete", ID: id}
for client := range clients {
if err := client.WriteJSON(event); err != nil {
if err := writeJSON(client, event); err != nil {
client.Close()
delete(clients, client)
}