Skip to content

Commit ae1d03b

Browse files
authored
bugfix: utils: catch configparser.Error (#1240)
1 parent 4038f7b commit ae1d03b

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

changelog/1240.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``twine`` now catches ``configparser.Error`` to prevent accidental
2+
leaks of secret tokens or passwords to the user's console.

tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,36 @@ def test_get_repository_config_missing_repository(write_config_file):
227227
utils.get_repository_from_config(config_file, "missing-repository")
228228

229229

230+
@pytest.mark.parametrize(
231+
"invalid_config",
232+
[
233+
# No surrounding [server] section
234+
"""
235+
username = testuser
236+
password = testpassword
237+
""",
238+
# Valid section but bare API token
239+
"""
240+
[pypi]
241+
pypi-lolololol
242+
""",
243+
# No section, bare API token
244+
"""
245+
pypi-lolololol
246+
""",
247+
],
248+
)
249+
def test_get_repository_config_invalid_syntax(write_config_file, invalid_config):
250+
"""Raise an exception when the .pypirc has invalid syntax."""
251+
config_file = write_config_file(invalid_config)
252+
253+
with pytest.raises(
254+
exceptions.InvalidConfiguration,
255+
match="Malformed configuration",
256+
):
257+
utils.get_repository_from_config(config_file, "pypi")
258+
259+
230260
@pytest.mark.parametrize("repository", ["pypi", "missing-repository"])
231261
def test_get_repository_config_missing_file(repository):
232262
"""Raise an exception when a custom config file doesn't exist."""

twine/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ def get_repository_from_config(
159159
f"Missing '{repository}' section from {config_file}.\n"
160160
f"More info: https://packaging.python.org/specifications/pypirc/ "
161161
)
162+
except configparser.Error:
163+
# NOTE: We intentionally fully mask the configparser exception here,
164+
# since it could leak tokens and other sensitive values.
165+
raise exceptions.InvalidConfiguration(
166+
f"Malformed configuration in {config_file}.\n"
167+
f"More info: https://packaging.python.org/specifications/pypirc/ "
168+
)
162169

163170
config["repository"] = normalize_repository_url(cast(str, config["repository"]))
164171
return config

0 commit comments

Comments
 (0)