python.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package python
  2. import (
  3. "crypto/rand"
  4. _ "embed"
  5. "encoding/base64"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/google/uuid"
  14. "github.com/langgenius/dify-sandbox/internal/core/runner"
  15. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  16. "github.com/langgenius/dify-sandbox/internal/static"
  17. )
  18. type PythonRunner struct {
  19. runner.TempDirRunner
  20. }
  21. //go:embed prescript.py
  22. var sandbox_fs []byte
  23. func (p *PythonRunner) Run(
  24. code string,
  25. timeout time.Duration,
  26. stdin []byte,
  27. preload string,
  28. options *types.RunnerOptions,
  29. ) (chan []byte, chan []byte, chan bool, error) {
  30. configuration := static.GetDifySandboxGlobalConfigurations()
  31. // initialize the environment
  32. untrusted_code_path, key, err := p.InitializeEnvironment(code, preload, options)
  33. if err != nil {
  34. return nil, nil, nil, err
  35. }
  36. // capture the output
  37. output_handler := runner.NewOutputCaptureRunner()
  38. output_handler.SetTimeout(timeout)
  39. // create a new process
  40. cmd := exec.Command(
  41. configuration.PythonPath,
  42. untrusted_code_path,
  43. LIB_PATH,
  44. key,
  45. )
  46. cmd.Env = []string{}
  47. cmd.Dir = LIB_PATH
  48. if configuration.Proxy.Socks5 != "" {
  49. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTPS_PROXY=%s", configuration.Proxy.Socks5))
  50. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_PROXY=%s", configuration.Proxy.Socks5))
  51. } else if configuration.Proxy.Https != "" || configuration.Proxy.Http != "" {
  52. if configuration.Proxy.Https != "" {
  53. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTPS_PROXY=%s", configuration.Proxy.Https))
  54. }
  55. if configuration.Proxy.Http != "" {
  56. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_PROXY=%s", configuration.Proxy.Http))
  57. }
  58. }
  59. if len(configuration.AllowedSyscalls) > 0 {
  60. cmd.Env = append(cmd.Env,
  61. fmt.Sprintf("ALLOWED_SYSCALLS=%s",
  62. strings.Trim(strings.Join(strings.Fields(fmt.Sprint(configuration.AllowedSyscalls)), ","), "[]"),
  63. ),
  64. )
  65. }
  66. err = output_handler.CaptureOutput(cmd)
  67. if err != nil {
  68. return nil, nil, nil, err
  69. }
  70. return output_handler.GetStdout(), output_handler.GetStderr(), output_handler.GetDone(), nil
  71. }
  72. func (p *PythonRunner) InitializeEnvironment(code string, preload string, options *types.RunnerOptions) (string, string, error) {
  73. if !checkLibAvaliable() {
  74. // ensure environment is reversed
  75. releaseLibBinary(false)
  76. }
  77. // create a tmp dir and copy the python script
  78. temp_code_name := strings.ReplaceAll(uuid.New().String(), "-", "_")
  79. temp_code_name = strings.ReplaceAll(temp_code_name, "/", ".")
  80. script := strings.Replace(
  81. string(sandbox_fs),
  82. "{{uid}}", strconv.Itoa(static.SANDBOX_USER_UID), 1,
  83. )
  84. script = strings.Replace(
  85. script,
  86. "{{gid}}", strconv.Itoa(static.SANDBOX_GROUP_ID), 1,
  87. )
  88. if options.EnableNetwork {
  89. script = strings.Replace(
  90. script,
  91. "{{enable_network}}", "1", 1,
  92. )
  93. } else {
  94. script = strings.Replace(
  95. script,
  96. "{{enable_network}}", "0", 1,
  97. )
  98. }
  99. script = strings.Replace(
  100. script,
  101. "{{preload}}",
  102. fmt.Sprintf("%s\n", preload),
  103. 1,
  104. )
  105. // generate a random 512 bit key
  106. key_len := 64
  107. key := make([]byte, key_len)
  108. _, err := rand.Read(key)
  109. if err != nil {
  110. return "", "", err
  111. }
  112. // encrypt the code
  113. encrypted_code := make([]byte, len(code))
  114. for i := 0; i < len(code); i++ {
  115. encrypted_code[i] = code[i] ^ key[i%key_len]
  116. }
  117. // encode code using base64
  118. code = base64.StdEncoding.EncodeToString(encrypted_code)
  119. // encode key using base64
  120. encoded_key := base64.StdEncoding.EncodeToString(key)
  121. code = strings.Replace(
  122. script,
  123. "{{code}}",
  124. code,
  125. 1,
  126. )
  127. untrusted_code_path := fmt.Sprintf("%s/tmp/%s.py", LIB_PATH, temp_code_name)
  128. err = os.MkdirAll(path.Dir(untrusted_code_path), 0755)
  129. if err != nil {
  130. return "", "", err
  131. }
  132. err = os.WriteFile(untrusted_code_path, []byte(code), 0755)
  133. if err != nil {
  134. return "", "", err
  135. }
  136. return untrusted_code_path, encoded_key, nil
  137. }