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

92 lines
3.0 KiB
Go

// 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
}