41e9b10c9d
# Conflicts: # internal/server/websocket.go
151 lines
5.8 KiB
Go
151 lines
5.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/servertoken"
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
const boundaryTestToken = "0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
func boundaryServer(t *testing.T, token string) *httptest.Server {
|
|
t.Helper()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{AccessToken: token}))
|
|
t.Cleanup(func() { srv.Close(); cancel() })
|
|
return srv
|
|
}
|
|
|
|
// raw sends one request to the server's loopback listener with an arbitrary
|
|
// Host header (a DNS-rebinding page names its own host).
|
|
func raw(t *testing.T, srv *httptest.Server, method, path, host string, header http.Header) int {
|
|
t.Helper()
|
|
req, err := http.NewRequest(method, srv.URL+path, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Host = host
|
|
for name, values := range header {
|
|
req.Header[name] = values
|
|
}
|
|
reply, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reply.Body.Close()
|
|
return reply.StatusCode
|
|
}
|
|
|
|
func upgradeHeader(extra map[string]string) http.Header {
|
|
h := http.Header{
|
|
"Connection": {"Upgrade"},
|
|
"Upgrade": {"websocket"},
|
|
"Sec-Websocket-Version": {"13"},
|
|
"Sec-Websocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="},
|
|
}
|
|
for k, v := range extra {
|
|
h.Set(k, v)
|
|
}
|
|
return h
|
|
}
|
|
|
|
func TestBoundaryRefusesRebindingHostOnEveryRoute(t *testing.T) {
|
|
srv := boundaryServer(t, "")
|
|
_, port, _ := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://"))
|
|
for _, path := range []string{"/connect", "/files?path=x", "/chat-uploads", "/discovery", "/mcp/chat/" + strings.Repeat("a", 64), "/healthz", "/artifacts/1/1/x"} {
|
|
for _, host := range []string{"rebind.example:" + port, "rebind.example", "127.0.0.1.nip.io:" + port, "127.0.0.1:1", "localhost:1", "[::1]:1", "10.0.0.5:" + port} {
|
|
if got := raw(t, srv, http.MethodGet, path, host, upgradeHeader(map[string]string{"Origin": "http://" + host})); got != http.StatusMisdirectedRequest {
|
|
t.Errorf("GET %s Host %s = %d, want 421", path, host, got)
|
|
}
|
|
}
|
|
}
|
|
// The reported reproduction: a rebinding upgrade must not switch protocols.
|
|
if got := raw(t, srv, http.MethodGet, "/connect", "rebind.example:"+port, upgradeHeader(map[string]string{"Origin": "http://rebind.example:" + port})); got == http.StatusSwitchingProtocols {
|
|
t.Fatal("rebinding upgrade accepted")
|
|
}
|
|
}
|
|
|
|
func TestBoundaryAcceptsLoopbackHosts(t *testing.T) {
|
|
srv := boundaryServer(t, "")
|
|
_, port, _ := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://"))
|
|
for _, host := range []string{"127.0.0.1:" + port, "localhost:" + port, "LOCALHOST:" + port, "[::1]:" + port} {
|
|
if got := raw(t, srv, http.MethodGet, "/healthz", host, nil); got != http.StatusOK {
|
|
t.Errorf("Host %s = %d, want 200", host, got)
|
|
}
|
|
if got := raw(t, srv, http.MethodGet, "/connect", host, upgradeHeader(nil)); got != http.StatusSwitchingProtocols {
|
|
t.Errorf("upgrade Host %s = %d, want 101", host, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBoundaryRefusesOriginOnConnect(t *testing.T) {
|
|
srv := boundaryServer(t, "")
|
|
host := strings.TrimPrefix(srv.URL, "http://")
|
|
for _, origin := range []string{"http://" + host, "http://evil.example", "null"} {
|
|
if got := raw(t, srv, http.MethodGet, "/connect", host, upgradeHeader(map[string]string{"Origin": origin})); got != http.StatusForbidden {
|
|
t.Errorf("Origin %q = %d, want 403", origin, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBoundaryRequiresTheServerToken(t *testing.T) {
|
|
srv := boundaryServer(t, boundaryTestToken)
|
|
host := strings.TrimPrefix(srv.URL, "http://")
|
|
for _, path := range []string{"/connect", "/files?path=x", "/discovery", "/artifacts/1/1/x"} {
|
|
for _, presented := range []string{"", "wrong", boundaryTestToken + "x"} {
|
|
header := upgradeHeader(nil)
|
|
if presented != "" {
|
|
header.Set(servertoken.Header, presented)
|
|
}
|
|
if got := raw(t, srv, http.MethodGet, path, host, header); got != http.StatusUnauthorized {
|
|
t.Errorf("%s with token %q = %d, want 401", path, presented, got)
|
|
}
|
|
}
|
|
}
|
|
if got := raw(t, srv, http.MethodGet, "/connect", host, upgradeHeader(map[string]string{servertoken.Header: boundaryTestToken})); got != http.StatusSwitchingProtocols {
|
|
t.Errorf("upgrade with token = %d, want 101", got)
|
|
}
|
|
if got := raw(t, srv, http.MethodGet, "/discovery", host, http.Header{servertoken.Header: {boundaryTestToken}}); got != http.StatusOK {
|
|
t.Errorf("discovery with token = %d, want 200", got)
|
|
}
|
|
// Liveness and the chat-tools MCP endpoint (per-chat tokens) stay reachable
|
|
// to local tools that cannot add the header; Host rules still apply to them.
|
|
if got := raw(t, srv, http.MethodGet, "/healthz", host, nil); got != http.StatusOK {
|
|
t.Errorf("healthz without token = %d, want 200", got)
|
|
}
|
|
if got := raw(t, srv, http.MethodPost, "/mcp/chat/"+strings.Repeat("a", 64), host, nil); got == http.StatusUnauthorized {
|
|
t.Errorf("mcp refused for missing server token")
|
|
}
|
|
}
|
|
|
|
func TestBoundaryTokenNeverReachesHandlers(t *testing.T) {
|
|
var seen string
|
|
handler := guardRequests(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
seen = r.Header.Get(servertoken.Header)
|
|
}), boundaryTestToken)
|
|
req := httptest.NewRequest(http.MethodGet, "/files", nil)
|
|
req.Header.Set(servertoken.Header, boundaryTestToken)
|
|
handler.ServeHTTP(httptest.NewRecorder(), req)
|
|
if seen != "" {
|
|
t.Fatalf("handler saw the server token")
|
|
}
|
|
}
|
|
|
|
// A client dialing through the coder/websocket library (the CLI and TUI
|
|
// transport) sends no Origin, so the Origin rule never refuses it.
|
|
func TestBoundaryLibraryDialSendsNoOrigin(t *testing.T) {
|
|
srv := boundaryServer(t, boundaryTestToken)
|
|
conn, _, err := websocket.Dial(context.Background(), "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect",
|
|
&websocket.DialOptions{HTTPHeader: http.Header{servertoken.Header: {boundaryTestToken}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.CloseNow()
|
|
}
|