__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # defusedxml
  2. #
  3. # Copyright (c) 2013 by Christian Heimes <christian@python.org>
  4. # Licensed to PSF under a Contributor Agreement.
  5. # See https://www.python.org/psf/license for licensing details.
  6. """Defuse XML bomb denial of service vulnerabilities
  7. """
  8. from __future__ import print_function, absolute_import
  9. import warnings
  10. from .common import (
  11. DefusedXmlException,
  12. DTDForbidden,
  13. EntitiesForbidden,
  14. ExternalReferenceForbidden,
  15. NotSupportedError,
  16. _apply_defusing,
  17. )
  18. def defuse_stdlib():
  19. """Monkey patch and defuse all stdlib packages
  20. :warning: The monkey patch is an EXPERIMETNAL feature.
  21. """
  22. defused = {}
  23. with warnings.catch_warnings():
  24. from . import cElementTree
  25. from . import ElementTree
  26. from . import minidom
  27. from . import pulldom
  28. from . import sax
  29. from . import expatbuilder
  30. from . import expatreader
  31. from . import xmlrpc
  32. xmlrpc.monkey_patch()
  33. defused[xmlrpc] = None
  34. defused_mods = [
  35. cElementTree,
  36. ElementTree,
  37. minidom,
  38. pulldom,
  39. sax,
  40. expatbuilder,
  41. expatreader,
  42. ]
  43. for defused_mod in defused_mods:
  44. stdlib_mod = _apply_defusing(defused_mod)
  45. defused[defused_mod] = stdlib_mod
  46. return defused
  47. __version__ = "0.7.1"
  48. __all__ = [
  49. "DefusedXmlException",
  50. "DTDForbidden",
  51. "EntitiesForbidden",
  52. "ExternalReferenceForbidden",
  53. "NotSupportedError",
  54. ]