Skip to content

Commit a72402c

Browse files
hashing: Add usedforsecurity=False on hashlib calls (#10826)
1 parent 08f3197 commit a72402c

File tree

6 files changed

+14
-9
lines changed

6 files changed

+14
-9
lines changed

dvc/lock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def _set_claimfile(self):
212212

213213
if self._tmp_dir is not None:
214214
# Under Windows file path length is limited so we hash it
215-
hasher = hashlib.md5(self._claimfile.encode()) # noqa: S324
215+
hasher = hashlib.md5(self._claimfile.encode(), usedforsecurity=False)
216216
filename = hasher.hexdigest()
217217
self._claimfile = os.path.join(self._tmp_dir, filename + ".lock")
218218

dvc/repo/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,11 @@ def site_cache_dir(self) -> str:
641641
# that just happened to be at the same path as old deleted ones.
642642
btime = self._btime or getattr(os.stat(root_dir), "st_birthtime", None)
643643

644-
md5 = hashlib.md5( # noqa: S324
644+
md5 = hashlib.md5(
645645
str(
646646
(root_dir, subdir, btime, getpass.getuser(), version_tuple[0], salt)
647-
).encode()
647+
).encode(),
648+
usedforsecurity=False,
648649
)
649650
repo_token = md5.hexdigest()
650651
return os.path.join(repos_dir, repo_token)

dvc/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
def bytes_hash(byts, typ):
21-
hasher = getattr(hashlib, typ)()
21+
hasher = getattr(hashlib, typ)(usedforsecurity=False)
2222
hasher.update(byts)
2323
return hasher.hexdigest()
2424

tests/func/test_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def digest(text):
12-
return hashlib.md5(bytes(text, "utf-8")).hexdigest()
12+
return hashlib.md5(bytes(text, "utf-8"), usedforsecurity=False).hexdigest()
1313

1414

1515
def test_no_scm(tmp_dir, dvc):

tests/unit/fs/test_dvcfs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def fs_10_files_with_hashed_names(self, tmp_dir, local_fs, local_join, local_pat
6767
└── 📄 {hashed([0-9])}.txt
6868
"""
6969
dir_contents = {
70-
md5(str(i).encode("utf-8")).hexdigest() + ".txt": str(i) for i in range(10)
70+
md5(str(i).encode("utf-8"), usedforsecurity=False).hexdigest()
71+
+ ".txt": str(i)
72+
for i in range(10)
7173
}
7274
tmp_dir.dvc_gen({"source": dir_contents}, commit="add source")
7375
tmp_dir.scm_gen(".gitignore", "/source", commit="add .gitignore")
@@ -751,7 +753,7 @@ def test_get_with_source_and_destination_as_list(
751753
source_files = []
752754
destination_files = []
753755
for i in range(10):
754-
hashed_i = md5(str(i).encode("utf-8")).hexdigest()
756+
hashed_i = md5(str(i).encode("utf-8"), usedforsecurity=False).hexdigest()
755757
source_files.append(fs_join(source, f"{hashed_i}.txt"))
756758
destination_files.append(
757759
make_path_posix(local_join(target, f"{hashed_i}.txt"))

tests/unit/test_analytics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_system_info():
176176
)
177177
def test_git_remote_hash(mocker, git_remote):
178178
m = mocker.patch("dvc.analytics._git_remote_url", return_value=git_remote)
179-
expected = hashlib.md5(b"iterative/dvc.git").hexdigest()
179+
expected = hashlib.md5(b"iterative/dvc.git", usedforsecurity=False).hexdigest()
180180

181181
assert analytics._git_remote_path_hash(None) == expected
182182
m.assert_called_once_with(None)
@@ -194,6 +194,8 @@ def test_git_remote_hash(mocker, git_remote):
194194
def test_git_remote_hash_local(mocker, git_remote):
195195
m = mocker.patch("dvc.analytics._git_remote_url", return_value=git_remote)
196196

197-
expected = hashlib.md5(git_remote.encode("utf-8")).hexdigest()
197+
expected = hashlib.md5(
198+
git_remote.encode("utf-8"), usedforsecurity=False
199+
).hexdigest()
198200
assert analytics._git_remote_path_hash(None) == expected
199201
m.assert_called_once_with(None)

0 commit comments

Comments
 (0)