Files
alan 84f7e33872 Guard the Go server's request boundary: loopback Host, no Origin on /connect, shared token
The server answered any Host, so a DNS-rebinding page reached /connect (101)
and could drive terminals. Every route now refuses a non-loopback Host on a
loopback connection (421), /connect refuses any Origin (403), and a server
started with -token-file requires X-Pipelines-Token (401; /mcp and /healthz
exempt). The CLI/TUI send the token only to loopback URLs.
2026-11-10 00:59:39 +00:00

102 lines
3.0 KiB
Go

package client
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/servertoken"
"github.com/coder/websocket"
)
const clientTestToken = "client-token-0123456789abcdef0123456789abcdef"
func writeToken(t *testing.T, mode os.FileMode) string {
t.Helper()
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
t.Setenv(servertoken.EnvKey, "")
path := filepath.Join(dir, "pipelines", "server-token")
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(clientTestToken+"\n"), mode); err != nil {
t.Fatal(err)
}
if err := os.Chmod(path, mode); err != nil {
t.Fatal(err)
}
return path
}
func TestDialSendsTheServerTokenToTheLoopbackServer(t *testing.T) {
writeToken(t, 0o600)
var got, origin string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, origin = r.Header.Get(servertoken.Header), r.Header.Get("Origin")
conn, err := websocket.Accept(w, r, nil)
if err == nil {
conn.CloseNow()
}
}))
defer srv.Close()
for _, base := range []string{srv.URL, strings.Replace(srv.URL, "127.0.0.1", "localhost", 1)} {
got = ""
conn, err := Dial(context.Background(), "ws"+strings.TrimPrefix(base, "http")+"/connect")
if err != nil {
t.Fatal(err)
}
conn.CloseNow()
if got != clientTestToken {
t.Fatalf("%s: token header = %q", base, got)
}
if origin != "" {
t.Fatalf("client sent Origin %q", origin)
}
}
}
func TestDialNeverSendsTheTokenToAnotherHost(t *testing.T) {
writeToken(t, 0o600)
for _, target := range []string{"ws://pipelines.example:8083/connect", "wss://box.tailnet.ts.net/connect", "ws://192.168.1.2:8083/connect", "ws://127.0.0.1.example/connect"} {
options, err := dialOptions(target)
if err != nil || options != nil {
t.Fatalf("%s: options %v err %v", target, options, err)
}
}
}
func TestDialRefusesAnUnprotectedTokenFile(t *testing.T) {
writeToken(t, 0o644)
if _, err := dialOptions("ws://127.0.0.1:8080/connect"); err == nil {
t.Fatal("a group/other-readable token file was used")
}
}
func TestDialWithoutATokenFileSendsNothing(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv(servertoken.EnvKey, "")
options, err := dialOptions("ws://127.0.0.1:8080/connect")
if err != nil || options != nil {
t.Fatalf("options %v err %v", options, err)
}
}
func TestDialUsesTheExplicitTokenFile(t *testing.T) {
path := writeToken(t, 0o600)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv(servertoken.EnvKey, path)
options, err := dialOptions("ws://[::1]:8080/connect")
if err != nil || options == nil || options.HTTPHeader.Get(servertoken.Header) != clientTestToken {
t.Fatalf("options %v err %v", options, err)
}
t.Setenv(servertoken.EnvKey, filepath.Join(t.TempDir(), "missing"))
if _, err := dialOptions("ws://127.0.0.1:8080/connect"); err == nil {
t.Fatal("a missing explicitly named token file was ignored")
}
}