Skip to content
Myr

Added the security tests.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent fb2295a · Browse files · 3 files+130 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+130 added−2 removed

  • other:+130 added−2 removed

3 files+130 added−2 removed

modified:jinja2/testsuite/__init__.py+2 added-1 removed
43 unmodified lines
444546def suite():47 from jinja2.testsuite import ext, filters, tests, core_tags, \48 loader, inheritance, imports, lexnparse48 loader, inheritance, imports, lexnparse, security49 suite = unittest.TestSuite()50 suite.addTest(ext.suite())51 suite.addTest(filters.suite())52 suite.addTest(tests.suite())
1 unmodified line
54 suite.addTest(loader.suite())55 suite.addTest(inheritance.suite())56 suite.addTest(imports.suite())57 suite.addTest(lexnparse.suite())58 suite.addTest(security.suite())59 return suite
added:jinja2/testsuite/security.py+127 added
1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.security4 ~~~~~~~~~~~~~~~~~~~~~~~~~56 Checks the sandbox and other security features.78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import os12import time13import tempfile14import unittest1516from jinja2.testsuite import JinjaTestCase1718from jinja2 import Environment19from jinja2.sandbox import SandboxedEnvironment, \20 ImmutableSandboxedEnvironment, unsafe21from jinja2 import Markup, escape22from jinja2.exceptions import SecurityError, TemplateSyntaxError232425class PrivateStuff(object):2627 def bar(self):28 return 232930 @unsafe31 def foo(self):32 return 423334 def __repr__(self):35 return 'PrivateStuff'363738class PublicStuff(object):39 bar = lambda self: 2340 _foo = lambda self: 424142 def __repr__(self):43 return 'PublicStuff'444546class SandboxTestCase(JinjaTestCase):4748 def test_unsafe(self):49 env = SandboxedEnvironment()50 self.assert_raises(SecurityError, env.from_string("{{ foo.foo() }}").render,51 foo=PrivateStuff())52 self.assert_equal(env.from_string("{{ foo.bar() }}").render(foo=PrivateStuff()), '23')5354 self.assert_raises(SecurityError, env.from_string("{{ foo._foo() }}").render,55 foo=PublicStuff())56 self.assert_equal(env.from_string("{{ foo.bar() }}").render(foo=PublicStuff()), '23')57 self.assert_equal(env.from_string("{{ foo.__class__ }}").render(foo=42), '')58 self.assert_equal(env.from_string("{{ foo.func_code }}").render(foo=lambda:None), '')59 self.assert_raises(SecurityError, env.from_string(60 "{{ foo.__class__.__subclasses__() }}").render, foo=42)6162 def test_restricted(self):63 env = SandboxedEnvironment()64 self.assert_raises(TemplateSyntaxError, env.from_string,65 "{% for item.attribute in seq %}...{% endfor %}")66 self.assert_raises(TemplateSyntaxError, env.from_string,67 "{% for foo, bar.baz in seq %}...{% endfor %}")6869 def test_markup_operations(self):70 # adding two strings should escape the unsafe one71 unsafe = '<script type="application/x-some-script">alert("foo");</script>'72 safe = Markup('<em>username</em>')73 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)7475 # string interpolations are safe to use too76 assert Markup('<em>%s</em>') % '<bad user>' == \77 '<em>&lt;bad user&gt;</em>'78 assert Markup('<em>%(username)s</em>') % {79 'username': '<bad user>'80 } == '<em>&lt;bad user&gt;</em>'8182 # an escaped object is markup too83 assert type(Markup('foo') + 'bar') is Markup8485 # and it implements __html__ by returning itself86 x = Markup("foo")87 assert x.__html__() is x8889 # it also knows how to treat __html__ objects90 class Foo(object):91 def __html__(self):92 return '<em>awesome</em>'93 def __unicode__(self):94 return 'awesome'95 assert Markup(Foo()) == '<em>awesome</em>'96 assert Markup('<strong>%s</strong>') % Foo() == \97 '<strong><em>awesome</em></strong>'9899 # escaping and unescaping100 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'101 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"102 assert Markup("&lt;test&gt;").unescape() == "<test>"103104105 def test_template_data(self):106 env = Environment(autoescape=True)107 t = env.from_string('{% macro say_hello(name) %}'108 '<p>Hello {{ name }}!</p>{% endmacro %}'109 '{{ say_hello("<blink>foo</blink>") }}')110 escaped_out = '<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>'111 assert t.render() == escaped_out112 assert unicode(t.module) == escaped_out113 assert escape(t.module) == escaped_out114 assert t.module.say_hello('<blink>foo</blink>') == escaped_out115 assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out116117118 def test_attr_filter(self):119 env = SandboxedEnvironment()120 tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}')121 self.assert_raises(SecurityError, tmpl.render)122123124def suite():125 suite = unittest.TestSuite()126 suite.addTest(unittest.makeSuite(SandboxTestCase))127 return suite
modified:setup.py+1 added-1 removed
74 unmodified lines
75 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',76 'Topic :: Software Development :: Libraries :: Python Modules',77 'Topic :: Text Processing :: Markup :: HTML'78 ],79 packages=['jinja2', 'jinja2.testsuite'],79 packages=['jinja2', 'jinja2.testsuite', 'jinja2.testsuite.res'],80 features={81 'speedups': Feature("optional C speed-enhancements",82 standard=False,83 ext_modules=[
12 unmodified lines