sandbox.py 13 KB

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