Skip to content

Commit 4389869

Browse files
committed
allowlist for request redirection
1 parent b698915 commit 4389869

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/huggingface_hub/file_download.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
# Regex to check if the file etag IS a valid sha256
8484
REGEX_SHA256 = re.compile(r"^[0-9a-f]{64}$")
8585

86+
# Redirect allowlist for use by relative redirect wrapper
87+
# Example: HF_DOWNLOAD_REDIRECT_ALLOWLIST=opendns.com
88+
REDIRECT_ALLOWLIST = os.environ.get("HF_DOWNLOAD_REDIRECT_ALLOWLIST", "").split(",")
89+
REDIRECT_ALLOWLIST = [domain for domain in REDIRECT_ALLOWLIST if len(domain) > 0]
90+
8691
_are_symlinks_supported_in_dir: Dict[str, bool] = {}
8792

8893

@@ -294,7 +299,7 @@ def _request_wrapper(
294299
# This is useful in case of a renamed repository.
295300
if 300 <= response.status_code <= 399:
296301
parsed_target = urlparse(response.headers["Location"])
297-
if parsed_target.netloc == "":
302+
if parsed_target.netloc == "" or any(parsed_target.netloc.endswith(domain) for domain in REDIRECT_ALLOWLIST):
298303
# This means it is a relative 'location' headers, as allowed by RFC 7231.
299304
# (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
300305
# We want to follow this relative redirect !

0 commit comments

Comments
 (0)