Skip to content

Commit a158fb9

Browse files
bpo-42051: Reject XML entity declarations in plist files (GH-22760) (GH-22801) (GH-22804)
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com> (cherry picked from commit e512bc7) Co-authored-by: Ned Deily <nad@python.org>
1 parent a69002c commit a158fb9

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
@@ -322,9 +322,16 @@ def parse(self, fileobj):
322322
self.parser.StartElementHandler = self.handle_begin_element
323323
self.parser.EndElementHandler = self.handle_end_element
324324
self.parser.CharacterDataHandler = self.handle_data
325+
self.parser.EntityDeclHandler = self.handle_entity_decl
325326
self.parser.ParseFile(fileobj)
326327
return self.root
327328

329+
def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
330+
# Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
331+
# Regular plist files don't contain those declerations, and Apple's plutil tool does not
332+
# accept them either.
333+
raise InvalidFileException("XML entity declarations are not supported in plist files")
334+
328335
def handle_begin_element(self, element, attrs):
329336
self.data = []
330337
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
@@ -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

@@ -444,6 +457,11 @@ def test_xml_encodings(self):
444457
pl2 = plistlib.loads(data)
445458
self.assertEqual(dict(pl), dict(pl2))
446459

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

448466
class TestBinaryPlistlib(unittest.TestCase):
449467

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy