Skip to content

Commit 5d17a43

Browse files
authored
Merge pull request #1099 from woodruffw-forks/ww/attestations-error
upload: prevent --attestations on non-PyPI indices
2 parents 0ec5d18 + 6af785e commit 5d17a43

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

tests/test_upload.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,16 @@ def test_check_status_code_for_wrong_repo_url(repo_url, upload_settings, stub_re
670670
helpers.NEW_WHEEL_FIXTURE,
671671
],
672672
)
673+
674+
675+
def test_upload_rejects_attestations_non_pypi(upload_settings):
676+
upload_settings.repository_config["repository"] = "https://notpypi.example.com"
677+
upload_settings.attestations = True
678+
679+
with pytest.raises(
680+
exceptions.InvalidConfiguration, match="may only be used with PyPI and TestPyPI"
681+
):
682+
upload.upload(
683+
upload_settings,
684+
[helpers.WHEEL_FIXTURE, helpers.WHEEL_FIXTURE + ".foo.attestation"],
685+
)

twine/commands/upload.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,24 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
170170
:raises requests.HTTPError:
171171
The repository responded with an error.
172172
"""
173+
upload_settings.check_repository_url()
174+
repository_url = cast(str, upload_settings.repository_config["repository"])
175+
176+
# Attestations are only supported on PyPI and TestPyPI at the moment.
177+
# We fail early here if the user requests any other index, to prevent
178+
# users from attempting to use `--attestations` on other indices and
179+
# failing bugs when upload fails.
180+
if upload_settings.attestations and not repository_url.startswith(
181+
(utils.DEFAULT_REPOSITORY, utils.TEST_REPOSITORY)
182+
):
183+
raise exceptions.InvalidConfiguration(
184+
"The --attestations flag may only be used with PyPI and TestPyPI"
185+
)
186+
173187
dists = commands._find_dists(dists)
174188
# Determine if the user has passed in pre-signed distributions or any attestations.
175189
uploads, signatures, attestations_by_dist = _split_inputs(dists)
176190

177-
upload_settings.check_repository_url()
178-
repository_url = cast(str, upload_settings.repository_config["repository"])
179191
print(f"Uploading distributions to {repository_url}")
180192

181193
packages_to_upload = [

0 commit comments

Comments
 (0)