Skip to content

Commit 4a93947

Browse files
committed
bpo-42051: Reject XML entity declarations in plist files (python#22760) (GC-22801)
Co-authored-by: Ronald Oussoren <[email protected]> Co-authored-by: Ned Deily <[email protected]> Add InvalidFileException class. RaisesRegex is RaisesRegexp in python2. plistlib.loads is in the python3 version of plistlib, but there are too many changes to backport this simply.
1 parent 8ebb54f commit 4a93947

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

Lib/plistlib.py

+12
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,16 @@ def parse(self, fileobj):
403403
parser.StartElementHandler = self.handleBeginElement
404404
parser.EndElementHandler = self.handleEndElement
405405
parser.CharacterDataHandler = self.handleData
406+
parser.EntityDeclHandler = self.handle_entity_decl
406407
parser.ParseFile(fileobj)
407408
return self.root
408409

410+
def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
411+
# Reject plist files with entity declarations to avoid XML vulnerabilies in expat.
412+
# Regular plist files don't contain those declerations, and Apple's plutil tool does not
413+
# accept them either.
414+
raise InvalidFileException("XML entity declarations are not supported in plist files")
415+
409416
def handleBeginElement(self, element, attrs):
410417
self.data = []
411418
handler = getattr(self, "begin_" + element, None)
@@ -472,3 +479,8 @@ def end_data(self):
472479
self.addObject(Data.fromBase64(self.getData()))
473480
def end_date(self):
474481
self.addObject(_dateFromString(self.getData()))
482+
483+
484+
class InvalidFileException (ValueError):
485+
def __init__(self, message="Invalid file"):
486+
ValueError.__init__(self, message)

Lib/test/test_plistlib.py

+17
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
</plist>
8787
""".replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs
8888

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

90103
class TestPlistlib(unittest.TestCase):
91104

@@ -195,6 +208,10 @@ def test_nondictroot(self):
195208
self.assertEqual(test1, result1)
196209
self.assertEqual(test2, result2)
197210

211+
def test_xml_plist_with_entity_decl(self):
212+
with self.assertRaisesRegexp(plistlib.InvalidFileException,
213+
"XML entity declarations are not supported"):
214+
plistlib.readPlistFromString(XML_PLIST_WITH_ENTITY)
198215

199216
def test_main():
200217
test_support.run_unittest(TestPlistlib)

Misc/NEWS.d/2.7.18.8.rst

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. bpo: 37428
2+
.. date: 2024-02-15
3+
.. nonce:
4+
.. release date: 2024-02-15
5+
.. section: Core and Builtins
6+
7+
CVE-2023-40217
8+
9+
SSLContext.post_handshake_auth = True no longer sets
10+
SSL_VERIFY_POST_HANDSHAKE verify flag for client connections. Although the
11+
option is documented as ignored for clients, OpenSSL implicitly enables cert
12+
chain validation when the flag is set.
13+
14+
.. bpo: ?
15+
.. date: 2024-02-15
16+
.. nonce:
17+
.. release date: 2024-02-15
18+
.. section: Core and Builtins
19+
20+
CVE-2023-24329
21+
22+
Start stripping C0 control and space chars in urlsplit (#… …102508)
23+
24+
`urllib.parse.urlsplit` has already been respecting the WHATWG spec a bit #25595.
25+
26+
This adds more sanitizing to respect the "Remove any leading C0 control or space from input" [rule](https://url.spec.whatwg.org/#url-parsing:~:text=Remove%20any%20leading%20and%20trailing%20C0%20control%20or%20space%20from%20input.) in response to [CVE-2023-24329](https://nvd.nist.gov/vuln/detail/CVE-2023-24329).
27+
28+
.. bpo: 43882
29+
.. date: 2024-02-15
30+
.. nonce:
31+
.. release date: 2024-02-15
32+
.. section: Core and Builtins
33+
34+
CVE-2022-0391
35+
36+
A flaw was found in Python, specifically within the urllib.parse module. This module helps break Uniform Resource Locator (URL) strings into components. The issue involves how the urlparse method does not sanitize input and allows characters like '\r' and '\n' in the URL path. This flaw allows an attacker to input a crafted URL, leading to injection attacks. This flaw affects Python versions prior to 3.10.0b1, 3.9.5, 3.8.11, 3.7.11 and 3.6.14.
37+
38+
.. bpo: 43285
39+
.. date: 2024-02-15
40+
.. nonce:
41+
.. release date: 2024-02-15
42+
.. section: Core and Builtins
43+
44+
CVE-2021-4189
45+
46+
A flaw was found in Python, specifically in the FTP (File Transfer Protocol) client library in PASV (passive) mode. The issue is how the FTP client trusts the host from the PASV response by default. This flaw allows an attacker to set up a malicious FTP server that can trick FTP clients into connecting back to a given IP address and port. This vulnerability could lead to FTP client scanning ports, which otherwise would not have been possible.
47+
48+
.. bpo: 42051
49+
.. date: 2024-03-12
50+
.. nonce:
51+
.. release date: 2024-03-12
52+
.. section: Core and Builtins
53+
54+
CVE-2022-48565
55+
56+
An XML External Entity (XXE) issue was discovered in Python through 3.9.1. The plistlib module no longer accepts entity declarations in XML plist files to avoid XML vulnerabilities.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :mod: 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)