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