Dify 的 API、Plugin Daemon、Code Sandbox 与 SSRF 代理实际如何隔离流量,哪些架构假设最容易误读?
先说结论
在固定提交 8394eb1 中,Nginx 存在两条不同入口:/console/api、/api 与 /v1 代理到 api:5001,而 /e/ 直接代理到 plugin_daemon:5002 并注入 Dify-Hook-Url。在 Python 客户端内部,API/Worker 调用 plugin_daemon 时显式设置 trust_env=False 禁用环境代理,而 CodeExecutor 调用 sandbox:8194 时未关闭环境代理继承。
两大网络边界最易误读:sandbox 仅挂接 internal: true 的 ssrf_proxy_network,出网必须经 ssrf_proxy:3128,而 plugin_daemon 同时挂接 ssrf_proxy_network 与 default;此外 Dify 还单独运行面向 Agent shell 的 local_sandbox(shellctl:5004)及专属 agent_ssrf_proxy:3128。本文为独立静态源码分析,不代表上游背书,也不是运行时 trace。
固定提交证据表
以下证据全部绑定 langgenius/dify 固定提交 8394eb190b16544e0d6c8b005fd18d7f07a00e71。每条链接都指向该提交的精确行范围。
打开离线交互成品 · 查看 JSON 源输入 · 打开固定提交 ↗
| 起点 | 终点 | 证据分类 | 结论与永久链接 |
|---|---|---|---|
nginx |
api_worker |
config / deployment wiring | Nginx 将 /console/api、/api 与 /v1 路由至 api:5001。 docker/nginx/conf.d/default.conf.template#L8-L33 |
nginx |
plugin_daemon |
config / deployment wiring | Nginx 将 /e/ 直接代理至 plugin_daemon:5002 并设置 Dify-Hook-Url。 docker/nginx/conf.d/default.conf.template#L47-L52 |
api_worker |
plugin_daemon |
direct HTTP/RPC call | 连接池 httpx.Client 使用 PLUGIN_DAEMON_URL、X-Api-Key,并显式设置 trust_env=False。 api/core/plugin/impl/base.py#L52-L79api/core/plugin/impl/base.py#L154-L187 |
plugin_daemon |
api_worker |
config / deployment wiring | Plugin Daemon 配置 DIFY_INNER_API_URL=http://api:5001,同时接入 ssrf_proxy_network 与 default。 docker/envs/core-services/plugin-daemon.defaults#L69-L82docker/docker-compose.yaml#L657-L678 |
api_worker |
sandbox |
direct HTTP/RPC call | CodeExecutor 向 /v1/sandbox/run 发起 POST(enable_network=True),其 httpx.Client 未设 trust_env=False。 api/core/helper/code_executor/code_executor.py#L19-L47api/core/helper/code_executor/code_executor.py#L81-L105 |
sandbox |
ssrf_proxy |
config / deployment wiring | sandbox 仅接入内部 ssrf_proxy_network,经 ssrf_proxy:3128 出网;Squid 拒绝 to_private_networks。 docker/docker-compose.yaml#L604-L620docker/envs/core-services/sandbox.defaults#L5-L25docker/ssrf_proxy/squid.conf.template#L3-L17 |
local_sandbox |
agent_ssrf_proxy |
config / deployment wiring | local_sandbox 在 5004 运行 shellctl,通过 local_sandbox_proxy_network 走专属 agent_ssrf_proxy:3128。 docker/docker-compose.yaml#L631-L643docker/docker-compose.yaml#L813-L815docker/docker-compose.yaml#L1421-L1435 |
最易误读的边界
Plugin 客户端显式禁用环境代理(trust_env=False),CodeExecutor 未禁用
api/core/plugin/impl/base.py 在创建连接池 httpx.Client 时显式传入 trust_env=False,避免容器内的 HTTP_PROXY 劫持 api 到 plugin_daemon 的内网请求;而 api/core/helper/code_executor/code_executor.py 未传入该参数,若容器配置了全局代理且 NO_PROXY 漏掉 sandbox,代码执行调用可能被代理拦截。
api/core/plugin/impl/base.py#L76-L79api/core/helper/code_executor/code_executor.py#L43-L47
sandbox 仅在内部隔离网,而 plugin_daemon 双网挂接且 /e/ 绕过 api
sandbox 仅连接 internal: true 的 ssrf_proxy_network,出网必须经过 Squid ssrf_proxy:3128;但 plugin_daemon 同时挂接 ssrf_proxy_network 与 default,且 Nginx 直接把 /e/ 暴露给 plugin_daemon:5002,不经 api:5001。
docker/docker-compose.yaml#L619-L620docker/docker-compose.yaml#L676-L678docker/nginx/conf.d/default.conf.template#L47-L52
调用与职责拆解
1. Nginx 在入口就把 /e/ 直接分流给 Plugin Daemon
docker/nginx/conf.d/default.conf.template 将 /console/api、/api、/v1 转发至 api:5001,但 location /e/ 直接代理到 plugin_daemon:5002 并附加 Dify-Hook-Url。插件端点入站流量并不经过 Python API 容器。
2. API/Worker 内部两个 httpx 客户端对代理的处理并不对称
BasePluginClient 使用连接池 httpx.Client 时显式声明 trust_env=False(max_connections=100、max_keepalive_connections=50)并携带 PLUGIN_DAEMON_KEY;而 _build_code_executor_client 向 /v1/sandbox/run 发送 enable_network=True 请求时,没有传入 trust_env=False。
3. 两套独立的沙箱隔离网与两台 Squid 代理
代码执行 sandbox 仅接入 internal: true 的 ssrf_proxy_network,通过 HTTP_PROXY/HTTPS_PROXY 指向 ssrf_proxy:3128,由 Squid 拦截 to_private_networks 并放行 .marketplace.dify.ai。另一侧 local_sandbox 运行 shellctl:5004,通过 local_sandbox_proxy_network 接入专属 agent_ssrf_proxy:3128。
适用边界与不能证明什么
- 本文是独立静态源码分析,不代表 Dify 维护者背书;结论仅限 langgenius/dify 固定提交 8394eb190b16544e0d6c8b005fd18d7f07a00e71。
- Python 直接 HTTP 调用与 Docker Compose / Nginx 部署连线已分别标注;docker/.env 覆写、自定义 Squid 模板、K8s 部署或外部反向代理都可能改变实际流量路径。
- 7 条证据边聚焦 Nginx、API/Worker、Plugin Daemon、Code Sandbox、Local Sandbox 与 Squid SSRF 代理边界;数据库、向量库、Redis 与模型供应商调用不在本轮范围内。
- 静态源码核对不能证明运行期 SSRF 拦截生效、容器防火墙规则或生产请求延迟。
用你的 Agent 复核
把固定提交与下列复核提示词一起交给 Agent;要求它逐条打开永久链接,并把未知项保留为未知。
只审查 langgenius/dify 固定提交 8394eb190b16544e0d6c8b005fd18d7f07a00e71。梳理 Nginx /e/ 直连 plugin_daemon:5002,对比 api/core/plugin/impl/base.py 的 trust_env=False 与 code_executor.py 的差异,区分仅挂内部网的 sandbox + ssrf_proxy 与双网 plugin_daemon,并单独标出 local_sandbox + agent_ssrf_proxy。不要推断运行期防火墙行为或未核证调用点。
以 Markdown 引用本专题
[Dify 的 API、Plugin Daemon、Code Sandbox 与 SSRF 代理实际如何隔离流量,哪些架构假设最容易误读](https://archify.si/zh/cases/dify-plugin-sandbox)
Archify 独立静态源码专题,结论仅限固定提交。