Unvalidated open redirect in the `/gradio_api/file=` route

在使用 Gradio 默认配置(无认证模式)启动应用时,攻击者可通过构造 GET /gradio_api/file=https://evil.example.com/x 或 GET /gradio_api/file/https://evil.example.org/p 请求,使服务返回 302 重定

Unvalidated open redirect in the `/gradio_api/file=` route

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/xGET /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

解决步骤

  1. 可优先尝试:升级 Gradio 到包含 PR #13596(commit 1c5c538,2026 年 7 月 6 日合并)的版本。该修复将 302 重定向替换为 secure_url_stream_response(),服务端自行通过 safehttpx 获取 URL 内容并流式返回,从而消除了 Location 头注入风险。
  2. 如果无法升级,可临时在 gradio/route_utils.py 中为 file_fetch 添加 URL 校验逻辑,仅允许同源或相对路径的重定向。
  3. 对于生产环境,建议始终配置 auth 参数启用认证检查。

验证方法

确认修复后,尝试发送以下请求应不再返回 302 重定向:

curl -v "http://your-gradio-app:7860/gradio_api/file=https://evil.example.com/x"

正常响应应为 200 状态码,且响应内容由服务端代理获取,而非直接跳转。

参考来源

gradio-app/gradio #13608

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 13177

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注