blob: f793baec9b1534fd8fa4529c5bd17125b99bf044 [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
Zachary Ware19c1f3d2014-01-31 11:30:36 -060032 XML processor
Georg Brandlfe7b00f2012-10-06 13:49:34 +020033
34..
35
36* :mod:`xml.dom`: the DOM API definition
Antoine Pitrouf20ea132013-12-22 01:57:01 +010037* :mod:`xml.dom.minidom`: a minimal DOM implementation
Georg Brandlfe7b00f2012-10-06 13:49:34 +020038* :mod:`xml.dom.pulldom`: support for building partial DOM trees
39
40..
41
42* :mod:`xml.sax`: SAX2 base classes and convenience functions
43* :mod:`xml.parsers.expat`: the Expat parser binding
Christian Heimes7380a672013-03-26 17:35:55 +010044
45
46.. _xml-vulnerabilities:
47
48XML vulnerabilities
49===================
50
51The XML processing modules are not secure against maliciously constructed data.
52An attacker can abuse vulnerabilities for e.g. denial of service attacks, to
53access local files, to generate network connections to other machines, or
54to or circumvent firewalls. The attacks on XML abuse unfamiliar features
55like inline `DTD`_ (document type definition) with entities.
56
Georg Brandl57f936e2013-10-12 18:19:33 +020057The following table gives an overview of the known attacks and if the various
58modules are vulnerable to them.
Christian Heimes7380a672013-03-26 17:35:55 +010059
60========================= ======== ========= ========= ======== =========
61kind sax etree minidom pulldom xmlrpc
62========================= ======== ========= ========= ======== =========
Georg Brandl57f936e2013-10-12 18:19:33 +020063billion laughs **Yes** **Yes** **Yes** **Yes** **Yes**
64quadratic blowup **Yes** **Yes** **Yes** **Yes** **Yes**
65external entity expansion **Yes** No (1) No (2) **Yes** No (3)
66DTD retrieval **Yes** No No **Yes** No
67decompression bomb No No No No **Yes**
Christian Heimes7380a672013-03-26 17:35:55 +010068========================= ======== ========= ========= ======== =========
69
701. :mod:`xml.etree.ElementTree` doesn't expand external entities and raises a
71 ParserError when an entity occurs.
722. :mod:`xml.dom.minidom` doesn't expand external entities and simply returns
73 the unexpanded entity verbatim.
743. :mod:`xmlrpclib` doesn't expand external entities and omits them.
75
76
77billion laughs / exponential entity expansion
78 The `Billion Laughs`_ attack -- also known as exponential entity expansion --
79 uses multiple levels of nested entities. Each entity refers to another entity
80 several times, the final entity definition contains a small string. Eventually
81 the small string is expanded to several gigabytes. The exponential expansion
82 consumes lots of CPU time, too.
83
84quadratic blowup entity expansion
85 A quadratic blowup attack is similar to a `Billion Laughs`_ attack; it abuses
86 entity expansion, too. Instead of nested entities it repeats one large entity
87 with a couple of thousand chars over and over again. The attack isn't as
88 efficient as the exponential case but it avoids triggering countermeasures of
89 parsers against heavily nested entities.
90
91external entity expansion
92 Entity declarations can contain more than just text for replacement. They can
93 also point to external resources by public identifiers or system identifiers.
94 System identifiers are standard URIs or can refer to local files. The XML
95 parser retrieves the resource with e.g. HTTP or FTP requests and embeds the
96 content into the XML document.
97
98DTD retrieval
R David Murray66c93502014-01-13 13:51:17 -050099 Some XML libraries like Python's :mod:`xml.dom.pulldom` retrieve document type
Christian Heimes7380a672013-03-26 17:35:55 +0100100 definitions from remote or local locations. The feature has similar
101 implications as the external entity expansion issue.
102
103decompression bomb
104 The issue of decompression bombs (aka `ZIP bomb`_) apply to all XML libraries
105 that can parse compressed XML stream like gzipped HTTP streams or LZMA-ed
106 files. For an attacker it can reduce the amount of transmitted data by three
107 magnitudes or more.
108
109The documentation of `defusedxml`_ on PyPI has further information about
110all known attack vectors with examples and references.
111
112defused packages
113----------------
114
115`defusedxml`_ is a pure Python package with modified subclasses of all stdlib
116XML parsers that prevent any potentially malicious operation. The courses of
117action are recommended for any server code that parses untrusted XML data. The
118package also ships with example exploits and an extended documentation on more
119XML exploits like xpath injection.
120
121`defusedexpat`_ provides a modified libexpat and patched replacment
122:mod:`pyexpat` extension module with countermeasures against entity expansion
123DoS attacks. Defusedexpat still allows a sane and configurable amount of entity
124expansions. The modifications will be merged into future releases of Python.
125
126The workarounds and modifications are not included in patch releases as they
127break backward compatibility. After all inline DTD and entity expansion are
128well-definied XML features.
129
130
Georg Brandl6ba6b132013-03-28 09:11:44 +0100131.. _defusedxml: https://pypi.python.org/pypi/defusedxml/
132.. _defusedexpat: https://pypi.python.org/pypi/defusedexpat/
Christian Heimes7380a672013-03-26 17:35:55 +0100133.. _Billion Laughs: http://en.wikipedia.org/wiki/Billion_laughs
134.. _ZIP bomb: http://en.wikipedia.org/wiki/Zip_bomb
135.. _DTD: http://en.wikipedia.org/wiki/Document_Type_Definition
Christian Heimes768f6a52013-03-26 17:47:23 +0100136