gridea-pro icon indicating copy to clipboard operation
gridea-pro copied to clipboard

[Security] 下载客户端 HTTP 重定向未限制目标域

Open Tespera opened this issue 7 months ago • 1 comments

优先级

P1 — 中优先级

问题描述

doDownload 使用默认配置的 http.Client(仅设置了 Timeout)下载二进制。Go 默认 http.Client 会跟随最多 10 次 HTTP 3xx 重定向,但不校验重定向目标是否仍在预期域名内。

影响 / 风险

即使入口 URL 已做白名单(见 issue 下载 URL 白名单),服务器可以返回 302 指向恶意域名绕过防御。

实际场景:GitHub Release 下载会先重定向到 objects.githubusercontent.com(正常),若 GitHub 后端被攻陷、DNS 被污染,或 CDN 前缀替换,可把用户引导到受控域名。

问题位置

  • backend/internal/facade/update_facade.go:327 — dlClient := &http.Client{Timeout: 30 * time.Minute}

最佳解决方案

给 dlClient 设置 CheckRedirect 回调,限制重定向目标仅在白名单域名内:

allowedHosts := map[string]bool{
    "github.com":                           true,
    "objects.githubusercontent.com":        true,
    "release-assets.githubusercontent.com": true,
    "codeload.github.com":                  true,
}
dlClient := &http.Client{
    Timeout: 30 * time.Minute,
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        if len(via) >= 10 {
            return errors.New("重定向次数过多")
        }
        if !allowedHosts[req.URL.Host] {
            return fmt.Errorf("重定向到非预期域名: %s", req.URL.Host)
        }
        return nil
    },
}

替代方案

宽松版:只允许重定向到 *.github.com 和 *.githubusercontent.com(suffix 匹配),避免 GitHub 将来新增 CDN 域名后兼容性问题。

验收标准

  • [ ] dlClient 设置 CheckRedirect 白名单
  • [ ] 伪造 302 重定向到非白名单域名 → 下载被拒
  • [ ] 真实 GitHub Release 下载流程(包括 objects.githubusercontent.com)能正常走通

Tespera avatar Apr 19 '26 12:04 Tespera

问题原因(Security)

doDownload 使用默认 http.Client(仅设 Timeout)下载二进制。Go 默认 client 会自动跟随最多 10 次 3xx 重定向但不校验目标域名。即便入口 URL 做了白名单(#52),服务端仍可用 302 把客户端引到第三方域绕过防御。

修复措施

PR #81(merge 2817347):

  • 新增 trustedRedirectSuffixes = ["github.com", "githubusercontent.com"]
  • 用「host 严格等于 suffix 或以 . + suffix 结尾」的边界校验,防 github.com.evil.com / xgithub.com 这类后缀伪装
  • 用后缀列表而非穷举 objects.githubusercontent.com 等子域,兼容 GitHub 未来新增 CDN 子域
  • trustedRedirectChecker 作为 http.Client.CheckRedirect 同时校验:HTTPS scheme + host 白名单 + 跳转次数 < 10
  • doDownload 的 dlClient 挂上该 checker
  • 附 TestHasTrustedRedirectHost / TestTrustedRedirectChecker / TestDoDownload_RejectsThirdPartyRedirect 集成测

与 #52 的关系

#52(PR #80)锁住入口 URL 前缀,本 PR 锁住重定向链。两者结合:攻击者即便控制了 browser_download_url 字段,也过不了入口白名单;即便过了入口,也跳不出 github 体系 —— 防御纵深。

发布计划

将在 v1.1.2 发布后正式生效。

Tespera avatar Apr 22 '26 15:04 Tespera