Skip to content

Commit 8f0dc33

Browse files
committed
feat: use agent-infra sandbox instead of seccomp
1 parent 43262ea commit 8f0dc33

25 files changed

Lines changed: 1131 additions & 59 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# log
22
logs/
3+
log/
34
# editor config
45
.idea/
56
.vscode/
@@ -10,4 +11,5 @@ logs/
1011
env
1112
main
1213

13-
*.gen.dockerfile
14+
*.gen.dockerfile
15+
.menv/

README.md

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,78 @@
22
## Introduction
33
Dify-Sandbox offers a simple way to run untrusted code in a secure environment. It is designed to be used in a multi-tenant environment, where multiple users can submit code to be executed. The code is executed in a sandboxed environment, which restricts the resources and system calls that the code can access.
44

5+
## Features
6+
- **Multi-backend Support**: Supports both native Linux sandbox (chroot + seccomp) and agent-infra/sandbox for cross-platform isolation
7+
- **Cross-platform**: When using agent-infra/sandbox backend, works on macOS, Linux, and Windows
8+
- **Multiple Languages**: Supports Python 3 and Node.js code execution
9+
- **Secure Isolation**: Hardware or OS-level isolation for secure code execution
10+
- **Flexible Configuration**: Easy configuration via YAML file
11+
512
## Use
6-
### Requirements
7-
DifySandbox currently only supports Linux, as it's designed for docker containers. It requires the following dependencies:
13+
14+
### Native Backend (Linux Only)
15+
The native backend uses Linux chroot and seccomp for isolation.
16+
17+
#### Requirements
18+
- Linux operating system
819
- libseccomp
920
- pkg-config
1021
- gcc
11-
- golang 1.20.6
22+
- golang 1.25.4 or higher
1223

