added `ImmutableSandboxedEnvironment`.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent ab5ad8c · Browse files · 2 files·+63 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+63 added−0 removed
- other:+63 added−0 removed
Provenance
- Commit
522cad6- Tree
5458478- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ab5ad8c
2 files changed+63 added−0 removed
modified:docs/sandbox.rst+4 added
18 unmodified lines19 20 .. autoclass:: SandboxedEnvironment([options])21 :members: is_safe_attribute, is_safe_callable22 23+.. autoclass:: ImmutableSandboxedEnvironment([options])24+25 .. autoexception:: SecurityError26 27 .. autofunction:: unsafe28 29 .. autofunction:: is_internal_attribute30+31+.. autofunction:: modifies_builtin_mutablemodified:jinja2/sandbox.py+59 added
10 unmodified lines11 12 :copyright: Copyright 2008 by Armin Ronacher.13 :license: BSD.14 """15+import operator16+from UserDict import UserDict, DictMixin17+from UserList import UserList18+from sets import Set, ImmutableSet19 from types import FunctionType, MethodType, TracebackType, CodeType, \20 FrameType, GeneratorType21 from jinja2.runtime import Undefined22 from jinja2.environment import Environment9 unmodified lines32 33 #: unsafe method attributes. function attributes are unsafe for methods too34 UNSAFE_METHOD_ATTRIBUTES = set(['im_class', 'im_func', 'im_self'])35 36+SET_TYPES = (ImmutableSet, Set, set)37+MODIFYING_SET_ATTRIBUTES = set([38+ 'add', 'clear', 'difference_update', 'discard', 'pop', 'remove',39+ 'symmetric_difference_update', 'update'40+])41 42+DICT_TYPES = (UserDict, DictMixin, dict)43+MODIFYING_DICT_ATTRIBUTES = set(['clear', 'pop', 'popitem', 'setdefault',44+ 'update'])45+46+LIST_TYPES = (UserList, list)47+MODIFYING_LIST_ATTRIBUTES = set(['append', 'reverse', 'insert', 'sort',48+ 'extend', 'remove'])49+50+51 def safe_range(*args):52 """A range that can't generate ranges with a length of more than53 MAX_RANGE items.54 """43 unmodified lines98 return attr == 'gi_frame'99 return attr.startswith('__')100 101 102+def modifies_builtin_mutable(obj, attr):103+ """This function checks if an attribute on a builtin mutable object104+ (list, dict or set) would modify it if called. It also supports105+ the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.)106+107+ >>> modifies_builtin_mutable({}, "clear")108+ True109+ >>> modifies_builtin_mutable({}, "keys")110+ False111+ >>> modifies_builtin_mutable([], "append")112+ True113+ >>> modifies_builtin_mutable([], "index")114+ False115+116+ If called with an unsupported object (such as unicode) `False` is117+ returned.118+119+ >>> modifies_builtin_mutable("foo", "upper")120+ False121+ """122+ if isinstance(obj, LIST_TYPES):123+ return attr in MODIFYING_LIST_ATTRIBUTES124+ elif isinstance(obj, DICT_TYPES):125+ return attr in MODIFYING_DICT_ATTRIBUTES126+ elif isinstance(obj, SET_TYPES):127+ return attr in MODIFYING_SET_ATTRIBUTES128+ return False129+130+131 class SandboxedEnvironment(Environment):132 """The sandboxed environment. It works like the regular environment but133 tells the compiler to generate sandboxed code. Additionally subclasses of134 this environment may override the methods that tell the runtime what61 unmodified lines196 # errors when proxying the call.197 if not __self.is_safe_callable(__obj):198 raise SecurityError('%r is not safely callable' % (__obj,))199 return __obj(*args, **kwargs)200+201+202+class ImmutableSandboxedEnvironment(SandboxedEnvironment):203+ """Works exactly like the regular `SandboxedEnvironment` but does not204+ permit modifications on the builtin mutable objects `list`, `set`, and205+ `dict` by using the :func:`modifies_builtin_mutable` function.206+ """207+208+ def is_safe_attribute(self, obj, attr, value):209+ if not SandboxedEnvironment.is_safe_attribute(self, obj, attr, value):210+ return False211+ return not modifies_builtin_mutable(obj, attr)