Repository navigation
[Download] Consider port when following /resolve redirects - #4942
Merged
Merged
Conversation
`_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.
|
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
force-pushed
the
fix/resolve-redirect-port
branch
from
September 21, 2026 05:08
896c6b1 to
65cf074
Compare
hanouticelina
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR! i left one comment to simplify things a bit but otherwise looks good to me 🙂
hanouticelina
approved these changes
Oct 6, 2026
hanouticelina
left a comment
Collaborator
There was a problem hiding this comment.
all good, thank you! sorry for the delay!
Contributor
|
This PR has been shipped as part of the v2.2.0 release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
👨 issue identified while using
hf cli/huggingface_hubwith 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
/resolveredirect 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 withLocalEntryNotFoundError), and theAuthorizationheader is forwarded to the storage host — the opposite of the "auth must not leave the Hub" rule.localhost:15564hub +localhost:14886xet bridge), port-forwarded Kubernetes setups, some on-prem installs. Productionhuggingface.co→cdn-lfs.hf.cois unaffected (hostnames differ)._is_same_or_hub_hostcomparedurlparse(...).hostname, which drops the port. But comparing origins alone is not enough:HF_URL_HOSTSis a set of hostnames built fromHF_ENDPOINT, so withHF_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 sohttps://huggingface.co≡https://huggingface.co:443) and (2) trusts known Hub hosts only on their scheme's standard port._httpx_follow_hub_redirects_with_backoffand auth stripping in_get_metadata_or_catch_error.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
:8001redirecting to a fake storage host on:8002, same hostname):Full
hf_hub_downloadagainst the same fake setup, with a token set: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 inHF_URL_HOSTSis 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
/resolveredirect 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 forwardAuthorizationoff the Hub._is_same_or_hub_hostnow compares full origins (scheme, host, default-normalized port) via new_url_originand a fixed_HUB_ORIGINSset (known Hub endpoints on their standard ports only), instead of matching hostnames againstHF_URL_HOSTS. Redirect following in_httpx_follow_hub_redirects_with_backoffand the check infile_downloadboth use this helper.Adds a parametrized
test_is_same_or_hub_hostcovering 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.