diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index f17571ca..3959ffd5 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -21,6 +21,7 @@ import ( "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/servertoken" "git.a-wan.net/alan/pipelines/internal/summarise" ) @@ -39,7 +40,22 @@ func main() { db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)") mdnsOn := flag.Bool("mdns", envEnabled(os.Getenv("PIPELINES_MDNS")), "Advertise the server on the LAN with mDNS/DNS-SD ("+discovery.ServiceType+"); PIPELINES_MDNS=off disables") mdnsPort := flag.Int("mdns-port", envPort(os.Getenv("PIPELINES_MDNS_PORT")), "Port LAN clients dial: the authenticated web proxy fronting this server (default: the -addr port when it is not loopback)") + tokenFile := flag.String("token-file", os.Getenv(servertoken.EnvKey), "Require the shared secret in this private file on every request (the installer's server-token; PIPELINES_SERVER_TOKEN_FILE)") flag.Parse() + accessToken := "" + if *tokenFile != "" { + if absolute, absErr := filepath.Abs(*tokenFile); absErr == nil { + *tokenFile = absolute + } + if accessToken, err = servertoken.Read(*tokenFile); err != nil { + log.Fatalf("server token: %v", err) + } + // Stages, chat harnesses and their `pipelines` CLI calls inherit the + // file's location, never the token itself. + _ = os.Setenv(servertoken.EnvKey, *tokenFile) + } else { + log.Printf("no server token configured (-token-file): local clients reach this server without the shared secret") + } if err := os.MkdirAll(filepath.Dir(*db), 0700); err != nil { log.Fatal(err) } @@ -80,7 +96,7 @@ func main() { // Each new run gets a bounded one-line title from the configured default // harness; the summariser falls back to the prompt's first line, so title // generation never blocks or fails run creation. - options := server.Options{AllowAuthoring: *allowAuthoring, ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv), DetachSessions: detach} + options := server.Options{AllowAuthoring: *allowAuthoring, ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv), DetachSessions: detach, AccessToken: accessToken} recorded := "" if value, err := store.OpenCodeEndpoint(ctx); err != nil { log.Printf("OpenCode endpoint setting unreadable: %v", err) diff --git a/docs/cli.md b/docs/cli.md index ff9d9b9f..0485e5ac 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -15,6 +15,9 @@ service manager directly. Legacy Python-managed installations require an explici `PIPELINES_PREFIX`; no configuration is inferred from the executable's location. For source development, use `go run ./cmd/pipelines help`; start the native server separately. Set `PIPELINES_URL` or pass `--url` to select another server. +A loopback server started with `-token-file` requires the shared token; the CLI +reads it from `PIPELINES_SERVER_TOKEN_FILE` or `~/.config/pipelines/server-token` +and sends it only to loopback URLs (`docs/notes/security-boundary.md`). ## Dashboard and status diff --git a/docs/feature-changes.md b/docs/feature-changes.md index a6473a6d..567def13 100644 --- a/docs/feature-changes.md +++ b/docs/feature-changes.md @@ -30,7 +30,11 @@ additive conflicts. websocket. Home-folder access uses `expandHomePath`, `resolveInsideHome` and `specialFilesystem`; symlinks are operated on, not followed. Internal endpoints (for example `/mcp`) must not be reachable through the web proxy; - add a proxy test. Typed input, passwords and tokens are never logged or + add a proxy test. The server answers only loopback `Host`s, refuses + `/connect` with an `Origin`, and requires the shared server token + (`docs/notes/security-boundary.md`); a new local client of the server + sends the token, and the peer relay's address policy covers new remote + paths. Typed input, passwords and tokens are never logged or stored. Credentials never cross machines. - **Bounds.** Every retained buffer, list and payload has a limit (for example 1 MiB output, 50 runs, 512 KiB views, 5 notifications a minute) and reports diff --git a/docs/notes/security-boundary.md b/docs/notes/security-boundary.md new file mode 100644 index 00000000..90069ff4 --- /dev/null +++ b/docs/notes/security-boundary.md @@ -0,0 +1,187 @@ +# Security boundary: the Go server's front door and the peer relay + +Fixes from the 2026-11-10 security review (one HIGH, two LOW). + +## Threats + +1. **HIGH: DNS rebinding reached the unauthenticated Go API.** The server + (`127.0.0.1:8080`) had no authentication. `websocket.Accept` only checks + `Origin == Host`, so a malicious page that rebinds its own name to + 127.0.0.1 sent a matching Host and Origin and got `101`. From there it could + send `terminal_open`/`terminal_input` (a shell as the operator). + Reproduced on the pre-fix build: + `curl -H "Host: rebind.example:57577" -H "Origin: http://rebind.example:57577" …/connect` + returned `101`, and `/healthz` answered for any Host. +2. **LOW: any local process had full control.** Other OS users, containers + (`host.docker.internal`) and anything else that could reach loopback were + trusted. +3. **LOW: peer relay SSRF.** Any signed-in user could register + `ws:///connect`. The proxy then fetched `/login`, `/files`, + `/artifacts`, `/updates` and `/connect` from that host, including loopback + services and `169.254.169.254`. + +## Fix + +### Go server request boundary (`internal/server/request_boundary.go`) + +Every route goes through `guardRequests` before the mux: `/connect`, `/files`, +`/chat-uploads`, `/artifacts`, `/discovery`, `/mcp/chat/…` and `/healthz`. + +- **Host.** If the connection arrived on a loopback local address, `Host` must + be a loopback literal (`127.x.x.x`, `localhost`, `[::1]`) with the port the + connection reached. Anything else gets `421 Misdirected Request`. A + connection that arrived on another interface can only exist when the server + was explicitly bound beyond loopback (`bind_address`, Docker). Its Host is + not checked; the token still is. +- **Origin.** `/connect` refuses any request that carries `Origin` with `403`. + Browsers always send it on a WebSocket upgrade. The web proxy strips it. The + CLI and TUI (`coder/websocket` Dial) and the native app (`dart:io` + `WebSocket.connect`) send none. +- **Token.** When the server runs with `-token-file` (or + `PIPELINES_SERVER_TOKEN_FILE`), every request must carry + `X-Pipelines-Token: `. A missing or wrong token gets `401`, compared + in constant time. There are two exemptions: + - `/mcp/chat/`: harnesses register the URL and cannot add headers. + The endpoint already has 64-character per-chat tokens, a loopback check + and a no-Origin check. + - `/healthz`: a bare `{"healthy":…}`, still subject to the Host rule. + + The header is removed before any handler sees it. The server exports the + file's absolute path (never the token) as `PIPELINES_SERVER_TOKEN_FILE` to + the stages, chats and CLI calls it launches. A server started without a + token file logs that, and only the Host and Origin rules apply. Docker and + scratch servers work this way. + +### The shared secret (`internal/servertoken`, `web/src/serverToken.js`) + +- **File:** a new private file, `server-token`, next to the web sign-in file. + By default that is `~/.config/pipelines/server-token`, or + `$XDG_CONFIG_HOME/pipelines/server-token`. It holds 64 URL-safe characters + (`secrets.token_urlsafe(48)`). + - The web auth file's secret is not reused. Rotating the cookie secret or + password (`scripts/password.py`) must not break the server link, and the + other way round. +- **Permission model:** the file is mode 0600, owned by the operator, and its + directory is 0700. Readers refuse a symlink, a non-regular file, a file + owned by another user, any group/other bits, or a token shorter than 32 + characters: + - the Go server and CLI (`servertoken.Read`) + - the proxy (`serverToken`) + - the installer + + Processes of the same OS user can read it, which is the trust boundary. + Other users, containers without the file mounted, and browsers cannot. +- **Installer:** `./install` (`scripts/setup-native.py` `ensure_server_token`) + creates the file if it is missing and keeps it on reinstall. It records the + path in the manifest as `web.server_token_file`. `scripts/manage.py` sets + `PIPELINES_SERVER_TOKEN_FILE` for the server, the proxy (whose environment + is derived from the server's) and `pipelines` CLI/TUI invocations. A + manifest from before this change has no key, and nothing changes until + `./install` runs again. + +### How each client authenticates + +| Client | How | +|---|---| +| Web proxy (`web/serve.mjs`) | Adds `X-Pipelines-Token` to `/connect`, `/files`, `/artifacts/`, `/chat-uploads` and `/api/discovery` upstream requests. It drops any client-supplied copy first, sets `Host` to the upstream loopback address and strips `Origin`. Browsers authenticate to the proxy with the sign-in cookie. If the token file is unreadable or unsafe, the proxy answers 503. | +| CLI, TUI, automation (`internal/client.Dial`) | Reads `PIPELINES_SERVER_TOKEN_FILE`, else the default path, and sends the header only when the dialed URL is loopback (`127.x`, `::1`, `localhost`). Remote servers are reached through their own web proxy, so the token never leaves the machine. With no file it sends nothing. An unsafe or explicitly named but missing file is an error. | +| Stages and chats launched by the server | Inherit `PIPELINES_SERVER_TOKEN_FILE` from the server, and their `pipelines` calls go through `client.Dial`. Chat hosts that outlived a restart fall back to the default path. | +| Harness MCP registration | `/mcp/chat/` on the loopback base, with no server token (exempt). | +| Native app and browser app | Always through the web proxy (cookie). The app never dials 8080 directly. | +| Peer relay to another machine | Uses that machine's proxy sign-in cookie. The local server token is never forwarded (tested). | +| `manage.py` health checks | `/healthz` (exempt). | + +### Peer relay address policy (`web/src/peerAddress.js`) + +- **Allowed by default:** + - tailnet addresses: `100.64.0.0/10` and `fd7a:115c:a1e0::/48` + - private LAN addresses: RFC 1918 and IPv6 unique-local `fc00::/7` + - public addresses, only for `*.ts.net` names (Funnel) or hosts that this + machine's own `/api/discovery` returned +- **Always refused:** unspecified, multicast and reserved addresses. +- **Refused unless widened:** loopback (`127/8`, `::1`, `::ffff:127…`), + link-local (`169.254/16`, `fe80::/10`), metadata (`169.254.169.254`, + Tailscale's `100.100.100.100` and `fd7a:115c:a1e0::53`, AWS + `fd00:ec2::/32`) and other public addresses. +- **Explicit widening:** `PIPELINES_PEER_ALLOW=loopback,public,link-local,metadata` + or `any`, set in the proxy's environment. +- **DNS pinning:** each registration and each relayed request resolves the + name once and checks every answer; one refused answer refuses the name. The + connection then goes to the checked address through the request's `lookup`. + `Host` and TLS SNI stay the original name. A rebinding answer between check + and connect cannot redirect it. +- **Registration responses:** + - A registration whose addresses are all refused gets `400` with + `error: "Server address is not allowed ()"` and + `refused_endpoints`. No request is sent. + - When only some addresses are refused, they are listed in + `refused_endpoints`. + - `insecure_transport: true` is set on `POST` and `GET /api/peers/` when + a `ws://` endpoint is used, because the password and cookie travel as + plain HTTP. **App follow-up (not done):** show a warning before sending a + password when `insecure_transport` is set. + +## Residual risks + +- A local process of the same user can read the token. That user is the trust + boundary. +- If the server is down and another local user binds its port, the CLI would + send the token to that impostor. Keep the service running. A + challenge-response handshake would close this gap but is out of scope here. +- A server bound beyond loopback skips the Host rule for non-loopback + connections. The token and Origin rules still protect it when a token is + configured; an unconfigured server bound beyond loopback remains open. + +## Verification + +- Go (`umask 022; go test ./...`): all packages ok. New tests: + - `internal/server` `TestBoundary*`: rebinding and other Hosts get 421 on + every route, loopback Hosts are accepted, Origin on `/connect` gets 403, + the token is required and accepted, `/healthz` and `/mcp` are exempt, the + token never reaches handlers, and a library dial sends no Origin. + - `internal/client` `TestDial*`: the CLI sends the token to 127.0.0.1 and + localhost and never to remote hosts, and it refuses an unsafe file. + - `internal/servertoken`. + - `internal/installation` `TestEnvironmentPointsLocalClientsAtTheServerToken`. +- Web (`cd web && node --test test/`): 26/26 pass. New tests: + - `serverToken.test.mjs`: the proxy sends the token on files, artifacts, + uploads, discovery and the upgrade, replaces a forged one, and the relay + never forwards it. + - `peerAddress.test.mjs`: classification; loopback, metadata, link-local + and public refused; tailnet, LAN, ts.net and discovered hosts accepted; + pinning; a refused peer is never contacted; `insecure_transport`. +- Installer: `python3 -m unittest test_manage_native test_setup_native`, 78 + tests OK. +- Scratch end-to-end: isolated HOME and DB, OS-chosen ports, server with + `PIPELINES_SERVER_TOKEN_FILE`, proxy from this tree. + + ``` + 1 rebinding upgrade (Host+Origin rebind.example): 421 + 2 rebinding upgrade even with the token: 421 + 3 other Host 10.0.0.5: on /healthz: 421 + 4 other Host on /files with token: 421 + 5 loopback upgrade without token: 401 + 6 loopback /discovery without token: 401 + 7 loopback upgrade with token + same-host Origin: 403 + 8 loopback upgrade with token (no Origin): 101 + 9 /healthz (token-exempt) on localhost:: 200 + --- CLI with the token file (default path): {"type":"pong","id":"cli-1",…} + --- CLI without a token file: {"error":"failed to WebSocket dial: expected handshake response status code 101 but got 401","type":"error"} + --- browser flow through the proxy: + proxy sign-in: 303 + proxy /files (reaches server, token added): 404 file not found + proxy /api/discovery: 200 + proxy /connect ping reply: pong ok + proxy /connect from cross-origin page: refused + ``` + +## Deploying + +1. Run `./install`. It creates `~/.config/pipelines/server-token` (0600) if + missing, records `web.server_token_file` in the manifest and copies the + new `manage.py`, `serve.mjs` and binaries. +2. Restart the service when no run or chat is active. The supervisor starts + the server and the proxy together, both with `PIPELINES_SERVER_TOKEN_FILE`, + so they switch at once. +3. CLI and TUI sessions that are already open hold an old connection; new + invocations read the token. diff --git a/internal/client/client.go b/internal/client/client.go index 462682ad..8d650bb6 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -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) } diff --git a/internal/client/token.go b/internal/client/token.go new file mode 100644 index 00000000..579e21b6 --- /dev/null +++ b/internal/client/token.go @@ -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() +} diff --git a/internal/client/token_test.go b/internal/client/token_test.go new file mode 100644 index 00000000..ab57a659 --- /dev/null +++ b/internal/client/token_test.go @@ -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") + } +} diff --git a/internal/installation/installation.go b/internal/installation/installation.go index 9859ce0d..e248cd14 100644 --- a/internal/installation/installation.go +++ b/internal/installation/installation.go @@ -20,6 +20,10 @@ type Config struct { Ntfy struct { Mode string `json:"mode"` } `json:"ntfy"` + Web struct { + // ServerTokenFile is the shared server secret (internal/servertoken). + ServerTokenFile string `json:"server_token_file"` + } `json:"web"` } // Load selects an explicit prefix, or a native installation containing this exact @@ -87,8 +91,8 @@ func (c Config) Environment() error { } } } - for key, value := range map[string]string{"PIPELINES_URL": "ws://127.0.0.1:" + strconv.Itoa(c.ServerPort) + "/connect", "PIPELINES_OPENCODE": c.OpenCode} { - if os.Getenv(key) != "" { + for key, value := range map[string]string{"PIPELINES_URL": "ws://127.0.0.1:" + strconv.Itoa(c.ServerPort) + "/connect", "PIPELINES_OPENCODE": c.OpenCode, "PIPELINES_SERVER_TOKEN_FILE": c.Web.ServerTokenFile} { + if os.Getenv(key) != "" || value == "" { continue } if err := os.Setenv(key, value); err != nil { diff --git a/internal/installation/installation_test.go b/internal/installation/installation_test.go index c9af0f61..ce456445 100644 --- a/internal/installation/installation_test.go +++ b/internal/installation/installation_test.go @@ -39,6 +39,27 @@ func TestCompiledCommandFindsManifestThroughSystemLink(t *testing.T) { } } +func TestEnvironmentPointsLocalClientsAtTheServerToken(t *testing.T) { + var config Config + if err := json.Unmarshal([]byte(`{"server_port":18080,"web":{"server_token_file":"/home/u/.config/pipelines/server-token"}}`), &config); err != nil { + t.Fatal(err) + } + t.Setenv("PIPELINES_URL", "") + t.Setenv("PIPELINES_OPENCODE", "") + t.Setenv("PIPELINES_SERVER_TOKEN_FILE", "") + if err := config.Environment(); err != nil { + t.Fatal(err) + } + if got := os.Getenv("PIPELINES_SERVER_TOKEN_FILE"); got != "/home/u/.config/pipelines/server-token" { + t.Fatalf("token file = %q", got) + } + // A manifest from before the token existed sets nothing. + t.Setenv("PIPELINES_SERVER_TOKEN_FILE", "") + if err := (Config{ServerPort: 18080}).Environment(); err != nil || os.Getenv("PIPELINES_SERVER_TOKEN_FILE") != "" { + t.Fatalf("legacy manifest set a token file: %v", err) + } +} + func TestStandaloneGoInstalledCommandHasNoServiceDependency(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") binary := filepath.Join(t.TempDir(), "pipelines") diff --git a/internal/server/request_boundary.go b/internal/server/request_boundary.go new file mode 100644 index 00000000..fe04bba6 --- /dev/null +++ b/internal/server/request_boundary.go @@ -0,0 +1,84 @@ +package server + +import ( + "net" + "net/http" + "strings" + + "git.a-wan.net/alan/pipelines/internal/servertoken" +) + +// requestBoundary is the server's HTTP front door, applied before any route +// (docs/notes/security-boundary.md): +// +// - Host: a connection that arrived on a loopback address must name a +// loopback literal (127.0.0.1, localhost, [::1]) with the port it reached, +// so a DNS-rebinding page (Host: rebind.example:8080) is refused with 421. +// A connection that arrived on another interface exists only when the +// server was explicitly bound beyond loopback; its Host is not checked. +// - Origin: /connect refuses any request carrying Origin. Browsers always +// send it on a WebSocket upgrade; the web proxy strips it and the CLI, the +// TUI and the native app send none. +// - Token: when the server is configured with the shared secret, every +// request must carry it in servertoken.Header, except the chat-tools MCP +// endpoint (its own per-chat token, loopback and no-Origin checks) and +// /healthz (a bare liveness boolean). +type requestBoundary struct { + next http.Handler + token string +} + +func guardRequests(next http.Handler, token string) http.Handler { + return requestBoundary{next: next, token: token} +} + +func (b requestBoundary) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if !allowedHost(r) { + http.Error(w, "misdirected request: this server answers only its own loopback address", http.StatusMisdirectedRequest) + return + } + if r.URL.Path == "/connect" && r.Header.Get("Origin") != "" { + http.Error(w, "browser origins may not connect directly; use the web proxy", http.StatusForbidden) + return + } + if b.token != "" && !tokenExempt(r.URL.Path) && !servertoken.Equal(r.Header.Get(servertoken.Header), b.token) { + http.Error(w, "server token required", http.StatusUnauthorized) + return + } + // The secret never reaches a handler, a log line or a forwarded request. + r.Header.Del(servertoken.Header) + b.next.ServeHTTP(w, r) +} + +func tokenExempt(path string) bool { + return path == "/healthz" || strings.HasPrefix(path, chatToolPath) +} + +// allowedHost applies the loopback Host rule. A request with no recorded local +// address (an in-process test handler) has no listener to compare against. +func allowedHost(r *http.Request) bool { + local, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr) + if !ok || local == nil { + return true + } + localHost, localPort, err := net.SplitHostPort(local.String()) + if err != nil { + return false + } + if ip := net.ParseIP(localHost); ip == nil || !ip.IsLoopback() { + return true + } + host, port, err := net.SplitHostPort(r.Host) + if err != nil { + // No port: only the default HTTP port could match. + host, port = strings.TrimSuffix(strings.TrimPrefix(r.Host, "["), "]"), "80" + } + if port != localPort { + return false + } + if strings.EqualFold(host, "localhost") { + return true + } + ip := net.ParseIP(host) + return ip != nil && ip.IsLoopback() && !strings.Contains(host, "%") +} diff --git a/internal/server/request_boundary_test.go b/internal/server/request_boundary_test.go new file mode 100644 index 00000000..35b208b7 --- /dev/null +++ b/internal/server/request_boundary_test.go @@ -0,0 +1,150 @@ +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() +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 9d7e1a8a..f0f17a11 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -66,6 +66,9 @@ type Options struct { // False keeps the previous behaviour (tests, embedded servers): a stopping // server interrupts its runs and the next start marks them interrupted. DetachSessions bool + // AccessToken, when set, is required on every request (servertoken.Header) + // except the chat-tools MCP endpoint and /healthz (request_boundary.go). + AccessToken string } // Handler serves the persistent connection with default options. Canceling ctx @@ -567,7 +570,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin // runner's collaborators are wired and before any request is served. runs.reattachRuns() } - return &runHandler{Handler: mux, runner: runs, hub: hub} + return &runHandler{Handler: guardRequests(mux, options.AccessToken), runner: runs, hub: hub} } type runHandler struct { diff --git a/internal/servertoken/private_other.go b/internal/servertoken/private_other.go new file mode 100644 index 00000000..71fa7bc9 --- /dev/null +++ b/internal/servertoken/private_other.go @@ -0,0 +1,8 @@ +//go:build !unix + +package servertoken + +import "os" + +// Windows has no POSIX mode bits; the file lives in the user's profile. +func private(info os.FileInfo) bool { return true } diff --git a/internal/servertoken/private_unix.go b/internal/servertoken/private_unix.go new file mode 100644 index 00000000..e2ba66aa --- /dev/null +++ b/internal/servertoken/private_unix.go @@ -0,0 +1,13 @@ +//go:build unix + +package servertoken + +import ( + "os" + "syscall" +) + +func private(info os.FileInfo) bool { + stat, ok := info.Sys().(*syscall.Stat_t) + return ok && int(stat.Uid) == os.Getuid() && info.Mode().Perm()&0o077 == 0 +} diff --git a/internal/servertoken/servertoken.go b/internal/servertoken/servertoken.go new file mode 100644 index 00000000..1de434f2 --- /dev/null +++ b/internal/servertoken/servertoken.go @@ -0,0 +1,91 @@ +// Package servertoken is the shared secret between the Pipelines server and its +// local clients (the authenticated web proxy, the CLI, the TUI and the +// harnesses the server launches). +// +// The installer writes a random token to a private (0600) file next to the web +// sign-in file, by default $XDG_CONFIG_HOME/pipelines/server-token (else +// ~/.config/pipelines/server-token). A server started with the file configured +// (-token-file or PIPELINES_SERVER_TOKEN_FILE) refuses every request that does +// not carry the token in the Header header; local processes of the same OS +// user can read the file, other users, containers and browsers cannot. +package servertoken + +import ( + "crypto/subtle" + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +// Header carries the token on every request to the server. +const Header = "X-Pipelines-Token" + +// EnvKey names the token file explicitly; it overrides the default location. +const EnvKey = "PIPELINES_SERVER_TOKEN_FILE" + +// minLength bounds a usable token (the installer writes 64 URL-safe chars). +const minLength = 32 + +// DefaultPath is the installer's default location, beside web-auth.json. +func DefaultPath() (string, error) { + directory := os.Getenv("XDG_CONFIG_HOME") + if directory == "" { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + directory = filepath.Join(home, ".config") + } + return filepath.Join(directory, "pipelines", "server-token"), nil +} + +// Read loads a token from path. The file must be a regular file (not a +// symlink) owned by this user with no group or other permissions, so a token +// another user could read or replace is refused instead of trusted. +func Read(path string) (string, error) { + info, err := os.Lstat(path) + if err != nil { + return "", err + } + if !info.Mode().IsRegular() { + return "", fmt.Errorf("server token %s is not a regular file", path) + } + if !private(info) { + return "", fmt.Errorf("server token %s must be owned by this user and private (chmod 600)", path) + } + data, err := os.ReadFile(path) + if err != nil { + return "", err + } + token := strings.TrimSpace(string(data)) + if len(token) < minLength || len(token) > 4096 || strings.ContainsAny(token, " \t\r\n") { + return "", fmt.Errorf("server token %s is malformed", path) + } + return token, nil +} + +// ForClient returns the token a local client sends: the explicitly named file +// (PIPELINES_SERVER_TOKEN_FILE) or else the default file when it exists. No +// file means no token (an unconfigured or Docker server needs none); an +// explicitly named file that cannot be read is an error. +func ForClient() (string, error) { + if path := os.Getenv(EnvKey); path != "" { + return Read(path) + } + path, err := DefaultPath() + if err != nil { + return "", nil + } + token, err := Read(path) + if errors.Is(err, os.ErrNotExist) { + return "", nil + } + return token, err +} + +// Equal compares a presented token in constant time. +func Equal(presented, want string) bool { + return want != "" && subtle.ConstantTimeCompare([]byte(presented), []byte(want)) == 1 +} diff --git a/internal/servertoken/servertoken_test.go b/internal/servertoken/servertoken_test.go new file mode 100644 index 00000000..eb019a03 --- /dev/null +++ b/internal/servertoken/servertoken_test.go @@ -0,0 +1,45 @@ +//go:build unix + +package servertoken + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestReadAcceptsOnlyAPrivateRegularFile(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "server-token") + token := strings.Repeat("t", 48) + if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil { + t.Fatal(err) + } + if got, err := Read(path); err != nil || got != token { + t.Fatalf("Read = %q, %v", got, err) + } + if err := os.Chmod(path, 0o640); err != nil { + t.Fatal(err) + } + if _, err := Read(path); err == nil { + t.Fatal("group-readable token accepted") + } + os.Chmod(path, 0o600) + link := filepath.Join(dir, "link") + os.Symlink(path, link) + if _, err := Read(link); err == nil { + t.Fatal("symlinked token accepted") + } + short := filepath.Join(dir, "short") + os.WriteFile(short, []byte("short"), 0o600) + if _, err := Read(short); err == nil { + t.Fatal("short token accepted") + } +} + +func TestEqualIsExact(t *testing.T) { + if Equal("", "") || Equal("a", "") || !Equal("abc", "abc") || Equal("abc", "abd") { + t.Fatal("Equal mismatch") + } +} diff --git a/scripts/manage.py b/scripts/manage.py index 9f958559..5b7d0629 100644 --- a/scripts/manage.py +++ b/scripts/manage.py @@ -38,6 +38,12 @@ def server_address(config): return f"{config.get('bind_address') or '127.0.0.1'}:{config['server_port']}" +def server_token_env(config): + """PIPELINES_SERVER_TOKEN_FILE for the server, the web proxy and the CLI.""" + path = (config.get("web") or {}).get("server_token_file") + return {"PIPELINES_SERVER_TOKEN_FILE": path} if path else {} + + def mdns_args(config): """The LAN port the server advertises over mDNS. @@ -416,6 +422,9 @@ def supervise(config): PIPELINES_NTFY_PUBLIC_URL=config["public_url"], PIPELINES_NTFY_TOKEN_FILE=str(root / "secrets/publisher-token")) env.pop("PIPELINES_NTFY_TOKEN", None) + # The server requires, and the proxy and every child CLI send, the + # shared token in this file (installs from before it have none). + env.update(server_token_env(config)) server_command = [str(root / "bin/pipelines-server"), "-addr", server_address(config), "-db", str(root / "data/pipelines.db")] if config.get("allow_authoring") is True: @@ -520,6 +529,7 @@ def main(): env.update(config.get("opencode_env", {})) env.update(PIPELINES_URL=f"ws://127.0.0.1:{config['server_port']}/connect", PIPELINES_OPENCODE=config["opencode"], PATH=config["path"]) + env.update(server_token_env(config)) os.execve(binary, [binary, *(forwarded or ["tui"])], env) return parser = argparse.ArgumentParser(description=__doc__) diff --git a/scripts/setup-native.py b/scripts/setup-native.py index 7ed9bdde..ba7b2223 100644 --- a/scripts/setup-native.py +++ b/scripts/setup-native.py @@ -359,6 +359,7 @@ class Setup: auth.parent.mkdir(mode=0o700, parents=True, exist_ok=True) self._write_atomic(auth, (json.dumps({"password": password, "secret": secrets.token_urlsafe(48)}) + "\n").encode(), 0o600) + server_token = self.ensure_server_token(auth.parent / "server-token") target = prefix / "lib/web" shutil.copytree(Path(__file__).resolve().parents[1] / "web", target, dirs_exist_ok=True, ignore=shutil.ignore_patterns("test", "node_modules", ".DS_Store")) @@ -366,8 +367,24 @@ class Setup: updates.mkdir(parents=True, exist_ok=True) return {"node": node, "script": str(target / "serve.mjs"), "auth_file": str(auth), "host": args.web_host, "port": args.web_port, + "server_token_file": str(server_token), "root": self.install_app_web(prefix, args, web_build), "updates": str(updates)} + def ensure_server_token(self, path): + """The proxy/CLI ↔ server shared secret (docs/notes/security-boundary.md). + + Created once (0600, beside the web auth file) and kept across reinstalls.""" + if path.is_symlink() or (path.exists() and not path.is_file()): + raise ValueError(f"Refusing a server token that is not a regular file: {path}") + if path.exists(): + if path.stat().st_mode & 0o077 or path.stat().st_uid != os.getuid(): + raise ValueError(f"Server token must be owned by you and private (chmod 600): {path}") + if len(path.read_text().strip()) >= 32: + return path + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + self._write_atomic(path, (secrets.token_urlsafe(48) + "\n").encode(), 0o600) + return path + def choose_password(self): """The web sign-in password for a new computer, chosen by the operator. diff --git a/scripts/test_manage_native.py b/scripts/test_manage_native.py index 4ab25f72..af699494 100644 --- a/scripts/test_manage_native.py +++ b/scripts/test_manage_native.py @@ -281,6 +281,7 @@ class ManageNativeTests(unittest.TestCase): config = self.config(root) config["web"] = {"node": "/bin/node", "script": "/prefix/lib/web/serve.mjs", "host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json", + "server_token_file": "/private/server-token", "root": str(root / "lib/app-web"), "updates": "/prefix/lib/app-updates"} ntfy, server, web = MagicMock(), MagicMock(), MagicMock() ntfy.poll.return_value = None @@ -299,6 +300,10 @@ class ManageNativeTests(unittest.TestCase): "--updates", "/prefix/lib/app-updates"]) self.assertNotIn("PIPELINES_WEB_AUTH", call.kwargs["env"]) self.assertEqual(call.kwargs["env"]["PIPELINES_WEB_AUTH_FILE"], "/private/auth.json") + # The server requires and the proxy sends the shared server token. + for spawned in popen.call_args_list: + self.assertEqual(spawned.kwargs["env"]["PIPELINES_SERVER_TOKEN_FILE"], "/private/server-token") + self.assertEqual(manage.server_token_env({"web": None}), {}) web.terminate.assert_called_once() server.terminate.assert_called_once() ntfy.terminate.assert_called_once() diff --git a/scripts/test_setup_native.py b/scripts/test_setup_native.py index 8a74f682..4d2db223 100644 --- a/scripts/test_setup_native.py +++ b/scripts/test_setup_native.py @@ -257,6 +257,24 @@ class SetupNativeTests(unittest.TestCase): self.assertTrue((prefix / "lib/web/src/proxySecurity.js").is_file()) self.assertEqual(web["port"], 8082) + def test_server_token_is_created_private_once_beside_the_web_auth(self): + with tempfile.TemporaryDirectory() as directory: + installer = self.installer(directory, Recorder()) + prefix = Path(directory) / "prefix" + installer.prepare_layout(prefix) + with patch.object(setup_native.shutil, "which", side_effect=self.which_all()): + web = installer.configure_web(prefix, setup_native.arguments([])) + token = Path(web["server_token_file"]) + before = token.read_bytes() + installer.configure_web(prefix, setup_native.arguments([])) + self.assertEqual(token.parent, Path(web["auth_file"]).parent) + self.assertEqual(token.read_bytes(), before) + self.assertEqual(token.stat().st_mode & 0o777, 0o600) + self.assertGreaterEqual(len(before.strip()), 32) + token.chmod(0o644) + with self.assertRaises(ValueError): + installer.ensure_server_token(token) + def test_new_web_password_must_be_chosen(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) diff --git a/web/serve.mjs b/web/serve.mjs index 1f8e316d..dc82c191 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -13,8 +13,9 @@ // upgrade whose Origin host does not match the request Host — a browser would // get 403 from a cross-origin page (a Node/curl client sends no Origin and is // unaffected, which is why the smoke test passes). Proxying makes the page -// same-origin, and the Origin header is dropped on the way through because the -// server has no authentication and needs none. +// same-origin, and the Origin header is dropped on the way through (the server +// refuses any /connect carrying Origin). Every request to the server carries the +// shared server token (src/serverToken.js, docs/notes/security-boundary.md). // // The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN. import { createServer, request } from "node:http"; @@ -26,6 +27,7 @@ import { extname, join, normalize, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; import net from "node:net"; import { sameOriginUpgrade } from "./src/proxySecurity.js"; +import { withServerToken } from "./src/serverToken.js"; import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js"; // The proxy's own directory holds the sign-in page's icons. @@ -376,7 +378,13 @@ async function serveIndex(res) { * export would all 404 while the WebSocket worked. */ function proxyArtifact(req, res) { - const headers = { ...req.headers }; + let headers; + try { + headers = withServerToken(req.headers); + } catch { + res.writeHead(503, { "Content-Type": "text/plain; charset=utf-8" }).end("Server token unavailable"); + return; + } delete headers.origin; headers.host = `${targetHost}:${targetPort}`; const upstream = request( @@ -543,8 +551,14 @@ const targetPort = Number(pipelinesTarget.split(":").at(-1)); const targetHost = pipelinesTarget.slice(0, pipelinesTarget.lastIndexOf(":")) || "127.0.0.1"; function proxyUpgrade(req, clientSocket, clientHead) { + let headers; + try { + headers = withServerToken(req.headers); + } catch { + clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n"); + return; + } const upstream = net.connect(targetPort, targetHost, () => { - const headers = { ...req.headers }; delete headers.origin; // see the header comment: the server forbids a mismatched Origin headers.host = `${targetHost}:${targetPort}`; const block = Object.entries(headers) diff --git a/web/src/peerAddress.js b/web/src/peerAddress.js new file mode 100644 index 00000000..7e7f8250 --- /dev/null +++ b/web/src/peerAddress.js @@ -0,0 +1,139 @@ +// Which machines the peer relay may reach (docs/notes/security-boundary.md). +// +// A signed-in browser names other Pipelines machines by URL and this proxy +// fetches /login, /files, /artifacts, /updates and /connect from them, so an +// unrestricted address would let any signed-in user make this machine request +// its own loopback services, link-local cloud metadata or arbitrary hosts. +// Allowed by default: tailnet addresses (100.64.0.0/10 and fd7a:115c:a1e0::/48, +// minus Tailscale's own 100.100.100.100 service), private LANs (RFC 1918 and +// IPv6 unique-local fc00::/7), and public addresses only for *.ts.net names +// (Tailscale Funnel) or hosts this machine's own discovery reported. Loopback, +// unspecified, link-local, metadata, multicast and reserved addresses are always +// refused. PIPELINES_PEER_ALLOW=loopback,public,link-local,metadata (or "any") +// widens the policy explicitly. +// +// A name is resolved once per request and the connection is pinned to the +// checked address (the request's `lookup`), so a DNS answer cannot change +// between the check and the connect (rebinding). +import { lookup as dnsLookup } from "node:dns/promises"; +import net from "node:net"; + +const ALWAYS_REFUSED = new Set(["unspecified", "multicast", "reserved"]); +const OPTIONAL = new Set(["loopback", "public", "link-local", "metadata"]); + +// Hosts the local server's LAN discovery returned (proxyDiscovery), bounded. +export const discoveredHosts = new Set(); +export function rememberDiscovered(body) { + try { + for (const server of JSON.parse(body)?.discovered_servers ?? []) { + const host = new URL(server.endpoint).hostname.toLowerCase(); + if (discoveredHosts.size >= 256) discoveredHosts.delete(discoveredHosts.values().next().value); + discoveredHosts.add(host); + } + } catch {} +} + +function v4(address) { + const parts = address.split(".").map(Number); + return ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0; +} +const inV4 = (value, base, bits) => bits === 0 || value >>> (32 - bits) === v4(base) >>> (32 - bits); + +function classifyV4(address) { + const value = v4(address); + if (inV4(value, "0.0.0.0", 8)) return "unspecified"; + if (inV4(value, "127.0.0.0", 8)) return "loopback"; + if (value === v4("169.254.169.254") || value === v4("100.100.100.100")) return "metadata"; + if (inV4(value, "169.254.0.0", 16)) return "link-local"; + if (inV4(value, "100.64.0.0", 10)) return "tailnet"; + if (inV4(value, "10.0.0.0", 8) || inV4(value, "172.16.0.0", 12) || inV4(value, "192.168.0.0", 16)) return "private"; + if (inV4(value, "224.0.0.0", 4)) return "multicast"; + if (inV4(value, "240.0.0.0", 4)) return "reserved"; + return "public"; +} + +// The 8 hextets of an IPv6 literal (already validated by net.isIPv6). +function hextets(address) { + let text = address.toLowerCase().split("%")[0]; + const dotted = /(\d+\.\d+\.\d+\.\d+)$/.exec(text); + if (dotted) { + const value = v4(dotted[1]); + text = text.slice(0, -dotted[1].length) + `${(value >>> 16).toString(16)}:${(value & 0xffff).toString(16)}`; + } + const [head, tail] = text.split("::"); + const left = head ? head.split(":") : []; + const right = tail === undefined ? [] : tail ? tail.split(":") : []; + const fill = tail === undefined ? [] : Array(8 - left.length - right.length).fill("0"); + return [...left, ...fill, ...right].map((part) => parseInt(part, 16)); +} + +function classifyV6(address) { + const h = hextets(address); + if (h.every((part) => part === 0)) return "unspecified"; + if (h.slice(0, 7).every((part) => part === 0) && h[7] === 1) return "loopback"; + if (h.slice(0, 5).every((part) => part === 0) && h[5] === 0xffff) + return classifyV4(`${h[6] >> 8}.${h[6] & 255}.${h[7] >> 8}.${h[7] & 255}`); + if ((h[0] & 0xffc0) === 0xfe80) return "link-local"; + if ((h[0] & 0xff00) === 0xff00) return "multicast"; + if (h[0] === 0xfd7a && h[1] === 0x115c && h[2] === 0xa1e0) + return h.slice(3, 7).every((part) => part === 0) && h[7] === 0x53 ? "metadata" : "tailnet"; + // AWS's IPv6 instance metadata endpoint sits inside the unique-local range. + if (h[0] === 0xfd00 && h[1] === 0x0ec2) return "metadata"; + if ((h[0] & 0xfe00) === 0xfc00) return "private"; + return "public"; +} + +export function classify(address) { + if (net.isIPv4(address)) return classifyV4(address); + if (net.isIPv6(address)) return classifyV6(address); + return "reserved"; +} + +export function allowedCategories(env = process.env) { + const extra = String(env.PIPELINES_PEER_ALLOW ?? "") + .split(",") + .map((value) => value.trim().toLowerCase()) + .filter(Boolean); + const any = extra.includes("any"); + return new Set(["tailnet", "private", ...(any ? OPTIONAL : extra.filter((value) => OPTIONAL.has(value)))]); +} + +export class PeerAddressError extends Error {} + +export class PeerAddressPolicy { + constructor({ env = process.env, lookup = dnsLookup, discovered = discoveredHosts } = {}) { + this.allowed = allowedCategories(env); + this.lookup = lookup; + this.discovered = discovered; + } + permits(hostname, category) { + if (ALWAYS_REFUSED.has(category)) return false; + if (this.allowed.has(category)) return true; + if (category !== "public") return false; + return hostname.endsWith(".ts.net") || this.discovered.has(hostname); + } + // Resolve once, check every answer, and return a `lookup` that pins the + // request's connection to the first checked address. + async pin(url) { + const hostname = url.hostname.replace(/^\[|\]$/g, "").toLowerCase(); + const answers = net.isIP(hostname) + ? [{ address: hostname, family: net.isIP(hostname) }] + : await this.lookup(hostname, { all: true, verbatim: true }); + if (!answers.length) throw new PeerAddressError("Server address did not resolve"); + for (const { address } of answers) { + const category = classify(address); + if (!this.permits(hostname, category)) + throw new PeerAddressError(`Server address is not allowed (${category})`); + } + const { address, family } = answers[0]; + return { + address, + family, + lookup: (_name, options, callback) => { + const done = typeof options === "function" ? options : callback; + if (options?.all) done(null, [{ address, family }]); + else done(null, address, family); + }, + }; + } +} diff --git a/web/src/peerRegistry.js b/web/src/peerRegistry.js index 6af2a649..a6f65ca9 100644 --- a/web/src/peerRegistry.js +++ b/web/src/peerRegistry.js @@ -1,6 +1,12 @@ import { createHash } from "node:crypto"; import { authenticate, endpoint, json } from "./peerTransport.js"; import { sameOriginUpgrade } from "./proxySecurity.js"; +import { PeerAddressError, PeerAddressPolicy } from "./peerAddress.js"; + +// A password or session cookie sent to a ws:// endpoint crosses the network +// unencrypted (unless the path is a tailnet's WireGuard tunnel); the response +// says so, so the app can warn before the operator types a password. +const plaintext = (urls) => urls.some((url) => url.startsWith("ws:")); async function body(req) { const chunks = []; @@ -14,8 +20,9 @@ async function body(req) { } export class PeerRegistry { - constructor(scopeFor) { + constructor(scopeFor, policy = new PeerAddressPolicy()) { this.scopeFor = scopeFor; + this.policy = policy; this.sessions = new Map(); } owner(req) { @@ -82,6 +89,7 @@ export class PeerRegistry { json(res, 200, { connected: Boolean(peer?.connected), needs_password: !peer?.connected, + ...(peer && plaintext([peer.active]) ? { insecure_transport: true } : {}), }); return true; } @@ -113,6 +121,24 @@ export class PeerRegistry { urls.every((url) => previous.endpoints.includes(url)); const version = (owner.versions.get(id) ?? 0) + 1; owner.versions.set(id, version); + // Every address is checked (and its DNS answer pinned) before any + // request leaves this machine; a refused address is never contacted. + const pins = new Map(); + const refused = []; + await Promise.all( + urls.map(async (value) => { + try { + pins.set(value, await this.policy.pin(new URL(value))); + } catch (error) { + if (error instanceof PeerAddressError) + refused.push({ endpoint: value, error: error.message }); + } + }), + ); + if (refused.length === urls.length) { + json(res, 400, { error: refused[0].error, refused_endpoints: refused }); + return true; + } if (previous && !sameEndpoints) { for (const socket of previous.sockets) socket.destroy(); owner.peers.delete(id); @@ -127,6 +153,8 @@ export class PeerRegistry { const results = await Promise.all( urls.map(async (value) => { try { + const pin = pins.get(value); + if (!pin) return { connected: false, needs_password: false }; const old = previous?.endpoints.includes(value) ? (previous.cookies.get(value) ?? "") : ""; @@ -134,6 +162,7 @@ export class PeerRegistry { new URL(value), old, data.password, + pin.lookup, ); if (status.connected) { peer.cookies.set(value, status.cookie); @@ -163,6 +192,8 @@ export class PeerRegistry { owner.peers.set(id, peer); json(res, 200, { connected_endpoints: [...peer.cookies.keys()], + ...(plaintext(urls) ? { insecure_transport: true } : {}), + ...(refused.length ? { refused_endpoints: refused } : {}), connected: peer.connected, needs_password: !peer.connected && results.some((r) => r.needs_password), diff --git a/web/src/peerRelay.js b/web/src/peerRelay.js index 18525a84..77708ac3 100644 --- a/web/src/peerRelay.js +++ b/web/src/peerRelay.js @@ -6,6 +6,8 @@ import { json, } from "./peerTransport.js"; import { PeerRegistry } from "./peerRegistry.js"; +import { withServerToken } from "./serverToken.js"; +import { rememberDiscovered } from "./peerAddress.js"; // Peer files are untrusted content, never privileged control-UI documents. // Apply this even for older peers whose /files endpoint has no sandbox header. @@ -39,6 +41,22 @@ export class PeerRelay extends PeerRegistry { throw new Error("Remote origin changed"); return { url, cookie: peer.cookies.get(selected), peer }; } + // Resolve the peer once for this request, check the answer against the + // address policy (peerAddress.js) and connect only to that address. + pinned(target, reply, send) { + this.policy + .pin(target.url) + .then((pin) => { + if (!reply.destroyed) send(pin.lookup); + }) + .catch(() => { + if (typeof reply.writeHead !== "function") + reply.end("HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n"); + else if (!reply.headersSent) + json(reply, 403, { error: "Server address is not allowed" }); + else reply.destroy(); + }); + } http(req, res) { if (!req.url.startsWith("/peers/")) return false; try { @@ -52,59 +70,62 @@ export class PeerRelay extends PeerRegistry { json(res, 404, { error: "Unsupported remote resource" }); return true; } - const headers = { ...(target.cookie ? { cookie: target.cookie } : {}) }; - for (const name of [ - "content-type", - "content-length", - "range", - "x-file-name", - "x-upload-name", - "x-chat-file-name", - "x-pipelines-upload-name", - ]) - if (req.headers[name]) headers[name] = req.headers[name]; - const upstream = remoteRequest(target.url, { - method: req.method, - headers, - }); - upstream.setTimeout(30000, () => upstream.destroy()); - upstream.on("response", (response) => { - const safe = {}; + this.pinned(target, res, (lookup) => { + const headers = { ...(target.cookie ? { cookie: target.cookie } : {}) }; for (const name of [ "content-type", "content-length", - "content-range", - "accept-ranges", - "content-disposition", + "range", + "x-file-name", + "x-upload-name", + "x-chat-file-name", + "x-pipelines-upload-name", ]) - if (response.headers[name]) safe[name] = response.headers[name]; - res.writeHead( - response.statusCode >= 300 && response.statusCode < 400 - ? 502 - : response.statusCode, - { - ...safe, - "Cache-Control": "private, no-store", - // Multiple CSP policies intersect: preserve a peer's stricter - // restrictions while always imposing the local sandbox boundary. - "Content-Security-Policy": response.headers[ - "content-security-policy" - ] - ? [response.headers["content-security-policy"], contentPolicy] - : contentPolicy, - "X-Content-Type-Options": "nosniff", - }, - ); - response.pipe(res); + if (req.headers[name]) headers[name] = req.headers[name]; + const upstream = remoteRequest(target.url, { + method: req.method, + lookup, + headers, + }); + upstream.setTimeout(30000, () => upstream.destroy()); + upstream.on("response", (response) => { + const safe = {}; + for (const name of [ + "content-type", + "content-length", + "content-range", + "accept-ranges", + "content-disposition", + ]) + if (response.headers[name]) safe[name] = response.headers[name]; + res.writeHead( + response.statusCode >= 300 && response.statusCode < 400 + ? 502 + : response.statusCode, + { + ...safe, + "Cache-Control": "private, no-store", + // Multiple CSP policies intersect: preserve a peer's stricter + // restrictions while always imposing the local sandbox boundary. + "Content-Security-Policy": response.headers[ + "content-security-policy" + ] + ? [response.headers["content-security-policy"], contentPolicy] + : contentPolicy, + "X-Content-Type-Options": "nosniff", + }, + ); + response.pipe(res); + }); + upstream.on("error", () => { + if (!res.headersSent) + json(res, 502, { error: "Remote server unavailable" }); + else res.destroy(); + }); + req.on("aborted", () => upstream.destroy()); + res.on("close", () => upstream.destroy()); + req.pipe(upstream); }); - upstream.on("error", () => { - if (!res.headersSent) - json(res, 502, { error: "Remote server unavailable" }); - else res.destroy(); - }); - req.on("aborted", () => upstream.destroy()); - res.on("close", () => upstream.destroy()); - req.pipe(upstream); } catch { json(res, 401, { error: "Sign in to the remote server again" }); } @@ -122,47 +143,49 @@ export class PeerRelay extends PeerRegistry { "sec-websocket-version": "13", ...(target.cookie ? { cookie: target.cookie } : {}), }; - const upstream = remoteRequest(target.url, { headers }); - upstream.setTimeout(10000, () => upstream.destroy()); - upstream.on("upgrade", (response, remote, remoteHead) => { - socket.write( - `HTTP/1.1 101 Switching Protocols\r\n${Object.entries( - response.headers, - ) - .filter(([k]) => - [ - "upgrade", - "connection", - "sec-websocket-accept", - "sec-websocket-protocol", - ].includes(k), + this.pinned(target, socket, (lookup) => { + const upstream = remoteRequest(target.url, { headers, lookup }); + upstream.setTimeout(10000, () => upstream.destroy()); + upstream.on("upgrade", (response, remote, remoteHead) => { + socket.write( + `HTTP/1.1 101 Switching Protocols\r\n${Object.entries( + response.headers, ) - .map(([k, v]) => `${k}: ${v}`) - .join("\r\n")}\r\n\r\n`, - ); - target.peer.sockets.add(socket); - remote.setTimeout(0); - socket.once("close", () => { - target.peer.sockets.delete(socket); - remote.destroy(); + .filter(([k]) => + [ + "upgrade", + "connection", + "sec-websocket-accept", + "sec-websocket-protocol", + ].includes(k), + ) + .map(([k, v]) => `${k}: ${v}`) + .join("\r\n")}\r\n\r\n`, + ); + target.peer.sockets.add(socket); + remote.setTimeout(0); + socket.once("close", () => { + target.peer.sockets.delete(socket); + remote.destroy(); + }); + remote.once("close", () => socket.destroy()); + socket.on("error", () => remote.destroy()); + remote.on("error", () => socket.destroy()); + if (head?.length) remote.write(head); + if (remoteHead?.length) socket.write(remoteHead); + remote.pipe(socket); + socket.pipe(remote); }); - remote.once("close", () => socket.destroy()); - socket.on("error", () => remote.destroy()); - remote.on("error", () => socket.destroy()); - if (head?.length) remote.write(head); - if (remoteHead?.length) socket.write(remoteHead); - remote.pipe(socket); - socket.pipe(remote); + upstream.on("response", (response) => { + response.resume(); + socket.end( + `HTTP/1.1 ${response.statusCode === 401 ? 401 : 502} Remote Unavailable\r\nContent-Length: 0\r\n\r\n`, + ); + }); + upstream.on("error", () => socket.destroy()); + socket.once("close", () => upstream.destroy()); + upstream.end(); }); - upstream.on("response", (response) => { - response.resume(); - socket.end( - `HTTP/1.1 ${response.statusCode === 401 ? 401 : 502} Remote Unavailable\r\nContent-Length: 0\r\n\r\n`, - ); - }); - upstream.on("error", () => socket.destroy()); - socket.once("close", () => upstream.destroy()); - upstream.end(); } catch { socket.end("HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n"); } @@ -176,7 +199,9 @@ export async function proxyDiscovery(req, res, host, port) { new URL( `http://${host.includes(":") ? `[${host}]` : host}:${port}/discovery`, ), + { headers: withServerToken({}) }, ); + if (response.status === 200) rememberDiscovered(response.body); res .writeHead(response.status, { "Content-Type": "application/json", diff --git a/web/src/peerTransport.js b/web/src/peerTransport.js index ffcbc2f7..61a422e0 100644 --- a/web/src/peerTransport.js +++ b/web/src/peerTransport.js @@ -28,12 +28,14 @@ export function remoteRequest(url, options = {}) { // Never follows redirects or sends local browser cookies to another machine. export function boundedRequest( url, - { method = "GET", cookie = "", body = "", headers = {} } = {}, + { method = "GET", cookie = "", body = "", headers = {}, lookup } = {}, ) { return new Promise((resolve, reject) => { const request = remoteRequest(url, { method, headers: { ...headers, ...(cookie ? { Cookie: cookie } : {}) }, + // A pinned peer address (peerAddress.js) is connected to directly. + ...(lookup ? { lookup } : {}), }); const timer = setTimeout( () => request.destroy(new Error("Server timed out")), @@ -79,7 +81,7 @@ export function sessionCookie(response) { .join("; "); } -export async function authenticate(url, cookie, password) { +export async function authenticate(url, cookie, password, lookup) { const login = new URL("/login", webOrigin(url)); let response; if (password !== undefined) { @@ -88,6 +90,7 @@ export async function authenticate(url, cookie, password) { method: "POST", body, cookie, + lookup, headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": Buffer.byteLength(body), @@ -99,7 +102,7 @@ export async function authenticate(url, cookie, password) { return { connected: false, needs_password: true, cookie: "" }; } } - response = await boundedRequest(login, { cookie }); + response = await boundedRequest(login, { cookie, lookup }); if ([302, 303].includes(response.status)) return { connected: true, needs_password: false, cookie }; if ( @@ -111,7 +114,7 @@ export async function authenticate(url, cookie, password) { if (response.status === 200) return { connected: true, needs_password: false, cookie }; if (response.status === 404) { - const health = await boundedRequest(new URL("/healthz", webOrigin(url))); + const health = await boundedRequest(new URL("/healthz", webOrigin(url)), { lookup }); if (health.status === 200 && JSON.parse(health.body)?.healthy === true) return { connected: true, needs_password: false, cookie: "" }; } diff --git a/web/src/serverToken.js b/web/src/serverToken.js new file mode 100644 index 00000000..490974c8 --- /dev/null +++ b/web/src/serverToken.js @@ -0,0 +1,48 @@ +// The shared secret between this proxy and the local Pipelines server +// (internal/servertoken, docs/notes/security-boundary.md). The installer writes +// it to a private file beside web-auth.json; the server, when configured with +// it, refuses any request without it. Only requests to the local upstream carry +// it: the peer relay never forwards it to another machine. +import { lstatSync, readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; + +export const SERVER_TOKEN_HEADER = "x-pipelines-token"; + +export function serverTokenPath(env = process.env) { + if (env.PIPELINES_SERVER_TOKEN_FILE) return env.PIPELINES_SERVER_TOKEN_FILE; + return join(env.XDG_CONFIG_HOME || join(homedir(), ".config"), "pipelines", "server-token"); +} + +let cached = { key: "", token: "" }; + +// The token, re-read when the file changes; "" when no file exists. A file +// another user could read or replace is refused (same rule as the Go reader). +export function serverToken(path = serverTokenPath()) { + let info; + try { + info = lstatSync(path); + } catch { + return ""; + } + if (!info.isFile()) throw new Error(`server token ${path} is not a regular file`); + if (typeof process.getuid === "function" && (info.uid !== process.getuid() || info.mode & 0o077)) + throw new Error(`server token ${path} must be owned by this user and private (chmod 600)`); + const key = `${path}:${info.ino}:${info.mtimeMs}:${info.size}`; + if (cached.key !== key) { + const token = readFileSync(path, "utf8").trim(); + if (token.length < 32 || /\s/.test(token)) throw new Error(`server token ${path} is malformed`); + cached = { key, token }; + } + return cached.token; +} + +// Headers for the local upstream: any client-supplied token is dropped and the +// proxy's own is set, so the browser never chooses what the server sees. +export function withServerToken(headers, token = serverToken()) { + const result = { ...headers }; + for (const name of Object.keys(result)) + if (name.toLowerCase() === SERVER_TOKEN_HEADER) delete result[name]; + if (token) result[SERVER_TOKEN_HEADER] = token; + return result; +} diff --git a/web/test/peerAddress.test.mjs b/web/test/peerAddress.test.mjs new file mode 100644 index 00000000..ebe5ece6 --- /dev/null +++ b/web/test/peerAddress.test.mjs @@ -0,0 +1,175 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import { once } from "node:events"; +import { spawn } from "node:child_process"; +import { mkdtemp, writeFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { classify, PeerAddressPolicy, PeerAddressError } from "../src/peerAddress.js"; +import { remoteRequest } from "../src/peerTransport.js"; + +const fakeDns = (answers) => { + const calls = []; + const lookup = async (name) => { + calls.push(name); + const value = answers[name]; + return Array.isArray(value) ? value : value === undefined ? [] : [value]; + }; + return { lookup, calls }; +}; +const v4 = (address) => ({ address, family: 4 }); +const v6 = (address) => ({ address, family: 6 }); + +test("addresses are classified by range", () => { + const cases = { + "127.0.0.1": "loopback", "127.8.9.10": "loopback", "::1": "loopback", "::ffff:127.0.0.1": "loopback", + "0.0.0.0": "unspecified", "::": "unspecified", + "169.254.169.254": "metadata", "100.100.100.100": "metadata", "fd7a:115c:a1e0::53": "metadata", "fd00:ec2::254": "metadata", + "169.254.10.1": "link-local", "fe80::1": "link-local", + "100.64.0.1": "tailnet", "100.127.255.254": "tailnet", "fd7a:115c:a1e0:ab12::1": "tailnet", + "10.1.2.3": "private", "172.16.0.1": "private", "172.31.255.255": "private", "192.168.1.2": "private", "fd12:3456::1": "private", + "172.32.0.1": "public", "100.128.0.1": "public", "8.8.8.8": "public", "2606:4700::1111": "public", "::ffff:8.8.8.8": "public", + "224.0.0.251": "multicast", "ff02::fb": "multicast", "255.255.255.255": "reserved", + }; + for (const [address, category] of Object.entries(cases)) assert.equal(classify(address), category, address); +}); + +test("loopback, metadata, link-local and public peers are refused by default", async () => { + const dns = fakeDns({ "evil.example": v4("127.0.0.1"), "public.example": v4("93.184.216.34") }); + const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup, discovered: new Set() }); + for (const url of [ + "ws://127.0.0.1:8080/connect", "ws://localhost.:8080/connect", "ws://[::1]:8083/connect", + "ws://169.254.169.254/connect", "ws://[fe80::1]:8083/connect", "ws://0.0.0.0:8083/connect", + "ws://100.100.100.100/connect", "ws://evil.example:8083/connect", "ws://public.example:8083/connect", + "ws://8.8.8.8:8083/connect", "ws://unresolved.example/connect", + ]) { + await assert.rejects(policy.pin(new URL(url)), PeerAddressError, url); + } +}); + +test("tailnet, LAN, ts.net and discovered peers are accepted", async () => { + const dns = fakeDns({ + "box.tail1234.ts.net": v4("100.101.102.103"), + "funnel.tail1234.ts.net": v4("203.0.113.9"), + "desktop.local": [v4("192.168.1.20"), v6("fd12::20")], + "lab.example": v4("198.51.100.7"), + }); + const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup, discovered: new Set(["lab.example"]) }); + for (const [url, address] of [ + ["ws://100.64.1.2:8083/connect", "100.64.1.2"], + ["wss://box.tail1234.ts.net/connect", "100.101.102.103"], + ["wss://funnel.tail1234.ts.net/connect", "203.0.113.9"], + ["ws://desktop.local:8083/connect", "192.168.1.20"], + ["ws://10.0.0.75:8083/connect", "10.0.0.75"], + ["ws://[fd7a:115c:a1e0::1]:8083/connect", "fd7a:115c:a1e0::1"], + ["ws://lab.example:8083/connect", "198.51.100.7"], + ]) + assert.equal((await policy.pin(new URL(url))).address, address, url); + // A name that also resolves to a refused address is refused outright. + const mixed = new PeerAddressPolicy({ env: {}, lookup: fakeDns({ "mixed.local": [v4("192.168.1.2"), v4("127.0.0.1")] }).lookup }); + await assert.rejects(mixed.pin(new URL("ws://mixed.local/connect")), PeerAddressError); + // Discovery never unlocks loopback or metadata. + const discovered = new PeerAddressPolicy({ env: {}, lookup: fakeDns({ "self.local": v4("127.0.0.1") }).lookup, discovered: new Set(["self.local"]) }); + await assert.rejects(discovered.pin(new URL("ws://self.local/connect")), PeerAddressError); +}); + +test("PIPELINES_PEER_ALLOW widens the policy explicitly", async () => { + const loopback = new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "loopback" } }); + assert.equal((await loopback.pin(new URL("ws://127.0.0.1:1/connect"))).address, "127.0.0.1"); + await assert.rejects(loopback.pin(new URL("ws://169.254.169.254/connect")), PeerAddressError); + const any = new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "any" } }); + assert.equal((await any.pin(new URL("ws://169.254.169.254/connect"))).address, "169.254.169.254"); + await assert.rejects(any.pin(new URL("ws://0.0.0.0/connect")), PeerAddressError); +}); + +test("the checked DNS answer is pinned for the request (no rebinding)", async (t) => { + const answers = { "peer.example": v4("192.168.1.9") }; + const dns = fakeDns(answers); + const policy = new PeerAddressPolicy({ env: {}, lookup: dns.lookup }); + const pin = await policy.pin(new URL("ws://peer.example:8083/connect")); + // The name now "rebinds" to loopback; the pinned lookup still answers the checked address. + answers["peer.example"] = v4("127.0.0.1"); + const one = await new Promise((done) => pin.lookup("peer.example", {}, (e, a, f) => done([a, f]))); + const all = await new Promise((done) => pin.lookup("peer.example", { all: true }, (e, list) => done(list))); + assert.deepEqual(one, ["192.168.1.9", 4]); + assert.deepEqual(all, [{ address: "192.168.1.9", family: 4 }]); + assert.deepEqual(dns.calls, ["peer.example"]); + // A real request connects to the pinned address while naming the original host. + const server = createServer((req, res) => res.end(req.headers.host)); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + t.after(() => server.close()); + const port = server.address().port; + const pinnedLoopback = await new PeerAddressPolicy({ env: { PIPELINES_PEER_ALLOW: "loopback" }, lookup: fakeDns({ "never-resolves.invalid": v4("127.0.0.1") }).lookup }) + .pin(new URL(`ws://never-resolves.invalid:${port}/connect`)); + const host = await new Promise((done, fail) => { + const request = remoteRequest(new URL(`http://never-resolves.invalid:${port}/`), { lookup: pinnedLoopback.lookup }); + request.on("response", async (response) => { + let body = ""; + for await (const chunk of response) body += chunk; + done(body); + }); + request.on("error", fail); + request.end(); + }); + assert.equal(host, `never-resolves.invalid:${port}`); +}); + +test("the relay refuses a loopback peer without contacting it and flags plain http", async (t) => { + const seen = []; + const remote = createServer((req, res) => { + seen.push(req.url); + res.writeHead(302, { Location: "/" }).end(); + }); + remote.listen(0, "127.0.0.1"); + await once(remote, "listening"); + t.after(() => remote.close()); + const dir = await mkdtemp(join(tmpdir(), "peer-address-")); + t.after(() => rm(dir, { recursive: true, force: true })); + const auth = join(dir, "auth.json"); + await writeFile(auth, JSON.stringify({ password: "local-password", secret: "s".repeat(40) })); + const reserve = createServer().listen(0, "127.0.0.1"); + await once(reserve, "listening"); + const port = reserve.address().port; + await new Promise((done) => reserve.close(done)); + const start = async (allow) => { + const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", `127.0.0.1:${remote.address().port}`], { + cwd: fileURLToPath(new URL("..", import.meta.url)), + env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth, PIPELINES_PEER_ALLOW: allow, PIPELINES_SERVER_TOKEN_FILE: join(dir, "absent") }, + stdio: ["ignore", "pipe", "inherit"], + }); + await once(child.stdout, "data"); + const base = `http://127.0.0.1:${port}`; + const login = await fetch(base + "/login", { method: "POST", body: new URLSearchParams({ password: "local-password" }), redirect: "manual" }); + const cookie = login.headers.get("set-cookie").split(";")[0]; + const register = (endpoints) => + fetch(base + "/api/peers/p", { + method: "POST", + headers: { Cookie: cookie, Origin: base, "Content-Type": "application/json" }, + body: JSON.stringify({ endpoints, password: "remote-password" }), + }); + return { child, register }; + }; + const strict = await start(""); + for (const endpoint of [`ws://127.0.0.1:${remote.address().port}/connect`, "ws://169.254.169.254/connect", `ws://[::1]:${remote.address().port}/connect`]) { + const reply = await strict.register([endpoint]); + assert.equal(reply.status, 400, endpoint); + const body = await reply.json(); + assert.match(body.error, /not allowed/); + assert.equal(body.refused_endpoints[0].endpoint, endpoint); + } + assert.deepEqual(seen, []); + const stopped = once(strict.child, "exit"); + strict.child.kill(); + await stopped; + const allowed = await start("loopback"); + t.after(() => allowed.child.kill()); + const reply = await allowed.register([`ws://127.0.0.1:${remote.address().port}/connect`]); + const body = await reply.json(); + assert.equal(reply.status, 200); + assert.equal(body.connected, true); + assert.equal(body.insecure_transport, true); + assert.ok(seen.includes("/login")); +}); diff --git a/web/test/peerRelay.test.mjs b/web/test/peerRelay.test.mjs index 77aa60e7..e6cabd53 100644 --- a/web/test/peerRelay.test.mjs +++ b/web/test/peerRelay.test.mjs @@ -175,6 +175,8 @@ async function fixture(t) { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth, + // The fixture machines listen on loopback, which the relay refuses by default. + PIPELINES_PEER_ALLOW: "loopback", }, stdio: ["ignore", "pipe", "inherit"], }, diff --git a/web/test/serverToken.test.mjs b/web/test/serverToken.test.mjs new file mode 100644 index 00000000..1715f274 --- /dev/null +++ b/web/test/serverToken.test.mjs @@ -0,0 +1,109 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createServer, request } from "node:http"; +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import { spawn } from "node:child_process"; +import { mkdtemp, writeFile, chmod, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { serverToken, withServerToken } from "../src/serverToken.js"; + +const TOKEN = "proxy-token-0123456789abcdef0123456789abcdef"; + +async function freePort() { + const reserve = createServer().listen(0, "127.0.0.1"); + await once(reserve, "listening"); + const port = reserve.address().port; + await new Promise((done) => reserve.close(done)); + return port; +} + +test("the token file must be private and is never taken from the client", async (t) => { + const dir = await mkdtemp(join(tmpdir(), "server-token-")); + t.after(() => rm(dir, { recursive: true, force: true })); + const path = join(dir, "server-token"); + assert.equal(serverToken(path), ""); + await writeFile(path, TOKEN + "\n", { mode: 0o600 }); + await chmod(path, 0o600); + assert.equal(serverToken(path), TOKEN); + await chmod(path, 0o644); + assert.throws(() => serverToken(path), /private/); + assert.deepEqual(withServerToken({ "X-Pipelines-Token": "browser-chosen", a: "b" }, TOKEN), { a: "b", "x-pipelines-token": TOKEN }); + assert.deepEqual(withServerToken({ "x-pipelines-token": "browser-chosen" }, ""), {}); +}); + +test("the proxy sends the server token on every upstream request and the relay never forwards it", async (t) => { + const dir = await mkdtemp(join(tmpdir(), "server-token-")); + t.after(() => rm(dir, { recursive: true, force: true })); + const tokenFile = join(dir, "server-token"); + await writeFile(tokenFile, TOKEN, { mode: 0o600 }); + await chmod(tokenFile, 0o600); + const seen = []; + const upstream = createServer((req, res) => { + seen.push({ path: req.url.split("?")[0], token: req.headers["x-pipelines-token"], host: req.headers.host, origin: req.headers.origin }); + if (req.url === "/discovery") { + res.setHeader("Content-Type", "application/json"); + res.end(JSON.stringify({ discovered_servers: [] })); + return; + } + if (req.url === "/login") { + res.writeHead(302, { Location: "/" }).end(); + return; + } + res.end("ok"); + }); + upstream.on("upgrade", (req, socket) => { + seen.push({ path: req.url, token: req.headers["x-pipelines-token"], host: req.headers.host, origin: req.headers.origin }); + const accept = createHash("sha1").update(req.headers["sec-websocket-key"] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64"); + socket.end(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + t.after(() => { upstream.closeAllConnections(); upstream.close(); }); + const upstreamHost = `127.0.0.1:${upstream.address().port}`; + const port = await freePort(); + const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", upstreamHost], { + cwd: fileURLToPath(new URL("..", import.meta.url)), + env: { ...process.env, PIPELINES_WEB_AUTH: "disabled", PIPELINES_SERVER_TOKEN_FILE: tokenFile, PIPELINES_PEER_ALLOW: "loopback" }, + stdio: ["ignore", "pipe", "inherit"], + }); + t.after(() => child.kill()); + await once(child.stdout, "data"); + const base = `http://127.0.0.1:${port}`; + const forged = { "X-Pipelines-Token": "browser-chosen" }; + assert.equal((await fetch(`${base}/files?path=a.txt`, { headers: forged })).status, 200); + assert.equal((await fetch(`${base}/artifacts/1/1/a.txt`, { headers: forged })).status, 200); + assert.equal((await fetch(`${base}/chat-uploads`, { method: "POST", headers: forged, body: "x" })).status, 200); + assert.equal((await fetch(`${base}/api/discovery`, { headers: forged })).status, 200); + const upgraded = await new Promise((done, fail) => { + const req = request(`${base}/connect`, { + headers: { Connection: "Upgrade", Upgrade: "websocket", "Sec-WebSocket-Version": "13", "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==", Origin: base, ...forged }, + }); + req.on("upgrade", (response, socket) => { socket.destroy(); done(response.statusCode); }); + req.on("response", (response) => done(response.statusCode)); + req.on("error", fail); + req.end(); + }); + assert.equal(upgraded, 101); + const local = seen.filter((entry) => entry.path !== "/login"); + assert.deepEqual(local.map((entry) => entry.path), ["/files", "/artifacts/1/1/a.txt", "/chat-uploads", "/discovery", "/connect"]); + for (const entry of local) { + assert.equal(entry.token, TOKEN, entry.path); + assert.equal(entry.host, upstreamHost, entry.path); + assert.equal(entry.origin, undefined, entry.path); + } + // A peer machine (here the same fixture, reached through the relay) never + // receives this machine's server token. + seen.length = 0; + const reg = await fetch(`${base}/api/peers/peer`, { + method: "POST", + headers: { Origin: base, "Content-Type": "application/json" }, + body: JSON.stringify({ endpoints: [`ws://${upstreamHost}/connect`] }), + }); + assert.equal((await reg.json()).connected, true); + assert.equal((await fetch(`${base}/peers/peer/files?path=a.txt`, { headers: forged })).status, 200); + assert.ok(seen.length >= 2); + for (const entry of seen) assert.equal(entry.token, undefined, entry.path); +});