You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.9 KiB

4 years ago
  1. # defusedxml
  2. #
  3. # Copyright (c) 2013 by Christian Heimes <christian@python.org>
  4. # Licensed to PSF under a Contributor Agreement.
  5. # See http://www.python.org/psf/license for licensing details.
  6. """Common constants, exceptions and helpe functions
  7. """
  8. import sys
  9. PY3 = sys.version_info[0] == 3
  10. class DefusedXmlException(ValueError):
  11. """Base exception
  12. """
  13. def __repr__(self):
  14. return str(self)
  15. class DTDForbidden(DefusedXmlException):
  16. """Document type definition is forbidden
  17. """
  18. def __init__(self, name, sysid, pubid):
  19. super(DTDForbidden, self).__init__()
  20. self.name = name
  21. self.sysid = sysid
  22. self.pubid = pubid
  23. def __str__(self):
  24. tpl = "DTDForbidden(name='{}', system_id={!r}, public_id={!r})"
  25. return tpl.format(self.name, self.sysid, self.pubid)
  26. class EntitiesForbidden(DefusedXmlException):
  27. """Entity definition is forbidden
  28. """
  29. def __init__(self, name, value, base, sysid, pubid, notation_name):
  30. super(EntitiesForbidden, self).__init__()
  31. self.name = name
  32. self.value = value
  33. self.base = base
  34. self.sysid = sysid
  35. self.pubid = pubid
  36. self.notation_name = notation_name
  37. def __str__(self):
  38. tpl = "EntitiesForbidden(name='{}', system_id={!r}, public_id={!r})"
  39. return tpl.format(self.name, self.sysid, self.pubid)
  40. class ExternalReferenceForbidden(DefusedXmlException):
  41. """Resolving an external reference is forbidden
  42. """
  43. def __init__(self, context, base, sysid, pubid):
  44. super(ExternalReferenceForbidden, self).__init__()
  45. self.context = context
  46. self.base = base
  47. self.sysid = sysid
  48. self.pubid = pubid
  49. def __str__(self):
  50. tpl = "ExternalReferenceForbidden(system_id='{}', public_id={})"
  51. return tpl.format(self.sysid, self.pubid)
  52. class NotSupportedError(DefusedXmlException):
  53. """The operation is not supported
  54. """
  55. def _apply_defusing(defused_mod):
  56. assert defused_mod is sys.modules[defused_mod.__name__]
  57. stdlib_name = defused_mod.__origin__
  58. __import__(stdlib_name, {}, {}, ["*"])
  59. stdlib_mod = sys.modules[stdlib_name]
  60. stdlib_names = set(dir(stdlib_mod))
  61. for name, obj in vars(defused_mod).items():
  62. if name.startswith("_") or name not in stdlib_names:
  63. continue
  64. setattr(stdlib_mod, name, obj)
  65. return stdlib_mod
  66. def _generate_etree_functions(DefusedXMLParser, _TreeBuilder,
  67. _parse, _iterparse):
  68. """Factory for functions needed by etree, dependent on whether
  69. cElementTree or ElementTree is used."""
  70. def parse(source, parser=None, forbid_dtd=False, forbid_entities=True,
  71. forbid_external=True):
  72. if parser is None:
  73. parser = DefusedXMLParser(target=_TreeBuilder(),
  74. forbid_dtd=forbid_dtd,
  75. forbid_entities=forbid_entities,
  76. forbid_external=forbid_external)
  77. return _parse(source, parser)
  78. def iterparse(source, events=None, parser=None, forbid_dtd=False,
  79. forbid_entities=True, forbid_external=True):
  80. if parser is None:
  81. parser = DefusedXMLParser(target=_TreeBuilder(),
  82. forbid_dtd=forbid_dtd,
  83. forbid_entities=forbid_entities,
  84. forbid_external=forbid_external)
  85. return _iterparse(source, events, parser)
  86. def fromstring(text, forbid_dtd=False, forbid_entities=True,
  87. forbid_external=True):
  88. parser = DefusedXMLParser(target=_TreeBuilder(),
  89. forbid_dtd=forbid_dtd,
  90. forbid_entities=forbid_entities,
  91. forbid_external=forbid_external)
  92. parser.feed(text)
  93. return parser.close()
  94. return parse, iterparse, fromstring