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:
2026-11-10 00:59:39 +00:00
parent a706739088
commit 84f7e33872
13 changed files with 585 additions and 5 deletions
+6 -2
View File
@@ -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 {
@@ -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")