Hello {{ name }}!
{% endmacro %}' '{{ say_hello("") }}') escaped_out = 'Hello <blink>foo</blink>!
' assert t.render() == escaped_out assert text_type(t.module) == escaped_out assert escape(t.module) == escaped_out assert t.module.say_hello('') == escaped_out assert escape(t.module.say_hello( EvalContext(env), '')) == escaped_out assert escape(t.module.say_hello( '')) == escaped_out def test_attr_filter(self, env): env = SandboxedEnvironment() tmpl = env.from_string('{{ cls|attr("__subclasses__")() }}') pytest.raises(SecurityError, tmpl.render, cls=int) def test_binary_operator_intercepting(self, env): def disable_op(left, right): raise TemplateRuntimeError('that operator so does not work') for expr, ctx, rv in ('1 + 2', {}, '3'), ('a + 2', {'a': 2}, '4'): env = SandboxedEnvironment() env.binop_table['+'] = disable_op t = env.from_string('{{ %s }}' % expr) assert t.render(ctx) == rv env.intercepted_binops = frozenset(['+']) t = env.from_string('{{ %s }}' % expr) try: t.render(ctx) except TemplateRuntimeError as e: pass else: assert False, 'expected runtime error' def test_unary_operator_intercepting(self, env): def disable_op(arg): raise TemplateRuntimeError('that operator so does not work') for expr, ctx, rv in ('-1', {}, '-1'), ('-a', {'a': 2}, '-2'): env = SandboxedEnvironment() env.unop_table['-'] = disable_op t = env.from_string('{{ %s }}' % expr) assert t.render(ctx) == rv env.intercepted_unops = frozenset(['-']) t = env.from_string('{{ %s }}' % expr) try: t.render(ctx) except TemplateRuntimeError as e: pass else: assert False, 'expected runtime error' @pytest.mark.sandbox class TestStringFormat(object): def test_basic_format_safety(self): env = SandboxedEnvironment() t = env.from_string('{{ "a{0.__class__}b".format(42) }}') assert t.render() == 'ab' def test_basic_format_all_okay(self): env = SandboxedEnvironment() t = env.from_string('{{ "a{0.foo}b".format({"foo": 42}) }}') assert t.render() == 'a42b' def test_safe_format_safety(self): env = SandboxedEnvironment() t = env.from_string('{{ ("a{0.__class__}b{1}"|safe).format(42, "