-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexecutor.go
More file actions
245 lines (212 loc) · 6.25 KB
/
executor.go
File metadata and controls
245 lines (212 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"
"time"
"github.com/rs/zerolog"
)
type ExecutionResult struct {
Status string `json:"status"`
ExitCode int `json:"exit_code"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
Command string `json:"command"`
ExecutionTime time.Duration `json:"execution_time"`
SecurityInfo *SecurityInfo `json:"security_info,omitempty"`
}
type SecurityInfo struct {
SecurityEnabled bool `json:"security_enabled"`
WorkingDir string `json:"working_dir,omitempty"`
RunAsUser string `json:"run_as_user,omitempty"`
TimeoutApplied bool `json:"timeout_applied"`
}
type CommandExecutor struct {
config SecurityConfig
logger zerolog.Logger
}
func newCommandExecutor(cfg SecurityConfig, logger zerolog.Logger) *CommandExecutor {
return &CommandExecutor{
config: cfg,
logger: logger.With().Str("component", "executor").Logger(),
}
}
func (e *CommandExecutor) execute(
ctx context.Context,
command string,
useBase64 bool,
) (*ExecutionResult, error) {
start := time.Now()
e.logger.Info().
Str("command", command).
Bool("base64", useBase64).
Msg("Executing command")
timeout := 30 * time.Second
if e.config.MaxExecutionTime > 0 {
timeout = e.config.MaxExecutionTime
}
cmdCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
result, err := e.executeSecureCommand(cmdCtx, command, useBase64)
if err != nil {
e.logger.Error().
Err(err).
Str("command", command).
Msg("Command execution failed")
return nil, err
}
result.ExecutionTime = time.Since(start)
result.SecurityInfo = &SecurityInfo{
SecurityEnabled: e.config.Enabled,
TimeoutApplied: true,
}
if e.config.WorkingDirectory != "" {
result.SecurityInfo.WorkingDir = e.config.WorkingDirectory
}
if e.config.RunAsUser != "" {
result.SecurityInfo.RunAsUser = e.config.RunAsUser
}
e.logger.Info().
Str("command", command).
Str("status", result.Status).
Int("exit_code", result.ExitCode).
Dur("execution_time", result.ExecutionTime).
Msg("Command execution completed")
return result, nil
}
// parseCommand securely parses a command string into executable and arguments
// without using shell interpretation. This prevents command injection through
// shell metacharacters and substitution.
func (e *CommandExecutor) parseCommand(command string) (string, []string, error) {
command = strings.TrimSpace(command)
if command == "" {
return "", nil, fmt.Errorf("empty command")
}
// Simple whitespace-based splitting - no shell interpretation
parts := strings.Fields(command)
if len(parts) == 0 {
return "", nil, fmt.Errorf("no command found")
}
executable := parts[0]
args := parts[1:]
// Validate that the executable doesn't contain shell metacharacters
if containsShellMetacharacters(executable) {
return "", nil, fmt.Errorf("executable contains shell metacharacters: %s", executable)
}
// Validate arguments don't contain dangerous shell constructs
// In secure mode, this should be an error, not just a warning
for _, arg := range args {
if containsDangerousShellConstructs(arg) {
return "", nil, fmt.Errorf("argument contains dangerous shell constructs: %s", arg)
}
}
return executable, args, nil
}
func (e *CommandExecutor) executeSecureCommand(
ctx context.Context,
command string,
useBase64 bool,
) (*ExecutionResult, error) {
var cmd *exec.Cmd
// Use secure execution unless legacy shell mode is explicitly enabled
if e.config.UseShellExecution {
e.logger.Warn().
Str("command", command).
Msg("Using legacy shell execution mode - vulnerable to injection attacks")
cmd = exec.CommandContext(ctx, "bash", "-c", command)
} else {
// Secure execution: parse command and execute directly
executable, args, err := e.parseCommand(command)
if err != nil {
e.logger.Error().
Err(err).
Str("command", command).
Msg("Failed to parse command securely")
return nil, fmt.Errorf("command parsing failed: %w", err)
}
e.logger.Debug().
Str("executable", executable).
Strs("args", args).
Msg("Executing command with direct execution")
cmd = exec.CommandContext(ctx, executable, args...)
}
if e.config.WorkingDirectory != "" {
if err := os.MkdirAll(e.config.WorkingDirectory, 0755); err == nil {
cmd.Dir = e.config.WorkingDirectory
e.logger.Debug().
Str("working_dir", e.config.WorkingDirectory).
Msg("Set working directory")
}
}
if e.config.RunAsUser != "" {
if u, err := user.Lookup(e.config.RunAsUser); err == nil {
if uid, err := strconv.Atoi(u.Uid); err == nil {
if gid, err := strconv.Atoi(u.Gid); err == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
},
}
e.logger.Debug().
Str("user", e.config.RunAsUser).
Int("uid", uid).
Int("gid", gid).
Msg("Set process credentials")
}
}
}
}
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err := cmd.Run()
if e.config.MaxOutputSize > 0 {
if stdoutBuf.Len() > e.config.MaxOutputSize {
e.logger.Warn().
Int("stdout_size", stdoutBuf.Len()).
Int("max_size", e.config.MaxOutputSize).
Msg("Stdout exceeds maximum size limit")
return nil, fmt.Errorf("stdout exceeds maximum size limit")
}
if stderrBuf.Len() > e.config.MaxOutputSize {
e.logger.Warn().
Int("stderr_size", stderrBuf.Len()).
Int("max_size", e.config.MaxOutputSize).
Msg("Stderr exceeds maximum size limit")
return nil, fmt.Errorf("stderr exceeds maximum size limit")
}
}
exitCode := 0
status := "success"
if err != nil {
status = "error"
if exitError, ok := err.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
} else {
exitCode = -1
}
}
var stdout, stderr string
if useBase64 {
stdout = base64.StdEncoding.EncodeToString(stdoutBuf.Bytes())
stderr = base64.StdEncoding.EncodeToString(stderrBuf.Bytes())
} else {
stdout = strings.TrimRight(stdoutBuf.String(), "\n")
stderr = strings.TrimRight(stderrBuf.String(), "\n")
}
return &ExecutionResult{
Status: status,
ExitCode: exitCode,
Stdout: stdout,
Stderr: stderr,
Command: command,
}, nil
}