]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 24 Jun 2026 09:35:18 +0000 (11:35 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Jun 2026 09:35:18 +0000 (11:35 +0200)
gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser (GH-143929)
(cherry picked from commit 5858e42c539dac8394636a6e9b30472b8994851f)

Co-authored-by: Seth Larson <seth@python.org>
Lib/configparser.py
Lib/test/test_configparser.py
Misc/NEWS.d/next/Security/2026-01-16-11-58-19.gh-issue-143927.aviFeG.rst [new file with mode: 0644]

index a53ac87276445adbd4c7222bc88b68b06798866d..3c452afe8ade48501f4561db2d5505206fbca8db 100644 (file)
@@ -992,7 +992,9 @@ class RawConfigParser(MutableMapping):
             value = self._interpolation.before_write(self, section_name, key,
                                                      value)
             if value is not None or not self._allow_no_value:
-                value = delimiter + str(value).replace('\n', '\n\t')
+                # Convert all possible line-endings into '\n\t'
+                value = (delimiter + str(value).replace('\r\n', '\n')
+                         .replace('\r', '\n').replace('\n', '\n\t'))
             else:
                 value = ""
             fp.write("{}{}\n".format(key, value))
index 8d8dd2a2bf27fbf358489850dcb22cbc7af46845..4783943f71a1092cd849c6dcc751a1ec573fb076 100644 (file)
@@ -526,6 +526,17 @@ boolean {0[0]} NO
             cf.get(self.default_section, "Foo"), "Bar",
             "could not locate option, expecting case-insensitive defaults")
 
+    def test_crlf_normalization(self):
+        cf = self.newconfig({"key1": "a\nb","key2": "a\rb", "key3": "a\r\nb", "key4": "a\r\nb"})
+        buf = io.StringIO()
+        cf.write(buf)
+        cf_str = buf.getvalue()
+        self.assertNotIn("\r", cf_str)
+        self.assertNotIn("\r\n", cf_str)
+        self.assertEqual(cf_str.count("\n"), 10)
+        self.assertEqual(cf_str.count("\n\t"), 4)
+        self.assertTrue(cf_str.endswith("\n\n"))
+
     def test_parse_errors(self):
         cf = self.newconfig()
         self.parse_error(cf, configparser.ParsingError,
diff --git a/Misc/NEWS.d/next/Security/2026-01-16-11-58-19.gh-issue-143927.aviFeG.rst b/Misc/NEWS.d/next/Security/2026-01-16-11-58-19.gh-issue-143927.aviFeG.rst
new file mode 100644 (file)
index 0000000..ca55499
--- /dev/null
@@ -0,0 +1,2 @@
+Normalize all line endings (CR, CRLF, and LF) to LF+TAB when writing
+multi-line configparser values.