python_malicious_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package integrationtests_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  6. "github.com/langgenius/dify-sandbox/internal/service"
  7. )
  8. func TestSysFork(t *testing.T) {
  9. // Test case for sys_fork
  10. resp := service.RunPython3Code(`
  11. import os
  12. os.fork()
  13. `, "", &types.RunnerOptions{})
  14. if resp.Code != 0 {
  15. t.Error(resp)
  16. }
  17. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  18. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  19. }
  20. }
  21. func TestExec(t *testing.T) {
  22. // Test case for exec
  23. resp := service.RunPython3Code(`
  24. import os
  25. os.execl("/bin/ls", "ls")
  26. `, "", &types.RunnerOptions{})
  27. if resp.Code != 0 {
  28. t.Error(resp)
  29. }
  30. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  31. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  32. }
  33. }
  34. func TestRunCommand(t *testing.T) {
  35. // Test case for run_command
  36. resp := service.RunPython3Code(`
  37. import subprocess
  38. subprocess.run(["ls", "-l"])
  39. `, "", &types.RunnerOptions{})
  40. if resp.Code != 0 {
  41. t.Error(resp)
  42. }
  43. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  44. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  45. }
  46. }