main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "github.com/langgenius/dify-sandbox/internal/static/nodejs_syscall"
  10. )
  11. const (
  12. SYSCALL_NUMS = 400
  13. )
  14. func run(allowed_syscalls []int) {
  15. os.Chdir("/tmp/sandbox-f5faacb9-f441-43ec-a077-a09a8b7cc7f0/var/sandbox/sandbox-nodejs/nodejs-project/node_temp/node_temp")
  16. nums := []string{}
  17. for _, syscall := range allowed_syscalls {
  18. nums = append(nums, strconv.Itoa(syscall))
  19. }
  20. os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
  21. _, err := exec.Command("node", "test.js", "65537", "1001", "{\"enable_network\":true}").Output()
  22. if err == nil {
  23. fmt.Println("success")
  24. } else {
  25. fmt.Println("failed")
  26. }
  27. }
  28. func find_syscall(syscall int, syscalls []int) int {
  29. for i, s := range syscalls {
  30. if s == syscall {
  31. return i
  32. }
  33. }
  34. return -1
  35. }
  36. func main() {
  37. original := nodejs_syscall.ALLOW_SYSCALLS
  38. original = append(original, nodejs_syscall.ALLOW_NETWORK_SYSCALLS...)
  39. original = append(original, nodejs_syscall.ALLOW_ERROR_SYSCALLS...)
  40. // generate task list
  41. list := make([][]int, SYSCALL_NUMS)
  42. for i := 0; i < SYSCALL_NUMS; i++ {
  43. list[i] = make([]int, len(original))
  44. copy(list[i], original)
  45. // add i
  46. if find_syscall(i, original) == -1 {
  47. list[i] = append(list[i], i)
  48. }
  49. for j := 0; j < 0; j++ {
  50. if find_syscall(j, list[i]) == -1 {
  51. list[i] = append(list[i], j)
  52. }
  53. }
  54. }
  55. lock := sync.Mutex{}
  56. wg := sync.WaitGroup{}
  57. i := 0
  58. // run 4 tasks concurrently
  59. for j := 0; j < 4; j++ {
  60. wg.Add(1)
  61. go func() {
  62. defer wg.Done()
  63. for {
  64. lock.Lock()
  65. if i >= len(list) {
  66. lock.Unlock()
  67. return
  68. }
  69. task := list[i]
  70. i++
  71. lock.Unlock()
  72. run(task)
  73. }
  74. }()
  75. }
  76. // wait for all tasks to finish
  77. wg.Wait()
  78. }