ARTEX/server/auth_transport_test.go
BaaRai 4af245b3f4 fix(auth): remove JWT from SSE query strings and align CORS preflight with #633 (#660)
Applies #660 (remove ?token= query extraction, switch web SSE to fetch+
Authorization Bearer, drop client-written auth cookie) cleanly on top of #633's
same-origin CORS policy. Preserves #633's same-origin ACAO + Vary: Origin while
allowing Authorization and Last-Event-ID on same-origin preflights, aligns
openSSE 401 cookie clearing attributes, and updates the upload.test.mjs and
traffic-stream.test.mjs harnesses for @/lib/sse (SSEStream stub + EventTarget,
Event, MessageEvent, AbortController in VM context).

Co-authored-by: Coff0xc <184964491+Coff0xc@users.noreply.github.com>
2026-11-28 23:07:48 +01:00

68 lines
2.8 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestSessionTokenNeverAuthenticatesFromURL(t *testing.T) {
s := &Server{jwtKey: []byte("0123456789abcdef0123456789abcdef")}
token, err := signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
h := s.requireAuth(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) }))
for _, path := range []string{"/api/tasks", "/api/logs/stream", "/api/exploration/activity/stream?task=1", "/api/side-questions/1/events", "/api/browser/stream"} {
for _, method := range []string{http.MethodGet, http.MethodPost} {
t.Run(method+path, func(t *testing.T) {
r := httptest.NewRequest(method, path, nil)
q := r.URL.Query()
q.Set("token", token)
r.URL.RawQuery = q.Encode()
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusUnauthorized {
t.Fatalf("query credential accepted: %d", w.Code)
}
r.Header.Set("Authorization", "Bearer "+token)
w = httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusNoContent {
t.Fatalf("header credential refused: %d", w.Code)
}
})
}
}
}
func TestSSEBearerPreflight(t *testing.T) {
// Same-origin preflight receives 204 with Authorization and Last-Event-ID allowed.
r := httptest.NewRequest(http.MethodOptions, "http://example.com/api/logs/stream", nil)
r.Header.Set("Origin", "http://example.com")
r.Header.Set("Access-Control-Request-Headers", "authorization,last-event-id")
w := httptest.NewRecorder()
cors(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { t.Fatal("preflight reached handler") })).ServeHTTP(w, r)
if w.Code != http.StatusNoContent {
t.Fatalf("same-origin preflight status=%d", w.Code)
}
if got := w.Header().Get("Access-Control-Allow-Origin"); got != "http://example.com" {
t.Fatalf("same-origin ACAO=%q, want http://example.com", got)
}
if got := w.Header().Get("Access-Control-Allow-Headers"); got != "Content-Type, Authorization, Last-Event-ID" {
t.Fatalf("SSE credential/cursor headers not allowed: %q", got)
}
// Cross-origin preflight must NOT receive ACAO or Allow-Headers (interacts with #633).
xr := httptest.NewRequest(http.MethodOptions, "http://example.com/api/logs/stream", nil)
xr.Header.Set("Origin", "http://localhost:3000")
xr.Header.Set("Access-Control-Request-Headers", "authorization,last-event-id")
xw := httptest.NewRecorder()
cors(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })).ServeHTTP(xw, xr)
if got := xw.Header().Get("Access-Control-Allow-Origin"); got != "" {
t.Fatalf("cross-origin preflight echoed ACAO=%q", got)
}
if got := xw.Header().Get("Access-Control-Allow-Headers"); got != "" {
t.Fatalf("cross-origin preflight echoed ACAH=%q", got)
}
}