Skip to content

fix(offline-download): block cloud metadata endpoints#2487

Open
jyxjjj wants to merge 1 commit into
mainfrom
fix-metadata
Open

fix(offline-download): block cloud metadata endpoints#2487
jyxjjj wants to merge 1 commit into
mainfrom
fix-metadata

Conversation

@jyxjjj
Copy link
Copy Markdown
Member

@jyxjjj jyxjjj commented May 19, 2026

Description / 描述

Add shared validation for offline download URLs to block access to cloud metadata endpoints before URLs enter specific download backends.

This change rejects 169.254.169.254, including URL forms with schemes, ports, DNS names resolving to this IP, and redirect targets pointing to this IP.

The validation is reused by:

  • tool.AddURL
  • SimpleHttp
  • Transmission

It intentionally does not block ordinary private network or LAN addresses.

Motivation and Context / 背景

Offline download backends may fetch user-provided URLs directly. Without centralized validation, a URL can target cloud metadata endpoints such as 169.254.169.254, or reach them through DNS resolution or HTTP redirects.

This PR limits only the high-risk cloud metadata endpoint case while preserving compatibility with user self-hosted private network services.

Relates to security hardening for offline download URL handling.

How Has This Been Tested? / 测试

Tested with:

go test ./internal/offline_download/tool
go test ./internal/offline_download/http ./internal/offline_download/transmission

Covered cases:

  • Direct access to 169.254.169.254 is rejected
  • Access with port is rejected
  • Public URL is allowed
  • Private/LAN URL is allowed
  • DNS resolving to 169.254.169.254 is rejected
  • Redirect target pointing to 169.254.169.254 is rejected

Note: go test ./internal/offline_download/... still fails because of existing unrelated fmt.Errorf vet issues in 115, 115_open, and pikpak.

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。

- Add shared validation for cloud metadata endpoint URLs
- Reuse validation in offline download entry and HTTP backends
- Cover direct, DNS, private URL, and redirect validation cases

Co-authored-by: Codex <267193182+codex@users.noreply.github.com>
Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com>
@jyxjjj jyxjjj requested a review from sevxn007 May 19, 2026 02:54
@jyxjjj jyxjjj marked this pull request as ready for review May 19, 2026 02:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds shared offline-download URL validation to block cloud metadata endpoint access, then wires it into task creation and HTTP-based download paths.

Changes:

  • Added metadata endpoint validation and redirect-aware HTTP client helper.
  • Added tests for direct IP, DNS resolution, private/public URLs, and redirect checks.
  • Integrated validation into tool.AddURL, SimpleHttp.Run, and Transmission.AddURL.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/offline_download/tool/metadata_url.go Adds shared URL validation and redirect-checking HTTP client wrapper.
internal/offline_download/tool/metadata_url_test.go Adds unit tests for metadata endpoint blocking behavior.
internal/offline_download/tool/add.go Validates URLs before creating offline download tasks.
internal/offline_download/http/client.go Applies validation and redirect checks to SimpleHttp downloads.
internal/offline_download/transmission/client.go Applies validation and redirect checks to Transmission torrent URL fetching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +76 to +79
addrs, err := lookupIPAddr(ctx, host)
if err != nil {
return err
}
Comment on lines +47 to +49
if err := ValidateOfflineDownloadURL(ctx, args.URL); err != nil {
return nil, err
}
Comment on lines +50 to +54
func NewOfflineDownloadHTTPClient(base http.Client) *http.Client {
client := base
previousCheckRedirect := client.CheckRedirect
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if err := ValidateOfflineDownloadURL(req.Context(), req.URL.String()); err != nil {
Comment on lines +47 to +49
if err := ValidateOfflineDownloadURL(ctx, args.URL); err != nil {
return nil, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants