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