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:
1 parent
8e23a5395a
commit
86b183e2d7
15 files changed
+233
-28
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -12,8 +13,19 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
const maxWSMessageBytes = 1 << 20
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{CheckOrigin: sameOrigin}
|
||||
|
||||
// Browsers send Origin for a cross-site WebSocket handshake. Permit clients
|
||||
// without Origin, but reject browser origins that differ from this application.
|
||||
func sameOrigin(r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
return true
|
||||
}
|
||||
u, err := url.Parse(origin)
|
||||
return err == nil && strings.EqualFold(u.Host, r.Host)
|
||||
}
|
||||
|
||||
func (st *state) mountWSRoutes(mux *http.ServeMux) {
|
||||
@@ -57,11 +69,16 @@ func pathValuesOf(r *http.Request, routePath string) map[string]string {
|
||||
}
|
||||
|
||||
func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, spec ws.RouteSpec) {
|
||||
if spec.RequireAuth && session.UsernameFromCookie(r.Header.Get("Cookie")) == "" {
|
||||
http.Error(w, "WebSocket authentication required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetReadLimit(maxWSMessageBytes)
|
||||
|
||||
vals := pathValuesOf(r, path)
|
||||
switch spec.Mode {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
func TestHandleWSRejectsUnauthenticatedHandshake(t *testing.T) {
|
||||
st := &state{}
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
w := httptest.NewRecorder()
|
||||
st.handleWS(w, r, "/live", ws.RouteSpec{Mode: ws.ModeBroadcast, RequireAuth: true})
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unauthenticated ws handshake code=%d, want 401", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWSAllowsPublicRouteThroughAuthGate(t *testing.T) {
|
||||
st := &state{}
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
w := httptest.NewRecorder()
|
||||
// Public route: must not 401. Upgrade then fails on the recorder (no
|
||||
// Hijacker), which is fine — only the auth gate is under test.
|
||||
st.handleWS(w, r, "/live", ws.RouteSpec{Mode: ws.ModeBroadcast, RequireAuth: false})
|
||||
if w.Code == http.StatusUnauthorized {
|
||||
t.Fatalf("public ws route unexpectedly requires authentication")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameOriginWebSocketHandshake(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
origin string
|
||||
want bool
|
||||
}{
|
||||
{name: "non-browser client", want: true},
|
||||
{name: "same origin", origin: "https://app.example", want: true},
|
||||
{name: "different origin", origin: "https://evil.example", want: false},
|
||||
{name: "malformed origin", origin: "://bad", want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
if tc.origin != "" {
|
||||
r.Header.Set("Origin", tc.origin)
|
||||
}
|
||||
if got := sameOrigin(r); got != tc.want {
|
||||
t.Fatalf("sameOrigin(%q)=%v, want %v", tc.origin, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user