Skip to content

Commit

Permalink
fix: Apply environment variables to startup script (#2099)
Browse files Browse the repository at this point in the history
This was stopping `coder` from being in the path, and allowed
applications started in the script to bypass injected environmnet
variables like `GIT_SSH_COMMAND`.
  • Loading branch information
kylecarbs committed Jun 6, 2022
1 parent 1a39931 commit 66cf59b
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions agent/agent.go
Expand Up @@ -155,20 +155,10 @@ func (a *agent) run(ctx context.Context) {
}
}

func (*agent) runStartupScript(ctx context.Context, script string) error {
func (a *agent) runStartupScript(ctx context.Context, script string) error {
if script == "" {
return nil
}
currentUser, err := user.Current()
if err != nil {
return xerrors.Errorf("get current user: %w", err)
}
username := currentUser.Username

shell, err := usershell.Get(username)
if err != nil {
return xerrors.Errorf("get user shell: %w", err)
}

writer, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
Expand All @@ -178,12 +168,10 @@ func (*agent) runStartupScript(ctx context.Context, script string) error {
_ = writer.Close()
}()

caller := "-c"
if runtime.GOOS == "windows" {
caller = "/c"
cmd, err := a.createCommand(ctx, script, nil)
if err != nil {
return xerrors.Errorf("create command: %w", err)
}

cmd := exec.CommandContext(ctx, shell, caller, script)
cmd.Stdout = writer
cmd.Stderr = writer
err = cmd.Run()
Expand Down

0 comments on commit 66cf59b

Please sign in to comment.