Skip to content

Commit 6ae5455

Browse files
miss-islingtonlincolnj1blurb-it[bot]
authored
[3.14] gh-65697: Improved error msg for configparser key validation (GH-135527) (#135541)
gh-65697: Improved error msg for configparser key validation (GH-135527) * Improved error msg for configparser key validation and added note in 3.14 whatsnew * Properly added change to configparser * 📜🤖 Added by blurb_it. --------- (cherry picked from commit 81237fb) Co-authored-by: Jacob Austin Lincoln <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent ee141f0 commit 6ae5455

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
@@ -1244,6 +1244,14 @@ concurrent.futures
12441244
buffer.
12451245
(Contributed by Enzo Bonnal and Josh Rosenberg in :gh:`74028`.)
12461246

1247+
configparser
1248+
------------
1249+
1250+
* Security fix: will no longer write config files it cannot read. Attempting
1251+
to :meth:`configparser.ConfigParser.write` keys containing delimiters or
1252+
beginning with the section header pattern will raise a
1253+
:class:`configparser.InvalidWriteError`.
1254+
(Contributed by Jacob Lincoln in :gh:`129270`)
12471255

12481256
contextvars
12491257
-----------

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)