File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ ``twine `` now catches ``configparser.Error `` to prevent accidental
2+ leaks of secret tokens or passwords to the user's console.
Original file line number Diff line number Diff 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" ])
231261def test_get_repository_config_missing_file (repository ):
232262 """Raise an exception when a custom config file doesn't exist."""
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments