]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-65697: Improved error msg for configparser key validation (#135527)
authorJacob Austin Lincoln <99031153+lincolnj1@users.noreply.github.com>
Sun, 15 Jun 2025 16:13:19 +0000 (09:13 -0700)
committerGitHub <noreply@github.com>
Sun, 15 Jun 2025 16:13:19 +0000 (12:13 -0400)
* 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>
Doc/whatsnew/3.14.rst
Lib/configparser.py
Misc/NEWS.d/next/Library/2025-06-15-03-03-22.gh-issue-65697.COdwZd.rst [new file with mode: 0644]

index 705bf46d603697f43eb55415393a69f542af9723..895446e2721ca50abf426a699f96c0d3e755daf6 100644 (file)
@@ -1259,6 +1259,14 @@ concurrent.futures
   buffer.
   (Contributed by Enzo Bonnal and Josh Rosenberg in :gh:`74028`.)
 
+configparser
+------------
+
+* Security fix: will no longer write config files it cannot read. Attempting
+  to :meth:`configparser.ConfigParser.write` keys containing delimiters or
+  beginning with the section header pattern will raise a
+  :class:`configparser.InvalidWriteError`.
+  (Contributed by Jacob Lincoln in :gh:`129270`)
 
 contextvars
 -----------
index 239fda60a02ca0355c31729cc5ac57f660c72034..18af1eadaad1110bee819d2c24199a3626c63245 100644 (file)
@@ -1218,11 +1218,14 @@ class RawConfigParser(MutableMapping):
 
     def _validate_key_contents(self, key):
         """Raises an InvalidWriteError for any keys containing
-        delimiters or that match the section header pattern"""
+        delimiters or that begins with the section header pattern"""
         if re.match(self.SECTCRE, key):
-            raise InvalidWriteError("Cannot write keys matching section pattern")
-        if any(delim in key for delim in self._delimiters):
-            raise InvalidWriteError("Cannot write key that contains delimiters")
+            raise InvalidWriteError(
+                f"Cannot write key {key}; begins with section pattern")
+        for delim in self._delimiters:
+            if delim in key:
+                raise InvalidWriteError(
+                    f"Cannot write key {key}; contains delimiter {delim}")
 
     def _validate_value_types(self, *, section="", option="", value=""):
         """Raises a TypeError for illegal non-string values.
diff --git a/Misc/NEWS.d/next/Library/2025-06-15-03-03-22.gh-issue-65697.COdwZd.rst b/Misc/NEWS.d/next/Library/2025-06-15-03-03-22.gh-issue-65697.COdwZd.rst
new file mode 100644 (file)
index 0000000..d374220
--- /dev/null
@@ -0,0 +1 @@
+:class:`configparser`'s error message when attempting to write an invalid key is now more helpful.