Skip to main content

XML bomb protection with modified expat parser

Project description

defusedexpat protects the XML packages of Python’s standard library from several denial of service vulnerabilities and external entity exploits. It contains

  • a modified and enhanced version of expat parser library

  • replacements for pyexpat and cElementTree’s _elementtree extension modules

  • loader code that replaces built-in extensions with the modified extensions

  • monkey patches for xml.sax and xml.dom to prevent external entity expansions

In order to protect your application you have to import the defusedxml module before any of the stdlib’s XML modules.

Countermeasures

  • limited entity expansion level to antagonize billion laugh attacks

  • limited total length of expansions to prevent quadratic blowups

  • monkey patch to prevent retrieval of external entities and DTDs

Modifications

Modifications in pyexpat

Parser object

New parser attributes (r/w)

  • max_entity_indirections

  • max_entity_expansions

  • reset_dtd

Module constants

  • XML_DEFAULT_MAX_ENTITY_INDIRECTIONS

  • XML_DEFAULT_MAX_ENTITY_EXPANSIONS

  • XML_BOMB_PROTECTION

Modules functions

  • get_reset_dtd(), set_reset_dtd(bool)

  • get_max_entity_expansions(), set_max_entity_expansions(int)

  • get_max_entity_indirections(), et_max_entity_indirections(int)

New CAPI members

  • capi.GetFeature

  • capi.SetFeature

  • capi.GetFeatureDefault

  • capi.SetFeatureDefault

Modifications in _elementtree

_elementtree.XMLParser

New arguments and r/o attributes

  • max_entity_indirections

  • max_entity_expansions

  • ignore_dtd

Modifications in expat

new definitions:

XML_BOMB_PROTECTION
XML_DEFAULT_MAX_ENTITY_INDIRECTIONS
XML_DEFAULT_MAX_ENTITY_EXPANSIONS
XML_DEFAULT_RESET_DTD

new XML_FeatureEnum members:

XML_FEATURE_MAX_ENTITY_INDIRECTIONS
XML_FEATURE_MAX_ENTITY_EXPANSIONS
XML_FEATURE_IGNORE_DTD

new XML_Error members:

XML_ERROR_ENTITY_INDIRECTIONS
XML_ERROR_ENTITY_EXPANSION

new API functions:

int XML_GetFeature(XML_Parser parser,
                   enum XML_FeatureEnum feature,
                   long *value);
int XML_SetFeature(XML_Parser parser,
                   enum XML_FeatureEnum feature,
                   long value);
int XML_GetFeatureDefault(enum XML_FeatureEnum feature,
                          long *value);
int XML_SetFeatureDefault(enum XML_FeatureEnum feature,
                          long value);
XML_FEATURE_MAX_ENTITY_INDIRECTIONS

Limit the amount of indirections that are allowed to occur during the expansion of a nested entity. A counter starts when an entity reference is encountered. It resets after the entity is fully expanded. The limit protects the parser against exponential entity expansion attacks (aka billion laughs attack). When the limit is exceeded the parser stops and fails with XML_ERROR_ENTITY_INDIRECTIONS. A value of 0 disables the protection.

Supported range

0 .. UINT_MAX

Default

40

XML_FEATURE_MAX_ENTITY_EXPANSIONS

Limit the total length of all entity expansions throughout the entire document. The lengths of all entities are accumulated in a parser variable. The setting protects against quadratic blowup attacks (lots of expansions of a large entity declaration). When the sum of all entities exceeds the limit, the parser stops and fails with XML_ERROR_ENTITY_EXPANSION. A value of 0 disables the protection.

Supported range

0 .. UINT_MAX

Default

8 MiB

XML_FEATURE_RESET_DTD

Reset all DTD information after the <!DOCTYPE> block has been parsed. When the flag is set (default: false) all DTD information after the endDoctypeDeclHandler has been called. The flag can be set inside the endDoctypeDeclHandler. Without DTD information any entity reference in the document body leads to XML_ERROR_UNDEFINED_ENTITY.

Supported range

0, 1

Default

0

Requirements

  • Python 2.6.6 or newer (2.6.8 for randomized hashing)

  • Python 2.7.3 or newer

  • Python 3.1.5 or newer

  • Python 3.2.3 or newer

  • Python 3.3.0 or newer

TODO

  • Add functions to get and set default parser values

License

Copyright (c) 2013 by Christian Heimes <christian@python.org>

Licensed to PSF under a Contributor Agreement.

See http://www.python.org/psf/license for licensing details.

Contributors

Antoine Pitrou

code review

Brett Cannon

code review

Changelog

defusedexpat 0.3

Release date: 19-Feb-2013

  • Disable some tests on Windows because proxy trick doesn’t work

  • Remove ‘bomb protection’ suffix from CAPI MAGIC

  • Don’t support additional kwargs in _elementtree when XML_BOMB_PROTECTION is not available.

  • Implement better and more flexibel get/set feature API in expat

  • Add module functions to set global settings

defusedexpat 0.2

Release date: 15-Feb-2013

  • Python 3.1 support

  • Misc fixes and improvements

defusedexpat 0.1

Release date: 11-Feb-2013

  • Initial and internal release for PSRT review

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

defusedexpat-0.3.zip (275.6 kB view details)

Uploaded Source

