Skip to content

Commit 81237fb

Browse files
gh-65697: Improved error msg for configparser key validation (#135527)
* Improved error msg for configparser key validation and added note in 3.14 whatsnew * Properly added change to configparser * πŸ“œπŸ€– Added by blurb_it. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 2bd3895 commit 81237fb

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

β€ŽDoc/whatsnew/3.14.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,14 @@ concurrent.futures
12591259
buffer.
12601260
(Contributed by Enzo Bonnal and Josh Rosenberg in :gh:`74028`.)
12611261

1262+
configparser
1263+
------------
1264+
1265+
* Security fix: will no longer write config files it cannot read. Attempting
1266+
to :meth:`configparser.ConfigParser.write` keys containing delimiters or
1267+
beginning with the section header pattern will raise a
1268+
:class:`configparser.InvalidWriteError`.
1269+
(Contributed by Jacob Lincoln in :gh:`129270`)
12621270

12631271
contextvars
12641272
-----------

β€ŽLib/configparser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,14 @@ def _convert_to_boolean(self, value):
12181218

12191219
def _validate_key_contents(self, key):
12201220
"""Raises an InvalidWriteError for any keys containing
1221-
delimiters or that match the section header pattern"""
1221+
delimiters or that begins with the section header pattern"""
12221222
if re.match(self.SECTCRE, key):
1223-
raise InvalidWriteError("Cannot write keys matching section pattern")
1224-
if any(delim in key for delim in self._delimiters):
1225-
raise InvalidWriteError("Cannot write key that contains delimiters")
1223+
raise InvalidWriteError(
1224+
f"Cannot write key {key}; begins with section pattern")
1225+
for delim in self._delimiters:
1226+
if delim in key:
1227+
raise InvalidWriteError(
1228+
f"Cannot write key {key}; contains delimiter {delim}")
12261229

12271230
def _validate_value_types(self, *, section="", option="", value=""):
12281231
"""Raises a TypeError for illegal non-string values.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`configparser`'s error message when attempting to write an invalid key is now more helpful.

0 commit comments

Comments
Β (0)