Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-4952: pass agent custom env secrets to Codex shells

Multica Evef738e8ca1fc5
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index e87493794e..16e214b1bd 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -4278,7 +4278,7 @@ func (d *Daemon) runTask(ctx context.Context, task Task, provider string, slot i
agentCustomEnv = task.Agent.CustomEnv
}
layerCustomEnvAndHermesHome(agentEnv, agentCustomEnv, env.HermesHome, d.logger)
- if err := configureCodexTaskShellEnvironment(provider, env.CodexHome, os.Environ(), agentEnv, d.logger); err != nil {
+ if err := configureCodexTaskShellEnvironment(provider, env.CodexHome, os.Environ(), agentEnv, agentCustomEnv, d.logger); err != nil {
return TaskResult{}, err
}
backend, err := agent.New(provider, agent.Config{
@@ -5409,20 +5409,37 @@ func layerCustomEnvAndHermesHome(agentEnv, customEnv map[string]string, overlayH
}
}
+// codexShellAuthorizedCustomEnvNames returns names from the current agent's
+// custom_env that pass the same daemon blocklist used when assembling the child
+// environment. Returning names only keeps credential values out of the managed
+// Codex config path.
+func codexShellAuthorizedCustomEnvNames(customEnv map[string]string) []string {
+ names := make([]string, 0, len(customEnv))
+ for key := range customEnv {
+ if key == "" || isBlockedEnvKey(key) {
+ continue
+ }
+ names = append(names, key)
+ }
+ return names
+}
+
// configureCodexTaskShellEnvironment writes the managed shell policy only
// after the task and agent custom environment are fully assembled. This makes
// the allowlist reflect the child environment that will actually be launched,
-// including platform-specific essentials and non-secret custom_env values.
+// including platform-specific essentials and blocklist-checked custom_env
+// credentials.
// Failure is fatal: launching with an unowned or malformed policy could either
// drop the task-scoped token again or expose inherited daemon credentials.
-func configureCodexTaskShellEnvironment(provider, codexHome string, inherited []string, agentEnv map[string]string, logger *slog.Logger) error {
+func configureCodexTaskShellEnvironment(provider, codexHome string, inherited []string, agentEnv, agentCustomEnv map[string]string, logger *slog.Logger) error {
if provider != "codex" {
return nil
}
if strings.TrimSpace(codexHome) == "" {
return errors.New("configure Codex shell environment: task CODEX_HOME is missing")
}
- includeOnly := execenv.CodexShellEnvAllowlist(inherited, agentEnv)
+ authorizedExplicit := codexShellAuthorizedCustomEnvNames(agentCustomEnv)
+ includeOnly := execenv.CodexShellEnvAllowlist(inherited, agentEnv, authorizedExplicit)
configPath := filepath.Join(codexHome, "config.toml")
if err := execenv.EnsureCodexShellEnvPolicyConfig(configPath, includeOnly, logger); err != nil {
return fmt.Errorf("configure Codex shell environment: %w", err)
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.go
index 8ec9ae96c5..4e6af3ab0a 100644
--- a/server/internal/daemon/daemon_test.go
+++ b/server/internal/daemon/daemon_test.go
@@ -266,7 +266,7 @@ func TestConfigureCodexTaskShellEnvironment(t *testing.T) {
t.Run("non-Codex runtime is unchanged", func(t *testing.T) {
t.Parallel()
codexHome := t.TempDir()
- if err := configureCodexTaskShellEnvironment("claude", codexHome, nil, nil, slog.Default()); err != nil {
+ if err := configureCodexTaskShellEnvironment("claude", codexHome, nil, nil, nil, slog.Default()); err != nil {
t.Fatalf("configureCodexTaskShellEnvironment: %v", err)
}
if _, err := os.Stat(filepath.Join(codexHome, "config.toml")); !os.IsNotExist(err) {
@@ -284,11 +284,17 @@ func TestConfigureCodexTaskShellEnvironment(t *testing.T) {
"MULTICA_LLM_API_KEY=daemon-secret",
}
agentEnv := map[string]string{
- "CUSTOM_FLAG": "enabled",
- "MULTICA_SERVER_URL": "https://task.example",
- "MULTICA_TOKEN": "mat_task",
+ "CUSTOM_ACCESS_TOKEN": "agent-secret",
+ "CUSTOM_FLAG": "enabled",
+ "UNAUTHORIZED_TOKEN": "daemon-secret",
+ "MULTICA_SERVER_URL": "https://task.example",
+ "MULTICA_TOKEN": "mat_task",
}
- if err := configureCodexTaskShellEnvironment("codex", codexHome, inherited, agentEnv, slog.Default()); err != nil {
+ agentCustomEnv := map[string]string{
+ "CUSTOM_ACCESS_TOKEN": "agent-secret",
+ "CUSTOM_FLAG": "enabled",
+ }
+ if err := configureCodexTaskShellEnvironment("codex", codexHome, inherited, agentEnv, agentCustomEnv, slog.Default()); err != nil {
t.Fatalf("configureCodexTaskShellEnvironment: %v", err)
}
data, err := os.ReadFile(filepath.Join(codexHome, "config.toml"))
@@ -296,12 +302,12 @@ func TestConfigureCodexTaskShellEnvironment(t *testing.T) {
t.Fatalf("read config.toml: %v", err)
}
config := string(data)
- for _, want := range []string{"SystemRoot", "USERPROFILE", "CUSTOM_FLAG", "MULTICA_SERVER_URL", "MULTICA_TOKEN"} {
+ for _, want := range []string{"SystemRoot", "USERPROFILE", "CUSTOM_ACCESS_TOKEN", "CUSTOM_FLAG", "MULTICA_SERVER_URL", "MULTICA_TOKEN"} {
if !strings.Contains(config, want) {
t.Errorf("config.toml missing %q:\n%s", want, config)
}
}
- for _, unwanted := range []string{"OPENAI_API_KEY", "MULTICA_LLM_API_KEY", "MULTICA_*"} {
+ for _, unwanted := range []string{"OPENAI_API_KEY", "MULTICA_LLM_API_KEY", "UNAUTHORIZED_TOKEN", "MULTICA_*", "agent-secret", "daemon-secret", "mat_task"} {
if strings.Contains(config, unwanted) {
t.Errorf("config.toml unexpectedly contains %q:\n%s", unwanted, config)
}
@@ -310,13 +316,32 @@ func TestConfigureCodexTaskShellEnvironment(t *testing.T) {
t.Run("Codex without task home fails closed", func(t *testing.T) {
t.Parallel()
- err := configureCodexTaskShellEnvironment("codex", "", nil, map[string]string{"MULTICA_TOKEN": "mat_task"}, slog.Default())
+ err := configureCodexTaskShellEnvironment("codex", "", nil, map[string]string{"MULTICA_TOKEN": "mat_task"}, nil, slog.Default())
if err == nil || !strings.Contains(err.Error(), "CODEX_HOME is missing") {
t.Fatalf("error = %v, want missing CODEX_HOME", err)
}
})
}
+func TestCodexShellAuthorizedCustomEnvNamesUsesDaemonBlocklist(t *testing.T) {
+ t.Parallel()
+
+ got := codexShellAuthorizedCustomEnvNames(map[string]string{
+ "CUSTOM_ACCESS_TOKEN": "agent-secret",
+ "custom_secret": "agent-secret",
+ "MULTICA_TOKEN": "must-not-authorize",
+ "PATH": "/must/not/override",
+ "HOME": "/must/not/override",
+ "CODEX_HOME": "/must/not/override",
+ "": "must-not-authorize",
+ })
+ slices.Sort(got)
+ want := []string{"CUSTOM_ACCESS_TOKEN", "custom_secret"}
+ if !slices.Equal(got, want) {
+ t.Fatalf("codexShellAuthorizedCustomEnvNames() = %#v, want %#v", got, want)
+ }
+}
+
func TestTaskScopedAuthToken(t *testing.T) {
t.Parallel()
diff --git a/server/internal/daemon/execenv/codex_shell_env.go b/server/internal/daemon/execenv/codex_shell_env.go
index 4a21b242ea..50d1bff489 100644
--- a/server/internal/daemon/execenv/codex_shell_env.go
+++ b/server/internal/daemon/execenv/codex_shell_env.go
@@ -43,14 +43,22 @@ type tomlByteRange struct {
// managed shell policy.
//
// Inherited process variables keep Codex's documented default filtering for
-// names containing KEY, SECRET, or TOKEN. Inherited MULTICA_* variables are
-// always dropped because they belong to the daemon process, not necessarily to
-// this task. Explicit MULTICA_* values are safe to include because daemon.go
-// blocklists that namespace from agent custom_env and constructs those values
-// from the current task. Non-secret explicit custom_env values keep their
-// existing shell visibility; secret-looking custom_env remains filtered just
-// as it was under Codex's default policy.
-func CodexShellEnvAllowlist(inherited []string, explicit map[string]string) []string {
+// names containing KEY, SECRET, or TOKEN. Credential-looking explicit values
+// are included only when their names also appear in authorizedExplicit, which
+// daemon.go derives solely from the current agent's blocklist-checked
+// custom_env. Inherited MULTICA_* variables are always dropped because they
+// belong to the daemon process, not necessarily to this task. Explicit
+// MULTICA_* values are safe to include because daemon.go blocklists that
+// namespace from agent custom_env and constructs those values from the current
+// task.
+func CodexShellEnvAllowlist(inherited []string, explicit map[string]string, authorizedExplicit []string) []string {
+ authorized := make(map[string]struct{}, len(authorizedExplicit))
+ for _, key := range authorizedExplicit {
+ if key != "" {
+ authorized[strings.ToUpper(key)] = struct{}{}
+ }
+ }
+
// Codex's glob matching is case-insensitive. De-duplicate on the same basis
// so Windows Path/PATH aliases cannot create ambiguous policy entries.
allowed := make(map[string]string, len(inherited)+len(explicit))
@@ -64,7 +72,12 @@ func CodexShellEnvAllowlist(inherited []string, explicit map[string]string) []st
return
}
} else if codexDefaultExcludesEnvKey(upper) {
- return
+ if !isExplicit {
+ return
+ }
+ if _, ok := authorized[upper]; !ok {
+ return
+ }
}
allowed[upper] = key
}
diff --git a/server/internal/daemon/execenv/codex_shell_env_test.go b/server/internal/daemon/execenv/codex_shell_env_test.go
index 5404bf85a2..9f95082e13 100644
--- a/server/internal/daemon/execenv/codex_shell_env_test.go
+++ b/server/internal/daemon/execenv/codex_shell_env_test.go
@@ -55,9 +55,11 @@ func TestCodexShellEnvAllowlistUsesExactTaskAndSafeInheritedNames(t *testing.T)
"CUSTOM_FLAG": "enabled",
"ANTHROPIC_API_KEY": "agent-secret",
}
+ authorizedExplicit := []string{"ANTHROPIC_API_KEY"}
- got := CodexShellEnvAllowlist(inherited, explicit)
+ got := CodexShellEnvAllowlist(inherited, explicit, authorizedExplicit)
want := []string{
+ "ANTHROPIC_API_KEY",
"APPDATA",
"COMSPEC",
"CUSTOM_FLAG",
@@ -78,6 +80,42 @@ func TestCodexShellEnvAllowlistUsesExactTaskAndSafeInheritedNames(t *testing.T)
}
}
+func TestCodexShellEnvAllowlistOnlyAuthorizesExplicitCustomSecrets(t *testing.T) {
+ t.Parallel()
+
+ inherited := []string{
+ "PATH=/usr/bin",
+ "CUSTOM_ACCESS_TOKEN=host-secret",
+ "HOST_SECRET=host-secret",
+ "MULTICA_TOKEN=daemon-secret",
+ }
+ explicit := map[string]string{
+ "CUSTOM_ACCESS_TOKEN": "agent-secret",
+ "x_secret": "agent-secret",
+ "Y_KEY": "agent-secret",
+ "UNAUTHORIZED_TOKEN": "daemon-secret",
+ "MULTICA_TOKEN": "mat_task",
+ }
+ authorizedExplicit := []string{
+ "custom_access_token", // Authorization matching is case-insensitive.
+ "X_SECRET",
+ "Y_KEY",
+ "HOST_SECRET", // Authorization alone cannot expose an inherited value.
+ }
+
+ got := CodexShellEnvAllowlist(inherited, explicit, authorizedExplicit)
+ want := []string{
+ "CUSTOM_ACCESS_TOKEN",
+ "MULTICA_TOKEN",
+ "PATH",
+ "x_secret",
+ "Y_KEY",
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("CodexShellEnvAllowlist() = %#v, want %#v", got, want)
+ }
+}
+
func TestEnsureCodexShellEnvPolicyConfigReplacesAllLegalPolicyForms(t *testing.T) {
t.Parallel()