84f7e33872
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.
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
// Package installation binds the compiled CLI to its per-user managed services.
|
|
package installation
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Format int `json:"format"`
|
|
Prefix string `json:"prefix"`
|
|
Python string `json:"python"`
|
|
ServerPort int `json:"server_port"`
|
|
OpenCode string `json:"opencode"`
|
|
OpenCodeEnv map[string]string `json:"opencode_env"`
|
|
Path string `json:"path"`
|
|
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
|
|
// executable. It never guesses a default prefix for an unrelated Go-installed CLI.
|
|
func Load(executable string) (*Config, error) {
|
|
prefix := os.Getenv("PIPELINES_PREFIX")
|
|
inferred := prefix == ""
|
|
if prefix == "" {
|
|
resolved, err := filepath.EvalSymlinks(executable)
|
|
if err != nil || filepath.Base(resolved) != "pipelines" || filepath.Base(filepath.Dir(resolved)) != "bin" {
|
|
return nil, nil
|
|
}
|
|
prefix = filepath.Dir(filepath.Dir(resolved))
|
|
}
|
|
prefix, err := filepath.Abs(prefix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The native installer and scripts/manage.py record the symlink-resolved
|
|
// prefix, so resolve the same way here instead of rejecting a symlinked
|
|
// HOME or prefix as "invalid".
|
|
if resolved, err := filepath.EvalSymlinks(prefix); err == nil {
|
|
prefix = resolved
|
|
}
|
|
manifest := filepath.Join(prefix, "pipelines-install.json")
|
|
info, err := os.Lstat(manifest)
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !info.Mode().IsRegular() || !ownedByUser(info) {
|
|
return nil, fmt.Errorf("refusing installation manifest not a regular file owned by this user")
|
|
}
|
|
data, err := os.ReadFile(manifest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var config Config
|
|
if err = json.Unmarshal(data, &config); err != nil {
|
|
return nil, err
|
|
}
|
|
if config.Format != 1 || config.Prefix != prefix || config.ServerPort < 1024 || config.ServerPort > 65535 {
|
|
return nil, fmt.Errorf("invalid managed installation manifest")
|
|
}
|
|
if inferred && config.Ntfy.Mode != "native" {
|
|
return nil, nil
|
|
}
|
|
if inferred {
|
|
binary, err := os.Stat(filepath.Join(prefix, "bin", "pipelines"))
|
|
if err != nil || !binary.Mode().IsRegular() || binary.Mode().Perm()&0111 == 0 || !ownedByUser(binary) {
|
|
return nil, fmt.Errorf("native installation executable must be owned by this user")
|
|
}
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
func (c Config) Environment() error {
|
|
for key, value := range c.OpenCodeEnv {
|
|
switch key {
|
|
case "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "OPENCODE_SERVER", "OPENCODE_DB":
|
|
if err := os.Setenv(key, value); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
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 {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Service(action string) bool {
|
|
switch action {
|
|
case "start", "stop", "restart", "doctor", "logs", "uninstall", "update":
|
|
return true
|
|
}
|
|
return false
|
|
}
|