Skip to content

[Download] Consider port when following /resolve redirects - #4942

Merged
hanouticelina merged 10 commits into
mainfrom
fix/resolve-redirect-port
Oct 6, 2026
Merged

hanouticelina merged 10 commits into
mainfrom
fix/resolve-redirect-port

Conversation

@assafvayner

@assafvayner assafvayner commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

👨 issue identified while using hf cli/huggingface_hub with a local hub server running that redirected to another local service for data and caused a failure.

CI failure is caused by fsspec incompatibility, separate fix for that in #4951

🤖 Summary

  • Bug: since v1.30.0 ([Download] Follow redirects between Hub hosts when resolving files #4739), a /resolve redirect to the same hostname on a different port is treated as an intra-Hub redirect and followed. The file metadata is then read from the storage host's response instead of the Hub's 302 (commit_hash=None → downloads fail with LocalEntryNotFoundError), and the Authorization header is forwarded to the storage host — the opposite of the "auth must not leave the Hub" rule.
  • This bites any deployment where the Hub and the storage endpoint share a hostname and differ by port: local dev stacks (localhost:15564 hub + localhost:14886 xet bridge), port-forwarded Kubernetes setups, some on-prem installs. Production huggingface.co → cdn-lfs.hf.co is unaffected (hostnames differ).
  • Root cause: _is_same_or_hub_host compared urlparse(...).hostname, which drops the port. But comparing origins alone is not enough: HF_URL_HOSTS is a set of hostnames built from HF_ENDPOINT, so with HF_ENDPOINT=http://localhost:15564, "localhost" is a trusted Hub host and any port on it was trusted. The fix therefore (1) compares origins (scheme + host + port, with scheme-default ports so https://huggingface.co ≡ https://huggingface.co:443) and (2) trusts known Hub hosts only on their scheme's standard port.
  • Fixing the single helper fixes both call sites for free: redirect following in _httpx_follow_hub_redirects_with_backoff and auth stripping in _get_metadata_or_catch_error.
  • Also stricter on scheme changes (http:// → https:// on the same host:port is not followed anymore) — that's not a regression: before [Download] Follow redirects between Hub hosts when resolving files #4739 only relative redirects were followed, so cross-scheme redirects were never followed either.

Repro / manual testing

Standalone repro (fake Hub on :8001 redirecting to a fake storage host on :8002, same hostname):

$ python repro.py   # huggingface_hub 1.32.0
huggingface_hub 1.32.0
commit_hash: None                 <-- bug
etag:        deadbeef
location:    http://localhost:8002/blob/deadbeef?signature=abc
(exit code 1)

$ python repro.py   # this branch (1.33.0.dev0)
huggingface_hub 1.33.0.dev0
commit_hash: 0000000000000000000000000000000000000000
etag:        deadbeef
location:    http://localhost:8002/blob/deadbeef?signature=abc
(exit code 0)

Full hf_hub_download against the same fake setup, with a token set:

downloaded: b"hello world\n"
Storage requests Authorization headers: {}
OK: download succeeded and Authorization was not forwarded to storage

Tests

  • TestFollowHubRedirects::test_is_same_or_hub_host — parametrized: same origin, explicit default port, other Hub host on standard port (followed) vs same-hostname-other-port, Hub host on non-standard port, scheme change, CDN (not followed).
  • test_is_same_or_hub_host_with_ported_endpoint — a hostname in HF_URL_HOSTS is only trusted on its scheme's standard port.
  • test_resolve_redirect_to_same_hostname_other_port_is_not_followed / test_resolve_redirect_to_same_origin_is_followed — end-to-end against local HTTP servers: metadata is taken from the Hub's 302, and same-origin redirects (renamed repo) are still followed.
  • pytest tests/test_utils_http.py: 76 passed (10 new).

Note

Medium Risk
Changes security-sensitive download redirect and auth-forwarding rules; behavior shifts for custom/local Hub setups but is covered by new unit tests.

Overview
Fixes Hub /resolve redirect handling so same-hostname, different-port targets (e.g. local Hub → storage on another port) are not treated as in-Hub redirects. That bug caused missing commit metadata on downloads and could forward Authorization off the Hub.

_is_same_or_hub_host now compares full origins (scheme, host, default-normalized port) via new _url_origin and a fixed _HUB_ORIGINS set (known Hub endpoints on their standard ports only), instead of matching hostnames against HF_URL_HOSTS. Redirect following in _httpx_follow_hub_redirects_with_backoff and the check in file_download both use this helper.

Adds a parametrized test_is_same_or_hub_host covering same-origin redirects, explicit :443, cross-host Hub URLs, localhost port changes, non-standard Hub ports, and CDN redirects.

Reviewed by Cursor Bugbot for commit 902a9e0. Bugbot is set up for automated code reviews on this repo. Configure here.

`_is_same_or_hub_host` compared hostnames only, so a /resolve redirect
to the same hostname on a different port (e.g. an xet bridge or CDN
behind the same hostname) was treated as an intra-Hub redirect and
followed. The file metadata was then read from the storage host's
response instead of the Hub's 302 (commit_hash=None => downloads fail
with LocalEntryNotFoundError), and the Authorization header was
forwarded to the storage host, contradicting the "auth must not leave
the Hub" rule.

Compare origins (scheme + host + port, with scheme-default ports) and
only trust known Hub hosts on their scheme's standard port, so e.g.
`HF_ENDPOINT=http://localhost:15564` does not make
`http://localhost:14886` a trusted Hub host.

Fixes both call sites for free: redirect following in
`_httpx_follow_hub_redirects_with_backoff` and auth stripping in
`_get_metadata_or_catch_error`.

Regression since #4739 (v1.30.0), which started following redirects
between Hub hosts. Before that, only relative redirects were followed.
Comment thread tests/test_utils_http.py Fixed
@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

…rver

Silences a CodeQL HTTP Response Splitting false positive on the test
fixture: the redirect target is fixed for the test, so build it from
literals instead of from self.path.
@assafvayner
assafvayner force-pushed the fix/resolve-redirect-port branch from 896c6b1 to 65cf074 Compare September 21, 2026 05:08

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR! i left one comment to simplify things a bit but otherwise looks good to me 🙂

Comment thread src/huggingface_hub/utils/_http.py Resolved

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

all good, thank you! sorry for the delay!

@hanouticelina
hanouticelina merged commit 83ce8b0 into main Oct 6, 2026
28 of 31 checks passed
@hanouticelina
hanouticelina deleted the fix/resolve-redirect-port branch October 6, 2026 13:22
@huggingface-hub-bot

Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v2.2.0 release.

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.

3 participants