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>
82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/agentenv"
|
|
"git.a-wan.net/alan/pipelines/internal/workenv"
|
|
"git.a-wan.net/alan/pipelines/internal/workenv/workenvtest"
|
|
)
|
|
|
|
func plantServerSecrets(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("PIPELINES_SERVER_TOKEN_FILE", "/secret/server-token")
|
|
t.Setenv("PIPELINES_WEB_AUTH_FILE", "/secret/web-auth.json")
|
|
t.Setenv("PIPELINES_UPDATE_SIGNING_KEY", "/secret/update-signing-key.pem")
|
|
}
|
|
|
|
func assertNoSecretLines(t *testing.T, what, output string) {
|
|
t.Helper()
|
|
if leaks := agentenv.Leaks(strings.Split(strings.ReplaceAll(output, "\r", ""), "\n")); len(leaks) > 0 {
|
|
t.Fatalf("%s leaks Pipelines secrets: %v", what, leaks)
|
|
}
|
|
if !strings.Contains(output, "PATH=") {
|
|
t.Fatalf("%s lost PATH: %q", what, output)
|
|
}
|
|
}
|
|
|
|
// `!` shell lines run without Pipelines' own secret pointers.
|
|
func TestChatShellCommandEnvironmentIsScrubbed(t *testing.T) {
|
|
plantServerSecrets(t)
|
|
cmd := exec.Command("sh", "-c", "env")
|
|
reader, _, err := startChatShellCommand(cmd, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
output, _ := io.ReadAll(reader) // a pseudo-terminal ends with EIO
|
|
reader.Close()
|
|
_ = cmd.Wait()
|
|
assertNoSecretLines(t, "chat shell command", string(output))
|
|
}
|
|
|
|
// A delivery gate's check command runs without them too.
|
|
func TestDeliveryCheckEnvironmentIsScrubbed(t *testing.T) {
|
|
plantServerSecrets(t)
|
|
err := runDeliveryCheck(context.Background(), t.TempDir(), "env; exit 1")
|
|
if err == nil {
|
|
t.Fatal("the failing check passed")
|
|
}
|
|
assertNoSecretLines(t, "delivery check", err.Error())
|
|
}
|
|
|
|
// A run's resolved workspace environment, the base of every stage process,
|
|
// check and OpenCode stage service, starts from the scrubbed server
|
|
// environment.
|
|
func TestRunEnvironmentStartsScrubbed(t *testing.T) {
|
|
workenvtest.InstallDirenv(t)
|
|
plantServerSecrets(t)
|
|
project, _ := filepath.EvalSymlinks(t.TempDir())
|
|
workenvtest.WriteEnvrc(t, project, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("from-envrc")})
|
|
ctx, r, _ := concurrentStore(t)
|
|
r.dirEnv = &workenv.Resolver{}
|
|
env, _ := workenv.FromContext(r.withRunEnvironment(ctx, 1, project))
|
|
if !env.Applies() {
|
|
t.Fatalf("the workspace environment did not apply: %+v", env)
|
|
}
|
|
if leaks := agentenv.Leaks(env.Env); len(leaks) > 0 {
|
|
t.Fatalf("run environment leaks %v", leaks)
|
|
}
|
|
if value, _ := envValue(env.Env, "SCRATCH_VAR"); value != "from-envrc" {
|
|
t.Fatalf("workspace variable missing: %v", env.Env)
|
|
}
|
|
if _, ok := envValue(env.Env, "HOME"); !ok && os.Getenv("HOME") != "" {
|
|
t.Fatal("HOME dropped")
|
|
}
|
|
}
|