Skip to content

bugfix: utils: catch configparser.Error #1240

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

Merged
merged 2 commits into from
Apr 11, 2025
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: 2 additions & 0 deletions changelog/1240.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``twine`` now catches ``configparser.Error`` to prevent accidental
leaks of secret tokens or passwords to the user's console.
30 changes: 30 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,36 @@ def test_get_repository_config_missing_repository(write_config_file):
utils.get_repository_from_config(config_file, "missing-repository")


@pytest.mark.parametrize(
"invalid_config",
[
# No surrounding [server] section
"""
username = testuser
password = testpassword
""",
# Valid section but bare API token
"""
[pypi]
pypi-lolololol
""",
# No section, bare API token
"""
pypi-lolololol
""",
],
)
def test_get_repository_config_invalid_syntax(write_config_file, invalid_config):
"""Raise an exception when the .pypirc has invalid syntax."""
config_file = write_config_file(invalid_config)

with pytest.raises(
exceptions.InvalidConfiguration,
match="Malformed configuration",
):
utils.get_repository_from_config(config_file, "pypi")


@pytest.mark.parametrize("repository", ["pypi", "missing-repository"])
def test_get_repository_config_missing_file(repository):
"""Raise an exception when a custom config file doesn't exist."""
Expand Down
7 changes: 7 additions & 0 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def get_repository_from_config(
f"Missing '{repository}' section from {config_file}.\n"
f"More info: https://packaging.python.org/specifications/pypirc/ "
)
except configparser.Error:
# NOTE: We intentionally fully mask the configparser exception here,
# since it could leak tokens and other sensitive values.
raise exceptions.InvalidConfiguration(
f"Malformed configuration in {config_file}.\n"
f"More info: https://packaging.python.org/specifications/pypirc/ "
)

config["repository"] = normalize_repository_url(cast(str, config["repository"]))
return config
Expand Down