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

169 lines
5.5 KiB
Go

package installation
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestCompiledCommandFindsManifestThroughSystemLink(t *testing.T) {
t.Setenv("PIPELINES_PREFIX", "")
root, _ := filepath.EvalSymlinks(t.TempDir())
os.Mkdir(filepath.Join(root, "bin"), 0700)
binary := filepath.Join(root, "bin/pipelines")
os.WriteFile(binary, []byte("binary"), 0700)
config := Config{Format: 1, Prefix: root, ServerPort: 18080, OpenCode: "/host/opencode", Path: "/usr/bin"}
data, _ := json.Marshal(config)
os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600)
link := filepath.Join(t.TempDir(), "pipelines")
os.Symlink(binary, link)
t.Setenv("PIPELINES_PREFIX", root)
loaded, err := Load(link)
if err != nil || loaded == nil || loaded.Prefix != root {
t.Fatal(loaded, err)
}
t.Setenv("PIPELINES_URL", "")
t.Setenv("PIPELINES_OPENCODE", "")
t.Setenv("PATH", os.Getenv("PATH"))
if err = loaded.Environment(); err != nil {
t.Fatal(err)
}
if os.Getenv("PIPELINES_URL") != "ws://127.0.0.1:18080/connect" {
t.Fatal("wrong installation selected")
}
t.Setenv("PIPELINES_URL", "ws://explicit/connect")
loaded.Environment()
if os.Getenv("PIPELINES_URL") != "ws://explicit/connect" {
t.Fatal("explicit server overridden")
}
}
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")
os.WriteFile(binary, []byte("binary"), 0700)
config, err := Load(binary)
if err != nil || config != nil {
t.Fatal(config, err)
}
}
func nativeFixture(t *testing.T) (string, string) {
t.Helper()
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
if err = os.Mkdir(filepath.Join(root, "bin"), 0700); err != nil {
t.Fatal(err)
}
binary := filepath.Join(root, "bin", "pipelines")
if err = os.WriteFile(binary, []byte("binary"), 0700); err != nil {
t.Fatal(err)
}
config := Config{Format: 1, Prefix: root, ServerPort: 18080, Python: filepath.Join(root, "python3")}
config.Ntfy.Mode = "native"
data, _ := json.Marshal(config)
if err = os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600); err != nil {
t.Fatal(err)
}
return root, binary
}
func TestNativeExecutableDiscoveryAndExplicitPrefixPrecedence(t *testing.T) {
t.Setenv("PIPELINES_PREFIX", "")
root, binary := nativeFixture(t)
link := filepath.Join(t.TempDir(), "pipelines")
if err := os.Symlink(binary, link); err != nil {
t.Fatal(err)
}
loaded, err := Load(link)
if err != nil || loaded == nil || loaded.Prefix != root {
t.Fatal("native executable not selected", loaded, err)
}
other, _ := nativeFixture(t)
t.Setenv("PIPELINES_PREFIX", other)
loaded, err = Load(link)
if err != nil || loaded == nil || loaded.Prefix != other {
t.Fatal("explicit selection lost precedence", loaded, err)
}
}
func TestDiscoveryNeverGuessesHomeOrLegacyInstallation(t *testing.T) {
t.Setenv("PIPELINES_PREFIX", "")
root, binary := nativeFixture(t)
home := t.TempDir()
t.Setenv("HOME", home)
if err := os.MkdirAll(filepath.Join(home, ".local", "share"), 0700); err != nil {
t.Fatal(err)
}
if err := os.Symlink(root, filepath.Join(home, ".local", "share", "pipelines")); err != nil {
t.Fatal(err)
}
unrelated := filepath.Join(t.TempDir(), "pipelines")
if err := os.WriteFile(unrelated, []byte("binary"), 0700); err != nil {
t.Fatal(err)
}
loaded, err := Load(unrelated)
if err != nil || loaded != nil {
t.Fatal("unrelated executable selected installation", loaded, err)
}
config := Config{Format: 1, Prefix: root, ServerPort: 18080}
data, _ := json.Marshal(config)
os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600)
loaded, err = Load(binary)
if err != nil || loaded != nil {
t.Fatal("legacy manifest was inferred", loaded, err)
}
t.Setenv("PIPELINES_PREFIX", root)
loaded, err = Load(binary)
if err != nil || loaded == nil {
t.Fatal("explicit legacy selection stopped working", loaded, err)
}
}
func TestNativeDiscoveryRejectsSymlinkManifestAndMismatchedPrefix(t *testing.T) {
for _, test := range []string{"symlink", "mismatch"} {
t.Run(test, func(t *testing.T) {
t.Setenv("PIPELINES_PREFIX", "")
root, binary := nativeFixture(t)
manifest := filepath.Join(root, "pipelines-install.json")
if test == "symlink" {
target := filepath.Join(root, "other.json")
os.Rename(manifest, target)
os.Symlink(target, manifest)
} else {
config := Config{Format: 1, Prefix: "/another/prefix", ServerPort: 18080}
config.Ntfy.Mode = "native"
data, _ := json.Marshal(config)
os.WriteFile(manifest, data, 0600)
}
if _, err := Load(binary); err == nil {
t.Fatal("unsafe manifest was accepted")
}
})
}
}