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:
@@ -0,0 +1,8 @@
|
||||
//go:build !unix
|
||||
|
||||
package servertoken
|
||||
|
||||
import "os"
|
||||
|
||||
// Windows has no POSIX mode bits; the file lives in the user's profile.
|
||||
func private(info os.FileInfo) bool { return true }
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build unix
|
||||
|
||||
package servertoken
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func private(info os.FileInfo) bool {
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
return ok && int(stat.Uid) == os.Getuid() && info.Mode().Perm()&0o077 == 0
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//go:build unix
|
||||
|
||||
package servertoken
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadAcceptsOnlyAPrivateRegularFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "server-token")
|
||||
token := strings.Repeat("t", 48)
|
||||
if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := Read(path); err != nil || got != token {
|
||||
t.Fatalf("Read = %q, %v", got, err)
|
||||
}
|
||||
if err := os.Chmod(path, 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Read(path); err == nil {
|
||||
t.Fatal("group-readable token accepted")
|
||||
}
|
||||
os.Chmod(path, 0o600)
|
||||
link := filepath.Join(dir, "link")
|
||||
os.Symlink(path, link)
|
||||
if _, err := Read(link); err == nil {
|
||||
t.Fatal("symlinked token accepted")
|
||||
}
|
||||
short := filepath.Join(dir, "short")
|
||||
os.WriteFile(short, []byte("short"), 0o600)
|
||||
if _, err := Read(short); err == nil {
|
||||
t.Fatal("short token accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqualIsExact(t *testing.T) {
|
||||
if Equal("", "") || Equal("a", "") || !Equal("abc", "abc") || Equal("abc", "abd") {
|
||||
t.Fatal("Equal mismatch")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user