blob: 7c812c0942e357beccce07763e96d8cbacd9139a [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"""
Armin Ronacher24b65582008-05-26 13:35:58 +02009from py.test import raises
Armin Ronacher5411ce72008-05-25 11:36:22 +020010from jinja2 import Environment
Armin Ronacher6df604e2008-05-23 22:18:38 +020011from jinja2.sandbox import SandboxedEnvironment, \
12 ImmutableSandboxedEnvironment, unsafe
Armin Ronacher4e6f9a22008-05-23 23:57:38 +020013from jinja2 import Markup, escape
Armin Ronacher24b65582008-05-26 13:35:58 +020014from jinja2.exceptions import SecurityError
Armin Ronacherccf284b2007-05-21 16:44:26 +020015
16
17class PrivateStuff(object):
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020018
19 def bar(self):
20 return 23
21
22 @unsafe
23 def foo(self):
24 return 42
25
26 def __repr__(self):
27 return 'PrivateStuff'
Armin Ronacherccf284b2007-05-21 16:44:26 +020028
29
30class PublicStuff(object):
Armin Ronacherccf284b2007-05-21 16:44:26 +020031 bar = lambda self: 23
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020032 _foo = lambda self: 42
33
34 def __repr__(self):
35 return 'PublicStuff'
Armin Ronacherccf284b2007-05-21 16:44:26 +020036
37
38test_unsafe = '''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020039>>> env = MODULE.SandboxedEnvironment()
Armin Ronacherccf284b2007-05-21 16:44:26 +020040>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020041Traceback (most recent call last):
42 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020043SecurityError: <bound method PrivateStuff.foo of PrivateStuff> is not safely callable
Armin Ronacherccf284b2007-05-21 16:44:26 +020044>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())
45u'23'
46
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020047>>> env.from_string("{{ foo._foo() }}").render(foo=MODULE.PublicStuff())
48Traceback (most recent call last):
49 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020050SecurityError: access to attribute '_foo' of 'PublicStuff' object is unsafe.
Armin Ronacherccf284b2007-05-21 16:44:26 +020051>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())
52u'23'
53
54>>> env.from_string("{{ foo.__class__ }}").render(foo=42)
55u''
Armin Ronacherccf284b2007-05-21 16:44:26 +020056>>> env.from_string("{{ foo.func_code }}").render(foo=lambda:None)
57u''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020058>>> env.from_string("{{ foo.__class__.__subclasses__() }}").render(foo=42)
59Traceback (most recent call last):
60 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020061SecurityError: access to attribute '__class__' of 'int' object is unsafe.
Armin Ronacherccf284b2007-05-21 16:44:26 +020062'''
63
64
65test_restricted = '''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020066>>> env = MODULE.SandboxedEnvironment()
Armin Ronacherccf284b2007-05-21 16:44:26 +020067>>> env.from_string("{% for item.attribute in seq %}...{% endfor %}")
68Traceback (most recent call last):
69 ...
Armin Ronacher09c002e2008-05-10 22:21:30 +020070TemplateSyntaxError: expected token 'in', got '.' (line 1)
Armin Ronacherecc051b2007-06-01 18:25:28 +020071>>> env.from_string("{% for foo, bar.baz in seq %}...{% endfor %}")
72Traceback (most recent call last):
73 ...
Armin Ronacher09c002e2008-05-10 22:21:30 +020074TemplateSyntaxError: expected token 'in', got '.' (line 1)
Armin Ronacherccf284b2007-05-21 16:44:26 +020075'''
Armin Ronacher6df604e2008-05-23 22:18:38 +020076
77
78test_immutable_environment = '''
79>>> env = MODULE.ImmutableSandboxedEnvironment()
80>>> env.from_string('{{ [].append(23) }}').render()
81Traceback (most recent call last):
82 ...
83SecurityError: access to attribute 'append' of 'list' object is unsafe.
84>>> env.from_string('{{ {1:2}.clear() }}').render()
85Traceback (most recent call last):
86 ...
87SecurityError: access to attribute 'clear' of 'dict' object is unsafe.
88'''
Armin Ronacher4e6f9a22008-05-23 23:57:38 +020089
90def test_markup_operations():
91 # adding two strings should escape the unsafe one
92 unsafe = '<script type="application/x-some-script">alert("foo");</script>'
93 safe = Markup('<em>username</em>')
94 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)
95
96 # string interpolations are safe to use too
97 assert Markup('<em>%s</em>') % '<bad user>' == \
98 '<em>&lt;bad user&gt;</em>'
99 assert Markup('<em>%(username)s</em>') % {
100 'username': '<bad user>'
101 } == '<em>&lt;bad user&gt;</em>'
102
103 # an escaped object is markup too
104 assert type(Markup('foo') + 'bar') is Markup
105
106 # and it implements __html__ by returning itself
107 x = Markup("foo")
108 assert x.__html__() is x
109
110 # it also knows how to treat __html__ objects
111 class Foo(object):
112 def __html__(self):
113 return '<em>awesome</em>'
114 def __unicode__(self):
115 return 'awesome'
116 assert Markup(Foo()) == '<em>awesome</em>'
117 assert Markup('<strong>%s</strong>') % Foo() == \
118 '<strong><em>awesome</em></strong>'
Armin Ronacher9cf95912008-05-24 19:54:43 +0200119
120 # escaping and unescaping
121 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
122 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
123 assert Markup("&lt;test&gt;").unescape() == "<test>"
Armin Ronacher5411ce72008-05-25 11:36:22 +0200124
125
126def test_template_data():
127 env = Environment(autoescape=True)
128 t = env.from_string('{% macro say_hello(name) %}'
129 '<p>Hello {{ name }}!</p>{% endmacro %}'
130 '{{ say_hello("<blink>foo</blink>") }}')
131 escaped_out = '<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>'
132 assert t.render() == escaped_out
133 assert unicode(t.module) == escaped_out
134 assert escape(t.module) == escaped_out
135 assert t.module.say_hello('<blink>foo</blink>') == escaped_out
136 assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out
Armin Ronacher24b65582008-05-26 13:35:58 +0200137
138
139def test_attr_filter():
140 env = SandboxedEnvironment()
141 tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}')
142 raises(SecurityError, tmpl.render)