Skip to content
Myr

test_security.py

This file

Path

tests/test_security.py

138 lines · 4.3 KiB · Python

Last changed by

Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b

History of this file

Take it

The walk stopped after 200 commits: lines older than b217886 point at it.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 1–5why, one step back
1# -*- coding: utf-8 -*-
2"""3 unit test for security features4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5

Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b

line 6why, one step back
6 :copyright: (c) 2009 by the Jinja Team.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 7–8why, one step back
7 :license: BSD, see LICENSE for more details.
8"""

added attr filterArmin Ronacherbefore adoption24b6558

line 9why, one step back
9from py.test import raises

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

line 10why, one step back
10from jinja2 import Environment

more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e

lines 11–12why, one step back
11from jinja2.sandbox import SandboxedEnvironment, \
12 ImmutableSandboxedEnvironment, unsafe

added unittest for Markup and let the markup constructor accept `__html__` objectsArmin Ronacherbefore adoption4e6f9a2

line 13why, one step back
13from jinja2 import Markup, escape

Various cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055

line 14why, one step back
14from jinja2.exceptions import SecurityError, TemplateSyntaxError

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 15–42why, one step back
15
1617class PrivateStuff(object):1819 def bar(self):20 return 232122 @unsafe23 def foo(self):24 return 422526 def __repr__(self):27 return 'PrivateStuff'282930class PublicStuff(object):31 bar = lambda self: 2332 _foo = lambda self: 423334 def __repr__(self):35 return 'PublicStuff'363738test_unsafe = '''39>>> env = MODULE.SandboxedEnvironment()40>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())41Traceback (most recent call last):42 ...

documentation updateArmin Ronacherbefore adoption5cdc1ac

line 43why, one step back
43SecurityError: <bound method PrivateStuff.foo of PrivateStuff> is not safely callable

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 44–49why, one step back
44>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())
45u'23'4647>>> env.from_string("{{ foo._foo() }}").render(foo=MODULE.PublicStuff())48Traceback (most recent call last):49 ...

documentation updateArmin Ronacherbefore adoption5cdc1ac

line 50why, one step back
50SecurityError: access to attribute '_foo' of 'PublicStuff' object is unsafe.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 51–60why, one step back
51>>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())
52u'23'5354>>> env.from_string("{{ foo.__class__ }}").render(foo=42)55u''56>>> env.from_string("{{ foo.func_code }}").render(foo=lambda:None)57u''58>>> env.from_string("{{ foo.__class__.__subclasses__() }}").render(foo=42)59Traceback (most recent call last):60 ...

documentation updateArmin Ronacherbefore adoption5cdc1ac

line 61why, one step back
61SecurityError: access to attribute '__class__' of 'int' object is unsafe.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 62–64why, one step back
62'''
6364

Various cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055

lines 65–70why, one step back
65def test_restricted():
66 env = SandboxedEnvironment()67 raises(TemplateSyntaxError, env.from_string,68 "{% for item.attribute in seq %}...{% endfor %}")69 raises(TemplateSyntaxError, env.from_string,70 "{% for foo, bar.baz in seq %}...{% endfor %}")

more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e

lines 71–82why, one step back
71
7273test_immutable_environment = '''74>>> env = MODULE.ImmutableSandboxedEnvironment()75>>> env.from_string('{{ [].append(23) }}').render()76Traceback (most recent call last):77 ...78SecurityError: access to attribute 'append' of 'list' object is unsafe.79>>> env.from_string('{{ {1:2}.clear() }}').render()80Traceback (most recent call last):81 ...82SecurityError: access to attribute 'clear' of 'dict' object is unsafe.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

line 83why, one step back
83'''

Various cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055

line 84why, one step back
84

added unittest for Markup and let the markup constructor accept `__html__` objectsArmin Ronacherbefore adoption4e6f9a2

lines 85–114why, one step back
85
86def test_markup_operations():87 # adding two strings should escape the unsafe one88 unsafe = '<script type="application/x-some-script">alert("foo");</script>'89 safe = Markup('<em>username</em>')90 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)9192 # string interpolations are safe to use too93 assert Markup('<em>%s</em>') % '<bad user>' == \94 '<em>&lt;bad user&gt;</em>'95 assert Markup('<em>%(username)s</em>') % {96 'username': '<bad user>'97 } == '<em>&lt;bad user&gt;</em>'9899 # an escaped object is markup too100 assert type(Markup('foo') + 'bar') is Markup101102 # and it implements __html__ by returning itself103 x = Markup("foo")104 assert x.__html__() is x105106 # it also knows how to treat __html__ objects107 class Foo(object):108 def __html__(self):109 return '<em>awesome</em>'110 def __unicode__(self):111 return 'awesome'112 assert Markup(Foo()) == '<em>awesome</em>'113 assert Markup('<strong>%s</strong>') % Foo() == \114 '<strong><em>awesome</em></strong>'

fixed bug with static unicode strings and auto escapingArmin Ronacherbefore adoption9cf9591

lines 115–119why, one step back
115
116 # escaping and unescaping117 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'118 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"119 assert Markup("&lt;test&gt;").unescape() == "<test>"

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

lines 120–132why, one step back
120
121122def test_template_data():123 env = Environment(autoescape=True)124 t = env.from_string('{% macro say_hello(name) %}'125 '<p>Hello {{ name }}!</p>{% endmacro %}'126 '{{ say_hello("<blink>foo</blink>") }}')127 escaped_out = '<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>'128 assert t.render() == escaped_out129 assert unicode(t.module) == escaped_out130 assert escape(t.module) == escaped_out131 assert t.module.say_hello('<blink>foo</blink>') == escaped_out132 assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out

added attr filterArmin Ronacherbefore adoption24b6558

lines 133–138why, one step back
133
134135def test_attr_filter():136 env = SandboxedEnvironment()137 tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}')138 raises(SecurityError, tmpl.render)