Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set usedforsecurity=False in hashlib methods (FIPS compliance) #1782

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/huggingface_hub/_multi_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"""Contains utilities to multi-commits (i.e. push changes iteratively on a PR)."""
import re
from dataclasses import dataclass, field
from hashlib import sha256
from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Union

from ._commit_api import CommitOperationAdd, CommitOperationDelete
from .community import DiscussionWithDetails
from .utils import experimental
from .utils._cache_manager import _format_size
from .utils.insecure_hashlib import sha256


if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from contextlib import contextmanager
from dataclasses import dataclass
from functools import partial
from hashlib import sha256
from pathlib import Path
from typing import Any, BinaryIO, Dict, Generator, Literal, Optional, Tuple, Union
from urllib.parse import quote, urlparse
Expand Down Expand Up @@ -75,6 +74,7 @@
from .utils._headers import _http_user_agent
from .utils._runtime import _PY_VERSION # noqa: F401 # for backward compatibility
from .utils._typing import HTTP_METHOD_T
from .utils.insecure_hashlib import sha256


logger = logging.get_logger(__name__)
Expand Down
34 changes: 34 additions & 0 deletions src/huggingface_hub/utils/insecure_hashlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Taken from https://github.com/mlflow/mlflow/pull/10119
#
# DO NOT use this function for security purposes (e.g., password hashing).
#
# In Python >= 3.9, insecure hashing algorithms such as MD5 fail in FIPS-compliant
# environments unless `usedforsecurity=False` is explicitly passed.
#
# References:
# - https://github.com/mlflow/mlflow/issues/9905
# - https://github.com/mlflow/mlflow/pull/10119
# - https://docs.python.org/3/library/hashlib.html
# - https://github.com/huggingface/transformers/pull/27038
#
# Usage:
# ```python
# # Use
# from huggingface_hub.utils.insecure_hashlib import sha256
# # instead of
# from hashlib import sha256
#
# # Use
# from huggingface_hub.utils import insecure_hashlib
# # instead of
# import hashlib
# ```
import functools
import hashlib
import sys


_kwargs = {"usedforsecurity": False} if sys.version_info >= (3, 9) else {}
md5 = functools.partial(hashlib.md5, **_kwargs)
sha1 = functools.partial(hashlib.sha1, **_kwargs)
sha256 = functools.partial(hashlib.sha256, **_kwargs)
3 changes: 2 additions & 1 deletion src/huggingface_hub/utils/sha.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Utilities to efficiently compute the SHA 256 hash of a bunch of bytes."""
from hashlib import sha256
from typing import BinaryIO, Optional

from .insecure_hashlib import sha256


def sha_fileobj(fileobj: BinaryIO, chunk_size: Optional[int] = None) -> bytes:
"""
Expand Down
Loading