Expect format
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 73e2b51 · Browse files · 2 files·+37 added−43 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+37 added−43 removed
- tests:+1 added−2 removed
- other:+36 added−41 removed
Provenance
- Commit
ffe0caa- Tree
fb65638- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
73e2b51
2 files changed+37 added−43 removed
modified:jinja2/sandbox.py+36 added-41 removed
18 unmodified lines19 from jinja2.exceptions import SecurityError20 from jinja2._compat import string_types, text_type, PY221 from jinja2.utils import Markup22 23-has_format = False24-if hasattr(text_type, 'format'):25- from markupsafe import EscapeFormatter26- from string import Formatter23+from markupsafe import EscapeFormatter24+from string import Formatter27- has_format = True25 26 27 #: maximum number of items a range may produce28 MAX_RANGE = 10000049 unmodified lines78 except ImportError:79 pass80 81 #: register Python 2.6 abstract base classes85-try:86- from collections import MutableSet, MutableMapping, MutableSequence87- _mutable_set_types += (MutableSet,)88- _mutable_mapping_types += (MutableMapping,)89- _mutable_sequence_types += (MutableSequence,)82+from collections import MutableSet, MutableMapping, MutableSequence83+_mutable_set_types += (MutableSet,)84+_mutable_mapping_types += (MutableMapping,)85+_mutable_sequence_types += (MutableSequence,)90-except ImportError:91- pass86 87+88 _mutable_spec = (89 (_mutable_set_types, frozenset([90 'add', 'clear', 'difference_update', 'discard', 'pop', 'remove',91 'symmetric_difference_update', 'update'42 unmodified lines134 return len(self._kwargs)135 136 137 def inspect_format_method(callable):143- if not has_format:144- return None138 if not isinstance(callable, (types.MethodType,139 types.BuiltinMethodType)) or \140 callable.__name__ != 'format':141 return None296 unmodified lines438 return False439 return not modifies_known_mutable(obj, attr)440 441 449-if has_format:450- # This really is not a public API apparenlty.451- try:452- from _string import formatter_field_name_split453- except ImportError:454- def formatter_field_name_split(field_name):455- return field_name._formatter_field_name_split()442+# This really is not a public API apparenlty.443+try:444+ from _string import formatter_field_name_split445+except ImportError:446+ def formatter_field_name_split(field_name):447+ return field_name._formatter_field_name_split()448 457- class SandboxedFormatterMixin(object):449 459- def __init__(self, env):460- self._env = env450+class SandboxedFormatterMixin(object):451 462- def get_field(self, field_name, args, kwargs):463- first, rest = formatter_field_name_split(field_name)464- obj = self.get_value(first, args, kwargs)465- for is_attr, i in rest:466- if is_attr:467- obj = self._env.getattr(obj, i)468- else:469- obj = self._env.getitem(obj, i)470- return obj, first452+ def __init__(self, env):453+ self._env = env454 472- class SandboxedFormatter(SandboxedFormatterMixin, Formatter):473- def __init__(self, env):474- SandboxedFormatterMixin.__init__(self, env)475- Formatter.__init__(self)455+ def get_field(self, field_name, args, kwargs):456+ first, rest = formatter_field_name_split(field_name)457+ obj = self.get_value(first, args, kwargs)458+ for is_attr, i in rest:459+ if is_attr:460+ obj = self._env.getattr(obj, i)461+ else:462+ obj = self._env.getitem(obj, i)463+ return obj, first464 465+class SandboxedFormatter(SandboxedFormatterMixin, Formatter):466+467+ def __init__(self, env):468+ SandboxedFormatterMixin.__init__(self, env)469+ Formatter.__init__(self)470+471+class SandboxedEscapeFormatter(SandboxedFormatterMixin, EscapeFormatter):477- class SandboxedEscapeFormatter(SandboxedFormatterMixin, EscapeFormatter):478- def __init__(self, env, escape):479- SandboxedFormatterMixin.__init__(self, env)480- EscapeFormatter.__init__(self, escape)472+473+ def __init__(self, env, escape):474+ SandboxedFormatterMixin.__init__(self, env)475+ EscapeFormatter.__init__(self, escape)modified:tests/test_security.py+1 added-2 removed
10 unmodified lines11 import pytest12 13 from jinja2 import Environment14 from jinja2.sandbox import SandboxedEnvironment, \15- ImmutableSandboxedEnvironment, unsafe, has_format15+ ImmutableSandboxedEnvironment, unsafe16 from jinja2 import Markup, escape17 from jinja2.exceptions import SecurityError, TemplateSyntaxError, \18 TemplateRuntimeError19 from jinja2.nodes import EvalContext145 unmodified lines165 assert False, 'expected runtime error'166 167 168 @pytest.mark.sandbox169-@pytest.mark.skipif(not has_format, reason='No format support')169 class TestStringFormat(object):170 171 def test_basic_format_safety(self):172 env = SandboxedEnvironment()17 unmodified lines