[svn] improved jinja sandbox
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 ae16fd0 · Browse files · 4 files·+26 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+26 added−15 removed
- other:+26 added−15 removed
Provenance
- Commit
c2fde02- Tree
b239d17- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ae16fd0
4 files changed+26 added−15 removed
modified:jinja/translators/python.py+1 added-3 removed
238 unmodified lines239 self.last_debug_comment = None240 241 def translate(self):242 self.reset()243- rv = self.handle_node(self.node)244- print rv245- return rv243+ return self.handle_node(self.node)244 245 # -- jinja nodes246 247 def handle_template(self, node):639 unmodified linesmodified:jinja/environment.py+6 added-10 removed
11 unmodified lines12 from jinja.lexer import Lexer13 from jinja.parser import Parser14 from jinja.loaders import LoaderWrapper15 from jinja.datastructure import Undefined, Markup, Context, FakeTranslator16-from jinja.utils import escape, collect_translations17-from jinja.exceptions import FilterNotFound, TestNotFound16+from jinja.utils import escape, collect_translations, get_attribute17+from jinja.exceptions import FilterNotFound, TestNotFound, SecurityException18 from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE19 20 21 class Environment(object):141 unmodified lines163 """164 try:165 return obj[name]166 except (TypeError, KeyError, IndexError):167- if name[:2] == name[-2:] == '__':168- return Undefined169- if not hasattr(obj, name):170- return Undefined171- r = getattr(obj, 'jinja_allowed_attributes', None)172- if r is not None and name not in r:173- return Undefined174- return getattr(obj, name)167+ try:168+ return get_attribute(obj, name)169+ except (AttributeError, SecurityException):170+ pass171 return Undefined172 173 def call_function(self, f, context, args, kwargs, dyn_args, dyn_kwargs):174 """39 unmodified linesmodified:jinja/exceptions.py-2 removed
15 unmodified lines16 17 class SecurityException(TemplateError):18 """19 Raise if the template designer tried to do something dangerous.20-21- Not used any more. exists for backwards compatibility.20 """21 22 23 class FilterNotFound(KeyError, TemplateError):60 unmodified linesmodified:jinja/utils.py+19 added
17 unmodified lines18 from types import MethodType, FunctionType19 from compiler.ast import CallFunc, Name, Const20 from jinja.nodes import Trans21 from jinja.datastructure import Context, TemplateData22+from jinja.exceptions import SecurityException23 24 try:25 from collections import deque26 except ImportError:78 unmodified lines105 _from_string_env = Environment()106 return _from_string_env.from_string(source)107 108 109+def get_attribute(obj, name):110+ """111+ Return the attribute from name. Raise either `AttributeError`112+ or `SecurityException` if something goes wrong.113+ """114+ if not isinstance(name, basestring):115+ raise AttributeError(name)116+ if name[:2] == name[-2:] == '__' or name[:2] == '::':117+ raise SecurityException('not allowed to access internal attributes')118+ if (obj.__class__ is FunctionType and name.startswith('func_') or119+ obj.__class__ is MethodType and name.startswith('im_')):120+ raise SecurityException('not allowed to access function attributes')121+ r = getattr(obj, 'jinja_allowed_attributes', None)122+ if r is not None and name not in r:123+ raise SecurityException('not allowed attribute accessed')124+ return getattr(obj, name)125+126+127 def debug_context(env, context):128 """129 Use this function in templates to get a printed context.130 """290 unmodified lines