Skip to content
Myr

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

2 files+37 added−43 removed

modified:jinja2/sandbox.py+36 added-41 removed
18 unmodified lines
19from jinja2.exceptions import SecurityError20from jinja2._compat import string_types, text_type, PY221from jinja2.utils import Markup2223has_format = False24if hasattr(text_type, 'format'):25 from markupsafe import EscapeFormatter26 from string import Formatter23from markupsafe import EscapeFormatter24from string import Formatter27 has_format = True252627#: maximum number of items a range may produce28MAX_RANGE = 100000
49 unmodified lines
78except ImportError:79 pass8081#: register Python 2.6 abstract base classes85try:86 from collections import MutableSet, MutableMapping, MutableSequence87 _mutable_set_types += (MutableSet,)88 _mutable_mapping_types += (MutableMapping,)89 _mutable_sequence_types += (MutableSequence,)82from collections import MutableSet, MutableMapping, MutableSequence83_mutable_set_types += (MutableSet,)84_mutable_mapping_types += (MutableMapping,)85_mutable_sequence_types += (MutableSequence,)90except ImportError:91 pass868788_mutable_spec = (89 (_mutable_set_types, frozenset([90 'add', 'clear', 'difference_update', 'discard', 'pop', 'remove',91 'symmetric_difference_update', 'update'
42 unmodified lines
134 return len(self._kwargs)135136137def 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 None
296 unmodified lines
438 return False439 return not modifies_known_mutable(obj, attr)440441449if 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.443try:444 from _string import formatter_field_name_split445except ImportError:446 def formatter_field_name_split(field_name):447 return field_name._formatter_field_name_split()448457 class SandboxedFormatterMixin(object):449459 def __init__(self, env):460 self._env = env450class SandboxedFormatterMixin(object):451462 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 = env454472 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, first464465class SandboxedFormatter(SandboxedFormatterMixin, Formatter):466467 def __init__(self, env):468 SandboxedFormatterMixin.__init__(self, env)469 Formatter.__init__(self)470471class SandboxedEscapeFormatter(SandboxedFormatterMixin, EscapeFormatter):477 class SandboxedEscapeFormatter(SandboxedFormatterMixin, EscapeFormatter):478 def __init__(self, env, escape):479 SandboxedFormatterMixin.__init__(self, env)480 EscapeFormatter.__init__(self, escape)472473 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 lines
11import pytest1213from jinja2 import Environment14from jinja2.sandbox import SandboxedEnvironment, \15 ImmutableSandboxedEnvironment, unsafe, has_format15 ImmutableSandboxedEnvironment, unsafe16from jinja2 import Markup, escape17from jinja2.exceptions import SecurityError, TemplateSyntaxError, \18 TemplateRuntimeError19from jinja2.nodes import EvalContext
145 unmodified lines
165 assert False, 'expected runtime error'166167168@pytest.mark.sandbox169@pytest.mark.skipif(not has_format, reason='No format support')169class TestStringFormat(object):170171 def test_basic_format_safety(self):172 env = SandboxedEnvironment()
17 unmodified lines