Skip to content
Myr

test_security.py

This file

Path

tests/test_security.py

51 lines · 1.2 KiB · Python

Last changed by

[svn] added some more jinja unittestsArmin Ronacherbefore adoptionecc051b

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    unit test for security features4    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56    :copyright: 2007 by Armin Ronacher.7    :license: BSD, see LICENSE for more details.8"""91011class PrivateStuff(object):12    bar = lambda self: 2313    foo = lambda self: 4214    foo.jinja_unsafe_call = True151617class PublicStuff(object):18    jinja_allowed_attributes = ['bar']19    bar = lambda self: 2320    foo = lambda self: 42212223test_unsafe = '''24>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())25u''26>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())27u'23'2829>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PublicStuff())30u''31>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())32u'23'3334>>> env.from_string("{{ foo.__class__ }}").render(foo=42)35u''3637>>> env.from_string("{{ foo.func_code }}").render(foo=lambda:None)38u''39'''404142test_restricted = '''43>>> env.from_string("{% for item.attribute in seq %}...{% endfor %}")44Traceback (most recent call last):45    ...46TemplateSyntaxError: can't assign to expression. (line 1)47>>> env.from_string("{% for foo, bar.baz in seq %}...{% endfor %}")48Traceback (most recent call last):49    ...50TemplateSyntaxError: can't assign to expression. (line 1)51'''