Skip to content
Myr

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

3 files+43 added−3 removed

modified:jinja2/sandbox.py+14 added-3 removed
133 unmodified lines
134135def 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 obj
256 unmodified lines
399 attribute,400 obj.__class__.__name__401 ), name=attribute, obj=obj, exc=SecurityError)402403 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)411412 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 )418419 kwargs = args[0]420 args = None421422 kwargs = _MagicFormatMapping(args, kwargs)423 rv = formatter.vformat(s, args, kwargs)424 return type(s)(rv)425426 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)431432 # the double prefixes are to avoid double keyword argument433 # errors when proxying the call.434 if not __self.is_safe_callable(__obj):
50 unmodified lines
modified:tests/test_security.py+19 added
185 unmodified lines
186 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&lt;foo&gt;'190191192@pytest.mark.sandbox193@pytest.mark.skipif(not hasattr(str, 'format_map'), reason='requires str.format_map method')194class 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'199200 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'204205 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&lt;foo&gt;'
modified:CHANGES.rst+10 added
16 unmodified lines
1718.. _#765: https://github.com/pallets/jinja/issues/765192021Version 2.10.122--------------2324Released 2019-04-062526- ``SandboxedEnvironment`` securely handles ``str.format_map`` in27 order to prevent code execution through untrusted format strings.28 The sandbox already handled ``str.format``.293031Version 2.1032------------3334released on November 8th 2017
588 unmodified lines