| 1 |
.. _xml: |
| 2 |
|
| 3 |
XML Processing Modules |
| 4 |
====================== |
| 5 |
|
| 6 |
.. module:: xml |
| 7 |
:synopsis: Package containing XML processing modules |
| 8 |
|
| 9 |
.. sectionauthor:: Christian Heimes <[email protected]> |
| 10 |
.. sectionauthor:: Georg Brandl <[email protected]> |
| 11 |
|
| 12 |
**Source code:** :source:`Lib/xml/` |
| 13 |
|
| 14 |
-------------- |
| 15 |
|
| 16 |
Python's interfaces for processing XML are grouped in the ``xml`` package. |
| 17 |
|
| 18 |
.. note:: |
| 19 |
|
| 20 |
If you need to parse untrusted or unauthenticated data, see |
| 21 |
:ref:`xml-security`. |
| 22 |
|
| 23 |
It is important to note that modules in the :mod:`!xml` package require that |
| 24 |
there be at least one SAX-compliant XML parser available. The Expat parser is |
| 25 |
included with Python, so the :mod:`xml.parsers.expat` module will always be |
| 26 |
available. |
| 27 |
|
| 28 |
The documentation for the :mod:`xml.dom` and :mod:`xml.sax` packages are the |
| 29 |
definition of the Python bindings for the DOM and SAX interfaces. |
| 30 |
|
| 31 |
The XML handling submodules are: |
| 32 |
|
| 33 |
* :mod:`xml.etree.ElementTree`: the ElementTree API, a simple and lightweight |
| 34 |
XML processor |
| 35 |
|
| 36 |
.. |
| 37 |
|
| 38 |
* :mod:`xml.dom`: the DOM API definition |
| 39 |
* :mod:`xml.dom.minidom`: a minimal DOM implementation |
| 40 |
* :mod:`xml.dom.pulldom`: support for building partial DOM trees |
| 41 |
|
| 42 |
.. |
| 43 |
|
| 44 |
* :mod:`xml.sax`: SAX2 base classes and convenience functions |
| 45 |
* :mod:`xml.parsers.expat`: the Expat parser binding |
| 46 |
|
| 47 |
|
| 48 |
.. _xml-security: |
| 49 |
.. _xml-vulnerabilities: |
| 50 |
|
| 51 |
XML security |
| 52 |
------------ |
| 53 |
|
| 54 |
An attacker can abuse XML features to carry out denial of service attacks, |
| 55 |
access local files, generate network connections to other machines, or |
| 56 |
circumvent firewalls when attacker-controlled XML is being parsed, |
| 57 |
in Python or elsewhere. |
| 58 |
|
| 59 |
The built-in XML parsers of Python rely on the library `libexpat`_, commonly |
| 60 |
called Expat, for parsing XML. |
| 61 |
|
| 62 |
By default, Expat itself does not access local files or create network |
| 63 |
connections. |
| 64 |
|
| 65 |
Expat versions lower than 2.7.2 may be vulnerable to the "billion laughs", |
| 66 |
"quadratic blowup" and "large tokens" vulnerabilities, or to disproportional |
| 67 |
use of dynamic memory. |
| 68 |
Python bundles a copy of Expat, and whether Python uses the bundled or a |
| 69 |
system-wide Expat, depends on how the Python interpreter |
| 70 |
:option:`has been configured <--with-system-expat>` in your environment. |
| 71 |
Python may be vulnerable if it uses such older versions of Expat. |
| 72 |
Check :const:`!pyexpat.EXPAT_VERSION`. |
| 73 |
|
| 74 |
:mod:`xmlrpc` is **vulnerable** to the "decompression bomb" attack. |
| 75 |
|
| 76 |
|
| 77 |
billion 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, and the final entity definition contains a small string. |
| 81 |
The exponential expansion results in several gigabytes of text and |
| 82 |
consumes lots of memory and CPU time. |
| 83 |
|
| 84 |
quadratic 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 parser countermeasures |
| 89 |
that forbid deeply nested entities. |
| 90 |
|
| 91 |
decompression bomb |
| 92 |
Decompression bombs (aka `ZIP bomb`_) apply to all XML libraries |
| 93 |
that can parse compressed XML streams such as gzipped HTTP streams or |
| 94 |
LZMA-compressed |
| 95 |
files. For an attacker it can reduce the amount of transmitted data by three |
| 96 |
magnitudes or more. |
| 97 |
|
| 98 |
large tokens |
| 99 |
Expat needs to re-parse unfinished tokens; without the protection |
| 100 |
introduced in Expat 2.6.0, this can lead to quadratic runtime that can |
| 101 |
be used to cause denial of service in the application parsing XML. |
| 102 |
The issue is known as :cve:`2023-52425`. |
| 103 |
|
| 104 |
.. _libexpat: https://github.com/libexpat/libexpat |
| 105 |
.. _Billion Laughs: https://en.wikipedia.org/wiki/Billion_laughs |
| 106 |
.. _ZIP bomb: https://en.wikipedia.org/wiki/Zip_bomb |