python_malicious_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. print(os.fork())
  13. print(123)
  14. `, "", &types.RunnerOptions{})
  15. if resp.Code != 0 {
  16. t.Error(resp)
  17. }
  18. if resp.Data.(*service.RunCodeResponse).Stdout != "0\n123\n" {
  19. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  20. }
  21. }
  22. func TestExec(t *testing.T) {
  23. // Test case for exec
  24. resp := service.RunPython3Code(`
  25. import os
  26. os.execl("/bin/ls", "ls")
  27. `, "", &types.RunnerOptions{})
  28. if resp.Code != 0 {
  29. t.Error(resp)
  30. }
  31. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  32. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  33. }
  34. }
  35. func TestRunCommand(t *testing.T) {
  36. // Test case for run_command
  37. resp := service.RunPython3Code(`
  38. import subprocess
  39. subprocess.run(["ls", "-l"])
  40. `, "", &types.RunnerOptions{})
  41. if resp.Code != 0 {
  42. t.Error(resp)
  43. }
  44. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  45. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  46. }
  47. }