python_malicious_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. EnableNetwork: true,
  16. })
  17. if resp.Code != 0 {
  18. t.Error(resp)
  19. }
  20. if resp.Data.(*service.RunCodeResponse).Stdout != "0\n123\n" {
  21. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  22. }
  23. }
  24. func TestExec(t *testing.T) {
  25. // Test case for exec
  26. resp := service.RunPython3Code(`
  27. import os
  28. os.execl("/bin/ls", "ls")
  29. `, "", &types.RunnerOptions{
  30. EnableNetwork: true,
  31. })
  32. if resp.Code != 0 {
  33. t.Error(resp)
  34. }
  35. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  36. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  37. }
  38. }
  39. func TestRunCommand(t *testing.T) {
  40. // Test case for run_command
  41. resp := service.RunPython3Code(`
  42. import subprocess
  43. subprocess.run(["ls", "-l"])
  44. `, "", &types.RunnerOptions{
  45. EnableNetwork: true,
  46. })
  47. if resp.Code != 0 {
  48. t.Error(resp)
  49. }
  50. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "operation not permitted") {
  51. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  52. }
  53. }
  54. func TestReadEtcPasswd(t *testing.T) {
  55. resp := service.RunPython3Code(`
  56. print(open("/etc/passwd").read())
  57. `, "", &types.RunnerOptions{
  58. EnableNetwork: true,
  59. })
  60. if resp.Code != 0 {
  61. t.Error(resp)
  62. }
  63. if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stderr, "No such file or directory") {
  64. t.Error(resp.Data.(*service.RunCodeResponse).Stderr)
  65. }
  66. }