aLexzzz430/dify-agent-safety-review
GitHub: aLexzzz430/dify-agent-safety-review
Dify 的安全导向 fork,在 agent 发布流程中增加结构化安全审查门控,并修复远程文件抓取路径中的 SSRF 漏洞。
Stars: 0 | Forks: 0
# Dify Agent 安全审查
本仓库是对 [Dify](https://github.com/langgenius/dify) 的安全导向二次开发。它保留了上游 Dify 的代码库,然后添加了发布时的 agent 安全门控,以及在审计 Dify 的远程文件抓取路径时发现的一个具体的后端 SSRF 修复。
原始的上游 README 保留为 [README.dify.md](./README.dify.md)。
## 新增内容
### 1. Agent 安全审查门控
在 agent 工作流或 RAG pipeline 发布之前,Dify 现在会对工作流图运行结构化的安全审查。
审查内容包括:
- 针对 Dify 节点类型的 schema 感知字段,而非宽泛的字符串扫描;
- 真实 prompt/config 字段中的 prompt injection 模式;
- 高风险的外部 HTTP 动作;
- URL 安全问题,例如私有范围、编码 IP、重定向目标、DNS rebinding、不允许的 scheme 以及 metadata-service endpoint;
- 通过 `agent_safety_review.allowed_domains` 配置的域名 allowlist;
- 危险的图路径在外部动作之前是否通过了审批节点。
主要入口点:
- [api/services/agent_safety_review_service.py](./api/services/agent_safety_review_service.py)
- [api/services/workflow_service.py](./api/services/workflow_service.py)
- [api/services/rag_pipeline/rag_pipeline.py](./api/services/rag_pipeline/rag_pipeline.py)
- [api/controllers/console/app/workflow.py](./api/controllers/console/app/workflow.py)
实现细节位于 [docs/agent-safety-review-plugin.md](./docs/agent-safety-review-plugin.md)。
### 2. 远程文件重定向 SSRF 修复
在审查 Dify 自身的代码时,我追踪了用户可控的远程文件 URL,它们依次经过:
- `factories.file_factory.remote.get_remote_file_info()`
- `core.helper.download.download_with_size_limit()`
- `core.file.remote_fetcher.make_request()`
- `core.helper.ssrf_proxy.make_request()`
存在风险的行为是,远程文件的 GET/HEAD 调用可能会将 `follow_redirects=True` 直接传递给网络客户端。在 Dify 执行本地重定向目标验证之前,公共 URL 可能会重定向到私有网络目标、metadata-service 地址、编码的 loopback IP 或 DNS rebinding 主机。
本仓库在 [api/core/file/remote_fetcher.py](./api/core/file/remote_fetcher.py) 中修复了该问题:
- 远程文件 GET/HEAD 重定向每次只手动跟随一跳;
- 在此路径下禁用 HTTPX 的自动重定向;
- 每个初始 URL 和重定向 URL 必须是 HTTP(S);
- 解析十进制、十六进制和八进制形式的 IPv4 主机;
- 在每一跳都进行 DNS 解析;
- 阻断私有、loopback、link-local、保留、multicast、unspecified 以及其他非全局地址;
- 阻断无法解析的主机和过长的重定向链;
- 安全的相对重定向仍然有效。
回归测试位于 [api/tests/unit_tests/core/file/test_remote_fetcher.py](./api/tests/unit_tests/core/file/test_remote_fetcher.py)。
## 为什么它是 AI 原生的
这并不是一个围绕 Dify 的轻量级封装。它改变了 agent 发布路径本身。系统将 agent 工作流视为一个图,推断出高风险的 tool/action 路径是否通过了人工审批,并在它们生效之前阻断不安全的更改。
这使得它非常适合作为面试项目,因为它结合了:
- 真实的开源代码阅读;
- 生产级的后端集成;
- 图感知的 agent 安全;
- SSRF/安全加固;
- 测试和 CI 作为可见的证据。
## 验证
从 `api` 目录运行:
```
uv run --with pytest-mock pytest -o addopts='' \
tests/unit_tests/services/test_agent_safety_review_service.py \
tests/unit_tests/core/file/test_remote_fetcher.py \
tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.py \
tests/unit_tests/controllers/console/app/test_workflow.py \
tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow.py \
-q
```
当前结果:
```
175 passed
```
额外的远程文件路径回归测试套件:
```
uv run --with pytest-mock pytest -o addopts='' \
tests/unit_tests/core/file/test_remote_fetcher.py \
tests/unit_tests/core/helper/test_download.py \
tests/unit_tests/factories/test_file_factory.py \
tests/unit_tests/controllers/console/test_remote_files.py \
tests/unit_tests/controllers/web/test_remote_files.py \
-q
```
当前结果:
```
104 passed
```
使用的静态检查:
```
uv run --with ruff ruff check \
services/agent_safety_review_service.py \
core/file/remote_fetcher.py \
services/workflow_service.py \
services/rag_pipeline/rag_pipeline.py \
controllers/console/app/workflow.py \
controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py \
tests/unit_tests/services/test_agent_safety_review_service.py \
tests/unit_tests/core/file/test_remote_fetcher.py \
tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.py \
tests/unit_tests/controllers/console/app/test_workflow.py \
tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow.py
```
当前结果:
```
All checks passed
```
GitHub Actions 覆盖范围定义在 [.github/workflows/agent-safety-review.yml](./.github/workflows/agent-safety-review.yml)。
## 与上游的关系
本项目基于 Dify,并出于兼容性目的保留了原始代码库。它旨在作为一个独立的安全导向 fork,用于演示、面试和进一步实验。
原始的 Dify 项目归 LangGenius 及其贡献者所有。
标签:AI安全, AI工作流防护, Chat Copilot, CISA项目, Dify, SSRF防护, 自动化攻击, 逆向工具