fix(server): race WebSocket, SVG refusé, rows.Err, time.NewTicker

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gilles Soulier
2026-05-22 12:18:25 +02:00
co-authored by Claude Sonnet 4.6
parent c0c7152b47
commit feba5d6b93
4 changed files with 45 additions and 16 deletions
+6
View File
@@ -117,6 +117,9 @@ func (d *DB) GetAgents() ([]models.Agent, error) {
} }
agents = append(agents, a) agents = append(agents, a)
} }
if err := rows.Err(); err != nil {
return nil, err
}
return agents, nil return agents, nil
} }
@@ -143,6 +146,9 @@ func (d *DB) GetMetricsHistory(agentID string, from, to int64) ([]map[string]int
"hdd_used": hddUsed, "hdd_total": hddTotal, "hdd_used": hddUsed, "hdd_total": hddTotal,
}) })
} }
if err := rows.Err(); err != nil {
return nil, err
}
return result, nil return result, nil
} }
+6 -8
View File
@@ -5,6 +5,7 @@ import (
"image" "image"
_ "image/jpeg" _ "image/jpeg"
"image/png" "image/png"
"io"
"net/http" "net/http"
"strings" "strings"
@@ -40,18 +41,15 @@ func IconUploadHandler(database *db.DB) http.HandlerFunc {
mime = "image/png" mime = "image/png"
} }
// SVG refusé (risque XSS)
if strings.Contains(mime, "svg") { if strings.Contains(mime, "svg") {
var buf bytes.Buffer http.Error(w, "SVG non supporté — utilisez PNG, JPG ou WEBP", 400)
buf.ReadFrom(file)
if err := database.SaveIcon(agentID, buf.Bytes(), "image/svg+xml"); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusNoContent)
return return
} }
img, _, err := image.Decode(file) // Limite de taille
limited := io.LimitReader(file, 2<<20)
img, _, err := image.Decode(limited)
if err != nil { if err != nil {
http.Error(w, "image invalide", 400) http.Error(w, "image invalide", 400)
return return
+3 -1
View File
@@ -60,7 +60,9 @@ func main() {
} }
go func() { go func() {
for range time.Tick(time.Minute) { ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for range ticker.C {
srvCfg, _ := database.GetServerConfig() srvCfg, _ := database.GetServerConfig()
_ = database.PruneOldMetrics(srvCfg.RetentionDays) _ = database.PruneOldMetrics(srvCfg.RetentionDays)
_ = database.MarkOffline(30) _ = database.MarkOffline(30)
+30 -7
View File
@@ -8,24 +8,35 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
type client struct {
conn *websocket.Conn
send chan []byte
mu sync.Mutex
}
type Hub struct { type Hub struct {
mu sync.RWMutex mu sync.RWMutex
clients map[*websocket.Conn]struct{} clients map[*websocket.Conn]*client
} }
func NewHub() *Hub { func NewHub() *Hub {
return &Hub{clients: make(map[*websocket.Conn]struct{})} return &Hub{clients: make(map[*websocket.Conn]*client)}
} }
func (h *Hub) Register(conn *websocket.Conn) { func (h *Hub) Register(conn *websocket.Conn) {
c := &client{conn: conn, send: make(chan []byte, 64)}
h.mu.Lock() h.mu.Lock()
h.clients[conn] = struct{}{} h.clients[conn] = c
h.mu.Unlock() h.mu.Unlock()
go c.writePump()
} }
func (h *Hub) Unregister(conn *websocket.Conn) { func (h *Hub) Unregister(conn *websocket.Conn) {
h.mu.Lock() h.mu.Lock()
delete(h.clients, conn) if c, ok := h.clients[conn]; ok {
close(c.send)
delete(h.clients, conn)
}
h.mu.Unlock() h.mu.Unlock()
} }
@@ -37,9 +48,11 @@ func (h *Hub) Broadcast(msg interface{}) {
} }
h.mu.RLock() h.mu.RLock()
defer h.mu.RUnlock() defer h.mu.RUnlock()
for conn := range h.clients { for _, c := range h.clients {
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil { select {
log.Printf("[ws] write: %v", err) case c.send <- data:
default:
// canal plein, client lent — on skip
} }
} }
} }
@@ -49,3 +62,13 @@ func (h *Hub) Count() int {
defer h.mu.RUnlock() defer h.mu.RUnlock()
return len(h.clients) return len(h.clients)
} }
func (c *client) writePump() {
defer c.conn.Close()
for data := range c.send {
if err := c.conn.WriteMessage(websocket.TextMessage, data); err != nil {
log.Printf("[ws] write: %v", err)
return
}
}
}