
Unvalidated open redirect in the `/gradio_api/file=` route
快速结论:这是一个 CWE-601 开放重定向漏洞,攻击者可通过构造恶意 URL 让 Gradio 应用返回 302 跳转到任意外部网站。该漏洞已在 Gradio main 分支通过 PR #13596(commit 1c5c538)修复,官方建议升级到包含该修复的版本。
问题场景
在使用 Gradio 默认配置(无认证模式)启动应用时,攻击者可通过构造 GET /gradio_api/file=https://evil.example.com/x 或 GET /gradio_api/file/https://evil.example.org/p 请求,使服务返回 302 重定向到任意外部 URL。
报错原文
GET /gradio_api/file=https://evil.example.com/x -> 302 Found, Location: https://evil.example.com/x
GET /gradio_api/file/https://evil.example.org/p -> 302 Found (legacy route)
原因分析
问题出现在 gradio/route_utils.py 文件的 file_fetch 函数中(约 L1196-1199):
if client_utils.is_http_url_like(path_or_url):
from starlette.responses import RedirectResponse
return RedirectResponse(url=path_or_url, status_code=302)
该路由可通过 GET /gradio_api/file={path_or_url:path}(定义在 gradio/routes.py:1082-1086)以及遗留路由别名 GET /gradio_api/file/{path:path}(:1185-1187)访问。虽然路由带有 Depends(login_check),但在未配置 auth 参数时(常见于公共应用场景),该检查实际上是空操作(见 routes.py:410-419),导致未经认证即可触发重定向。
环境排查
- 确认 Gradio 版本:漏洞确认影响 6.17.3 版本(commit d345fb5)
- 确认应用是否配置了
auth参数(如demo.launch(auth=...)),未配置时风险最大 - 检查
gradio/route_utils.py中的file_fetch函数实现,确认是否仍使用RedirectResponse
解决步骤
- 可优先尝试:升级 Gradio 到包含 PR #13596(commit 1c5c538,2026 年 7 月 6 日合并)的版本。该修复将 302 重定向替换为
secure_url_stream_response(),服务端自行通过safehttpx获取 URL 内容并流式返回,从而消除了Location头注入风险。 - 如果无法升级,可临时在
gradio/route_utils.py中为file_fetch添加 URL 校验逻辑,仅允许同源或相对路径的重定向。 - 对于生产环境,建议始终配置
auth参数启用认证检查。
验证方法
确认修复后,尝试发送以下请求应不再返回 302 重定向:
curl -v "http://your-gradio-app:7860/gradio_api/file=https://evil.example.com/x"
正常响应应为 200 状态码,且响应内容由服务端代理获取,而非直接跳转。


![[Question]: Novice, seek help from the boss - prompt for gateway timeout issue](https://www.chat-gpts.plus/wp-content/uploads/2026/07/6089-03fe0c18-768x403.jpg)
