-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_test.go
More file actions
586 lines (550 loc) · 13.4 KB
/
security_test.go
File metadata and controls
586 lines (550 loc) · 13.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package main
import (
"bytes"
"strings"
"testing"
)
func TestValidateTaskName(t *testing.T) {
tests := []struct {
name string
input string
wantErr error
}{
{
name: "valid task name with letters",
input: "myTask",
wantErr: nil,
},
{
name: "valid task name with numbers",
input: "task123",
wantErr: nil,
},
{
name: "valid task name with underscore",
input: "my_task",
wantErr: nil,
},
{
name: "valid task name with hyphen",
input: "my-task",
wantErr: nil,
},
{
name: "valid task name mixed",
input: "task-123_test",
wantErr: nil,
},
{
name: "empty task name",
input: "",
wantErr: ErrEmptyTaskName,
},
{
name: "task name with space",
input: "my task",
wantErr: ErrInvalidTaskName,
},
{
name: "task name with special characters",
input: "task@123",
wantErr: ErrInvalidTaskName,
},
{
name: "task name with slash",
input: "task/123",
wantErr: ErrInvalidTaskName,
},
{
name: "task name too long",
input: strings.Repeat("a", maxTaskNameLength+1),
wantErr: ErrTaskNameTooLong,
},
{
name: "task name at max length",
input: strings.Repeat("a", maxTaskNameLength),
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateTaskName(tt.input)
if tt.wantErr == nil {
if err != nil {
t.Errorf("validateTaskName(%q) = %v, want nil", tt.input, err)
}
} else {
if err != tt.wantErr {
t.Errorf("validateTaskName(%q) = %v, want %v", tt.input, err, tt.wantErr)
}
}
})
}
}
func TestValidateTaskID(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "valid UUID v4",
input: "550e8400-e29b-41d4-a716-446655440000",
want: true,
},
{
name: "valid UUID v1",
input: "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
want: true,
},
{
name: "invalid UUID - too short",
input: "550e8400",
want: false,
},
{
name: "invalid UUID - wrong format",
input: "not-a-uuid",
want: false,
},
{
name: "invalid UUID - empty",
input: "",
want: false,
},
{
name: "valid UUID without hyphens (uuid.Parse accepts this)",
input: "550e8400e29b41d4a716446655440000",
want: true, // uuid.Parse actually accepts UUIDs without hyphens
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := validateTaskID(tt.input)
if got != tt.want {
t.Errorf("validateTaskID(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestEscapeBashCommand(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "simple command",
input: "echo hello",
want: "'echo hello'",
},
{
name: "command with single quote",
input: "echo 'hello world'",
want: "'echo '\\''hello world'\\'''",
},
{
name: "command with multiple single quotes",
input: "echo 'hello' and 'world'",
want: "'echo '\\''hello'\\'' and '\\''world'\\'''",
},
{
name: "empty command",
input: "",
want: "''",
},
{
name: "command with special characters",
input: "echo $PATH",
want: "'echo $PATH'",
},
{
name: "command with double quotes",
input: `echo "hello"`,
want: `'echo "hello"'`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := escapeBashCommand(tt.input)
if got != tt.want {
t.Errorf("escapeBashCommand(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestValidateParameterValue(t *testing.T) {
tests := []struct {
name string
paramName string
paramType string
value interface{}
want string
wantErr bool
errMsg string
}{
// Valid int parameters
{
name: "valid int as string",
paramName: "timeout",
paramType: "int",
value: "123",
want: "123",
wantErr: false,
},
{
name: "valid int as int",
paramName: "timeout",
paramType: "int",
value: 123,
want: "123",
wantErr: false,
},
{
name: "valid int as int64",
paramName: "timeout",
paramType: "int",
value: int64(456),
want: "456",
wantErr: false,
},
{
name: "valid int as float64 (whole number)",
paramName: "timeout",
paramType: "int",
value: float64(789),
want: "789",
wantErr: false,
},
{
name: "valid int zero",
paramName: "count",
paramType: "int",
value: "0",
want: "0",
wantErr: false,
},
// Invalid int parameters
{
name: "invalid int with letters",
paramName: "timeout",
paramType: "int",
value: "123abc",
want: "",
wantErr: true,
errMsg: "contains invalid characters",
},
{
name: "invalid int with special chars",
paramName: "timeout",
paramType: "int",
value: "12-3",
want: "",
wantErr: true,
errMsg: "contains invalid characters",
},
{
name: "invalid int as float64 (decimal)",
paramName: "timeout",
paramType: "int",
value: float64(123.45),
want: "",
wantErr: true,
errMsg: "must be an integer",
},
{
name: "invalid int type mismatch",
paramName: "timeout",
paramType: "string",
value: 123,
want: "",
wantErr: true,
errMsg: "must be of type 'string'",
},
// Valid string parameters
{
name: "valid string simple",
paramName: "filename",
paramType: "string",
value: "data.txt",
want: "data.txt",
wantErr: false,
},
{
name: "valid string with hyphen",
paramName: "filename",
paramType: "string",
value: "my-file.txt",
want: "my-file.txt",
wantErr: false,
},
{
name: "valid string with underscore",
paramName: "filename",
paramType: "string",
value: "my_file.txt",
want: "my_file.txt",
wantErr: false,
},
{
name: "valid string with colon",
paramName: "filename",
paramType: "string",
value: "file:name",
want: "file:name",
wantErr: false,
},
{
name: "valid string with comma",
paramName: "filename",
paramType: "string",
value: "file,name",
want: "file,name",
wantErr: false,
},
{
name: "valid string with dot",
paramName: "filename",
paramType: "string",
value: "file.name",
want: "file.name",
wantErr: false,
},
{
name: "valid string as float64",
paramName: "filename",
paramType: "string",
value: float64(123.45),
want: "123.45",
wantErr: false,
},
// Invalid string parameters
{
name: "invalid string with space",
paramName: "filename",
paramType: "string",
value: "file name",
want: "",
wantErr: true,
errMsg: "contains invalid characters",
},
{
name: "invalid string with slash",
paramName: "filename",
paramType: "string",
value: "file/name",
want: "",
wantErr: true,
errMsg: "contains invalid characters",
},
{
name: "invalid string with special chars",
paramName: "filename",
paramType: "string",
value: "file@name",
want: "",
wantErr: true,
errMsg: "contains invalid characters",
},
// Invalid type
{
name: "unsupported type bool",
paramName: "flag",
paramType: "int",
value: true,
want: "",
wantErr: true,
errMsg: "unsupported type",
},
{
name: "unsupported type slice",
paramName: "items",
paramType: "string",
value: []string{"a", "b"},
want: "",
wantErr: true,
errMsg: "unsupported type",
},
// Invalid param type
{
name: "unknown parameter type",
paramName: "value",
paramType: "float",
value: "123",
want: "",
wantErr: true,
errMsg: "unknown type",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := validateParameterValue(tt.paramName, tt.paramType, tt.value)
if tt.wantErr {
if err == nil {
t.Errorf("validateParameterValue(%q, %q, %v) = %q, nil; want error", tt.paramName, tt.paramType, tt.value, got)
} else if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("validateParameterValue(%q, %q, %v) error = %v, want error containing %q", tt.paramName, tt.paramType, tt.value, err, tt.errMsg)
}
} else {
if err != nil {
t.Errorf("validateParameterValue(%q, %q, %v) = %q, %v; want %q, nil", tt.paramName, tt.paramType, tt.value, got, err, tt.want)
} else if got != tt.want {
t.Errorf("validateParameterValue(%q, %q, %v) = %q, nil; want %q", tt.paramName, tt.paramType, tt.value, got, tt.want)
}
}
})
}
}
func TestDecodeJSONRequest(t *testing.T) {
tests := []struct {
name string
json string
maxSize int64
wantErr bool
}{
{
name: "valid JSON within limit",
json: `{"task_name": "test", "parameters": {"key": "value"}}`,
maxSize: 1024,
wantErr: false,
},
{
name: "valid JSON at limit",
json: `{"task_name": "test"}`,
maxSize: 30,
wantErr: false,
},
{
name: "JSON exceeds limit",
json: `{"task_name": "test", "data": "` + strings.Repeat("x", 1000) + `"}`,
maxSize: 50,
wantErr: true,
},
{
name: "invalid JSON",
json: `{"task_name": "test"`,
maxSize: 1024,
wantErr: true,
},
{
name: "empty JSON",
json: ``,
maxSize: 1024,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result map[string]interface{}
reader := bytes.NewReader([]byte(tt.json))
err := decodeJSONRequest(reader, &result, tt.maxSize)
if tt.wantErr {
if err == nil {
t.Errorf("decodeJSONRequest() = nil; want error")
}
} else {
if err != nil {
t.Errorf("decodeJSONRequest() = %v; want nil", err)
}
}
})
}
}
func TestDecodeJSONRequestSizeLimit(t *testing.T) {
// Test that the size limit is actually enforced
largeJSON := `{"data": "` + strings.Repeat("x", 1000) + `"}`
reader := bytes.NewReader([]byte(largeJSON))
var result map[string]interface{}
err := decodeJSONRequest(reader, &result, 100) // Small limit
if err == nil {
t.Error("decodeJSONRequest() with oversized input = nil; want error")
}
// Verify that only partial data was read
if result["data"] != nil {
data := result["data"].(string)
if len(data) > 100 {
t.Errorf("decodeJSONRequest() read more than limit: got %d bytes", len(data))
}
}
}
func TestDecodeJSONRequestWithStruct(t *testing.T) {
type TestRequest struct {
TaskName string `json:"task_name"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
}
jsonStr := `{"task_name": "my-task", "parameters": {"key": "value", "num": 42}}`
reader := bytes.NewReader([]byte(jsonStr))
var req TestRequest
err := decodeJSONRequest(reader, &req, maxJSONSize)
if err != nil {
t.Fatalf("decodeJSONRequest() = %v; want nil", err)
}
if req.TaskName != "my-task" {
t.Errorf("req.TaskName = %q; want %q", req.TaskName, "my-task")
}
if req.Parameters == nil {
t.Error("req.Parameters = nil; want map")
}
if req.Parameters["key"] != "value" {
t.Errorf("req.Parameters[\"key\"] = %v; want %q", req.Parameters["key"], "value")
}
// JSON numbers are decoded as float64
if req.Parameters["num"] != float64(42) {
t.Errorf("req.Parameters[\"num\"] = %v; want %v", req.Parameters["num"], float64(42))
}
}
func TestDecodeJSONRequestWithLargeReader(t *testing.T) {
// Create a reader that's larger than the limit
largeData := strings.Repeat("x", 2000)
jsonStr := `{"data": "` + largeData + `"}`
reader := bytes.NewReader([]byte(jsonStr))
var result map[string]interface{}
err := decodeJSONRequest(reader, &result, 100) // Limit to 100 bytes
// Should get an error or truncated data
if err == nil {
// If no error, verify data was truncated
if data, ok := result["data"].(string); ok {
if len(data) > 100 {
t.Errorf("decodeJSONRequest() read %d bytes; expected max 100", len(data))
}
}
}
}
func TestDecodeJSONRequestWithNilReader(t *testing.T) {
// This test verifies that passing nil reader causes an error or panic
// In practice, this shouldn't happen, but we test it for completeness
var result map[string]interface{}
// Use defer recover to catch potential panic
defer func() {
if r := recover(); r != nil {
// Panic is expected for nil reader
t.Logf("decodeJSONRequest with nil reader panicked as expected: %v", r)
}
}()
err := decodeJSONRequest(nil, &result, maxJSONSize)
// If no panic, should return an error
if err == nil {
t.Error("decodeJSONRequest(nil, ...) = nil; want error or panic")
}
}
func TestDecodeJSONRequestWithInvalidTarget(t *testing.T) {
jsonStr := `{"task_name": "test"}`
reader := bytes.NewReader([]byte(jsonStr))
// Try to decode into a non-pointer
var result map[string]interface{}
err := decodeJSONRequest(reader, result, maxJSONSize)
// This might succeed or fail depending on implementation
// Just verify it doesn't panic
_ = err
}
// Test that decodeJSONRequest properly handles io.LimitReader
func TestDecodeJSONRequestLimitReader(t *testing.T) {
// Create JSON that's exactly at the limit
exactSizeJSON := `{"data": "` + strings.Repeat("x", 50) + `"}`
reader := bytes.NewReader([]byte(exactSizeJSON))
var result map[string]interface{}
err := decodeJSONRequest(reader, &result, 100)
if err != nil {
t.Errorf("decodeJSONRequest() with exact limit = %v; want nil", err)
}
}