Merge branch '2.10.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents 7e417c5, c4c4088 · Browse files · 3 files·+43 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+43 added−3 removed
- tests:+19 added−0 removed
- other:+24 added−3 removed
Provenance
- Commit
fd89fed- Tree
39f6c64- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
7e417c5- Parent 2
c4c4088
3 files changed+43 added−3 removed
modified:jinja2/sandbox.py+14 added-3 removed
133 unmodified lines134 135 def inspect_format_method(callable):136 if not isinstance(callable, (types.MethodType,137 types.BuiltinMethodType)) or \138- callable.__name__ != 'format':138+ callable.__name__ not in ('format', 'format_map'):139 return None140 obj = callable.__self__141 if isinstance(obj, string_types):142 return obj256 unmodified lines399 attribute,400 obj.__class__.__name__401 ), name=attribute, obj=obj, exc=SecurityError)402 403- def format_string(self, s, args, kwargs):403+ def format_string(self, s, args, kwargs, format_func=None):404 """If a format call is detected, then this is routed through this405 method so that our safety sandbox can be used for it.406 """407 if isinstance(s, Markup):408 formatter = SandboxedEscapeFormatter(self, s.escape)409 else:410 formatter = SandboxedFormatter(self)411+412+ if format_func is not None and format_func.__name__ == 'format_map':413+ if len(args) != 1 or kwargs:414+ raise TypeError(415+ 'format_map() takes exactly one argument %d given'416+ % (len(args) + (kwargs is not None))417+ )418+419+ kwargs = args[0]420+ args = None421+422 kwargs = _MagicFormatMapping(args, kwargs)423 rv = formatter.vformat(s, args, kwargs)424 return type(s)(rv)425 426 def call(__self, __context, __obj, *args, **kwargs):427 """Call an object from sandboxed code."""428 fmt = inspect_format_method(__obj)429 if fmt is not None:419- return __self.format_string(fmt, args, kwargs)430+ return __self.format_string(fmt, args, kwargs, __obj)431 432 # the double prefixes are to avoid double keyword argument433 # errors when proxying the call.434 if not __self.is_safe_callable(__obj):50 unmodified linesmodified:tests/test_security.py+19 added
185 unmodified lines186 def test_safe_format_all_okay(self):187 env = SandboxedEnvironment()188 t = env.from_string('{{ ("a{0.foo}b{1}"|safe).format({"foo": 42}, "<foo>") }}')189 assert t.render() == 'a42b<foo>'190+191+192+@pytest.mark.sandbox193+@pytest.mark.skipif(not hasattr(str, 'format_map'), reason='requires str.format_map method')194+class TestStringFormatMap(object):195+ def test_basic_format_safety(self):196+ env = SandboxedEnvironment()197+ t = env.from_string('{{ "a{x.__class__}b".format_map({"x":42}) }}')198+ assert t.render() == 'ab'199+200+ def test_basic_format_all_okay(self):201+ env = SandboxedEnvironment()202+ t = env.from_string('{{ "a{x.foo}b".format_map({"x":{"foo": 42}}) }}')203+ assert t.render() == 'a42b'204+205+ def test_safe_format_all_okay(self):206+ env = SandboxedEnvironment()207+ t = env.from_string('{{ ("a{x.foo}b{y}"|safe).format_map({"x":{"foo": 42}, "y":"<foo>"}) }}')208+ assert t.render() == 'a42b<foo>'modified:CHANGES.rst+10 added
16 unmodified lines17 18 .. _#765: https://github.com/pallets/jinja/issues/76519 20 21+Version 2.10.122+--------------23+24+Released 2019-04-0625+26+- ``SandboxedEnvironment`` securely handles ``str.format_map`` in27+ order to prevent code execution through untrusted format strings.28+ The sandbox already handled ``str.format``.29+30+31 Version 2.1032 ------------33 34 released on November 8th 2017588 unmodified lines