Christian Heimes announces the release of his defusedxml and defusedexpat packages to address XML-related security issues which were reported to security@python.org over the last several months. Throughout the development of the patches, the security team has coordinated with other open source projects in order to make this announcement at 1500 UTC on Tuesday February 19.
Details will follow once releases of CPython have been organized.
Note: this post will be updated with more details as they switch from being private to publicly available, including links to the public bug reports on http://bugs.python.org.
defusedxml on PyPI: https://pypi.python.org/pypi/defusedxml
defusedexpat on PyPI: https://pypi.python.org/pypi/defusedexpat
"XML vulnerabilities" on bug tracker: http://bugs.python.org/issue17239
Synopsis
Attack vectors
billion laughs / exponential entity expansion
<!DOCTYPE xmlbomb [
<!ENTITY a "1234567890" >
<!ENTITY b "&a;&a;&a;&a;&a;&a;&a;&a;">
<!ENTITY c "&b;&b;&b;&b;&b;&b;&b;&b;">
<!ENTITY d "&c;&c;&c;&c;&c;&c;&c;&c;">
]>
<bomb>&d;</bomb>
quadratic blowup entity expansion
<!DOCTYPE bomb [
<!ENTITY a "xxxxxxx... a couple of ten thousand chars">
]>
<bomb>&a;&a;&a;... repeat</bomb>
external entity expansion (remote)
<!DOCTYPE external [
<!ENTITY ee SYSTEM "http://www.python.org/some.xml">
]>
<root>ⅇ</root>
- An attacker can circumvent firewalls and gain access to restricted resources as all the requests are made from an internal and trustworthy IP address, not from the outside.
- An attacker can abuse a service to attack, spy on or DoS your servers but also third party services. The attack is disguised with the IP address of the server and the attacker is able to utilize the high bandwidth of a big machine.
- An attacker can exhaust additional resources on the machine, e.g. with requests to a service that doesn't respond or responds with very large files.
- An attacker may gain knowledge, when, how often and from which IP address a XML document is accessed.
- An attacker could send mail from inside your network if the URL handler supports smtp:// URIs.
external entity expansion (local file)
<!DOCTYPE external [
<!ENTITY ee SYSTEM "file:///PATH/TO/simple.xml">
]>
<root>ⅇ</root>
Python XML Libraries
kind | sax | etree | minidom | pulldom | xmlrpc |
---|---|---|---|---|---|
billion laughs | True | True | True | True | True |
quadratic blowup | True | True | True | True | True |
external entity expansion (remote) | True | False (3) | False (4) | True | untested |
external entity expansion (local file) | True | False (3) | False (4) | True | untested |
DTD retrieval | True | False | False | True | untested |
gzip bomb | False | False | False | False | True |
xpath support (7) | False | False | False | False | False |
xsl(t) support (7) | False | False | False | False | False |
xinclude support (7) | False | True (6) | False | False | False |
C library | expat | expat | expat | expat | expat |
- Lxml is protected against billion laughs attacks and doesn't do network lookups by default.
- libxml2 and lxml are not directly vulnerable to gzip decompression bombs but they don't protect you against them either.
- xml.etree doesn't expand entities and raises a ParserError when an entity occurs.
- minidom doesn't expand entities and simply returns the unexpanded entity verbatim.
- genshi.input of genshi 0.6 doesn't support entity expansion and raises a ParserError when an entity occurs.
- Library has (limited) XInclude support but requires an additional step to process inclusion.
- These are features but they may introduce exploitable holes
How to avoid XML vulnerabilities
Best practices
- Don't allow DTDs
- Don't expand entities
- Don't resolve externals
- Limit parse depth
- Limit total input size
- Limit parse time
- Favor a SAX or iterparse-like parser for potential large data
- Validate and properly quote arguments to XSL transformations and XPath queries
- Don't use XPath expression from untrusted sources
- Don't apply XSL transformations that come untrusted sources
Related CVEs
- CVE-2013-1664
- Unrestricted entity expansion induces DoS vulnerabilities in Python XML libraries (XML bomb)
- CVE-2013-1665
- External entity expansion in Python XML libraries inflicts potential security flaws and DoS vulnerabilities
Acknowledgements
- Brett Cannon (Python Core developer)
- review and code cleanup
- Antoine Pitrou (Python Core developer)
- code review
- Aaron Patterson, Ben Murphy and Michael Koziarski (Ruby community)
- Many thanks to Aaron, Ben and Michael from the Ruby community for their report and assistance.
- Thierry Carrez (OpenStack)
- Many thanks to Thierry for his report to the Python Security Response Team on behalf of the OpenStack security team.
- Carl Meyer (Django)
- Many thanks to Carl for his report to PSRT on behalf of the Django security team.
- Daniel Veillard (libxml2)
- Many thanks to Daniel for his insight and assistance with libxml2.
- semantics GmbH (http://www.semantics.de/)
- Many thanks to my employer semantics for letting me work on the issue during working hours as part of semantics's open source initiative.