sandbox.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.sandbox
  4. ~~~~~~~~~~~~~~
  5. Adds a sandbox layer to Jinja as it was the default behavior in the old
  6. Jinja 1 releases. This sandbox is slightly different from Jinja 1 as the
  7. default behavior is easier to use.
  8. The behavior can be changed by subclassing the environment.
  9. :copyright: (c) 2010 by the Jinja Team.
  10. :license: BSD.
  11. """
  12. import types
  13. import operator
  14. from jinja2.environment import Environment
  15. from jinja2.exceptions import SecurityError
  16. from jinja2._compat import string_types, PY2
  17. #: maximum number of items a range may produce
  18. MAX_RANGE = 100000
  19. #: attributes of function objects that are considered unsafe.
  20. if PY2:
  21. UNSAFE_FUNCTION_ATTRIBUTES = set(['func_closure', 'func_code', 'func_dict',
  22. 'func_defaults', 'func_globals'])
  23. else:
  24. # On versions > python 2 the special attributes on functions are gone,
  25. # but they remain on methods and generators for whatever reason.
  26. UNSAFE_FUNCTION_ATTRIBUTES = set()
  27. #: unsafe method attributes. function attributes are unsafe for methods too
  28. UNSAFE_METHOD_ATTRIBUTES = set(['im_class', 'im_func', 'im_self'])
  29. #: unsafe generator attirbutes.
  30. UNSAFE_GENERATOR_ATTRIBUTES = set(['gi_frame', 'gi_code'])
  31. import warnings
  32. # make sure we don't warn in python 2.6 about stuff we don't care about
  33. warnings.filterwarnings('ignore', 'the sets module', DeprecationWarning,
  34. module='jinja2.sandbox')
  35. from collections import deque
  36. _mutable_set_types = (set,)
  37. _mutable_mapping_types = (dict,)
  38. _mutable_sequence_types = (list,)
  39. # on python 2.x we can register the user collection types
  40. try:
  41. from UserDict import UserDict, DictMixin
  42. from UserList import UserList
  43. _mutable_mapping_types += (UserDict, DictMixin)
  44. _mutable_set_types += (UserList,)
  45. except ImportError:
  46. pass
  47. # if sets is still available, register the mutable set from there as well
  48. try:
  49. from sets import Set
  50. _mutable_set_types += (Set,)
  51. except ImportError:
  52. pass
  53. #: register Python 2.6 abstract base classes
  54. try:
  55. from collections import MutableSet, MutableMapping, MutableSequence
  56. _mutable_set_types += (MutableSet,)
  57. _mutable_mapping_types += (MutableMapping,)
  58. _mutable_sequence_types += (MutableSequence,)
  59. except ImportError:
  60. pass
  61. _mutable_spec = (
  62. (_mutable_set_types, frozenset([
  63. 'add', 'clear', 'difference_update', 'discard', 'pop', 'remove',
  64. 'symmetric_difference_update', 'update'
  65. ])),
  66. (_mutable_mapping_types, frozenset([
  67. 'clear', 'pop', 'popitem', 'setdefault', 'update'
  68. ])),
  69. (_mutable_sequence_types, frozenset([
  70. 'append', 'reverse', 'insert', 'sort', 'extend', 'remove'
  71. ])),
  72. (deque, frozenset([
  73. 'append', 'appendleft', 'clear', 'extend', 'extendleft', 'pop',
  74. 'popleft', 'remove', 'rotate'
  75. ]))
  76. )
  77. def safe_range(*args):
  78. """A range that can't generate ranges with a length of more than
  79. MAX_RANGE items.
  80. """
  81. rng = range(*args)
  82. if len(rng) > MAX_RANGE:
  83. raise OverflowError('range too big, maximum size for range is %d' %
  84. MAX_RANGE)
  85. return rng
  86. def unsafe(f):
  87. """Marks a function or method as unsafe.
  88. ::
  89. @unsafe
  90. def delete(self):
  91. pass
  92. """
  93. f.unsafe_callable = True
  94. return f
  95. def is_internal_attribute(obj, attr):
  96. """Test if the attribute given is an internal python attribute. For
  97. example this function returns `True` for the `func_code` attribute of
  98. python objects. This is useful if the environment method
  99. :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.
  100. >>> from jinja2.sandbox import is_internal_attribute
  101. >>> is_internal_attribute(str, "mro")
  102. True
  103. >>> is_internal_attribute(str, "upper")
  104. False
  105. """
  106. if isinstance(obj, types.FunctionType):
  107. if attr in UNSAFE_FUNCTION_ATTRIBUTES:
  108. return True
  109. elif isinstance(obj, types.MethodType):
  110. if attr in UNSAFE_FUNCTION_ATTRIBUTES or \
  111. attr in UNSAFE_METHOD_ATTRIBUTES:
  112. return True
  113. elif isinstance(obj, type):
  114. if attr == 'mro':
  115. return True
  116. elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)):
  117. return True
  118. elif isinstance(obj, types.GeneratorType):
  119. if attr in UNSAFE_GENERATOR_ATTRIBUTES:
  120. return True
  121. return attr.startswith('__')
  122. def modifies_known_mutable(obj, attr):
  123. """This function checks if an attribute on a builtin mutable object
  124. (list, dict, set or deque) would modify it if called. It also supports
  125. the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
  126. with Python 2.6 onwards the abstract base classes `MutableSet`,
  127. `MutableMapping`, and `MutableSequence`.
  128. >>> modifies_known_mutable({}, "clear")
  129. True
  130. >>> modifies_known_mutable({}, "keys")
  131. False
  132. >>> modifies_known_mutable([], "append")
  133. True
  134. >>> modifies_known_mutable([], "index")
  135. False
  136. If called with an unsupported object (such as unicode) `False` is
  137. returned.
  138. >>> modifies_known_mutable("foo", "upper")
  139. False
  140. """
  141. for typespec, unsafe in _mutable_spec:
  142. if isinstance(obj, typespec):
  143. return attr in unsafe
  144. return False
  145. class SandboxedEnvironment(Environment):
  146. """The sandboxed environment. It works like the regular environment but
  147. tells the compiler to generate sandboxed code. Additionally subclasses of
  148. this environment may override the methods that tell the runtime what
  149. attributes or functions are safe to access.
  150. If the template tries to access insecure code a :exc:`SecurityError` is
  151. raised. However also other exceptions may occour during the rendering so
  152. the caller has to ensure that all exceptions are catched.
  153. """
  154. sandboxed = True
  155. #: default callback table for the binary operators. A copy of this is
  156. #: available on each instance of a sandboxed environment as
  157. #: :attr:`binop_table`
  158. default_binop_table = {
  159. '+': operator.add,
  160. '-': operator.sub,
  161. '*': operator.mul,
  162. '/': operator.truediv,
  163. '//': operator.floordiv,
  164. '**': operator.pow,
  165. '%': operator.mod
  166. }
  167. #: default callback table for the unary operators. A copy of this is
  168. #: available on each instance of a sandboxed environment as
  169. #: :attr:`unop_table`
  170. default_unop_table = {
  171. '+': operator.pos,
  172. '-': operator.neg
  173. }
  174. #: a set of binary operators that should be intercepted. Each operator
  175. #: that is added to this set (empty by default) is delegated to the
  176. #: :meth:`call_binop` method that will perform the operator. The default
  177. #: operator callback is specified by :attr:`binop_table`.
  178. #:
  179. #: The following binary operators are interceptable:
  180. #: ``//``, ``%``, ``+``, ``*``, ``-``, ``/``, and ``**``
  181. #:
  182. #: The default operation form the operator table corresponds to the
  183. #: builtin function. Intercepted calls are always slower than the native
  184. #: operator call, so make sure only to intercept the ones you are
  185. #: interested in.
  186. #:
  187. #: .. versionadded:: 2.6
  188. intercepted_binops = frozenset()
  189. #: a set of unary operators that should be intercepted. Each operator
  190. #: that is added to this set (empty by default) is delegated to the
  191. #: :meth:`call_unop` method that will perform the operator. The default
  192. #: operator callback is specified by :attr:`unop_table`.
  193. #:
  194. #: The following unary operators are interceptable: ``+``, ``-``
  195. #:
  196. #: The default operation form the operator table corresponds to the
  197. #: builtin function. Intercepted calls are always slower than the native
  198. #: operator call, so make sure only to intercept the ones you are
  199. #: interested in.
  200. #:
  201. #: .. versionadded:: 2.6
  202. intercepted_unops = frozenset()
  203. def intercept_unop(self, operator):
  204. """Called during template compilation with the name of a unary
  205. operator to check if it should be intercepted at runtime. If this
  206. method returns `True`, :meth:`call_unop` is excuted for this unary
  207. operator. The default implementation of :meth:`call_unop` will use
  208. the :attr:`unop_table` dictionary to perform the operator with the
  209. same logic as the builtin one.
  210. The following unary operators are interceptable: ``+`` and ``-``
  211. Intercepted calls are always slower than the native operator call,
  212. so make sure only to intercept the ones you are interested in.
  213. .. versionadded:: 2.6
  214. """
  215. return False
  216. def __init__(self, *args, **kwargs):
  217. Environment.__init__(self, *args, **kwargs)
  218. self.globals['range'] = safe_range
  219. self.binop_table = self.default_binop_table.copy()
  220. self.unop_table = self.default_unop_table.copy()
  221. def is_safe_attribute(self, obj, attr, value):
  222. """The sandboxed environment will call this method to check if the
  223. attribute of an object is safe to access. Per default all attributes
  224. starting with an underscore are considered private as well as the
  225. special attributes of internal python objects as returned by the
  226. :func:`is_internal_attribute` function.
  227. """
  228. return not (attr.startswith('_') or is_internal_attribute(obj, attr))
  229. def is_safe_callable(self, obj):
  230. """Check if an object is safely callable. Per default a function is
  231. considered safe unless the `unsafe_callable` attribute exists and is
  232. True. Override this method to alter the behavior, but this won't
  233. affect the `unsafe` decorator from this module.
  234. """
  235. return not (getattr(obj, 'unsafe_callable', False) or
  236. getattr(obj, 'alters_data', False))
  237. def call_binop(self, context, operator, left, right):
  238. """For intercepted binary operator calls (:meth:`intercepted_binops`)
  239. this function is executed instead of the builtin operator. This can
  240. be used to fine tune the behavior of certain operators.
  241. .. versionadded:: 2.6
  242. """
  243. return self.binop_table[operator](left, right)
  244. def call_unop(self, context, operator, arg):
  245. """For intercepted unary operator calls (:meth:`intercepted_unops`)
  246. this function is executed instead of the builtin operator. This can
  247. be used to fine tune the behavior of certain operators.
  248. .. versionadded:: 2.6
  249. """
  250. return self.unop_table[operator](arg)
  251. def getitem(self, obj, argument):
  252. """Subscribe an object from sandboxed code."""
  253. try:
  254. return obj[argument]
  255. except (TypeError, LookupError):
  256. if isinstance(argument, string_types):
  257. try:
  258. attr = str(argument)
  259. except Exception:
  260. pass
  261. else:
  262. try:
  263. value = getattr(obj, attr)
  264. except AttributeError:
  265. pass
  266. else:
  267. if self.is_safe_attribute(obj, argument, value):
  268. return value
  269. return self.unsafe_undefined(obj, argument)
  270. return self.undefined(obj=obj, name=argument)
  271. def getattr(self, obj, attribute):
  272. """Subscribe an object from sandboxed code and prefer the
  273. attribute. The attribute passed *must* be a bytestring.
  274. """
  275. try:
  276. value = getattr(obj, attribute)
  277. except AttributeError:
  278. try:
  279. return obj[attribute]
  280. except (TypeError, LookupError):
  281. pass
  282. else:
  283. if self.is_safe_attribute(obj, attribute, value):
  284. return value
  285. return self.unsafe_undefined(obj, attribute)
  286. return self.undefined(obj=obj, name=attribute)
  287. def unsafe_undefined(self, obj, attribute):
  288. """Return an undefined object for unsafe attributes."""
  289. return self.undefined('access to attribute %r of %r '
  290. 'object is unsafe.' % (
  291. attribute,
  292. obj.__class__.__name__
  293. ), name=attribute, obj=obj, exc=SecurityError)
  294. def call(__self, __context, __obj, *args, **kwargs):
  295. """Call an object from sandboxed code."""
  296. # the double prefixes are to avoid double keyword argument
  297. # errors when proxying the call.
  298. if not __self.is_safe_callable(__obj):
  299. raise SecurityError('%r is not safely callable' % (__obj,))
  300. return __context.call(__obj, *args, **kwargs)
  301. class ImmutableSandboxedEnvironment(SandboxedEnvironment):
  302. """Works exactly like the regular `SandboxedEnvironment` but does not
  303. permit modifications on the builtin mutable objects `list`, `set`, and
  304. `dict` by using the :func:`modifies_known_mutable` function.
  305. """
  306. def is_safe_attribute(self, obj, attr, value):
  307. if not SandboxedEnvironment.is_safe_attribute(self, obj, attr, value):
  308. return False
  309. return not modifies_known_mutable(obj, attr)