Skip to content

Commit fc20d94

Browse files
Send only the digests we have from PackageFile (#776)
* Send only the digests we have from PackageFile Once again, we have to deal with third-party package repositories doing their own thing and users needing this to just work. Those repositories combined with FIPS mean that we will need to send the information we actually have and only that when it comes to digests. Closes gh-775 * Update function and comments for clarity Let's be clearer about some of the bizarre logic we use to support repositories other than PyPI and distributions using FIPS Co-authored-by: Brian Rutledge <[email protected]> Co-authored-by: Brian Rutledge <[email protected]>
1 parent b4a196e commit fc20d94

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

changelog/776.bugfix.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do not include md5_digest or blake2_256_digest if FIPS mode is enabled on the
2+
host. This removes those fields from the metadata before sending the metadata
3+
to the repository.

tests/test_package.py

+18
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,24 @@ def test_fips_hash_manager_blake2(monkeypatch):
262262
assert hasher.hexdigest() == hashes
263263

264264

265+
def test_fips_metadata_excludes_md5_and_blake2(monkeypatch):
266+
"""Generate a valid metadata dictionary for Nexus when FIPS is enabled.
267+
268+
See also: https://github.com/pypa/twine/issues/775
269+
"""
270+
replaced_blake2b = pretend.raiser(ValueError("fipsmode"))
271+
replaced_md5 = pretend.raiser(ValueError("fipsmode"))
272+
monkeypatch.setattr(package_file.hashlib, "md5", replaced_md5)
273+
monkeypatch.setattr(package_file.hashlib, "blake2b", replaced_blake2b)
274+
275+
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
276+
pf = package_file.PackageFile.from_filename(filename, None)
277+
278+
mddict = pf.metadata_dictionary()
279+
assert "md5_digest" not in mddict
280+
assert "blake2_256_digest" not in mddict
281+
282+
265283
def test_pkginfo_returns_no_metadata(monkeypatch):
266284
"""Raise an exception when pkginfo can't interpret the metadata.
267285

twine/package.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ def metadata_dictionary(self) -> Dict[str, MetadataValue]:
152152
"download_url": meta.download_url,
153153
"supported_platform": meta.supported_platforms,
154154
"comment": self.comment,
155-
"md5_digest": self.md5_digest,
156155
"sha256_digest": self.sha2_digest,
157-
"blake2_256_digest": self.blake2_256_digest,
158156
# PEP 314
159157
"provides": meta.provides,
160158
"requires": meta.requires,
@@ -174,6 +172,15 @@ def metadata_dictionary(self) -> Dict[str, MetadataValue]:
174172
if self.gpg_signature is not None:
175173
data["gpg_signature"] = self.gpg_signature
176174

175+
# FIPS disables MD5 and Blake2, making the digest values None. Some package
176+
# repositories don't allow null values, so this only sends non-null values.
177+
# See also: https://github.com/pypa/twine/issues/775
178+
if self.md5_digest:
179+
data["md5_digest"] = self.md5_digest
180+
181+
if self.blake2_256_digest:
182+
data["blake2_256_digest"] = self.blake2_256_digest
183+
177184
return data
178185

179186
def add_gpg_signature(

0 commit comments

Comments
 (0)