8000 bpo-42051: Reject XML entity declarations in plist files (GH-22760) · python/cpython@65894ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 65894ca

Browse files
bpo-42051: Reject XML entity declarations in plist files (GH-22760)
(cherry picked from commit 05ee790) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
1 parent 3faef63 commit 65894ca

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/plistlib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,16 @@ def parse(self, fileobj):
285285
self.parser.StartElementHandler = self.handle_begin_element
286286
self.parser.EndElementHandler = self.handle_end_element
287287
self.parser.CharacterDataHandler = self.handle_data
288+
self.parser.EntityDeclHandler = self.handle_entity_decl
288289
self.parser.ParseFile(fileobj)
289290
return self.root
290291

292+
def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
293+
# Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
294+
# Regular plist files don't contain those declerations, and Apple's plutil tool does not
295+
# accept them either.
296+
raise InvalidFileException("XML entity declarations are not supported in plist files")
297+
291298
def handle_begin_element(self, element, attrs):
292299
self.data = []
293300
handler = getattr(self, "begin_" + element, None)

Lib/test/test_plistlib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@
105105
AAABOQ=='''),
106106
}
107107

108+
XML_PLIST_WITH_ENTITY=b'''\
109+
<?xml version="1.0" encoding="UTF-8"?>
110+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" [
111+
<!ENTITY entity "replacement text">
112+
]>
113+
<plist version="1.0">
114+
<dict>
115+
<key>A</key>
116+
<string>&entity;</string>
117+
</dict>
118+
</plist>
119+
'''
120+
108121

109122
class TestPlistlib(unittest.TestCase):
110123

@@ -525,6 +538,11 @@ def test_modified_uid_huge(self):
525538
with self.assertRaises(OverflowError):
526539
plistlib.dumps(huge_uid, fmt=plistlib.FMT_BINARY)
527540

541+
def test_xml_plist_with_entity_decl(self):
542+
with self.assertRaisesRegex(plistlib.InvalidFileException,
543+
"XML entity declarations are not supported"):
544+
plistlib.loads(XML_PLIST_WITH_ENTITY, fmt=plistlib.FMT_XML)
545+
528546

529547
class TestBinaryPlistlib(unittest.TestCase):
530548

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :mod:`plistlib` module no longer accepts entity declarations in XML
2+
plist files to avoid XML vulnerabilities. This should not affect users as
3+
entity declarations are not used in regular plist files.

0 commit comments

Comments
 (0)
0