1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>19.5. XML Processing Modules — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="19. Structured Markup Processing Tools" href="markup.html" />
34
<link rel="next" title="19.7. xml.etree.ElementTree — The ElementTree XML API" href="xml.etree.elementtree.html" />
35
<link rel="prev" title="19.3. htmllib — A parser for HTML documents" href="htmllib.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="xml.etree.elementtree.html" title="19.7. xml.etree.ElementTree — The ElementTree XML API"
55
accesskey="N">next</a> |</li>
57
<a href="htmllib.html" title="19.3. htmllib — A parser for HTML documents"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">19. Structured Markup Processing Tools</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-xml">
78
<span id="xml-processing-modules"></span><span id="xml"></span><h1>19.5. XML Processing Modules<a class="headerlink" href="#module-xml" title="Permalink to this headline">¶</a></h1>
79
<p>Python’s interfaces for processing XML are grouped in the <code class="docutils literal"><span class="pre">xml</span></code> package.</p>
80
<div class="admonition warning">
81
<p class="first admonition-title">Warning</p>
82
<p class="last">The XML modules are not secure against erroneous or maliciously
83
constructed data. If you need to parse untrusted or unauthenticated data see
84
<a class="reference internal" href="#xml-vulnerabilities"><span>XML vulnerabilities</span></a>.</p>
86
<p>It is important to note that modules in the <a class="reference internal" href="#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal"><span class="pre">xml</span></code></a> package require that
87
there be at least one SAX-compliant XML parser available. The Expat parser is
88
included with Python, so the <a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></code></a> module will always be
90
<p>The documentation for the <a class="reference internal" href="xml.dom.html#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom</span></code></a> and <a class="reference internal" href="xml.sax.html#module-xml.sax" title="xml.sax: Package containing SAX2 base classes and convenience functions."><code class="xref py py-mod docutils literal"><span class="pre">xml.sax</span></code></a> packages are the
91
definition of the Python bindings for the DOM and SAX interfaces.</p>
92
<p>The XML handling submodules are:</p>
94
<li><a class="reference internal" href="xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a>: the ElementTree API, a simple and lightweight
98
<li><a class="reference internal" href="xml.dom.html#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom</span></code></a>: the DOM API definition</li>
99
<li><a class="reference internal" href="xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom.minidom</span></code></a>: a minimal DOM implementation</li>
100
<li><a class="reference internal" href="xml.dom.pulldom.html#module-xml.dom.pulldom" title="xml.dom.pulldom: Support for building partial DOM trees from SAX events."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom.pulldom</span></code></a>: support for building partial DOM trees</li>
103
<li><a class="reference internal" href="xml.sax.html#module-xml.sax" title="xml.sax: Package containing SAX2 base classes and convenience functions."><code class="xref py py-mod docutils literal"><span class="pre">xml.sax</span></code></a>: SAX2 base classes and convenience functions</li>
104
<li><a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></code></a>: the Expat parser binding</li>
107
<div class="section" id="xml-vulnerabilities">
108
<span id="id1"></span><h1>19.6. XML vulnerabilities<a class="headerlink" href="#xml-vulnerabilities" title="Permalink to this headline">¶</a></h1>
109
<p>The XML processing modules are not secure against maliciously constructed data.
110
An attacker can abuse vulnerabilities for e.g. denial of service attacks, to
111
access local files, to generate network connections to other machines, or
112
to or circumvent firewalls. The attacks on XML abuse unfamiliar features
113
like inline <a class="reference external" href="https://en.wikipedia.org/wiki/Document_type_definition">DTD</a> (document type definition) with entities.</p>
114
<p>The following table gives an overview of the known attacks and if the various
115
modules are vulnerable to them.</p>
116
<table border="1" class="docutils">
125
<thead valign="bottom">
126
<tr class="row-odd"><th class="head">kind</th>
127
<th class="head">sax</th>
128
<th class="head">etree</th>
129
<th class="head">minidom</th>
130
<th class="head">pulldom</th>
131
<th class="head">xmlrpc</th>
135
<tr class="row-even"><td>billion laughs</td>
136
<td><strong>Vulnerable</strong></td>
137
<td><strong>Vulnerable</strong></td>
138
<td><strong>Vulnerable</strong></td>
139
<td><strong>Vulnerable</strong></td>
140
<td><strong>Vulnerable</strong></td>
142
<tr class="row-odd"><td>quadratic blowup</td>
143
<td><strong>Vulnerable</strong></td>
144
<td><strong>Vulnerable</strong></td>
145
<td><strong>Vulnerable</strong></td>
146
<td><strong>Vulnerable</strong></td>
147
<td><strong>Vulnerable</strong></td>
149
<tr class="row-even"><td>external entity expansion</td>
150
<td><strong>Vulnerable</strong></td>
153
<td><strong>Vulnerable</strong></td>
156
<tr class="row-odd"><td><a class="reference external" href="https://en.wikipedia.org/wiki/Document_type_definition">DTD</a> retrieval</td>
157
<td><strong>Vulnerable</strong></td>
160
<td><strong>Vulnerable</strong></td>
163
<tr class="row-even"><td>decompression bomb</td>
168
<td><strong>Vulnerable</strong></td>
172
<ol class="arabic simple">
173
<li><a class="reference internal" href="xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a> doesn’t expand external entities and raises a
174
ParserError when an entity occurs.</li>
175
<li><a class="reference internal" href="xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom.minidom</span></code></a> doesn’t expand external entities and simply returns
176
the unexpanded entity verbatim.</li>
177
<li><a class="reference internal" href="xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><code class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></code></a> doesn’t expand external entities and omits them.</li>
179
<dl class="docutils">
180
<dt>billion laughs / exponential entity expansion</dt>
181
<dd>The <a class="reference external" href="https://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> attack – also known as exponential entity expansion –
182
uses multiple levels of nested entities. Each entity refers to another entity
183
several times, the final entity definition contains a small string. Eventually
184
the small string is expanded to several gigabytes. The exponential expansion
185
consumes lots of CPU time, too.</dd>
186
<dt>quadratic blowup entity expansion</dt>
187
<dd>A quadratic blowup attack is similar to a <a class="reference external" href="https://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> attack; it abuses
188
entity expansion, too. Instead of nested entities it repeats one large entity
189
with a couple of thousand chars over and over again. The attack isn’t as
190
efficient as the exponential case but it avoids triggering countermeasures of
191
parsers against heavily nested entities.</dd>
192
<dt>external entity expansion</dt>
193
<dd>Entity declarations can contain more than just text for replacement. They can
194
also point to external resources by public identifiers or system identifiers.
195
System identifiers are standard URIs or can refer to local files. The XML
196
parser retrieves the resource with e.g. HTTP or FTP requests and embeds the
197
content into the XML document.</dd>
198
<dt><a class="reference external" href="https://en.wikipedia.org/wiki/Document_type_definition">DTD</a> retrieval</dt>
199
<dd>Some XML libraries like Python’s <a class="reference internal" href="xml.dom.pulldom.html#module-xml.dom.pulldom" title="xml.dom.pulldom: Support for building partial DOM trees from SAX events."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom.pulldom</span></code></a> retrieve document type
200
definitions from remote or local locations. The feature has similar
201
implications as the external entity expansion issue.</dd>
202
<dt>decompression bomb</dt>
203
<dd>The issue of decompression bombs (aka <a class="reference external" href="https://en.wikipedia.org/wiki/Zip_bomb">ZIP bomb</a>) apply to all XML libraries
204
that can parse compressed XML stream like gzipped HTTP streams or LZMA-ed
205
files. For an attacker it can reduce the amount of transmitted data by three
206
magnitudes or more.</dd>
208
<p>The documentation of <a class="reference external" href="https://pypi.python.org/pypi/defusedxml/">defusedxml</a> on PyPI has further information about
209
all known attack vectors with examples and references.</p>
210
<div class="section" id="defused-packages">
211
<h2>19.6.1. defused packages<a class="headerlink" href="#defused-packages" title="Permalink to this headline">¶</a></h2>
212
<p>These external packages are recommended for any code that parses
213
untrusted XML data.</p>
214
<p><a class="reference external" href="https://pypi.python.org/pypi/defusedxml/">defusedxml</a> is a pure Python package with modified subclasses of all stdlib
215
XML parsers that prevent any potentially malicious operation. The
216
package also ships with example exploits and extended documentation on more
217
XML exploits like xpath injection.</p>
218
<p><a class="reference external" href="https://pypi.python.org/pypi/defusedexpat/">defusedexpat</a> provides a modified libexpat and patched replacement
219
<code class="xref py py-mod docutils literal"><span class="pre">pyexpat</span></code> extension module with countermeasures against entity expansion
220
DoS attacks. Defusedexpat still allows a sane and configurable amount of entity
221
expansions. The modifications will be merged into future releases of Python.</p>
222
<p>The workarounds and modifications are not included in patch releases as they
223
break backward compatibility. After all inline DTD and entity expansion are
224
well-defined XML features.</p>
232
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
233
<div class="sphinxsidebarwrapper">
234
<h3><a href="../contents.html">Table Of Contents</a></h3>
236
<li><a class="reference internal" href="#">19.5. XML Processing Modules</a></li>
237
<li><a class="reference internal" href="#xml-vulnerabilities">19.6. XML vulnerabilities</a><ul>
238
<li><a class="reference internal" href="#defused-packages">19.6.1. defused packages</a></li>
243
<h4>Previous topic</h4>
244
<p class="topless"><a href="htmllib.html"
245
title="previous chapter">19.3. <code class="docutils literal"><span class="pre">htmllib</span></code> — A parser for HTML documents</a></p>
247
<p class="topless"><a href="xml.etree.elementtree.html"
248
title="next chapter">19.7. <code class="docutils literal"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a></p>
250
<ul class="this-page-menu">
251
<li><a href="../bugs.html">Report a Bug</a></li>
252
<li><a href="../_sources/library/xml.txt"
253
rel="nofollow">Show Source</a></li>
256
<div id="searchbox" style="display: none" role="search">
257
<h3>Quick search</h3>
258
<form class="search" action="../search.html" method="get">
259
<input type="text" name="q" />
260
<input type="submit" value="Go" />
261
<input type="hidden" name="check_keywords" value="yes" />
262
<input type="hidden" name="area" value="default" />
264
<p class="searchtip" style="font-size: 90%">
265
Enter search terms or a module, class or function name.
268
<script type="text/javascript">$('#searchbox').show(0);</script>
271
<div class="clearer"></div>
273
<div class="related" role="navigation" aria-label="related navigation">
276
<li class="right" style="margin-right: 10px">
277
<a href="../genindex.html" title="General Index"
280
<a href="../py-modindex.html" title="Python Module Index"
283
<a href="xml.etree.elementtree.html" title="19.7. xml.etree.ElementTree — The ElementTree XML API"
286
<a href="htmllib.html" title="19.3. htmllib — A parser for HTML documents"
288
<li><img src="../_static/py.png" alt=""
289
style="vertical-align: middle; margin-top: -1px"/></li>
290
<li><a href="https://www.python.org/">Python</a> »</li>
292
<span class="version_switcher_placeholder">2.7.12</span>
293
<a href="../index.html">Documentation</a> »
296
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
297
<li class="nav-item nav-item-2"><a href="markup.html" >19. Structured Markup Processing Tools</a> »</li>
301
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
303
The Python Software Foundation is a non-profit corporation.
304
<a href="https://www.python.org/psf/donations/">Please donate.</a>
306
Last updated on Oct 13, 2016.
307
<a href="../bugs.html">Found a bug</a>?
309
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'