blob: 78a1e0a2f5ad5661bde1084bd7f7cc2706ac6d3e [file] [log] [blame]
Armin Ronacherccf284b2007-05-21 16:44:26 +02001# -*- coding: utf-8 -*-
2"""
3 unit test for security features
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 :copyright: 2007 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8"""
9
10
11class PrivateStuff(object):
12 bar = lambda self: 23
13 foo = lambda self: 42
14 foo.jinja_unsafe_call = True
15
16
17class PublicStuff(object):
18 jinja_allowed_attributes = ['bar']
19 bar = lambda self: 23
20 foo = lambda self: 42
21
22
23test_unsafe = '''
24>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())
25u''
26>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())
27u'23'
28
29>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PublicStuff())
30u''
31>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())
32u'23'
33
34>>> env.from_string("{{ foo.__class__ }}").render(foo=42)
35u''
36
37>>> env.from_string("{{ foo.func_code }}").render(foo=lambda:None)
38u''
39'''
40
41
42test_restricted = '''
43>>> env.from_string("{% for item.attribute in seq %}...{% endfor %}")
44Traceback (most recent call last):
45 ...
Armin Ronacher1cc232c2007-09-07 17:52:41 +020046TemplateSyntaxError: cannot assign to expression (line 1)
Armin Ronacherecc051b2007-06-01 18:25:28 +020047>>> env.from_string("{% for foo, bar.baz in seq %}...{% endfor %}")
48Traceback (most recent call last):
49 ...
Armin Ronacher1cc232c2007-09-07 17:52:41 +020050TemplateSyntaxError: cannot assign to expression (line 1)
Armin Ronacherccf284b2007-05-21 16:44:26 +020051'''