defusedexpat-0.3.tar.gz (259.0 kB view details)

Uploaded Source

Built Distributions

defusedexpat-0.3.win-amd64-py3.3.exe (323.9 kB view details)

Uploaded Source

defusedexpat-0.3.win-amd64-py3.2.exe (323.5 kB view details)

Uploaded Source

defusedexpat-0.3.win-amd64-py2.7.exe (321.6 kB view details)

Uploaded Source

defusedexpat-0.3.win-amd64-py2.6.exe (320.2 kB view details)

Uploaded Source

defusedexpat-0.3.win32-py3.3.exe (283.7 kB view details)

Uploaded Source

defusedexpat-0.3.win32-py3.2.exe (283.4 kB view details)

Uploaded Source

defusedexpat-0.3.win32-py2.7.exe (281.9 kB view details)

Uploaded Source

defusedexpat-0.3.win32-py2.6.exe (281.1 kB view details)

Uploaded Source

File details

Details for the file defusedexpat-0.3.zip.

File metadata

  • Download URL: defusedexpat-0.3.zip
  • Upload date:
  • Size: 275.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for defusedexpat-0.3.zip
Algorithm Hash digest
SHA256 2f52b182e060e90bb16448e1a23000bc5ee462e0e699e0e72a63a2f835dab401
MD5 86514804f9245ed45887e9b4848c21e5
BLAKE2b-256 5715aab54487edf47a284ed421f0a4d86201f37f933d9ed0a008888f5ab4cd94

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.tar.gz.

File metadata

  • Download URL: defusedexpat-0.3.tar.gz
  • Upload date:
  • Size: 259.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for defusedexpat-0.3.tar.gz
Algorithm Hash digest
SHA256 0ecc7f343192fe461aa479f0ffbd5628aecf9e3bdc7df4335aa2b1f2f6ee8e07
MD5 6e4a68f88ac08540c04a6e9d35c8471e
BLAKE2b-256 b5a5b6849a0ddf44711693b78fe3e08b69ddf4489a33b3869c272fcf2ed1a085

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win-amd64-py3.3.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win-amd64-py3.3.exe
Algorithm Hash digest
SHA256 cd4fb34767458b2e8caa62bfe38e6908e232a8f5935db4da2dff8c50c11c8920
MD5 9ee26f969b2d488b1e7844fc9b306133
BLAKE2b-256 f3fe44da5c4e09b14660b8d619a3d93824e88eef2863f4a733b0c2994433453c

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win-amd64-py3.2.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win-amd64-py3.2.exe
Algorithm Hash digest
SHA256 779a2702044287a96876741908f2bf9116cafa839f7fa367dec62ca1747ee26e
MD5 f24a9478e20648840af82f5b30f970aa
BLAKE2b-256 59e4a23d64f99ec1c0e815eff8f6cb338861d5d06eac74de004b97c9828b3a05

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 b30e9bf62f0099becddd5a6d116bfdbd0cb9dc71ac9336080b151d65475c6332
MD5 c0c90d15cde606ef9a04fcbc7476d2e3
BLAKE2b-256 c3c3cfecf20a33f048d85f9b8016354987946d787963556faa075b93c6690b28

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win-amd64-py2.6.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win-amd64-py2.6.exe
Algorithm Hash digest
SHA256 35be0cdfa5b647182a30e4f41c692bfd705e8d2c422b5b07c82266604e8b4e41
MD5 9ebd32f234dc68d0180f2612d56fbfb8
BLAKE2b-256 11c3862f2f9e6d5e5794604d7a28ae1b3375bf2640cdf572f0f4241feb61859a

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win32-py3.3.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win32-py3.3.exe
Algorithm Hash digest
SHA256 0a06c36288814cec2a85a50b4b2a202a4e8ba6be5a2ac2b3830384236c209481
MD5 9d939ef0ab745bb504f5c6df29e1f5f5
BLAKE2b-256 8bfdc079a4939432b032530c48b666d46576fa65c21dc9e8848b701d043a60cd

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win32-py3.2.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win32-py3.2.exe
Algorithm Hash digest
SHA256 ef7c00163f6578463a63ce10a8b1c8ca71951813371ba3761218aaf37ae0c8bd
MD5 89a7e62aae70606aa00aeb3e11bc6886
BLAKE2b-256 93b588aa645ba12943f8cd24bebf4e678eb732997f3d4108c63e82e237bbbc58

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win32-py2.7.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win32-py2.7.exe
Algorithm Hash digest
SHA256 75e1be5a2b89532c4fe3c0443c803d54ef211398674e5c6256369a3c18765ffe
MD5 11a0be3bccf1227936732476c2f69209
BLAKE2b-256 308b5fcc07347ba63e469a97866efed963c265de6d9a4c79c8263638f645c35d

See more details on using hashes here.

File details

Details for the file defusedexpat-0.3.win32-py2.6.exe.

File metadata

File hashes

Hashes for defusedexpat-0.3.win32-py2.6.exe
Algorithm Hash digest
SHA256 0b082ac28cb46e9f20e339cc4a544937e47026fb9bc71c380831a341d843f6cb
MD5 57aba63ad95ec74b7465146388da3914
BLAKE2b-256 6c6da442044371511470d71500aa436cdab6683ee3a51dbd0a9933523c0a6ac3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page