Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions server/cmd/api/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ func (h *processHandle) setExited(code int) {
h.mu.Unlock()
}

func isUserCmdError(err error) bool {
return errors.Is(err, exec.ErrNotFound) ||
errors.Is(err, exec.ErrDot) ||
errors.Is(err, os.ErrNotExist) ||
errors.Is(err, os.ErrPermission) ||
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.ErrPermission catches server-side EPERM as user error

Medium Severity

errors.Is(err, os.ErrPermission) matches both syscall.EACCES and syscall.EPERM due to Go's Errno.Is mapping. Only EACCES (file permission denied) is a user-input error. EPERM (operation not permitted) can arise from server-side issues — most notably when AsRoot or AsUser sets SysProcAttr.Credential but the server lacks CAP_SETUID/CAP_SETGID. In that case cmd.Start() fails with EPERM, which gets misclassified as a 400 instead of 500, masking a server configuration problem. Using syscall.EACCES directly instead of os.ErrPermission would avoid catching EPERM.

Additional Locations (1)
Fix in Cursor Fix in Web

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 400 is fine for now in this case

errors.Is(err, syscall.EISDIR) ||
errors.Is(err, syscall.ENOEXEC) ||
errors.Is(err, syscall.ENOTDIR)
}

func buildCmd(body *oapi.ProcessExecRequest) (*exec.Cmd, error) {
if body == nil || body.Command == "" {
return nil, errors.New("command required")
Expand Down Expand Up @@ -177,6 +187,9 @@ func (s *ApiService) ProcessExec(ctx context.Context, request oapi.ProcessExecRe
defer cancel()
}
if err := cmd.Start(); err != nil {
if isUserCmdError(err) {
return oapi.ProcessExec400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: err.Error()}}, nil
}
log.Error("failed to start process", "err", err)
return oapi.ProcessExec500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{Message: "failed to start process"}}, nil
}
Expand Down Expand Up @@ -263,6 +276,9 @@ func (s *ApiService) ProcessSpawn(ctx context.Context, request oapi.ProcessSpawn
var errStart error
ptyFile, errStart = pty.Start(cmd)
if errStart != nil {
if isUserCmdError(errStart) {
return oapi.ProcessSpawn400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: errStart.Error()}}, nil
}
log.Error("failed to start PTY process", "err", errStart)
return oapi.ProcessSpawn500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{Message: "failed to start process"}}, nil
}
Expand Down Expand Up @@ -294,6 +310,9 @@ func (s *ApiService) ProcessSpawn(ctx context.Context, request oapi.ProcessSpawn
return oapi.ProcessSpawn500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{Message: "failed to open stdin"}}, nil
}
if err := cmd.Start(); err != nil {
if isUserCmdError(err) {
return oapi.ProcessSpawn400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: err.Error()}}, nil
}
log.Error("failed to start process", "err", err)
return oapi.ProcessSpawn500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{Message: "failed to start process"}}, nil
}
Expand Down
24 changes: 24 additions & 0 deletions server/cmd/api/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ func TestProcessNotFoundRoutes(t *testing.T) {
}
}

func TestProcessExec_CommandNotFound(t *testing.T) {
t.Parallel()
ctx := context.Background()
svc := &ApiService{procs: make(map[string]*processHandle)}

body := &oapi.ProcessExecRequest{Command: "nonexistent_binary_that_does_not_exist"}
resp, err := svc.ProcessExec(ctx, oapi.ProcessExecRequestObject{Body: body})
require.NoError(t, err)
_, ok := resp.(oapi.ProcessExec400JSONResponse)
require.True(t, ok, "expected 400 for nonexistent command, got %T", resp)
}

func TestProcessSpawn_CommandNotFound(t *testing.T) {
t.Parallel()
ctx := context.Background()
svc := &ApiService{procs: make(map[string]*processHandle), stz: scaletozero.NewNoopController()}

body := &oapi.ProcessSpawnRequest{Command: "nonexistent_binary_that_does_not_exist"}
resp, err := svc.ProcessSpawn(ctx, oapi.ProcessSpawnRequestObject{Body: body})
require.NoError(t, err)
_, ok := resp.(oapi.ProcessSpawn400JSONResponse)
require.True(t, ok, "expected 400 for nonexistent command, got %T", resp)
}

func TestBuildCmd_AsRootSetsCredential(t *testing.T) {
t.Parallel()
asRoot := true
Expand Down
Loading