Skip to content

Commit 354d0fc

Browse files
authored
fix: filter agent-exec env vars (coder#15764)
- Filters env vars specific to agent-exec from the exec'd process. This is to prevent any issues when developing Coder in Coder, particularly agent tests in the cli pkg.
1 parent f8d938d commit 354d0fc

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

agent/agentexec/cli_linux.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"runtime"
12+
"slices"
1213
"strconv"
1314
"strings"
1415
"syscall"
@@ -111,7 +112,16 @@ func CLI() error {
111112
return xerrors.Errorf("look path: %w", err)
112113
}
113114

114-
return syscall.Exec(path, args, os.Environ())
115+
// Remove environment variables specific to the agentexec command. This is
116+
// especially important for environments that are attempting to develop Coder in Coder.
117+
env := os.Environ()
118+
env = slices.DeleteFunc(env, func(e string) bool {
119+
return strings.HasPrefix(e, EnvProcPrioMgmt) ||
120+
strings.HasPrefix(e, EnvProcOOMScore) ||
121+
strings.HasPrefix(e, EnvProcNiceScore)
122+
})
123+
124+
return syscall.Exec(path, args, env)
115125
}
116126

117127
func defaultNiceScore() (int, error) {

agent/agentexec/cli_linux_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"slices"
1314
"strconv"
1415
"strings"
1516
"syscall"
@@ -20,6 +21,7 @@ import (
2021
"golang.org/x/sys/unix"
2122
"golang.org/x/xerrors"
2223

24+
"github.com/coder/coder/v2/agent/agentexec"
2325
"github.com/coder/coder/v2/testutil"
2426
)
2527

@@ -37,6 +39,32 @@ func TestCLI(t *testing.T) {
3739
requireNiceScore(t, cmd.Process.Pid, 12)
3840
})
3941

42+
t.Run("FiltersEnv", func(t *testing.T) {
43+
ctx := testutil.Context(t, testutil.WaitMedium)
44+
cmd, path := cmd(ctx, t, 123, 12)
45+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=true", agentexec.EnvProcPrioMgmt))
46+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=123", agentexec.EnvProcOOMScore))
47+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=12", agentexec.EnvProcNiceScore))
48+
// Ensure unrelated environment variables are preserved.
49+
cmd.Env = append(cmd.Env, "CODER_TEST_ME_AGENTEXEC=true")
50+
err := cmd.Start()
51+
require.NoError(t, err)
52+
go cmd.Wait()
53+
waitForSentinel(ctx, t, cmd, path)
54+
55+
env := procEnv(t, cmd.Process.Pid)
56+
hasExecEnvs := slices.ContainsFunc(
57+
env,
58+
func(e string) bool {
59+
return strings.HasPrefix(e, agentexec.EnvProcPrioMgmt) ||
60+
strings.HasPrefix(e, agentexec.EnvProcOOMScore) ||
61+
strings.HasPrefix(e, agentexec.EnvProcNiceScore)
62+
})
63+
require.False(t, hasExecEnvs, "expected environment variables to be filtered")
64+
userEnv := slices.Contains(env, "CODER_TEST_ME_AGENTEXEC=true")
65+
require.True(t, userEnv, "expected user environment variables to be preserved")
66+
})
67+
4068
t.Run("Defaults", func(t *testing.T) {
4169
ctx := testutil.Context(t, testutil.WaitMedium)
4270
cmd, path := cmd(ctx, t, 0, 0)
@@ -176,6 +204,15 @@ func expectedOOMScore(t *testing.T) int {
176204
return 998
177205
}
178206

207+
// procEnv returns the environment variables for a given process.
208+
func procEnv(t *testing.T, pid int) []string {
209+
t.Helper()
210+
211+
env, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", pid))
212+
require.NoError(t, err)
213+
return strings.Split(string(env), "\x00")
214+
}
215+
179216
func expectedNiceScore(t *testing.T) int {
180217
t.Helper()
181218

0 commit comments

Comments
 (0)