Skip to content

Commit

Permalink
Improve error messages with exec, especially if working dir missing (#…
Browse files Browse the repository at this point in the history
…236)

Fixes #235
  • Loading branch information
benhoyt authored Jun 30, 2023
1 parent 6892f25 commit 30f520b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion internals/daemon/api_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ func v1PostExec(c *Command, req *http.Request, _ *userState) Response {
// Check up-front that the executable exists.
_, err := exec.LookPath(payload.Command[0])
if err != nil {
return statusBadRequest("%v", err)
return statusBadRequest("cannot find executable %q", payload.Command[0])
}

// Also check that the working directory exists, to avoid a confusing
// error message later that implies the command doesn't exist:
//
// fork/exec /usr/local/bin/realcommand: no such file or directory
//
// Note that this check still doesn't check that the permissions are
// correct to use it as a working directory, but this is a good start.
if payload.WorkingDir != "" {
if !osutil.IsDir(payload.WorkingDir) {
return statusBadRequest("cannot find working directory %q", payload.WorkingDir)
}
}

// Convert User/UserID and Group/GroupID combinations into raw uid/gid.
Expand Down
2 changes: 1 addition & 1 deletion internals/daemon/api_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (s *execSuite) TestCommandNotFound(c *C) {
c.Check(httpResp.StatusCode, Equals, http.StatusBadRequest)
c.Check(execResp.StatusCode, Equals, http.StatusBadRequest)
c.Check(execResp.Type, Equals, "error")
c.Check(execResp.Result["message"], Matches, ".*executable file not found.*")
c.Check(execResp.Result["message"], Matches, "cannot find executable .*")
}

func (s *execSuite) TestUserGroupError(c *C) {
Expand Down

0 comments on commit 30f520b

Please sign in to comment.