13-
### Steps
24+
#### Steps
1425
1. Clone the repository using `git clone https://github.com/langgenius/dify-sandbox` and navigate to the project directory.
15-
2. Run ./install.sh to install the necessary dependencies.
16-
3. Run ./build/build_[amd64|arm64].sh to build the sandbox binary.
17-
4. Run ./main to start the server.
26+
2. Run `./install.sh` to install the necessary dependencies.
27+
3. Run `./build/build_[amd64|arm64].sh` to build the sandbox binary.
28+
4. Edit `conf/config.yaml` and set `sandbox_backend: "native"` (default).
29+
5. Run `./main` to start the server.
30+
31+
### agent-infra/sandbox Backend (Cross-platform)
32+
The sandbox backend uses [agent-infra/sandbox](https://github.com/agent-infra/sandbox) for cross-platform isolation.
33+
34+
#### Requirements
35+
- Docker
36+
- golang 1.25.4 or higher
37+
38+
#### Installation
39+
Run the sandbox server using Docker:
40+
41+
**Default (global):**
42+
```bash
43+
docker run --security-opt seccomp=unconfined --rm -it -p 10000:8080 ghcr.io/agent-infra/sandbox:latest
44+
```
45+
46+
**For users in mainland China:**
47+
```bash
48+
docker run --security-opt seccomp=unconfined --rm -it -p 10000:8080 enterprise-public-cn-beijing.cr.volces.com/vefaas-public/all-in-one-sandbox:latest
49+
```
50+
51+
**Use a specific version** (format: `1.0.0.${version}`):
52+
```bash
53+
docker run --security-opt seccomp=unconfined --rm -it -p 10000:8080 ghcr.io/agent-infra/sandbox:1.0.0.150
54+
```
55+
56+
Note: The command maps port 8080 in the container to port 10000 on the host to match the default configuration.
57+
58+
#### Configuration
59+
Edit `conf/config.yaml`:
60+
```yaml
61+
# Use sandbox backend
62+
sandbox_backend: "microsandbox"
63+
64+
microsandbox:
65+
enabled: true
66+
server_address: "http://127.0.0.1:10000" # sandbox server address
67+
```
68+
69+
#### Steps
70+
1. Clone the repository: `git clone https://github.com/langgenius/dify-sandbox`
71+
2. Navigate to the project directory
72+
3. Build the Go binary: `go build -o main ./cmd/server`
73+
4. Configure `conf/config.yaml` with sandbox settings
74+
5. Run `./main` to start the server
1875

76+
### Debugging
1977
If you want to debug the server, firstly use build script to build the sandbox library binaries, then debug as you want with your IDE.
2078

2179

@@ -25,4 +83,4 @@ Refer to the [FAQ document](FAQ.md)
2583

2684

2785
## Workflow
28-
![workflow](workflow.png)
86+
![workflow](workflow.png)

conf/config.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ app:
44
key: dify-sandbox
55
max_workers: 4
66
max_requests: 50
7-
worker_timeout: 5
7+
worker_timeout: 1
88
python_path: /usr/local/bin/python3
99
enable_network: True # please make sure there is no network risk in your environment
1010
enable_preload: False # please keep it as False for security purposes
@@ -13,3 +13,15 @@ proxy:
1313
socks5: ''
1414
http: ''
1515
https: ''
16+
17+
# Sandbox backend configuration
18+
# Available backends: "native" (default) or "microsandbox"
19+
# - native: Uses Linux chroot + seccomp for isolation (Linux only)
20+
# - microsandbox: Uses microsandbox for cross-platform isolation
21+
sandbox_backend: "microsandbox"
22+
23+
# Microsandbox configuration (only used when sandbox_backend is "microsandbox")
24+
microsandbox:
25+
enabled: true # Set to true to enable microsandbox backend
26+
server_address: "http://127.0.0.1:10000" # Optional: microsandbox server address (if using remote server)
27+

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module github.com/langgenius/dify-sandbox
22

3-
go 1.24.0
3+
go 1.25.4
44

55
require (
6+
github.com/agent-infra/sandbox-sdk-go v0.0.2
67
github.com/gin-gonic/gin v1.10.0
78
github.com/google/uuid v1.6.0
9+
github.com/microsandbox/microsandbox/sdk/go v0.0.0-20260105133323-6f13543feae0
810
github.com/seccomp/libseccomp-golang v0.11.0
911
gopkg.in/yaml.v3 v3.0.1
1012
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/agent-infra/sandbox-sdk-go v0.0.2 h1:excmpAxpVup0oQG3NyBBV1ZVMSODyS04Arm6fyo5NYs=
2+
github.com/agent-infra/sandbox-sdk-go v0.0.2/go.mod h1:LcR1ZvwCSafPmNj1+RA6myQRMHWs1DZL7HoPMFJGSDo=
13
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
24
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
35
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@@ -40,6 +42,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
4042
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
4143
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
4244
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
45+
github.com/microsandbox/microsandbox/sdk/go v0.0.0-20260105133323-6f13543feae0 h1:I/t88dcssSrsh3F/BED9rjeS0mlRf+lleg8g3szuIJg=
46+
github.com/microsandbox/microsandbox/sdk/go v0.0.0-20260105133323-6f13543feae0/go.mod h1:Dr+SHwunRAGqJgTrqraUD8kqgv9+hi51d5Rly/V/ljs=
4347
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4448
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
4549
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

internal/controller/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func RunSandboxController(c *gin.Context) {
1616
}) {
1717
switch req.Language {
1818
case "python3":
19-
c.JSON(200, service.RunPython3Code(req.Code, req.Preload, &runner_types.RunnerOptions{
19+
c.JSON(200, service.RunPython3Code(c, req.Code, req.Preload, &runner_types.RunnerOptions{
2020
EnableNetwork: req.EnableNetwork,
2121
}))
2222
case "nodejs":
23-
c.JSON(200, service.RunNodeJsCode(req.Code, req.Preload, &runner_types.RunnerOptions{
23+
c.JSON(200, service.RunNodeJsCode(c, req.Code, req.Preload, &runner_types.RunnerOptions{
2424
EnableNetwork: req.EnableNetwork,
2525
}))
2626
default:

internal/core/runner/nodejs/nodejs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//go:build linux
2+
13
package nodejs
24

35
import (
6+
"context"
47
_ "embed"
58
"encoding/base64"
69
"fmt"
@@ -36,6 +39,7 @@ var (
3639
)
3740

3841
func (p *NodeJsRunner) Run(
42+
ctx context.Context,
3943
code string,
4044
timeout time.Duration,
4145
stdin []byte,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !linux
2+
3+
package nodejs
4+
5+
import (
6+
"context"
7+
"time"
8+
9+
"github.com/langgenius/dify-sandbox/internal/core/runner"
10+
"github.com/langgenius/dify-sandbox/internal/core/runner/types"
11+
"github.com/langgenius/dify-sandbox/internal/utils/log"
12+
)
13+
14+
type NodeJsRunner struct {
15+
runner.TempDirRunner
16+
}
17+
18+
func (p *NodeJsRunner) Run(
19+
ctx context.Context,
20+
code string,
21+
timeout time.Duration,
22+
stdin []byte,
23+
preload string,
24+
options *types.RunnerOptions,
25+
) (chan []byte, chan []byte, chan bool, error) {
26+
log.Error("Node.js native runner is only supported on Linux. Please configure sandbox_backend to 'microsandbox' in config.yaml")
27+
return nil, nil, nil, nil
28+
}
29+
30+
func (p *NodeJsRunner) InitializeEnvironment(code string, preload string, root_path string) (string, error) {
31+
log.Error("Node.js native runner is only supported on Linux. Please configure sandbox_backend to 'microsandbox' in config.yaml")
32+
return "", nil
33+
}

internal/core/runner/nodejs/setup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux
2+
13
package nodejs
24

35
import (
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build !linux
2+
3+
package nodejs
4+
5+
import (
6+
"github.com/langgenius/dify-sandbox/internal/utils/log"
7+
)
8+
9+
const (
10+
LIB_PATH = "/var/sandbox/sandbox-nodejs"
11+
LIB_NAME = "nodejs.so"
12+
PROJECT_NAME = "nodejs-project"
13+
)
14+
15+
func init() {
16+
log.Warn("Node.js native runner is only supported on Linux. Please use microsandbox backend on this platform.")
17+
}
18+
19+
func releaseLibBinary() {
20+
log.Warn("Cannot release Node.js lib binary on non-Linux platform")
21+
}
22+
23+
func checkLibAvaliable() bool {
24+
return false
25+
}

0 commit comments

Comments
 (0)