Skip to content
Myr

[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

4 files+26 added−15 removed

modified:jinja/translators/python.py+1 added-3 removed
238 unmodified lines
239 self.last_debug_comment = None240241 def translate(self):242 self.reset()243 rv = self.handle_node(self.node)244 print rv245 return rv243 return self.handle_node(self.node)244245 # -- jinja nodes246247 def handle_template(self, node):
639 unmodified lines
modified:jinja/environment.py+6 added-10 removed
11 unmodified lines
12from jinja.lexer import Lexer13from jinja.parser import Parser14from jinja.loaders import LoaderWrapper15from jinja.datastructure import Undefined, Markup, Context, FakeTranslator16from jinja.utils import escape, collect_translations17from jinja.exceptions import FilterNotFound, TestNotFound16from jinja.utils import escape, collect_translations, get_attribute17from jinja.exceptions import FilterNotFound, TestNotFound, SecurityException18from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE192021class Environment(object):
141 unmodified lines
163 """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 Undefined172173 def call_function(self, f, context, args, kwargs, dyn_args, dyn_kwargs):174 """
39 unmodified lines
modified:jinja/exceptions.py-2 removed
15 unmodified lines
1617class SecurityException(TemplateError):18 """19 Raise if the template designer tried to do something dangerous.2021 Not used any more. exists for backwards compatibility.20 """212223class FilterNotFound(KeyError, TemplateError):
60 unmodified lines
modified:jinja/utils.py+19 added
17 unmodified lines
18from types import MethodType, FunctionType19from compiler.ast import CallFunc, Name, Const20from jinja.nodes import Trans21from jinja.datastructure import Context, TemplateData22from jinja.exceptions import SecurityException2324try:25 from collections import deque26except ImportError:
78 unmodified lines
105 _from_string_env = Environment()106 return _from_string_env.from_string(source)107108109def 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)125126127def debug_context(env, context):128 """129 Use this function in templates to get a printed context.130 """
290 unmodified lines