-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(agent/agentcontainers): prevent command injection in shell execer #26235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package agentcontainers | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "cdr.dev/slog/v3" | ||
| "cdr.dev/slog/v3/sloggers/slogtest" | ||
| "github.com/coder/coder/v2/agent/agentexec" | ||
| "github.com/coder/coder/v2/agent/usershell" | ||
| "github.com/coder/coder/v2/testutil" | ||
| ) | ||
|
|
||
| func TestCommandEnvExecer_Prepare(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| t.Skip("the POSIX shell quoting under test does not apply on Windows") | ||
| } | ||
|
|
||
| const shell = "/bin/sh" | ||
| commandEnv := func(usershell.EnvInfoer, []string) (string, string, []string, error) { | ||
| return shell, "/tmp", []string{"FOO=bar"}, nil | ||
| } | ||
| e := newCommandEnvExecer(slogtest.Make(t, nil).Leveled(slog.LevelDebug), commandEnv, agentexec.DefaultExecer) | ||
|
|
||
| t.Run("ArgvPassthrough", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| name, args, dir, env := e.prepare(context.Background(), "echo", "hello", "world") | ||
| // The command is run as: shell -c "$@" "" <argv...> so that the | ||
| // shell re-emits argv without re-parsing it. The empty $0 slot is | ||
| // discarded. | ||
| require.Equal(t, shell, name) | ||
| require.Equal(t, []string{"-c", `"$@"`, "", "echo", "hello", "world"}, args) | ||
| require.Equal(t, "/tmp", dir) | ||
| require.Equal(t, []string{"FOO=bar"}, env) | ||
| }) | ||
|
|
||
| t.Run("MetacharactersNotInterpreted", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| payloads := []string{ | ||
| "$(echo INJECTED)", | ||
| "`echo INJECTED`", | ||
| "$HOME", | ||
| "a; echo INJECTED", | ||
| "a && echo INJECTED", | ||
| "a | echo INJECTED", | ||
| "a\necho INJECTED", | ||
| "it's a \"test\" \\ end", | ||
| "", | ||
| } | ||
| for _, payload := range payloads { | ||
| ctx := testutil.Context(t, testutil.WaitShort) | ||
| cmd := e.CommandContext(ctx, "printf", "%s", payload) | ||
| var out bytes.Buffer | ||
| cmd.Stdout = &out | ||
| cmd.Stderr = &out | ||
| require.NoError(t, cmd.Run(), "payload %q", payload) | ||
| assert.Equal(t, payload, out.String(), "payload %q was altered by the shell", payload) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("CommandSubstitutionHasNoSideEffect", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| marker := filepath.Join(t.TempDir(), "pwned") | ||
| ctx := testutil.Context(t, testutil.WaitShort) | ||
| cmd := e.CommandContext(ctx, "echo", "$(touch "+marker+")") | ||
| var out bytes.Buffer | ||
| cmd.Stdout = &out | ||
| cmd.Stderr = &out | ||
| require.NoError(t, cmd.Run()) | ||
| require.Equal(t, "$(touch "+marker+")\n", out.String()) | ||
| require.NoFileExists(t, marker, "command substitution executed; injection is possible") | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem like we have anything preventing this from executing on Windows upstream of this call, so I was concerned that a similar vulnerability might existing for command parsing on
powershell.exeorcmd.exeusing this new approach, but it appears that the$@will cause a parse error on powershell. This is tested via a custom harness built with Claude that I can't seem to attach here - I'll DM you. I haven't checkedcmd.exe, nor am I sure if something structural doesn't prevent this running on windows in the first place.Ideally we would harden the POSIX requirement, but that's tricky.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the exact failure mode if someone tried Windows for this, but I do see we assume linux for devcontainers when creating the sub-agent (
proc.agent.OperatingSystemis only assigned a value on this line)coder/agent/agentcontainers/api.go
Line 1756 in c883db9
I'm not planning to dig any deeper on Windows here because it's not supported.