Skip to content
Prev Previous commit
Next Next commit
[Download] Keep auth header when download location stays on a Hub host
  • Loading branch information
hanouticelina committed Aug 25, 2026
commit 01944d9e670cebe160c0cad9d868b00603386fea
11 changes: 6 additions & 5 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Any, BinaryIO, Literal, NoReturn, overload
from urllib.parse import quote, urlparse
from urllib.parse import quote

import httpx
from tqdm.auto import tqdm as base_tqdm
Expand Down Expand Up @@ -51,6 +51,7 @@
_DEFAULT_RETRY_ON_STATUS_CODES,
_adjust_range_header,
_httpx_follow_hub_redirects_with_backoff,
_is_same_or_hub_host,
http_stream_backoff,
)
from .utils._runtime import is_xet_available
Expand Down Expand Up @@ -1765,12 +1766,12 @@ def _get_metadata_or_catch_error(
# and ensure we download the exact atomic version even if it changed
# between the HEAD and the GET (unlikely, but hey).
#
# If url domain is different => we are downloading from a CDN => url is signed => don't send auth
# If url domain is the same => redirect due to repo rename AND downloading a regular file => keep auth
# If the final location is on a Hub host (same host, or e.g. huggingface.co reached through an
# HF_ENDPOINT mirror redirect) => keep auth. Otherwise it's a signed CDN url => don't send auth.
if xet_file_data is None and url != metadata.location:
url_to_download = metadata.location
if urlparse(url).netloc != urlparse(metadata.location).netloc:
# Remove authorization header when downloading a LFS blob
if not _is_same_or_hub_host(url, metadata.location):
# Remove authorization header when downloading a LFS blob from a CDN
headers.pop("authorization", None)
except httpx.ProxyError:
# Actually raise on proxy error
Expand Down
Loading