sandbox str.format_map
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by David Lord · parent 78d2f67 · Browse files · 2 files·+33 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
2 files+33 added−3 removed
- tests:+19 added−0 removed
- other:+14 added−3 removed
Provenance
- Commit
a2a6c93- Tree
5a19258- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
David Lord <davidism@gmail.com>- Parent
78d2f67
2 files changed+33 added−3 removed
modified:jinja2/sandbox.py+14 added-3 removed
135 unmodified lines136 137 def inspect_format_method(callable):138 if not isinstance(callable, (types.MethodType,139 types.BuiltinMethodType)) or \140- callable.__name__ != 'format':140+ callable.__name__ not in ('format', 'format_map'):141 return None142 obj = callable.__self__143 if isinstance(obj, string_types):144 return obj256 unmodified lines401 attribute,402 obj.__class__.__name__403 ), name=attribute, obj=obj, exc=SecurityError)404 405- def format_string(self, s, args, kwargs):405+ def format_string(self, s, args, kwargs, format_func=None):406 """If a format call is detected, then this is routed through this407 method so that our safety sandbox can be used for it.408 """409 if isinstance(s, Markup):410 formatter = SandboxedEscapeFormatter(self, s.escape)411 else:412 formatter = SandboxedFormatter(self)413+414+ if format_func is not None and format_func.__name__ == 'format_map':415+ if len(args) != 1 or kwargs:416+ raise TypeError(417+ 'format_map() takes exactly one argument %d given'418+ % (len(args) + (kwargs is not None))419+ )420+421+ kwargs = args[0]422+ args = None423+424 kwargs = _MagicFormatMapping(args, kwargs)425 rv = formatter.vformat(s, args, kwargs)426 return type(s)(rv)427 428 def call(__self, __context, __obj, *args, **kwargs):429 """Call an object from sandboxed code."""430 fmt = inspect_format_method(__obj)431 if fmt is not None:421- return __self.format_string(fmt, args, kwargs)432+ return __self.format_string(fmt, args, kwargs, __obj)433 434 # the double prefixes are to avoid double keyword argument435 # errors when proxying the call.436 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>'