main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-463ec16c-8796-4e8f-988a-f61de7dc6976/tmp/sandbox-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 := 124; j < 125; j++ {
  50. if find_syscall(j, list[i]) == -1 {
  51. list[i] = append(list[i], j)
  52. }
  53. }
  54. for j := 220; j < 221; j++ {
  55. if find_syscall(j, list[i]) == -1 {
  56. list[i] = append(list[i], j)
  57. }
  58. }
  59. for j := 293; j < 294; j++ {
  60. if find_syscall(j, list[i]) == -1 {
  61. list[i] = append(list[i], j)
  62. }
  63. }
  64. }
  65. lock := sync.Mutex{}
  66. wg := sync.WaitGroup{}
  67. i := 0
  68. // run 4 tasks concurrently
  69. for j := 0; j < 4; j++ {
  70. wg.Add(1)
  71. go func() {
  72. defer wg.Done()
  73. for {
  74. lock.Lock()
  75. if i >= len(list) {
  76. lock.Unlock()
  77. return
  78. }
  79. task := list[i]
  80. i++
  81. lock.Unlock()
  82. run(task)
  83. }
  84. }()
  85. }
  86. // wait for all tasks to finish
  87. wg.Wait()
  88. }