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") } }