MCPcopy Create free account
hub / github.com/astercloud/aster / TestLocalSandbox_SecurityLevels

Function TestLocalSandbox_SecurityLevels

pkg/sandbox/local_test.go:343–396  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

341}
342
343func TestLocalSandbox_SecurityLevels(t *testing.T) {
344 tmpDir, err := os.MkdirTemp("", "sandbox-test-*")
345 if err != nil {
346 t.Fatalf("failed to create temp dir: %v", err)
347 }
348 defer func() { _ = os.RemoveAll(tmpDir) }()
349
350 tests := []struct {
351 name string
352 securityLevel SecurityLevel
353 command string
354 shouldBlock bool
355 }{
356 {
357 name: "basic level allows ls",
358 securityLevel: SecurityLevelBasic,
359 command: "ls -la",
360 shouldBlock: false,
361 },
362 {
363 name: "strict level allows whitelisted command",
364 securityLevel: SecurityLevelStrict,
365 command: "ls -la",
366 shouldBlock: false,
367 },
368 {
369 name: "strict level blocks non-whitelisted command",
370 securityLevel: SecurityLevelStrict,
371 command: "nc -l 8080",
372 shouldBlock: true,
373 },
374 }
375
376 for _, tt := range tests {
377 t.Run(tt.name, func(t *testing.T) {
378 sb, err := NewLocalSandbox(&LocalSandboxConfig{
379 WorkDir: tmpDir,
380 SecurityLevel: tt.securityLevel,
381 })
382 if err != nil {
383 t.Fatalf("failed to create sandbox: %v", err)
384 }
385
386 result, err := sb.Exec(context.Background(), tt.command, nil)
387 if err != nil {
388 t.Fatalf("exec failed: %v", err)
389 }
390
391 if tt.shouldBlock && result.Code == 0 {
392 t.Errorf("command should be blocked: %s", tt.command)
393 }
394 })
395 }
396}
397
398func TestLocalSandbox_EnhancedDangerousPatterns(t *testing.T) {
399 tmpDir, err := os.MkdirTemp("", "sandbox-test-*")

Callers

nothing calls this directly

Calls 3

ExecMethod · 0.95
NewLocalSandboxFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected