blob: d255f7fcc686e30f563d5858cf4fe318892b5d9d [file] [log] [blame]
Georg Brandlfe7b00f2012-10-06 13:49:34 +02001.. _xml:
2
3XML Processing Modules
4======================
5
Christian Heimes7380a672013-03-26 17:35:55 +01006.. module:: xml
7 :synopsis: Package containing XML processing modules
8.. sectionauthor:: Christian Heimes <christian@python.org>
9.. sectionauthor:: Georg Brandl <georg@python.org>
10
11
Georg Brandlfe7b00f2012-10-06 13:49:34 +020012Python's interfaces for processing XML are grouped in the ``xml`` package.
13
Christian Heimes7380a672013-03-26 17:35:55 +010014.. warning::
15
16 The XML modules are not secure against erroneous or maliciously
17 constructed data. If you need to parse untrusted or unauthenticated data see
18 :ref:`xml-vulnerabilities`.
19
Christian Heimes768f6a52013-03-26 17:47:23 +010020
Georg Brandlfe7b00f2012-10-06 13:49:34 +020021It is important to note that modules in the :mod:`xml` package require that
22there be at least one SAX-compliant XML parser available. The Expat parser is
23included with Python, so the :mod:`xml.parsers.expat` module will always be
24available.
25
26The documentation for the :mod:`xml.dom` and :mod:`xml.sax` packages are the
27definition of the Python bindings for the DOM and SAX interfaces.
28
29The XML handling submodules are:
30
31* :mod:`xml.etree.ElementTree`: the ElementTree API, a simple and lightweight
32
33..
34
35* :mod:`xml.dom`: the DOM API definition
36* :mod:`xml.dom.minidom`: a lightweight DOM implementation
37* :mod:`xml.dom.pulldom`: support for building partial DOM trees
38
39..
40
41* :mod:`xml.sax`: SAX2 base classes and convenience functions
42* :mod:`xml.parsers.expat`: the Expat parser binding
Christian Heimes7380a672013-03-26 17:35:55 +010043
44
45.. _xml-vulnerabilities:
46
47XML vulnerabilities
48===================
49
50The XML processing modules are not secure against maliciously constructed data.
51An attacker can abuse vulnerabilities for e.g. denial of service attacks, to
52access local files, to generate network connections to other machines, or
53to or circumvent firewalls. The attacks on XML abuse unfamiliar features
54like inline `DTD`_ (document type definition) with entities.
55
56
57========================= ======== ========= ========= ======== =========
58kind sax etree minidom pulldom xmlrpc
59========================= ======== ========= ========= ======== =========
60billion laughs **True** **True** **True** **True** **True**
61quadratic blowup **True** **True** **True** **True** **True**
62external entity expansion **True** False (1) False (2) **True** False (3)
63DTD retrieval **True** False False **True** False
64decompression bomb False False False False **True**
65========================= ======== ========= ========= ======== =========
66
671. :mod:`xml.etree.ElementTree` doesn't expand external entities and raises a
68 ParserError when an entity occurs.
692. :mod:`xml.dom.minidom` doesn't expand external entities and simply returns
70 the unexpanded entity verbatim.
713. :mod:`xmlrpclib` doesn't expand external entities and omits them.
72
73
74billion laughs / exponential entity expansion
75 The `Billion Laughs`_ attack -- also known as exponential entity expansion --
76 uses multiple levels of nested entities. Each entity refers to another entity
77 several times, the final entity definition contains a small string. Eventually
78 the small string is expanded to several gigabytes. The exponential expansion
79 consumes lots of CPU time, too.
80
81quadratic blowup entity expansion
82 A quadratic blowup attack is similar to a `Billion Laughs`_ attack; it abuses
83 entity expansion, too. Instead of nested entities it repeats one large entity
84 with a couple of thousand chars over and over again. The attack isn't as
85 efficient as the exponential case but it avoids triggering countermeasures of
86 parsers against heavily nested entities.
87
88external entity expansion
89 Entity declarations can contain more than just text for replacement. They can
90 also point to external resources by public identifiers or system identifiers.
91 System identifiers are standard URIs or can refer to local files. The XML
92 parser retrieves the resource with e.g. HTTP or FTP requests and embeds the
93 content into the XML document.
94
95DTD retrieval
96 Some XML libraries like Python's mod:'xml.dom.pulldom' retrieve document type
97 definitions from remote or local locations. The feature has similar
98 implications as the external entity expansion issue.
99
100decompression bomb
101 The issue of decompression bombs (aka `ZIP bomb`_) apply to all XML libraries
102 that can parse compressed XML stream like gzipped HTTP streams or LZMA-ed
103 files. For an attacker it can reduce the amount of transmitted data by three
104 magnitudes or more.
105
106The documentation of `defusedxml`_ on PyPI has further information about
107all known attack vectors with examples and references.
108
109defused packages
110----------------
111
112`defusedxml`_ is a pure Python package with modified subclasses of all stdlib
113XML parsers that prevent any potentially malicious operation. The courses of
114action are recommended for any server code that parses untrusted XML data. The
115package also ships with example exploits and an extended documentation on more
116XML exploits like xpath injection.
117
118`defusedexpat`_ provides a modified libexpat and patched replacment
119:mod:`pyexpat` extension module with countermeasures against entity expansion
120DoS attacks. Defusedexpat still allows a sane and configurable amount of entity
121expansions. The modifications will be merged into future releases of Python.
122
123The workarounds and modifications are not included in patch releases as they
124break backward compatibility. After all inline DTD and entity expansion are
125well-definied XML features.
126
127
Georg Brandl6ba6b132013-03-28 09:11:44 +0100128.. _defusedxml: https://pypi.python.org/pypi/defusedxml/
129.. _defusedexpat: https://pypi.python.org/pypi/defusedexpat/
Christian Heimes7380a672013-03-26 17:35:55 +0100130.. _Billion Laughs: http://en.wikipedia.org/wiki/Billion_laughs
131.. _ZIP bomb: http://en.wikipedia.org/wiki/Zip_bomb
132.. _DTD: http://en.wikipedia.org/wiki/Document_Type_Definition
Christian Heimes768f6a52013-03-26 17:47:23 +0100133