Files
pipelines/internal/agentenv/agentenv.go
T
alan b24fc31761 Scrub Pipelines secret pointers from agent environments (H3, layer 1)
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>
2026-12-03 23:16:57 +00:00

86 lines
2.9 KiB
Go

// Package agentenv builds the environment of every process the server starts
// on an agent's behalf: harness processes (stages and chats), `!` shell lines,
// check-stage commands and the tools those spawn.
//
// The server's own environment carries pointers to Pipelines' secrets (the
// server token, the web sign-in file with the cookie key, the update-signing
// key, harness service tokens). An agent has no use for them, and a
// prompt-injected one could follow them, so Scrub drops every such variable
// while keeping everything a harness legitimately needs (PATH, HOME, its own
// config directory and credentials, and the workspace's direnv/mise output).
//
// This only removes the pointers. The files themselves stay readable by the
// server's OS user: agents intentionally keep full host access (see
// docs/security-review.md, "Decisions").
// The operator's interactive terminal is deliberately out of scope.
package agentenv
import (
"os"
"strings"
)
// secretMarkers are name fragments that mark a PIPELINES_ variable as a
// secret, a credential or the path to one.
var secretMarkers = []string{"TOKEN", "SECRET", "PASSWORD", "PASSWD", "AUTH", "CREDENTIAL", "PRIVATE", "SIGNING", "KEY"}
// publicSuffixes name PIPELINES_ variables that match a marker but carry no
// secret (a public verification key).
var publicSuffixes = []string{"_PUBLIC_KEY"}
// Secret reports whether the variable name must not reach an agent process:
// any PIPELINES_ variable naming a token, password, auth file or key.
func Secret(name string) bool {
upper := strings.ToUpper(name)
if !strings.HasPrefix(upper, "PIPELINES_") {
return false
}
for _, suffix := range publicSuffixes {
if strings.HasSuffix(upper, suffix) {
return false
}
}
rest := strings.TrimPrefix(upper, "PIPELINES_")
for _, marker := range secretMarkers {
if strings.Contains(rest, marker) {
return true
}
}
return false
}
// Scrub returns env without the secret variables. A nil env means "inherit
// the server's environment" (exec.Cmd's convention), so it scrubs
// os.Environ() instead: a command given the result never falls back to the
// unscrubbed server environment. The input slice is not modified.
func Scrub(env []string) []string {
if env == nil {
env = os.Environ()
}
out := make([]string, 0, len(env))
for _, entry := range env {
name, _, _ := strings.Cut(entry, "=")
if Secret(name) {
continue
}
out = append(out, entry)
}
return out
}
// Environ is the server's environment without secrets: the base for any agent
// process that would otherwise inherit os.Environ().
func Environ() []string { return Scrub(nil) }
// Leaks returns the secret entries left in env, for tests and diagnostics.
func Leaks(env []string) []string {
var leaks []string
for _, entry := range env {
name, _, _ := strings.Cut(entry, "=")
if Secret(name) {
leaks = append(leaks, entry)
}
}
return leaks
}