XML Processing Modules

Source code: Lib/xml/


Python’s interfaces for processing XML are grouped in the xml package.Python用于处理XML的接口分组在xml包中。

Warning警告

The XML modules are not secure against erroneous or maliciously constructed data. XML模块对错误或恶意构造的数据不安全。If you need to parse untrusted or unauthenticated data see the XML vulnerabilities and The defusedxml Package sections.如果您需要分析不可信或未经身份验证的数据,请参阅XML漏洞defosedxml包部分。

It is important to note that modules in the xml package require that there be at least one SAX-compliant XML parser available. 需要注意的是,xml包中的模块要求至少有一个SAX兼容的xml解析器可用。The Expat parser is included with Python, so the xml.parsers.expat module will always be available.Expat解析器包含在Python中,因此xml.parsers.expat模块将始终可用。

The documentation for the xml.dom and xml.sax packages are the definition of the Python bindings for the DOM and SAX interfaces.xml.domxml.sax包的文档是DOM和SAX接口的Python绑定的定义。

The XML handling submodules are:XML处理子模块包括:

  • xml.etree.ElementTree: the ElementTree API, a simple and lightweight XML processor:ElementTree API,一个简单轻量级的XML处理器

  • xml.dom: the DOM API definition:DOM API定义

  • xml.dom.minidom: a minimal DOM implementation:最小DOM实现

  • xml.dom.pulldom: support for building partial DOM trees:支持构建部分DOM树

  • xml.sax: SAX2 base classes and convenience functions:SAX2基类和便利函数

  • xml.parsers.expat: the Expat parser binding:Expat分析器绑定

XML vulnerabilitiesXML漏洞

The XML processing modules are not secure against maliciously constructed data. XML处理模块对恶意构建的数据不安全。An attacker can abuse XML features to carry out denial of service attacks, access local files, generate network connections to other machines, or circumvent firewalls.攻击者可以滥用XML功能进行拒绝服务攻击、访问本地文件、生成与其他计算机的网络连接或绕过防火墙。

The following table gives an overview of the known attacks and whether the various modules are vulnerable to them.下表概述了已知的攻击以及各种模块是否容易受到攻击。

kind

sax

etree

minidom

pulldom

xmlrpc

billion laughs

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

quadratic blowup

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

Vulnerable (1)

external entity expansion

Safe (5)

Safe (2)

Safe (3)

Safe (5)

Safe (4)

DTD retrieval

Safe (5)

Safe

Safe

Safe (5)

Safe

decompression bomb

Safe

Safe

Safe

Safe

Vulnerable

  1. Expat 2.4.1 and newer is not vulnerable to the “billion laughs” and “quadratic blowup” vulnerabilities. Expat 2.4.1及更新版本不易受到“十亿笑点”和“二次爆破”漏洞的攻击。Items still listed as vulnerable due to potential reliance on system-provided libraries. 由于可能依赖系统提供的库,仍被列为易受攻击的项目。Check pyexpat.EXPAT_VERSION.检查pyexpat.EXPAT_VERSION

  2. xml.etree.ElementTree doesn’t expand external entities and raises a ParserError when an entity occurs.不展开外部实体,并在实体发生时引发ParserError。

  3. xml.dom.minidom doesn’t expand external entities and simply returns the unexpanded entity verbatim.不展开外部实体,只逐字返回未展开的实体。

  4. xmlrpclib doesn’t expand external entities and omits them.不会展开外部实体并忽略它们。

  5. Since Python 3.7.1, external general entities are no longer processed by default.从Python 3.7.1开始,默认情况下不再处理外部通用实体。

billion laughs / exponential entity expansion十亿笑点/指数级实体扩张

The Billion Laughs attack – also known as exponential entity expansion – uses multiple levels of nested entities. Billion Laughs攻击也称为指数实体扩展,使用多层嵌套实体。Each entity refers to another entity several times, and the final entity definition contains a small string. 每个实体多次引用另一个实体,最后的实体定义包含一个小字符串。The exponential expansion results in several gigabytes of text and consumes lots of memory and CPU time.指数级扩展会导致数GB的文本,并消耗大量内存和CPU时间。

quadratic blowup entity expansion二次爆破实体展开

A quadratic blowup attack is similar to a Billion Laughs attack; it abuses entity expansion, too. 二次爆破攻击类似于十亿笑攻击;它也滥用实体扩张。Instead of nested entities it repeats one large entity with a couple of thousand chars over and over again. 它不是嵌套的实体,而是一次又一次地重复一个包含数千个字符的大型实体。The attack isn’t as efficient as the exponential case but it avoids triggering parser countermeasures that forbid deeply-nested entities.该攻击没有指数情况那么有效,但它避免了触发解析器反措施,禁止深度嵌套实体。

external entity expansion外部实体扩展

Entity declarations can contain more than just text for replacement. 实体声明可以包含不仅仅是用于替换的文本。They can also point to external resources or local files. 它们还可以指向外部资源或本地文件。The XML parser accesses the resource and embeds the content into the XML document.XML解析器访问资源并将内容嵌入XML文档。

DTD retrieval检索

Some XML libraries like Python’s xml.dom.pulldom retrieve document type definitions from remote or local locations. 一些XML库(如Python的xml.dom.pulldom)从远程或本地位置检索文档类型定义。The feature has similar implications as the external entity expansion issue.该特性与外部实体扩展问题具有类似的含义。

decompression bomb

Decompression bombs (aka ZIP bomb) apply to all XML libraries that can parse compressed XML streams such as gzipped HTTP streams or LZMA-compressed files. 解压缩炸弹(又称ZIP bomb)适用于所有可以解析压缩XML流(如gzipped HTTP流或LZMA压缩文件)的XML库。For an attacker it can reduce the amount of transmitted data by three magnitudes or more.对于攻击者来说,它可以将传输的数据量减少三个数量级或更多。

The documentation for defusedxml on PyPI has further information about all known attack vectors with examples and references.PyPI上的defusedxml文档提供了关于所有已知攻击向量的更多信息,包括示例和参考。

The defusedxml Packagedefosedxml

defusedxml is a pure Python package with modified subclasses of all stdlib XML parsers that prevent any potentially malicious operation. 是一个纯Python包,包含所有stdlib XML解析器的修改子类,可防止任何潜在的恶意操作。Use of this package is recommended for any server code that parses untrusted XML data. 建议对解析不受信任的XML数据的任何服务器代码使用此包。The package also ships with example exploits and extended documentation on more XML exploits such as XPath injection.该软件包还附带了更多XML漏洞的示例和扩展文档,如XPath注入。