fix(web): harden WebSocket and net host boundaries

- WS handshake: same-origin CheckOrigin instead of allow-all
- route_ws require_auth/需要登录: broadcast/room need a session by default,
  explicit opt-out for public feeds; unauthenticated handshakes get 401
- WS limits: 1 MiB per message, 256 replies per connect; transport errors
  return {ok:false, error} instead of an empty success; empty rooms GC'd
- net host: enforce the 8 MiB response cap while reading (incl. error and
  SSE error paths) so a peer cannot exhaust memory
- url_encode: %20 for space (URL segment semantics, not form +)
- docs: sync 网页 (route_ws 需要登录, connect caps) and CHANGELOG
- tests: gold expectations, live fixture opt-out, hub GC, ws auth gate

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-22 15:14:35 +08:00
1 parent 8e23a5395a
commit 86b183e2d7
15 files changed
+233 -28

No files matched your search

+19 -4
View File
@@ -9,10 +9,11 @@ import (
// RouteSpec is one ws_routes entry (mode + optional room_key / on_message / presence).
type RouteSpec struct {
Mode Mode
RoomKey string
OnMessage string
Presence bool
Mode Mode
RoomKey string
OnMessage string
Presence bool
RequireAuth bool
}
// RouteWS registers path → {mode, room_key?, on_message?, presence?} on app.ws_routes.
@@ -28,6 +29,12 @@ func RouteWS(appBag map[string]any, path string, args map[string]any) (map[strin
}
onMsg := OnMessageFromArgs(args)
presence := PresenceFromArgs(args)
// Stateful fan-out routes are private by default. Echo and drain remain
// useful for public health checks and demos; authors can opt out explicitly.
requireAuth := mode == ModeBroadcast || mode == ModeRoom
if v, ok := first(args, "require_auth", "需要登录"); ok && v != nil {
requireAuth = boolish(v)
}
out := clone(appBag)
wsRoutes := map[string]any{}
if r, ok := out["ws_routes"].(map[string]any); ok {
@@ -43,6 +50,9 @@ func RouteWS(appBag map[string]any, path string, args map[string]any) (map[strin
if presence {
spec["presence"] = true
}
if requireAuth {
spec["require_auth"] = true
}
wsRoutes[path] = spec
out["ws_routes"] = wsRoutes
return out, nil
@@ -165,6 +175,11 @@ func parseRouteSpec(spec any) RouteSpec {
} else if p, ok := t["在场"]; ok {
out.Presence = boolish(p)
}
if a, ok := t["require_auth"]; ok {
out.RequireAuth = boolish(a)
} else if a, ok := t["需要登录"]; ok {
out.RequireAuth = boolish(a)
}
case string:
out.Mode = ParseMode(t)
default: