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

Skip to content

Commit 05ee790

Browse files
bpo-42051: Reject XML entity declarations in plist files (#22760)
1 parent 985f0ab commit 05ee790

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
@@ -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

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

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

110123
class TestPlistlib(unittest.TestCase):
111124

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

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

528546
class TestBinaryPlistlib(unittest.TestCase):
529547

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