-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_test.go
More file actions
651 lines (521 loc) · 17.5 KB
/
security_test.go
File metadata and controls
651 lines (521 loc) · 17.5 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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package relayer
import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"
)
// Security Test Suite for Phase 1 Fixes
// R-1: Test that panic errors don't leak stack traces
func TestSecurity_PanicNoStackTrace(t *testing.T) {
orch := New()
orch.RegisterRecipe("panic-recipe", func(ctx context.Context, payload interface{}) (interface{}, error) {
panic("sensitive internal error with file paths")
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "panic-recipe"},
}
results := orch.ExecuteBatch(context.Background(), batch)
if len(results) != 1 {
t.Fatalf("Expected 1 result, got %d", len(results))
}
result := results[0]
// Verify error code is PANIC, not RECIPE_EXECUTION
if result.Error.Code != ErrCodePanic {
t.Errorf("Error code = %s, want %s", result.Error.Code, ErrCodePanic)
}
// Verify error message is generic
if result.Error.Message != "internal error during recipe execution" {
t.Errorf("Error message = %q, want generic message", result.Error.Message)
}
// Verify NO stack trace information in message
errorMsg := result.Error.Message
forbiddenStrings := []string{
"goroutine",
"panic:",
".go:",
"runtime/",
"/home/",
"Stack:",
"\n", // No multi-line stack traces
}
for _, forbidden := range forbiddenStrings {
if strings.Contains(errorMsg, forbidden) {
t.Errorf("Error message contains forbidden string %q: %s", forbidden, errorMsg)
}
}
}
// R-1: Test that panic hook still receives full information
func TestSecurity_PanicHookReceivesFullInfo(t *testing.T) {
panicHook := &mockPanicHook{}
orch := New(WithPanicHook(panicHook))
orch.RegisterRecipe("panic-recipe", func(ctx context.Context, payload interface{}) (interface{}, error) {
panic("test panic value")
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "panic-recipe"},
}
orch.ExecuteBatch(context.Background(), batch)
panicCalls := panicHook.getPanicCalls()
if len(panicCalls) != 1 {
t.Fatalf("Expected 1 panic hook call, got %d", len(panicCalls))
}
// Verify hook received the actual panic value
if panicCalls[0].recovered != "test panic value" {
t.Errorf("Panic hook recovered = %v, want 'test panic value'", panicCalls[0].recovered)
}
}
// R-2: Test batch size limit enforcement
func TestSecurity_BatchSizeLimit(t *testing.T) {
orch := New(WithMaxBatchSize(10))
orch.RegisterRecipe("test", func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
})
// Create batch larger than limit
batch := make([]SubRequest, 20)
for i := 0; i < 20; i++ {
batch[i] = SubRequest{
ID: string(rune('a' + i)),
TenantID: "tenant-a",
Recipe: "test",
}
}
results := orch.ExecuteBatch(context.Background(), batch)
// All requests should fail with BATCH_TOO_LARGE
if len(results) != 20 {
t.Fatalf("Expected 20 results, got %d", len(results))
}
for i, result := range results {
if result.Status != 413 {
t.Errorf("Result %d status = %d, want 413", i, result.Status)
}
if result.Error.Code != ErrCodeBatchTooLarge {
t.Errorf("Result %d error code = %s, want %s", i, result.Error.Code, ErrCodeBatchTooLarge)
}
expectedMsg := "batch size 20 exceeds limit of 10"
if result.Error.Message != expectedMsg {
t.Errorf("Result %d error message = %q, want %q", i, result.Error.Message, expectedMsg)
}
}
}
// R-2: Test unlimited batch size (default)
func TestSecurity_UnlimitedBatchSize(t *testing.T) {
orch := New() // maxBatchSize defaults to 0 (unlimited)
orch.RegisterRecipe("test", func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
})
// Create large batch
batch := make([]SubRequest, 100)
for i := 0; i < 100; i++ {
batch[i] = SubRequest{
ID: string(rune(i)),
TenantID: "tenant-a",
Recipe: "test",
}
}
results := orch.ExecuteBatch(context.Background(), batch)
// All should succeed
if len(results) != 100 {
t.Fatalf("Expected 100 results, got %d", len(results))
}
for i, result := range results {
if result.Status != 200 {
t.Errorf("Result %d status = %d, want 200", i, result.Status)
}
}
}
// R-3: Test empty tenant ID validation
func TestSecurity_EmptyTenantIDRejected(t *testing.T) {
orch := New()
orch.RegisterRecipe("test", func(ctx context.Context, payload interface{}) (interface{}, error) {
tenantID, _ := TenantID(ctx)
if tenantID == "" {
t.Error("Recipe received empty tenant ID!")
}
return "ok", nil
})
batch := []SubRequest{
{ID: "1", TenantID: "", Recipe: "test"}, // Empty tenant ID
}
results := orch.ExecuteBatch(context.Background(), batch)
if results[0].Status != 400 {
t.Errorf("Status = %d, want 400 for empty tenant ID", results[0].Status)
}
if results[0].Error.Code != ErrCodeInvalidRequest {
t.Errorf("Error code = %s, want %s", results[0].Error.Code, ErrCodeInvalidRequest)
}
}
// R-3: Test empty request ID validation
func TestSecurity_EmptyRequestIDRejected(t *testing.T) {
orch := New()
orch.RegisterRecipe("test", func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
})
batch := []SubRequest{
{ID: "", TenantID: "tenant-a", Recipe: "test"}, // Empty ID
}
results := orch.ExecuteBatch(context.Background(), batch)
if results[0].Status != 400 {
t.Errorf("Status = %d, want 400 for empty request ID", results[0].Status)
}
if results[0].Error.Code != ErrCodeInvalidRequest {
t.Errorf("Error code = %s, want %s", results[0].Error.Code, ErrCodeInvalidRequest)
}
}
// R-3: Test empty recipe name validation
func TestSecurity_EmptyRecipeRejected(t *testing.T) {
orch := New()
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: ""}, // Empty recipe
}
results := orch.ExecuteBatch(context.Background(), batch)
if results[0].Status != 400 {
t.Errorf("Status = %d, want 400 for empty recipe", results[0].Status)
}
if results[0].Error.Code != ErrCodeInvalidRequest {
t.Errorf("Error code = %s, want %s", results[0].Error.Code, ErrCodeInvalidRequest)
}
}
// R-4: Test nil handler registration panics
func TestSecurity_NilHandlerPanics(t *testing.T) {
orch := New()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when registering nil handler, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "handler cannot be nil") {
t.Errorf("Panic message = %q, want 'handler cannot be nil'", msg)
}
}
}()
orch.RegisterRecipe("test", nil)
}
// R-4: Test empty recipe name registration panics
func TestSecurity_EmptyRecipeNamePanics(t *testing.T) {
orch := New()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when registering empty recipe name, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "recipe name cannot be empty") {
t.Errorf("Panic message = %q, want 'recipe name cannot be empty'", msg)
}
}
}()
handler := func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
}
orch.RegisterRecipe("", handler)
}
// R-5: Test nil execution hook defaults to NoOpHook
func TestSecurity_NilExecutionHookSafe(t *testing.T) {
orch := New(WithExecutionHook(nil))
orch.RegisterRecipe("test", func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "test"},
}
// Should not panic
results := orch.ExecuteBatch(context.Background(), batch)
if results[0].Status != 200 {
t.Errorf("Status = %d, want 200 (nil hook should be safe)", results[0].Status)
}
}
// R-5: Test nil panic hook defaults to NoOpHook
func TestSecurity_NilPanicHookSafe(t *testing.T) {
orch := New(WithPanicHook(nil))
orch.RegisterRecipe("panic-recipe", func(ctx context.Context, payload interface{}) (interface{}, error) {
panic("test")
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "panic-recipe"},
}
// Should not panic even though recipe panics and panic hook is nil
results := orch.ExecuteBatch(context.Background(), batch)
if results[0].Status != 500 {
t.Errorf("Status = %d, want 500", results[0].Status)
}
if results[0].Error.Code != ErrCodePanic {
t.Errorf("Error code = %s, want %s", results[0].Error.Code, ErrCodePanic)
}
}
// Integration: Test tenant isolation with validation
func TestSecurity_TenantIsolationWithValidation(t *testing.T) {
orch := New()
var receivedTenants []string
var mu sync.Mutex
orch.RegisterRecipe("capture", func(ctx context.Context, payload interface{}) (interface{}, error) {
tenantID, ok := TenantID(ctx)
if !ok {
return nil, errors.New("no tenant ID in context")
}
mu.Lock()
receivedTenants = append(receivedTenants, tenantID)
mu.Unlock()
return tenantID, nil
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "capture"},
{ID: "2", TenantID: "", Recipe: "capture"}, // Invalid - empty tenant
{ID: "3", TenantID: "tenant-b", Recipe: "capture"},
}
results := orch.ExecuteBatch(context.Background(), batch)
// Request 1: Success
if results[0].Status != 200 {
t.Errorf("Result 0 status = %d, want 200", results[0].Status)
}
// Request 2: Rejected due to empty tenant ID
if results[1].Status != 400 {
t.Errorf("Result 1 status = %d, want 400 (empty tenant)", results[1].Status)
}
// Request 3: Success
if results[2].Status != 200 {
t.Errorf("Result 2 status = %d, want 200", results[2].Status)
}
// Only valid tenants should have been processed
mu.Lock()
receivedCount := len(receivedTenants)
tenantsCopy := make([]string, len(receivedTenants))
copy(tenantsCopy, receivedTenants)
mu.Unlock()
if receivedCount != 2 {
t.Errorf("Received %d tenants, want 2 (invalid request not processed)", receivedCount)
}
// Check that both valid tenants were processed (order doesn't matter due to concurrency)
tenantMap := make(map[string]bool)
for _, tenant := range tenantsCopy {
tenantMap[tenant] = true
}
if !tenantMap["tenant-a"] || !tenantMap["tenant-b"] {
t.Errorf("Received tenants = %v, want both tenant-a and tenant-b", tenantsCopy)
}
}
// Regression: Ensure fixes don't break existing functionality
func TestSecurity_RegressionCheck(t *testing.T) {
orch := New(
WithTimeout(1*time.Second),
WithMaxBatchSize(100),
WithMaxConcurrency(10),
)
orch.RegisterRecipe("echo", func(ctx context.Context, payload interface{}) (interface{}, error) {
return payload, nil
})
orch.RegisterRecipe("error", func(ctx context.Context, payload interface{}) (interface{}, error) {
return nil, errors.New("test error")
})
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "echo", Payload: "test"},
{ID: "2", TenantID: "tenant-b", Recipe: "error"},
{ID: "3", TenantID: "tenant-c", Recipe: "nonexistent"},
}
results := orch.ExecuteBatch(context.Background(), batch)
// Result 1: Success
if results[0].Status != 200 {
t.Errorf("Result 0 status = %d, want 200", results[0].Status)
}
// Result 2: Recipe error
if results[1].Status != 500 || results[1].Error.Code != ErrCodeRecipeExecution {
t.Errorf("Result 1 status = %d code = %s, want 500 and RECIPE_EXECUTION",
results[1].Status, results[1].Error.Code)
}
// Result 3: Recipe not found
if results[2].Status != 404 || results[2].Error.Code != ErrCodeRecipeNotFound {
t.Errorf("Result 2 status = %d code = %s, want 404 and RECIPE_NOT_FOUND",
results[2].Status, results[2].Error.Code)
}
}
// Phase 2 Security Tests
// R-6: Test semaphore respects context cancellation
func TestSecurity_SemaphoreRespectsContextCancellation(t *testing.T) {
// Create orchestrator with very low concurrency limit
orch := New(WithMaxConcurrency(1))
// Register a slow recipe that blocks
blockChan := make(chan struct{})
orch.RegisterRecipe("slow", func(ctx context.Context, payload interface{}) (interface{}, error) {
<-blockChan // Block until we release
return "ok", nil
})
// Create context that we'll cancel
ctx, cancel := context.WithCancel(context.Background())
// Start first request to occupy the semaphore slot
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
batch := []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "slow"},
}
orch.ExecuteBatch(context.Background(), batch)
}()
// Give first request time to acquire semaphore
time.Sleep(50 * time.Millisecond)
// Now submit second request that should wait on semaphore
results := make(chan []Response, 1)
go func() {
batch := []SubRequest{
{ID: "2", TenantID: "tenant-b", Recipe: "slow"},
}
results <- orch.ExecuteBatch(ctx, batch)
}()
// Give second request time to start waiting
time.Sleep(50 * time.Millisecond)
// Cancel the context while second request is waiting
cancel()
// Get result from second request
result := <-results
// Verify second request got cancellation error
if result[0].Status != 504 {
t.Errorf("Status = %d, want 504 for cancelled request", result[0].Status)
}
if result[0].Error.Code != ErrCodeTimeout {
t.Errorf("Error code = %s, want %s", result[0].Error.Code, ErrCodeTimeout)
}
if !strings.Contains(result[0].Error.Message, "cancelled while waiting") {
t.Errorf("Error message = %q, want message about cancellation", result[0].Error.Message)
}
// Release first request and wait for completion
close(blockChan)
wg.Wait()
}
// R-7: Test RegisterRecipeStrict rejects duplicates
func TestSecurity_RegisterRecipeStrict_RejectsDuplicates(t *testing.T) {
orch := New()
handler := func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
}
// First registration should succeed
err := orch.RegisterRecipeStrict("test", handler)
if err != nil {
t.Errorf("First registration failed: %v", err)
}
// Second registration should fail
err = orch.RegisterRecipeStrict("test", handler)
if err == nil {
t.Error("Expected error for duplicate registration, got nil")
}
if err != nil && !strings.Contains(err.Error(), "already registered") {
t.Errorf("Error message = %q, want 'already registered'", err.Error())
}
}
// R-7: Test RegisterRecipeStrict with empty name panics
func TestSecurity_RegisterRecipeStrict_EmptyNamePanics(t *testing.T) {
orch := New()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for empty recipe name, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "recipe name cannot be empty") {
t.Errorf("Panic message = %q, want 'recipe name cannot be empty'", msg)
}
}
}()
handler := func(ctx context.Context, payload interface{}) (interface{}, error) {
return "ok", nil
}
_ = orch.RegisterRecipeStrict("", handler)
}
// R-7: Test RegisterRecipeStrict with nil handler panics
func TestSecurity_RegisterRecipeStrict_NilHandlerPanics(t *testing.T) {
orch := New()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for nil handler, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "handler cannot be nil") {
t.Errorf("Panic message = %q, want 'handler cannot be nil'", msg)
}
}
}()
_ = orch.RegisterRecipeStrict("test", nil)
}
// R-8: Test WithTimeout panics on zero timeout
func TestSecurity_WithTimeout_ZeroPanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for zero timeout, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "timeout must be positive") {
t.Errorf("Panic message = %q, want 'timeout must be positive'", msg)
}
}
}()
_ = New(WithTimeout(0))
}
// R-8: Test WithTimeout panics on negative timeout
func TestSecurity_WithTimeout_NegativePanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for negative timeout, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "timeout must be positive") {
t.Errorf("Panic message = %q, want 'timeout must be positive'", msg)
}
}
}()
_ = New(WithTimeout(-1 * time.Second))
}
// R-8: Test WithMaxConcurrency panics on negative value
func TestSecurity_WithMaxConcurrency_NegativePanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for negative max concurrency, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "max concurrency must be non-negative") {
t.Errorf("Panic message = %q, want 'max concurrency must be non-negative'", msg)
}
}
}()
_ = New(WithMaxConcurrency(-1))
}
// R-8: Test WithMaxBatchSize panics on negative value
func TestSecurity_WithMaxBatchSize_NegativePanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for negative max batch size, got none")
} else {
msg := r.(string)
if !strings.Contains(msg, "max batch size must be non-negative") {
t.Errorf("Panic message = %q, want 'max batch size must be non-negative'", msg)
}
}
}()
_ = New(WithMaxBatchSize(-1))
}
// Additional: Test recipe overwrite still works with normal RegisterRecipe
func TestSecurity_RecipeOverwrite(t *testing.T) {
orch := New()
handler1 := func(ctx context.Context, payload interface{}) (interface{}, error) {
return "v1", nil
}
handler2 := func(ctx context.Context, payload interface{}) (interface{}, error) {
return "v2", nil
}
// Register first handler
orch.RegisterRecipe("test", handler1)
// Execute and verify v1
results := orch.ExecuteBatch(context.Background(), []SubRequest{
{ID: "1", TenantID: "tenant-a", Recipe: "test"},
})
if results[0].Data != "v1" {
t.Errorf("First handler returned %v, want 'v1'", results[0].Data)
}
// Register second handler (overwrite)
orch.RegisterRecipe("test", handler2)
// Execute and verify v2
results = orch.ExecuteBatch(context.Background(), []SubRequest{
{ID: "2", TenantID: "tenant-a", Recipe: "test"},
})
if results[0].Data != "v2" {
t.Errorf("Second handler returned %v, want 'v2'", results[0].Data)
}
}