mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-09-16 06:02:37 +00:00
Closes #1794. Follow-up to #1793, decided **before** the upgrade because the handshake is the resource being protected. - Deny list of addresses and CIDRs → 403 - Per-IP concurrent connection cap → 403 - Per-IP upgrade rate limit over a rolling minute → **429**, not 403: a temporary refusal should not read as "never come back" - Rejection counters split by cause in `/api/stats` under `websocket` ### The decision this feature lives or dies on Most CoreScope installs sit behind nginx, Caddy, Traefik or an ingress. `cdn_detection.go` says so in as many words: it deliberately excludes `X-Forwarded-For` from its CDN signals precisely because *every* reverse-proxied install sets it. For those deployments `r.RemoteAddr` is the proxy, `127.0.0.1` for every visitor on earth. A per-IP cap keyed on that address protects nobody and hands the sixth legitimate browser tab a 403. That is a self-inflicted outage wearing the costume of hardening. So: - **`X-Forwarded-For` is believed only from an address listed in `webSocket.trustedProxies`.** From anywhere else it is attacker-supplied, and trusting it would let anyone mint a fresh source IP per connection, which is strictly worse than having no limit at all. - **When the peer looks like a local reverse proxy and no `trustedProxies` is set, the per-IP limits are skipped**, and one warning names the setting that fixes it. Silently refusing real users is the worse failure. - **The deny list still applies there**, because it is the operator's explicit instruction rather than an inference. That is the answer to @mcode6726's question on the thread: it is neither "always the socket address" nor "always the header", and the operator decides which by naming their proxy. ### Two deliberate departures from the issue body **`maxConnsPerIP` ships as 0 (off), not 5.** Carrier-grade NAT puts thousands of unrelated mobile subscribers behind a single public IPv4. A cap of 5 refuses real visitors on phones while a scraper simply rents more addresses: all of the cost, none of the benefit. `upgradesPerMinPerIP` ships at **30 and on**, because that one *is* safe under CGNAT: a real client upgrades a handful of times per minute even while reconnecting, so 30 leaves ordinary traffic untouched while flattening a reconnect loop. A pointer type distinguishes "unset" from an explicit `0` that turns it off. **The default deny list is not shipped.** The thread proposed seeding 44 CIDRs for one VPS provider after a single scraper was seen at `23.111.177.6`. I have left it out: blanket-blocking a hosting provider by default breaks legitimate operators who host there, is undiscoverable by the person locked out (they see a bare 403), and ages badly as ranges get reassigned. The mechanism is here and `config.example.json` shows exactly how to configure it, so any operator who wants that list can have it in one line. If you want it shipped as a default anyway, that is your call as maintainer and it is a one-line change. ### Verification 19 tests, including all five the issue specifies as TDD requirements, each marked with the issue's own wording. Beyond those five: - a **bare address** in the deny list works, not just CIDR form. Operators write `1.2.3.4`, and silently ignoring that would be the worst possible failure for a deny list: it looks configured and blocks nothing - an unparseable deny entry is skipped and logged, not fatal. One typo must not take the server down - one client behind a trusted proxy does **not** exhaust another client's budget behind the same proxy, which is the entire point of honouring XFF - changing a forged XFF from an untrusted peer buys no fresh budget - `release` frees a slot and is **idempotent**, because `Unregister` can run twice for one client and double-crediting would leak slots - a **rejected** upgrade does not consume rate budget, or a retrying client could never recover once its window cleared - limits skipped for loopback and private peers; deny list applies anyway - a nil limiter allows everything, so a `Hub` built without `ConfigureLimits` behaves exactly as before - idle per-IP state is collected, while a record with a live connection never is Full `cmd/server` suite green, `gofmt` clean. ### Not done - No runtime config reload; restart required. Listed as optional in the issue. - No `WS_DENY_IPS` env override. Also listed as optional. - From the OWASP expansion in the first comment: `maxPayload` and the idle/read timeout are **already in master** (`SetReadLimit`, `SetReadDeadline`). The ping/pong heartbeat is not, and is not in this PR either; it is a separate change to the read/write pumps and belongs in its own review. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
381 lines
10 KiB
Go
381 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Hub manages WebSocket clients and broadcasts.
|
|
type Hub struct {
|
|
mu sync.RWMutex
|
|
clients map[*Client]bool
|
|
upgrader websocket.Upgrader
|
|
allowedOrigins []string // exact-match allowlist for /ws CheckOrigin (see SetAllowedOrigins)
|
|
limits *wsLimiter // #1794: per-IP caps and deny list; nil allows everything
|
|
}
|
|
|
|
// SetAllowedOrigins configures the exact-match origin allowlist consulted by
|
|
// the WebSocket upgrader's CheckOrigin. The "*" wildcard is deliberately NOT
|
|
// honored here (it IS honored by the HTTP CORS middleware): OWASP's
|
|
// WebSocket Security Cheat Sheet recommends an explicit allowlist for CSWSH
|
|
// defense. If "*" appears in the slice, it is ignored and a startup WARN is
|
|
// logged once per call.
|
|
//
|
|
// See: https://cheatsheetseries.owasp.org/cheatsheets/WebSocket_Security_Cheat_Sheet.html
|
|
func (h *Hub) SetAllowedOrigins(origins []string) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
h.allowedOrigins = append(h.allowedOrigins[:0], origins...)
|
|
for _, o := range origins {
|
|
if o == "*" {
|
|
log.Println(`[ws] WARNING: CORSAllowedOrigins contains "*" — CORS allows any origin for XHR, but /ws upgrade enforces explicit allowlist only (OWASP CSWSH guidance). Add specific origins to allow cross-origin WebSocket clients.`)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// checkOrigin is the gorilla/websocket Upgrader.CheckOrigin hook. Rules:
|
|
// - empty Origin header → allow (non-browser client; rate-limit / IP gate
|
|
// is handled separately, see #1794).
|
|
// - Origin host == request Host (same-origin) → allow.
|
|
// - Origin in allowedOrigins by exact case-insensitive match → allow.
|
|
// - "*" in allowedOrigins is ignored (see SetAllowedOrigins).
|
|
// - anything else → reject (gorilla returns 403).
|
|
func (h *Hub) checkOrigin(r *http.Request) bool {
|
|
origin := r.Header.Get("Origin")
|
|
if origin == "" {
|
|
return true
|
|
}
|
|
u, err := url.Parse(origin)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if strings.EqualFold(u.Host, r.Host) {
|
|
return true
|
|
}
|
|
h.mu.RLock()
|
|
allowed := h.allowedOrigins
|
|
h.mu.RUnlock()
|
|
for _, o := range allowed {
|
|
if o == "*" {
|
|
continue // deliberately not honored — see SetAllowedOrigins
|
|
}
|
|
if strings.EqualFold(o, origin) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Client is a single WebSocket connection.
|
|
type Client struct {
|
|
conn *websocket.Conn
|
|
send chan []byte
|
|
closeOnce sync.Once
|
|
// release returns this connection's slot to the per-IP concurrent cap
|
|
// (#1794). Always non-nil so Unregister needs no nil check; it is a no-op
|
|
// when limits are disabled.
|
|
release func()
|
|
}
|
|
|
|
// ConfigureLimits installs the #1794 transport limits. Called once at
|
|
// startup, before the listener binds, so no upgrade can race a half-built
|
|
// limiter. Passing zero values for both caps leaves only the deny list
|
|
// active, which is a legitimate configuration.
|
|
func (h *Hub) ConfigureLimits(maxConnsPerIP, upgradesPerMin int, trustedProxies, deny []string) {
|
|
l := newWSLimiter()
|
|
l.maxConnsPerIP = maxConnsPerIP
|
|
l.upgradesPerMin = upgradesPerMin
|
|
l.trustedProxies = parseCIDRList(trustedProxies, "webSocket.trustedProxies")
|
|
l.denyNets = parseCIDRList(deny, "webSocket.deny")
|
|
h.mu.Lock()
|
|
h.limits = l
|
|
h.mu.Unlock()
|
|
if len(l.denyNets) > 0 {
|
|
log.Printf("[ws] deny list active: %d network(s)", len(l.denyNets))
|
|
}
|
|
if len(l.trustedProxies) == 0 && (maxConnsPerIP > 0 || upgradesPerMin > 0) {
|
|
log.Printf("[ws] per-IP limits configured with no trustedProxies: they apply only to " +
|
|
"directly-connected clients. Behind a reverse proxy, set webSocket.trustedProxies.")
|
|
}
|
|
}
|
|
|
|
func NewHub() *Hub {
|
|
h := &Hub{
|
|
clients: make(map[*Client]bool),
|
|
}
|
|
h.upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 4096,
|
|
CheckOrigin: h.checkOrigin,
|
|
}
|
|
return h
|
|
}
|
|
|
|
func (h *Hub) ClientCount() int {
|
|
h.mu.RLock()
|
|
defer h.mu.RUnlock()
|
|
return len(h.clients)
|
|
}
|
|
|
|
func (h *Hub) Register(c *Client) {
|
|
h.mu.Lock()
|
|
h.clients[c] = true
|
|
h.mu.Unlock()
|
|
log.Printf("[ws] client connected (%d total)", h.ClientCount())
|
|
}
|
|
|
|
func (h *Hub) Unregister(c *Client) {
|
|
h.mu.Lock()
|
|
if _, ok := h.clients[c]; ok {
|
|
delete(h.clients, c)
|
|
c.closeOnce.Do(func() { close(c.send) })
|
|
}
|
|
h.mu.Unlock()
|
|
// #1794: outside the lock — release takes the limiter's own mutex, and
|
|
// holding two locks in one order here and the other order elsewhere is how
|
|
// deadlocks are made. Idempotent, so a double Unregister cannot over-credit.
|
|
if c.release != nil {
|
|
c.release()
|
|
}
|
|
log.Printf("[ws] client disconnected (%d total)", h.ClientCount())
|
|
}
|
|
|
|
// Close gracefully disconnects all WebSocket clients.
|
|
func (h *Hub) Close() {
|
|
h.mu.Lock()
|
|
for c := range h.clients {
|
|
c.conn.WriteControl(
|
|
websocket.CloseMessage,
|
|
websocket.FormatCloseMessage(websocket.CloseGoingAway, "server shutting down"),
|
|
time.Now().Add(3*time.Second),
|
|
)
|
|
c.closeOnce.Do(func() { close(c.send) })
|
|
delete(h.clients, c)
|
|
}
|
|
h.mu.Unlock()
|
|
log.Println("[ws] all clients disconnected")
|
|
}
|
|
|
|
// Broadcast sends a message to all connected clients.
|
|
func (h *Hub) Broadcast(msg interface{}) {
|
|
data, err := json.Marshal(msg)
|
|
if err != nil {
|
|
log.Printf("[ws] marshal error: %v", err)
|
|
return
|
|
}
|
|
h.mu.RLock()
|
|
defer h.mu.RUnlock()
|
|
for c := range h.clients {
|
|
select {
|
|
case c.send <- data:
|
|
default:
|
|
// Client buffer full — drop
|
|
}
|
|
}
|
|
}
|
|
|
|
// ServeWS handles the WebSocket upgrade and runs the client.
|
|
func (h *Hub) ServeWS(w http.ResponseWriter, r *http.Request) {
|
|
// #1794: decide BEFORE the upgrade. Rejecting afterwards would already
|
|
// have allocated the connection and completed the handshake, which is the
|
|
// resource this is meant to protect.
|
|
ok, reason, key, release := h.limits.allow(r)
|
|
if !ok {
|
|
status := http.StatusForbidden
|
|
if reason == wsRejectRate {
|
|
// 429 tells a well-behaved client to back off; 403 would read as
|
|
// "never come back" for what is a temporary refusal.
|
|
status = http.StatusTooManyRequests
|
|
}
|
|
log.Printf("[ws] reject ip=%s reason=%s", key, reason)
|
|
http.Error(w, "websocket upgrade refused", status)
|
|
return
|
|
}
|
|
|
|
conn, err := h.upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Printf("[ws] upgrade error: %v", err)
|
|
release() // the slot was charged above and this connection never happened
|
|
return
|
|
}
|
|
|
|
client := &Client{
|
|
conn: conn,
|
|
send: make(chan []byte, 256),
|
|
release: release,
|
|
}
|
|
h.Register(client)
|
|
|
|
go client.writePump()
|
|
go client.readPump(h)
|
|
}
|
|
|
|
// wsOrStatic upgrades WebSocket requests at any path, serves static files otherwise.
|
|
func wsOrStatic(hub *Hub, static http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
|
|
hub.ServeWS(w, r)
|
|
return
|
|
}
|
|
static.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func (c *Client) readPump(hub *Hub) {
|
|
defer func() {
|
|
hub.Unregister(c)
|
|
c.conn.Close()
|
|
}()
|
|
c.conn.SetReadLimit(512)
|
|
c.conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
|
c.conn.SetPongHandler(func(string) error {
|
|
c.conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
|
return nil
|
|
})
|
|
for {
|
|
_, _, err := c.conn.ReadMessage()
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) writePump() {
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
defer func() {
|
|
ticker.Stop()
|
|
c.conn.Close()
|
|
}()
|
|
for {
|
|
select {
|
|
case message, ok := <-c.send:
|
|
c.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
|
if !ok {
|
|
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
|
|
return
|
|
}
|
|
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
|
return
|
|
}
|
|
case <-ticker.C:
|
|
c.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
|
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Poller watches for new transmissions in SQLite and broadcasts them.
|
|
type Poller struct {
|
|
db *DB
|
|
hub *Hub
|
|
store *PacketStore // optional: if set, new transmissions are ingested into memory
|
|
interval time.Duration
|
|
stop chan struct{}
|
|
}
|
|
|
|
func NewPoller(db *DB, hub *Hub, interval time.Duration) *Poller {
|
|
return &Poller{db: db, hub: hub, interval: interval, stop: make(chan struct{})}
|
|
}
|
|
|
|
func (p *Poller) Start() {
|
|
lastID := p.db.GetMaxTransmissionID()
|
|
lastObsID := p.db.GetMaxObservationID()
|
|
// If the store already loaded data, use its max IDs as a floor.
|
|
// This prevents replaying the entire DB when the DB query fails
|
|
// (e.g., corrupted DB returns 0 from COALESCE).
|
|
if p.store != nil {
|
|
if storeMax := p.store.MaxTransmissionID(); storeMax > lastID {
|
|
lastID = storeMax
|
|
}
|
|
if storeMaxObs := p.store.MaxObservationID(); storeMaxObs > lastObsID {
|
|
lastObsID = storeMaxObs
|
|
}
|
|
}
|
|
log.Printf("[poller] starting from transmission ID %d, obs ID %d, interval %v", lastID, lastObsID, p.interval)
|
|
|
|
ticker := time.NewTicker(p.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
if p.store != nil {
|
|
// Ingest new transmissions into in-memory store and broadcast
|
|
newTxs, newMax := p.store.IngestNewFromDB(lastID, 100)
|
|
if newMax > lastID {
|
|
lastID = newMax
|
|
}
|
|
// Ingest new observations for existing transmissions (fixes #174)
|
|
nextObsID := lastObsID
|
|
if err := p.db.conn.QueryRow(`
|
|
SELECT COALESCE(MAX(id), ?) FROM (
|
|
SELECT id FROM observations
|
|
WHERE id > ?
|
|
ORDER BY id ASC
|
|
LIMIT 500
|
|
)`, lastObsID, lastObsID).Scan(&nextObsID); err != nil {
|
|
nextObsID = lastObsID
|
|
}
|
|
newObs := p.store.IngestNewObservations(lastObsID, 500)
|
|
if nextObsID > lastObsID {
|
|
lastObsID = nextObsID
|
|
}
|
|
if len(newTxs) > 0 {
|
|
log.Printf("[broadcast] sending %d packets to %d clients (lastID now %d)", len(newTxs), p.hub.ClientCount(), lastID)
|
|
}
|
|
for _, tx := range newTxs {
|
|
p.hub.Broadcast(WSMessage{
|
|
Type: "packet",
|
|
Data: tx,
|
|
})
|
|
}
|
|
for _, obs := range newObs {
|
|
p.hub.Broadcast(WSMessage{
|
|
Type: "packet",
|
|
Data: obs,
|
|
})
|
|
}
|
|
} else {
|
|
// Fallback: direct DB query (used when store is nil, e.g. tests)
|
|
newTxs, err := p.db.GetNewTransmissionsSince(lastID, 100)
|
|
if err != nil {
|
|
log.Printf("[poller] error: %v", err)
|
|
continue
|
|
}
|
|
for _, tx := range newTxs {
|
|
id, _ := tx["id"].(int)
|
|
if id > lastID {
|
|
lastID = id
|
|
}
|
|
// Copy packet fields for the nested packet (avoids circular ref)
|
|
pkt := make(map[string]interface{}, len(tx))
|
|
for k, v := range tx {
|
|
pkt[k] = v
|
|
}
|
|
tx["packet"] = pkt
|
|
p.hub.Broadcast(WSMessage{
|
|
Type: "packet",
|
|
Data: tx,
|
|
})
|
|
}
|
|
}
|
|
case <-p.stop:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *Poller) Stop() {
|
|
close(p.stop)
|
|
}
|