]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42051: Reject XML entity declarations in plist files (#22760)
authorRonald Oussoren <ronaldoussoren@mac.com>
Mon, 19 Oct 2020 18:13:49 +0000 (20:13 +0200)
committerGitHub <noreply@github.com>
Mon, 19 Oct 2020 18:13:49 +0000 (20:13 +0200)
Lib/plistlib.py
Lib/test/test_plistlib.py
Misc/NEWS.d/next/Security/2020-10-19-10-56-27.bpo-42051.EU_B7u.rst [new file with mode: 0644]

index aff5fe36ca38b4c78dd7e0b21b4a01636c883918..ba7ac1936479f74a2a9359d506ace5f1e2f4f32d 100644 (file)
@@ -173,9 +173,16 @@ class _PlistParser:
         self.parser.StartElementHandler = self.handle_begin_element
         self.parser.EndElementHandler = self.handle_end_element
         self.parser.CharacterDataHandler = self.handle_data
+        self.parser.EntityDeclHandler = self.handle_entity_decl
         self.parser.ParseFile(fileobj)
         return self.root
 
+    def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
+        # Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
+        # Regular plist files don't contain those declerations, and Apple's plutil tool does not
+        # accept them either.
+        raise InvalidFileException("XML entity declarations are not supported in plist files")
+
     def handle_begin_element(self, element, attrs):
         self.data = []
         handler = getattr(self, "begin_" + element, None)
index e5c9b5b6b2cfea5ccfd47d595753477717e4926a..cb071da1f33a1bc92ba354d8bf511c90011c7d79 100644 (file)
@@ -106,6 +106,19 @@ TESTDATA={
         AAABOQ=='''),
 }
 
+XML_PLIST_WITH_ENTITY=b'''\
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" [
+   <!ENTITY entity "replacement text">
+  ]>
+<plist version="1.0">
+  <dict>
+    <key>A</key>
+    <string>&entity;</string>
+  </dict>
+</plist>
+'''
+
 
 class TestPlistlib(unittest.TestCase):
 
@@ -524,6 +537,11 @@ class TestPlistlib(unittest.TestCase):
         with self.assertRaises(OverflowError):
             plistlib.dumps(huge_uid, fmt=plistlib.FMT_BINARY)
 
+    def test_xml_plist_with_entity_decl(self):
+        with self.assertRaisesRegex(plistlib.InvalidFileException,
+                                    "XML entity declarations are not supported"):
+            plistlib.loads(XML_PLIST_WITH_ENTITY, fmt=plistlib.FMT_XML)
+
 
 class TestBinaryPlistlib(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Security/2020-10-19-10-56-27.bpo-42051.EU_B7u.rst b/Misc/NEWS.d/next/Security/2020-10-19-10-56-27.bpo-42051.EU_B7u.rst
new file mode 100644 (file)
index 0000000..e865ed1
--- /dev/null
@@ -0,0 +1,3 @@
+The :mod:`plistlib` module no longer accepts entity declarations in XML
+plist files to avoid XML vulnerabilities. This should not affect users as
+entity declarations are not used in regular plist files.