diff --git a/src/huggingface_hub/utils/_http.py b/src/huggingface_hub/utils/_http.py index 080d78d664..20c0d7f751 100644 --- a/src/huggingface_hub/utils/_http.py +++ b/src/huggingface_hub/utils/_http.py @@ -784,10 +784,30 @@ def _httpx2_follow_hub_redirects_with_backoff( raise httpx2.TooManyRedirects(f"Exceeded {_MAX_REDIRECTS} redirects while resolving '{url}'.") +_DEFAULT_PORTS = {"http": 80, "https": 443} + + +def _url_origin(url: str) -> tuple[str, str, int]: + """Origin of a url: (scheme, hostname, port), with the scheme's default port if not explicit.""" + parsed = urlparse(url) + return (parsed.scheme, (parsed.hostname or "").lower(), parsed.port or _DEFAULT_PORTS.get(parsed.scheme, 0)) + + +# Origins considered to be Hub endpoints. The auth header is forwarded to these: only add trusted ones. +_HUB_ORIGINS = frozenset( + _url_origin(endpoint) + for endpoint in ( + constants._HF_DEFAULT_ENDPOINT, + constants._HF_DEFAULT_STAGING_ENDPOINT, + constants.ENDPOINT, + "https://hf.co", + ) +) + + def _is_same_or_hub_host(url: str, target: str) -> bool: - """Whether `target` is served by the same host as `url`, or by a known Hub host.""" - target_host = (urlparse(target).hostname or "").lower() - return target_host == (urlparse(url).hostname or "").lower() or target_host in constants.HF_URL_HOSTS + """Whether `target` is served by the same origin as `url`, or by a known Hub origin.""" + return _url_origin(target) in {_url_origin(url), *_HUB_ORIGINS} def fix_hf_endpoint_in_url(url: str, endpoint: str | None) -> str: diff --git a/tests/test_utils_http.py b/tests/test_utils_http.py index 2a7d3be6e6..24e376db5a 100644 --- a/tests/test_utils_http.py +++ b/tests/test_utils_http.py @@ -23,6 +23,7 @@ _WARNED_TOPICS, RateLimitInfo, _adjust_range_header, + _is_same_or_hub_host, _parse_bucket_id_from_url, _parse_repo_info_from_url, _parse_retry_after, @@ -904,3 +905,22 @@ def is_flagged(response): assert is_flagged(response) else: assert not is_flagged(response) + + +@pytest.mark.parametrize( + ("url", "target", "expected"), + [ + ( + "https://huggingface.co/org/repo/resolve/main/file.bin", + "https://huggingface.co/org/renamed/resolve/main/file.bin", + True, + ), + ("https://huggingface.co/resolve/main/file.bin", "https://huggingface.co:443/resolve/main/file.bin", True), + ("https://example.com/resolve/main/file.bin", "https://huggingface.co/resolve/main/file.bin", True), + ("http://localhost:15564/resolve/main/file.bin", "http://localhost:14886/blob/deadbeef?signature=abc", False), + ("https://example.com/resolve/main/file.bin", "https://huggingface.co:8443/resolve/main/file.bin", False), + ("https://huggingface.co/org/repo/resolve/main/file.bin", "https://cdn-lfs.hf.co/abc", False), + ], +) +def test_is_same_or_hub_host(url, target, expected): + assert _is_same_or_hub_host(url, target) is expected