blob: 68b1515743923022c85549bda9bcfcc5aebd995e [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 Ronacher5411ce72008-05-25 11:36:22 +02009from jinja2 import Environment
Armin Ronacher6df604e2008-05-23 22:18:38 +020010from jinja2.sandbox import SandboxedEnvironment, \
11 ImmutableSandboxedEnvironment, unsafe
Armin Ronacher4e6f9a22008-05-23 23:57:38 +020012from jinja2 import Markup, escape
Armin Ronacherccf284b2007-05-21 16:44:26 +020013
14
15class PrivateStuff(object):
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020016
17 def bar(self):
18 return 23
19
20 @unsafe
21 def foo(self):
22 return 42
23
24 def __repr__(self):
25 return 'PrivateStuff'
Armin Ronacherccf284b2007-05-21 16:44:26 +020026
27
28class PublicStuff(object):
Armin Ronacherccf284b2007-05-21 16:44:26 +020029 bar = lambda self: 23
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020030 _foo = lambda self: 42
31
32 def __repr__(self):
33 return 'PublicStuff'
Armin Ronacherccf284b2007-05-21 16:44:26 +020034
35
36test_unsafe = '''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020037>>> env = MODULE.SandboxedEnvironment()
Armin Ronacherccf284b2007-05-21 16:44:26 +020038>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020039Traceback (most recent call last):
40 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020041SecurityError: <bound method PrivateStuff.foo of PrivateStuff> is not safely callable
Armin Ronacherccf284b2007-05-21 16:44:26 +020042>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())
43u'23'
44
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020045>>> env.from_string("{{ foo._foo() }}").render(foo=MODULE.PublicStuff())
46Traceback (most recent call last):
47 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020048SecurityError: access to attribute '_foo' of 'PublicStuff' object is unsafe.
Armin Ronacherccf284b2007-05-21 16:44:26 +020049>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())
50u'23'
51
52>>> env.from_string("{{ foo.__class__ }}").render(foo=42)
53u''
Armin Ronacherccf284b2007-05-21 16:44:26 +020054>>> env.from_string("{{ foo.func_code }}").render(foo=lambda:None)
55u''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020056>>> env.from_string("{{ foo.__class__.__subclasses__() }}").render(foo=42)
57Traceback (most recent call last):
58 ...
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +020059SecurityError: access to attribute '__class__' of 'int' object is unsafe.
Armin Ronacherccf284b2007-05-21 16:44:26 +020060'''
61
62
63test_restricted = '''
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020064>>> env = MODULE.SandboxedEnvironment()
Armin Ronacherccf284b2007-05-21 16:44:26 +020065>>> env.from_string("{% for item.attribute in seq %}...{% endfor %}")
66Traceback (most recent call last):
67 ...
Armin Ronacher09c002e2008-05-10 22:21:30 +020068TemplateSyntaxError: expected token 'in', got '.' (line 1)
Armin Ronacherecc051b2007-06-01 18:25:28 +020069>>> env.from_string("{% for foo, bar.baz in seq %}...{% endfor %}")
70Traceback (most recent call last):
71 ...
Armin Ronacher09c002e2008-05-10 22:21:30 +020072TemplateSyntaxError: expected token 'in', got '.' (line 1)
Armin Ronacherccf284b2007-05-21 16:44:26 +020073'''
Armin Ronacher6df604e2008-05-23 22:18:38 +020074
75
76test_immutable_environment = '''
77>>> env = MODULE.ImmutableSandboxedEnvironment()
78>>> env.from_string('{{ [].append(23) }}').render()
79Traceback (most recent call last):
80 ...
81SecurityError: access to attribute 'append' of 'list' object is unsafe.
82>>> env.from_string('{{ {1:2}.clear() }}').render()
83Traceback (most recent call last):
84 ...
85SecurityError: access to attribute 'clear' of 'dict' object is unsafe.
86'''
Armin Ronacher4e6f9a22008-05-23 23:57:38 +020087
88def test_markup_operations():
89 # adding two strings should escape the unsafe one
90 unsafe = '<script type="application/x-some-script">alert("foo");</script>'
91 safe = Markup('<em>username</em>')
92 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)
93
94 # string interpolations are safe to use too
95 assert Markup('<em>%s</em>') % '<bad user>' == \
96 '<em>&lt;bad user&gt;</em>'
97 assert Markup('<em>%(username)s</em>') % {
98 'username': '<bad user>'
99 } == '<em>&lt;bad user&gt;</em>'
100
101 # an escaped object is markup too
102 assert type(Markup('foo') + 'bar') is Markup
103
104 # and it implements __html__ by returning itself
105 x = Markup("foo")
106 assert x.__html__() is x
107
108 # it also knows how to treat __html__ objects
109 class Foo(object):
110 def __html__(self):
111 return '<em>awesome</em>'
112 def __unicode__(self):
113 return 'awesome'
114 assert Markup(Foo()) == '<em>awesome</em>'
115 assert Markup('<strong>%s</strong>') % Foo() == \
116 '<strong><em>awesome</em></strong>'
Armin Ronacher9cf95912008-05-24 19:54:43 +0200117
118 # escaping and unescaping
119 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
120 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
121 assert Markup("&lt;test&gt;").unescape() == "<test>"
Armin Ronacher5411ce72008-05-25 11:36:22 +0200122
123
124def test_template_data():
125 env = Environment(autoescape=True)
126 t = env.from_string('{% macro say_hello(name) %}'
127 '<p>Hello {{ name }}!</p>{% endmacro %}'
128 '{{ say_hello("<blink>foo</blink>") }}')
129 escaped_out = '<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>'
130 assert t.render() == escaped_out
131 assert unicode(t.module) == escaped_out
132 assert escape(t.module) == escaped_out
133 assert t.module.say_hello('<blink>foo</blink>') == escaped_out
134 assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out