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.
This commit is contained in:
@@ -19,7 +19,12 @@ import (
|
||||
func Dial(ctx context.Context, url string) (*websocket.Conn, error) {
|
||||
// O(1) exact string compare; only an explicit, validated Docker transport
|
||||
// alias for this same endpoint is ever rewritten.
|
||||
conn, _, err := websocket.Dial(ctx, transportTarget(url), nil)
|
||||
target := transportTarget(url)
|
||||
options, err := dialOptions(target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, _, err := websocket.Dial(ctx, target, options)
|
||||
if err == nil {
|
||||
conn.SetReadLimit(8 << 20)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/servertoken"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
// dialOptions attaches the shared server token (internal/servertoken) when the
|
||||
// dialed endpoint is this machine's loopback server. The token is never sent to
|
||||
// another host: a remote server is reached through its web proxy, which has its
|
||||
// own sign-in. No token file means no header (an unconfigured server needs none).
|
||||
func dialOptions(target string) (*websocket.DialOptions, error) {
|
||||
if !loopbackEndpoint(target) {
|
||||
return nil, nil
|
||||
}
|
||||
token, err := servertoken.ForClient()
|
||||
if err != nil || token == "" {
|
||||
return nil, err
|
||||
}
|
||||
return &websocket.DialOptions{HTTPHeader: http.Header{servertoken.Header: {token}}}, nil
|
||||
}
|
||||
|
||||
func loopbackEndpoint(target string) bool {
|
||||
u, err := url.Parse(target)
|
||||
if err != nil || (u.Scheme != "ws" && u.Scheme != "wss") {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
if strings.EqualFold(host, "localhost") {
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && ip.IsLoopback()
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user