~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/xml.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>19.5. XML Processing Modules &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
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.11 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.11 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>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="xml.etree.elementtree.html" title="19.7. xml.etree.ElementTree — The ElementTree XML API"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
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> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">19. Structured Markup Processing Tools</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
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&#8217;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>
85
 
</div>
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
89
 
available.</p>
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>
93
 
<ul class="simple">
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
95
 
XML processor</li>
96
 
</ul>
97
 
<ul class="simple">
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>
101
 
</ul>
102
 
<ul class="simple">
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>
105
 
</ul>
106
 
</div>
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="http://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">
117
 
<colgroup>
118
 
<col width="37%" />
119
 
<col width="12%" />
120
 
<col width="13%" />
121
 
<col width="13%" />
122
 
<col width="12%" />
123
 
<col width="13%" />
124
 
</colgroup>
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>
132
 
</tr>
133
 
</thead>
134
 
<tbody valign="top">
135
 
<tr class="row-even"><td>billion laughs</td>
136
 
<td><strong>Yes</strong></td>
137
 
<td><strong>Yes</strong></td>
138
 
<td><strong>Yes</strong></td>
139
 
<td><strong>Yes</strong></td>
140
 
<td><strong>Yes</strong></td>
141
 
</tr>
142
 
<tr class="row-odd"><td>quadratic blowup</td>
143
 
<td><strong>Yes</strong></td>
144
 
<td><strong>Yes</strong></td>
145
 
<td><strong>Yes</strong></td>
146
 
<td><strong>Yes</strong></td>
147
 
<td><strong>Yes</strong></td>
148
 
</tr>
149
 
<tr class="row-even"><td>external entity expansion</td>
150
 
<td><strong>Yes</strong></td>
151
 
<td>No    (1)</td>
152
 
<td>No    (2)</td>
153
 
<td><strong>Yes</strong></td>
154
 
<td>No    (3)</td>
155
 
</tr>
156
 
<tr class="row-odd"><td>DTD retrieval</td>
157
 
<td><strong>Yes</strong></td>
158
 
<td>No</td>
159
 
<td>No</td>
160
 
<td><strong>Yes</strong></td>
161
 
<td>No</td>
162
 
</tr>
163
 
<tr class="row-even"><td>decompression bomb</td>
164
 
<td>No</td>
165
 
<td>No</td>
166
 
<td>No</td>
167
 
<td>No</td>
168
 
<td><strong>Yes</strong></td>
169
 
</tr>
170
 
</tbody>
171
 
</table>
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&#8217;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&#8217;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&#8217;t expand external entities and omits them.</li>
178
 
</ol>
179
 
<dl class="docutils">
180
 
<dt>billion laughs / exponential entity expansion</dt>
181
 
<dd>The <a class="reference external" href="http://en.wikipedia.org/wiki/Billion_laughs">Billion Laughs</a> attack &#8211; also known as exponential entity expansion &#8211;
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="http://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&#8217;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>DTD retrieval</dt>
199
 
<dd>Some XML libraries like Python&#8217;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="http://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>
207
 
</dl>
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>
225
 
</div>
226
 
</div>
227
 
 
228
 
 
229
 
          </div>
230
 
        </div>
231
 
      </div>
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>
235
 
  <ul>
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>
239
 
</ul>
240
 
</li>
241
 
</ul>
242
 
 
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> &#8212; A parser for HTML documents</a></p>
246
 
  <h4>Next topic</h4>
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> &#8212; The ElementTree XML API</a></p>
249
 
<h3>This Page</h3>
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>
254
 
</ul>
255
 
 
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" />
263
 
    </form>
264
 
    <p class="searchtip" style="font-size: 90%">
265
 
    Enter search terms or a module, class or function name.
266
 
    </p>
267
 
</div>
268
 
<script type="text/javascript">$('#searchbox').show(0);</script>
269
 
        </div>
270
 
      </div>
271
 
      <div class="clearer"></div>
272
 
    </div>  
273
 
    <div class="related" role="navigation" aria-label="related navigation">
274
 
      <h3>Navigation</h3>
275
 
      <ul>
276
 
        <li class="right" style="margin-right: 10px">
277
 
          <a href="../genindex.html" title="General Index"
278
 
             >index</a></li>
279
 
        <li class="right" >
280
 
          <a href="../py-modindex.html" title="Python Module Index"
281
 
             >modules</a> |</li>
282
 
        <li class="right" >
283
 
          <a href="xml.etree.elementtree.html" title="19.7. xml.etree.ElementTree — The ElementTree XML API"
284
 
             >next</a> |</li>
285
 
        <li class="right" >
286
 
          <a href="htmllib.html" title="19.3. htmllib — A parser for HTML documents"
287
 
             >previous</a> |</li>
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> &raquo;</li>
291
 
        <li>
292
 
          <span class="version_switcher_placeholder">2.7.11</span>
293
 
          <a href="../index.html">Documentation</a> &raquo;
294
 
        </li>
295
 
 
296
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
297
 
          <li class="nav-item nav-item-2"><a href="markup.html" >19. Structured Markup Processing Tools</a> &raquo;</li> 
298
 
      </ul>
299
 
    </div>  
300
 
    <div class="footer">
301
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
302
 
    <br />
303
 
    The Python Software Foundation is a non-profit corporation.
304
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
305
 
    <br />
306
 
    Last updated on Jan 23, 2016.
307
 
    <a href="../bugs.html">Found a bug</a>?
308
 
    <br />
309
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
310
 
    </div>
311
 
 
312
 
  </body>
313
 
</html>
 
 
b'\\ No newline at end of file'