python.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package python
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/google/uuid"
  11. "github.com/langgenius/dify-sandbox/internal/core/runner"
  12. python_dependencies "github.com/langgenius/dify-sandbox/internal/core/runner/python/dependencies"
  13. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  14. "github.com/langgenius/dify-sandbox/internal/static"
  15. )
  16. type PythonRunner struct {
  17. runner.TempDirRunner
  18. }
  19. //go:embed prescript.py
  20. var python_sandbox_fs []byte
  21. var (
  22. PYTHON_REQUIRED_FS = []string{
  23. "/tmp/sandbox-python/python.so",
  24. "/etc/ssl/certs/ca-certificates.crt",
  25. "/usr/local/lib/python3.10/site-packages/certifi/cacert.pem",
  26. "/usr/local/lib/python3.10/dist-packages/certifi/cacert.pem",
  27. "/etc/nsswitch.conf",
  28. "/etc/resolv.conf",
  29. "/run/systemd/resolve/stub-resolv.conf",
  30. "/run/resolvconf/resolv.conf",
  31. "/etc/hosts",
  32. }
  33. )
  34. func (p *PythonRunner) Run(
  35. code string,
  36. timeout time.Duration,
  37. stdin []byte,
  38. preload string,
  39. options *types.RunnerOptions,
  40. ) (chan []byte, chan []byte, chan bool, error) {
  41. // initialize the environment
  42. untrusted_code_path, err := p.InitializeEnvironment(code, preload, options)
  43. if err != nil {
  44. return nil, nil, nil, err
  45. }
  46. // capture the output
  47. output_handler := runner.NewOutputCaptureRunner()
  48. output_handler.SetTimeout(timeout)
  49. err = p.WithTempDir(PYTHON_REQUIRED_FS, func(root_path string) error {
  50. // cleanup
  51. output_handler.SetAfterExitHook(func() {
  52. os.RemoveAll(root_path)
  53. os.Remove(root_path)
  54. })
  55. // create a new process
  56. cmd := exec.Command(
  57. static.GetDifySandboxGlobalConfigurations().PythonPath,
  58. untrusted_code_path,
  59. )
  60. cmd.Env = []string{}
  61. err = output_handler.CaptureOutput(cmd)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. })
  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, error) {
  73. // create a tmp dir and copy the python script
  74. temp_code_name := strings.ReplaceAll(uuid.New().String(), "-", "_")
  75. temp_code_name = strings.ReplaceAll(temp_code_name, "/", ".")
  76. packages_preload := make([]string, len(options.Dependencies))
  77. for i, dependency := range options.Dependencies {
  78. packages_preload[i] = python_dependencies.GetDependencies(dependency.Name, dependency.Version)
  79. }
  80. script := strings.Replace(
  81. string(python_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%s", preload, strings.Join(packages_preload, "\n")),
  103. 1,
  104. )
  105. code = strings.Replace(
  106. script,
  107. "{{code}}",
  108. code,
  109. 1,
  110. )
  111. untrusted_code_path := fmt.Sprintf("/tmp/code/%s.py", temp_code_name)
  112. err := os.MkdirAll("/tmp/code", 0755)
  113. if err != nil {
  114. return "", err
  115. }
  116. err = os.WriteFile(untrusted_code_path, []byte(code), 0755)
  117. if err != nil {
  118. return "", err
  119. }
  120. return untrusted_code_path, nil
  121. }