Skip to content
Myr

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

2 files+63 added−0 removed

modified:docs/sandbox.rst+4 added
18 unmodified lines
1920.. autoclass:: SandboxedEnvironment([options])21 :members: is_safe_attribute, is_safe_callable2223.. autoclass:: ImmutableSandboxedEnvironment([options])2425.. autoexception:: SecurityError2627.. autofunction:: unsafe2829.. autofunction:: is_internal_attribute3031.. autofunction:: modifies_builtin_mutable
modified:jinja2/sandbox.py+59 added
10 unmodified lines
1112 :copyright: Copyright 2008 by Armin Ronacher.13 :license: BSD.14"""15import operator16from UserDict import UserDict, DictMixin17from UserList import UserList18from sets import Set, ImmutableSet19from types import FunctionType, MethodType, TracebackType, CodeType, \20 FrameType, GeneratorType21from jinja2.runtime import Undefined22from jinja2.environment import Environment
9 unmodified lines
3233#: unsafe method attributes. function attributes are unsafe for methods too34UNSAFE_METHOD_ATTRIBUTES = set(['im_class', 'im_func', 'im_self'])3536SET_TYPES = (ImmutableSet, Set, set)37MODIFYING_SET_ATTRIBUTES = set([38 'add', 'clear', 'difference_update', 'discard', 'pop', 'remove',39 'symmetric_difference_update', 'update'40])4142DICT_TYPES = (UserDict, DictMixin, dict)43MODIFYING_DICT_ATTRIBUTES = set(['clear', 'pop', 'popitem', 'setdefault',44 'update'])4546LIST_TYPES = (UserList, list)47MODIFYING_LIST_ATTRIBUTES = set(['append', 'reverse', 'insert', 'sort',48 'extend', 'remove'])495051def safe_range(*args):52 """A range that can't generate ranges with a length of more than53 MAX_RANGE items.54 """
43 unmodified lines
98 return attr == 'gi_frame'99 return attr.startswith('__')100101102def 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.)106107 >>> modifies_builtin_mutable({}, "clear")108 True109 >>> modifies_builtin_mutable({}, "keys")110 False111 >>> modifies_builtin_mutable([], "append")112 True113 >>> modifies_builtin_mutable([], "index")114 False115116 If called with an unsupported object (such as unicode) `False` is117 returned.118119 >>> 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 False129130131class 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 what
61 unmodified lines
196 # 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)200201202class 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 """207208 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)