You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. # RAGFlow Sandbox
  2. A secure, pluggable code execution backend for RAGFlow and beyond.
  3. ## 🔧 Features
  4. - ✅ **Seamless RAGFlow Integration** — Out-of-the-box compatibility with the `code` component.
  5. - 🔐 **High Security** — Leverages [gVisor](https://gvisor.dev/) for syscall-level sandboxing.
  6. - 🔧 **Customizable Sandboxing** — Easily modify `seccomp` settings as needed.
  7. - 🧩 **Pluggable Runtime Support** — Easily extend to support any programming language.
  8. - ⚙️ **Developer Friendly** — Get started with a single command using `Makefile`.
  9. ## 🏗 Architecture
  10. <p align="center">
  11. <img src="asserts/code_executor_manager.svg" width="520" alt="Architecture Diagram">
  12. </p>
  13. ## 🚀 Quick Start
  14. ### 📋 Prerequisites
  15. #### Required
  16. - Linux distro compatible with gVisor
  17. - [gVisor](https://gvisor.dev/docs/user_guide/install/)
  18. - Docker >= `24.0.0`
  19. - Docker Compose >= `v2.26.1` like [RAGFlow](https://github.com/infiniflow/ragflow)
  20. - [uv](https://docs.astral.sh/uv/) as package and project manager
  21. #### Optional (Recommended)
  22. - [GNU Make](https://www.gnu.org/software/make/) for simplified CLI management
  23. ---
  24. ### 🐳 Build Docker Base Images
  25. We use isolated base images for secure containerized execution:
  26. ```bash
  27. # Build base images manually
  28. docker build -t sandbox-base-python:latest ./sandbox_base_image/python
  29. docker build -t sandbox-base-nodejs:latest ./sandbox_base_image/nodejs
  30. # OR use Makefile
  31. make build
  32. ```
  33. Then, build the executor manager image:
  34. ```bash
  35. docker build -t sandbox-executor-manager:latest ./executor_manager
  36. ```
  37. ---
  38. ### 📦 Running with RAGFlow
  39. 1. Ensure gVisor is correctly installed.
  40. 2. Configure your `.env` in `docker/.env`:
  41. - Uncomment sandbox-related variables.
  42. - Enable sandbox profile at the bottom.
  43. 3. Add the following line to `/etc/hosts` as recommended:
  44. ```text
  45. 127.0.0.1 sandbox-executor-manager
  46. ```
  47. 4. Start RAGFlow service.
  48. ---
  49. ### 🧭 Running Standalone
  50. #### Manual Setup
  51. 1. Initialize environment:
  52. ```bash
  53. cp .env.example .env
  54. ```
  55. 2. Launch:
  56. ```bash
  57. docker compose -f docker-compose.yml up
  58. ```
  59. 3. Test:
  60. ```bash
  61. source .venv/bin/activate
  62. export PYTHONPATH=$(pwd)
  63. uv pip install -r executor_manager/requirements.txt
  64. uv run tests/sandbox_security_tests_full.py
  65. ```
  66. #### With Make
  67. ```bash
  68. make # setup + build + launch + test
  69. ```
  70. ---
  71. ### 📈 Monitoring
  72. ```bash
  73. docker logs -f sandbox-executor-manager # Manual
  74. make logs # With Make
  75. ```
  76. ---
  77. ### 🧰 Makefile Toolbox
  78. | Command | Description |
  79. | ----------------- | ------------------------------------------------ |
  80. | `make` | Setup, build, launch and test all at once |
  81. | `make setup` | Initialize environment and install uv |
  82. | `make ensure_env` | Auto-create `.env` if missing |
  83. | `make ensure_uv` | Install `uv` package manager if missing |
  84. | `make build` | Build all Docker base images |
  85. | `make start` | Start services with safe env loading and testing |
  86. | `make stop` | Gracefully stop all services |
  87. | `make restart` | Shortcut for `stop` + `start` |
  88. | `make test` | Run full test suite |
  89. | `make logs` | Stream container logs |
  90. | `make clean` | Stop and remove orphan containers and volumes |
  91. ---
  92. ## 🔐 Security
  93. The RAGFlow sandbox is designed to balance security and usability, offering solid protection without compromising developer experience.
  94. ### ✅ gVisor Isolation
  95. At its core, we use [gVisor](https://gvisor.dev/docs/architecture_guide/security/), a user-space kernel, to isolate code execution from the host system. gVisor intercepts and restricts syscalls, offering robust protection against container escapes and privilege escalations.
  96. ### 🔒 Optional seccomp Support (Advanced)
  97. For users who need **zero-trust-level syscall control**, we support an additional `seccomp` profile. This feature restricts containers to only a predefined set of system calls, as specified in `executor_manager/seccomp-profile-default.json`.
  98. > ⚠️ This feature is **disabled by default** to maintain compatibility and usability. Enabling it may cause compatibility issues with some dependencies.
  99. #### To enable seccomp
  100. 1. Edit your `.env` file:
  101. ```dotenv
  102. SANDBOX_ENABLE_SECCOMP=true
  103. ```
  104. 2. Customize allowed syscalls in:
  105. ```
  106. executor_manager/seccomp-profile-default.json
  107. ```
  108. This profile is passed to the container with:
  109. ```bash
  110. --security-opt seccomp=/app/seccomp-profile-default.json
  111. ```
  112. ### 🧠 Python Code AST Inspection
  113. In addition to sandboxing, Python code is **statically analyzed via AST (Abstract Syntax Tree)** before execution. Potentially malicious code (e.g. file operations, subprocess calls, etc.) is rejected early, providing an extra layer of protection.
  114. ---
  115. This security model strikes a balance between **robust isolation** and **developer usability**. While `seccomp` can be highly restrictive, our default setup aims to keep things usable for most developers — no obscure crashes or cryptic setup required.
  116. ## 📦 Add Extra Dependencies for Supported Languages
  117. Currently, the following languages are officially supported:
  118. | Language | Priority |
  119. | -------- | -------- |
  120. | Python | High |
  121. | Node.js | Medium |
  122. ### 🐍 Python
  123. To add Python dependencies, simply edit the following file:
  124. ```bash
  125. sandbox_base_image/python/requirements.txt
  126. ```
  127. Add any additional packages you need, one per line (just like a normal pip requirements file).
  128. ### 🟨 Node.js
  129. To add Node.js dependencies:
  130. 1. Navigate to the Node.js base image directory:
  131. ```bash
  132. cd sandbox_base_image/nodejs
  133. ```
  134. 2. Use `npm` to install the desired packages. For example:
  135. ```bash
  136. npm install lodash
  137. ```
  138. 3. The dependencies will be saved to `package.json` and `package-lock.json`, and included in the Docker image when rebuilt.
  139. ---
  140. ## Usage
  141. ### 🐍 A Python example
  142. ```python
  143. def main(arg1: str, arg2: str) -> str:
  144. return f"result: {arg1 + arg2}"
  145. ```
  146. ### 🟨 JavaScript examples
  147. A simple sync function
  148. ```javascript
  149. function main({arg1, arg2}) {
  150. return arg1+arg2
  151. }
  152. ```
  153. Async funcion with aioxs
  154. ```javascript
  155. const axios = require('axios');
  156. async function main() {
  157. try {
  158. const response = await axios.get('https://github.com/infiniflow/ragflow');
  159. return 'Body:' + response.data;
  160. } catch (error) {
  161. return 'Error:' + error.message;
  162. }
  163. }
  164. ```
  165. ---
  166. ## 📋 FAQ
  167. ### ❓Sandbox Not Working?
  168. Follow this checklist to troubleshoot:
  169. - [ ] **Is your machine compatible with gVisor?**
  170. Ensure that your system supports gVisor. Refer to the [gVisor installation guide](https://gvisor.dev/docs/user_guide/install/).
  171. - [ ] **Is gVisor properly installed?**
  172. **Common error:**
  173. `HTTPConnectionPool(host='sandbox-executor-manager', port=9385): Read timed out.`
  174. Cause: `runsc` is an unknown or invalid Docker runtime.
  175. **Fix:**
  176. - Install gVisor
  177. - Restart Docker
  178. - Test with:
  179. ```bash
  180. docker run --rm --runtime=runsc hello-world
  181. ```
  182. - [ ] **Is `sandbox-executor-manager` mapped in `/etc/hosts`?**
  183. **Common error:**
  184. `HTTPConnectionPool(host='none', port=9385): Max retries exceeded.`
  185. **Fix:**
  186. Add the following entry to `/etc/hosts`:
  187. ```text
  188. 127.0.0.1 es01 infinity mysql minio redis sandbox-executor-manager
  189. ```
  190. - [ ] **Have you enabled sandbox-related configurations in RAGFlow?**
  191. Double-check that all sandbox settings are correctly enabled in your RAGFlow configuration.
  192. - [ ] **Have you pulled the required base images for the runners?**
  193. **Common error:**
  194. `HTTPConnectionPool(host='sandbox-executor-manager', port=9385): Read timed out.`
  195. Cause: no runner was started.
  196. **Fix:**
  197. Pull the necessary base images:
  198. ```bash
  199. docker pull infiniflow/sandbox-base-nodejs:latest
  200. docker pull infiniflow/sandbox-base-python:latest
  201. ```
  202. - [ ] **Did you restart the service after making changes?**
  203. Any changes to configuration or environment require a full service restart to take effect.
  204. ### ❓Container pool is busy?
  205. All available runners are currently in use, executing tasks/running code. Please try again shortly, or consider increasing the pool size in the configuration to improve availability and reduce wait times.
  206. ## 🤝 Contribution
  207. Contributions are welcome!