- 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>
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|