Skip to content

Commit e512bc7

Browse files
bpo-42051: Reject XML entity declarations in plist files (#22760) (GH-22801)
Co-authored-by: Ronald Oussoren <[email protected]>
1 parent 9b5a023 commit e512bc7

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/plistlib.py

+7
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,16 @@ def parse(self, fileobj):
257257
self.parser.StartElementHandler = self.handle_begin_element
258258
self.parser.EndElementHandler = self.handle_end_element
259259
self.parser.CharacterDataHandler = self.handle_data
260+
self.parser.EntityDeclHandler = self.handle_entity_decl
260261
self.parser.ParseFile(fileobj)
261262
return self.root
262263

264+
def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
265+
# Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
266+
# Regular plist files don't contain those declerations, and Apple's plutil tool does not
267+
# accept them either.
268+
raise InvalidFileException("XML entity declarations are not supported in plist files")
269+
263270
def handle_begin_element(self, element, attrs):
264271
self.data = []
265272
handler = getattr(self, "begin_" + element, None)

Lib/test/test_plistlib.py

+18
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@
9090
xQHHAsQC0gAAAAAAAAIBAAAAAAAAADkAAAAAAAAAAAAAAAAAAALs'''),
9191
}
9292

93+
XML_PLIST_WITH_ENTITY=b'''\
94+
<?xml version="1.0" encoding="UTF-8"?>
95+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" [
96+
<!ENTITY entity "replacement text">
97+
]>
98+
<plist version="1.0">
99+
<dict>
100+
<key>A</key>
101+
<string>&entity;</string>
102+
</dict>
103+
</plist>
104+
'''
105+
93106

94107
class TestPlistlib(unittest.TestCase):
95108

@@ -443,6 +456,11 @@ def test_xml_encodings(self):
443456
pl2 = plistlib.loads(data)
444457
self.assertEqual(dict(pl), dict(pl2))
445458

459+
def test_xml_plist_with_entity_decl(self):
460+
with self.assertRaisesRegex(plistlib.InvalidFileException,
461+
"XML entity declarations are not supported"):
462+
plistlib.loads(XML_PLIST_WITH_ENTITY, fmt=plistlib.FMT_XML)
463+
446464

447465
class TestBinaryPlistlib(unittest.TestCase):
448466

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)