-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhandler_test.go
More file actions
290 lines (249 loc) · 8.56 KB
/
Copy pathhandler_test.go
File metadata and controls
290 lines (249 loc) · 8.56 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"context"
"testing"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShellHandler_handle_secure_mode(t *testing.T) {
logger := zerolog.New(zerolog.NewTestWriter(t))
ctx := context.Background()
tests := []struct {
name string
config SecurityConfig
requestArgs map[string]interface{}
expectError bool
expectErrorText string
}{
{
name: "secure mode allows safe command",
config: SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo", "pwd"},
MaxExecutionTime: time.Second * 5,
},
requestArgs: map[string]interface{}{
"command": "echo hello world",
"base64": false,
},
expectError: false,
},
{
name: "secure mode blocks dangerous command",
config: SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo", "pwd"},
},
requestArgs: map[string]interface{}{
"command": "rm -rf /",
},
expectError: true,
expectErrorText: "not in allowed list",
},
{
name: "secure mode blocks injection attempt",
config: SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo"},
},
requestArgs: map[string]interface{}{
"command": "echo $($(echo -n c; echo -n h; echo -n m; echo -n o; echo -n d))",
},
expectError: true,
expectErrorText: "not in allowed list",
},
{
name: "missing command parameter",
config: SecurityConfig{
Enabled: false,
},
requestArgs: map[string]interface{}{
"base64": false,
},
expectError: true,
expectErrorText: "Missing 'command' parameter",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
validator := newSecurityValidator(tt.config, logger)
executor := newCommandExecutor(tt.config, logger)
handler := newShellHandler(validator, executor, logger)
// Create MCP request using the arguments map
request := mcp.CallToolRequest{}
request.Params.Arguments = tt.requestArgs
request.Params.Name = "shell_exec"
result, err := handler.handle(ctx, request)
require.NoError(t, err, "Handler should not return error, but result should contain error")
require.NotNil(t, result)
if tt.expectError {
// Check if result contains error
assert.True(t, result.IsError)
} else {
// Check if result is successful
assert.False(t, result.IsError)
}
})
}
}
func TestShellHandler_vulnerability_prevention_integration(t *testing.T) {
logger := zerolog.New(zerolog.NewTestWriter(t))
ctx := context.Background()
// Test the exact vulnerability from VULN.md
vulnerabilityRequest := mcp.CallToolRequest{}
vulnerabilityRequest.Params.Arguments = map[string]interface{}{
"command": "echo $($(echo -n c; echo -n h; echo -n m; echo -n o; echo -n d))",
"base64": false,
}
t.Run("secure_mode_blocks_vuln_md_example", func(t *testing.T) {
config := SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo", "ls", "pwd"},
}
validator := newSecurityValidator(config, logger)
executor := newCommandExecutor(config, logger)
handler := newShellHandler(validator, executor, logger)
result, err := handler.handle(ctx, vulnerabilityRequest)
require.NoError(t, err)
// Should be blocked at validation stage
assert.True(t, result.IsError, "Secure mode should block the injection attempt")
})
t.Run("legacy_mode_vulnerable_without_blocks", func(t *testing.T) {
config := SecurityConfig{
Enabled: true,
UseShellExecution: true,
// No blocks - vulnerable
MaxExecutionTime: time.Second * 1,
}
validator := newSecurityValidator(config, logger)
executor := newCommandExecutor(config, logger)
handler := newShellHandler(validator, executor, logger)
result, err := handler.handle(ctx, vulnerabilityRequest)
require.NoError(t, err)
// This demonstrates the vulnerability - legacy mode allows dangerous commands
// In a real attack, this would execute the obfuscated chmod
t.Logf("Legacy mode result - IsError: %v", result.IsError)
})
t.Run("legacy_mode_with_proper_blocks", func(t *testing.T) {
config := SecurityConfig{
Enabled: true,
UseShellExecution: true,
BlockedCommands: []string{"chmod"}, // This should catch the obfuscated chmod
}
validator := newSecurityValidator(config, logger)
executor := newCommandExecutor(config, logger)
handler := newShellHandler(validator, executor, logger)
result, err := handler.handle(ctx, vulnerabilityRequest)
require.NoError(t, err)
// This demonstrates the vulnerability - legacy mode cannot detect obfuscated commands
// even with keyword blocking, since "chmod" doesn't appear literally
assert.False(t, result.IsError, "Legacy mode with blocks still vulnerable to obfuscation")
})
}
func TestShellHandler_base64_encoding(t *testing.T) {
logger := zerolog.New(zerolog.NewTestWriter(t))
ctx := context.Background()
config := SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo"},
MaxExecutionTime: time.Second * 5,
}
validator := newSecurityValidator(config, logger)
executor := newCommandExecutor(config, logger)
handler := newShellHandler(validator, executor, logger)
tests := []struct {
name string
base64 bool
command string
}{
{
name: "without base64 encoding",
base64: false,
command: "echo hello world",
},
{
name: "with base64 encoding",
base64: true,
command: "echo hello world",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
request := mcp.CallToolRequest{}
request.Params.Arguments = map[string]interface{}{
"command": tt.command,
"base64": tt.base64,
}
result, err := handler.handle(ctx, request)
require.NoError(t, err)
assert.False(t, result.IsError, "Base64 encoding test should succeed")
})
}
}
// Test direct security validation and execution without MCP wrapper
func TestShellHandler_direct_security_tests(t *testing.T) {
logger := zerolog.New(zerolog.NewTestWriter(t))
t.Run("secure_execution_blocks_injection", func(t *testing.T) {
config := SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo"},
}
validator := newSecurityValidator(config, logger)
executor := newCommandExecutor(config, logger)
// Test validation - should pass (this is the vulnerability)
err := validator.validateCommand("echo $(rm -rf /)")
assert.Error(t, err, "Should block command with shell metacharacters")
// Test parsing - the executor's unfurler rejects it as well
res := executor.unfurler.unfurl("echo $(rm -rf /)")
assert.False(t, res.Allowed, "Executor should reject command with command substitution")
})
t.Run("legacy_execution_allows_injection", func(t *testing.T) {
config := SecurityConfig{
Enabled: true,
UseShellExecution: true,
// No restrictions - vulnerable
}
validator := newSecurityValidator(config, logger)
// Test validation - should pass (this is the vulnerability)
err := validator.validateCommand("echo $(rm -rf /)")
assert.NoError(t, err, "Legacy mode without blocks allows dangerous commands")
})
t.Run("VULN_MD_example_security_comparison", func(t *testing.T) {
vulnCommand := "echo $($(echo -n c; echo -n h; echo -n m; echo -n o; echo -n d))"
// Secure mode
secureConfig := SecurityConfig{
Enabled: true,
UseShellExecution: false,
AllowedExecutables: []string{"echo"},
}
secureValidator := newSecurityValidator(secureConfig, logger)
err := secureValidator.validateCommand(vulnCommand)
assert.Error(t, err, "Secure mode should block VULN.md example")
// Legacy mode without proper blocks (vulnerable)
vulnerableConfig := SecurityConfig{
Enabled: true,
UseShellExecution: true,
}
vulnerableValidator := newSecurityValidator(vulnerableConfig, logger)
err = vulnerableValidator.validateCommand(vulnCommand)
assert.NoError(t, err, "Legacy mode without blocks is vulnerable")
// Legacy mode with proper blocks - still vulnerable to obfuscated commands
protectedConfig := SecurityConfig{
Enabled: true,
UseShellExecution: true,
BlockedCommands: []string{"chmod"},
}
protectedValidator := newSecurityValidator(protectedConfig, logger)
err = protectedValidator.validateCommand(vulnCommand)
assert.NoError(t, err, "Legacy mode cannot detect obfuscated commands even with blocks")
})
}