b24fc31761
New internal/agentenv drops every PIPELINES_* variable naming a token, secret, password, auth file or key (except *_PUBLIC_KEY) from the environment of processes started for agents: Claude stages and chats (direct and hosted), Pi stages, OpenCode stage CLI calls and serve processes, check commands, delivery-gate checks, `!` shell lines, the run's workspace environment and turn-diff git snapshots. MCP servers are children of the harness and inherit its scrubbed environment. The interactive terminal is unchanged. An agent's `pipelines` CLI call falls back to the default server-token location. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.1 KiB
Go
94 lines
3.1 KiB
Go
// Package servertoken is the shared secret between the Pipelines server and its
|
|
// local clients (the authenticated web proxy, the CLI and the TUI). Processes
|
|
// the server launches for agents do not inherit PIPELINES_SERVER_TOKEN_FILE
|
|
// (internal/agentenv); a `pipelines` CLI call from an agent finds the token
|
|
// only at the default location.
|
|
//
|
|
// 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
|
|
}
|