Skip to content

Commit 479553c

Browse files
bpo-42051: Reject XML entity declarations in plist files (GH-22760)
(cherry picked from commit 05ee790) Co-authored-by: Ronald Oussoren <[email protected]>
1 parent e43bee7 commit 479553c

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
@@ -173,9 +173,16 @@ def parse(self, fileobj):
173173
self.parser.StartElementHandler = self.handle_begin_element
174174
self.parser.EndElementHandler = self.handle_end_element
175175
self.parser.CharacterDataHandler = self.handle_data
176+
self.parser.EntityDeclHandler = self.handle_entity_decl
176177
self.parser.ParseFile(fileobj)
177178
return self.root
178179

180+
def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
181+
# Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
182+
# Regular plist files don't contain those declerations, and Apple's plutil tool does not
183+
# accept them either.
184+
raise InvalidFileException("XML entity declarations are not supported in plist files")
185+
179186
def handle_begin_element(self, element, attrs):
180187
self.data = []
181188
handler = getattr(self, "begin_" + element, None)

Lib/test/test_plistlib.py

+18
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

@@ -523,6 +536,11 @@ def test_modified_uid_huge(self):
523536
with self.assertRaises(OverflowError):
524537
plistlib.dumps(huge_uid, fmt=plistlib.FMT_BINARY)
525538

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

527545
class TestBinaryPlistlib(unittest.TestCase):
528546

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)