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
Provenance
- Commit
644a281- Tree
fad2c63- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fb2295a
3 files changed+130 added−2 removed
modified:jinja2/testsuite/__init__.py+2 added-1 removed
43 unmodified lines44 45 46 def 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 line54 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 suiteadded:jinja2/testsuite/security.py+127 added
1+# -*- coding: utf-8 -*-2+"""3+ jinja2.testsuite.security4+ ~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ Checks the sandbox and other security features.7+8+ :copyright: (c) 2010 by the Jinja Team.9+ :license: BSD, see LICENSE for more details.10+"""11+import os12+import time13+import tempfile14+import unittest15+16+from jinja2.testsuite import JinjaTestCase17+18+from jinja2 import Environment19+from jinja2.sandbox import SandboxedEnvironment, \20+ ImmutableSandboxedEnvironment, unsafe21+from jinja2 import Markup, escape22+from jinja2.exceptions import SecurityError, TemplateSyntaxError23+24+25+class PrivateStuff(object):26+27+ def bar(self):28+ return 2329+30+ @unsafe31+ def foo(self):32+ return 4233+34+ def __repr__(self):35+ return 'PrivateStuff'36+37+38+class PublicStuff(object):39+ bar = lambda self: 2340+ _foo = lambda self: 4241+42+ def __repr__(self):43+ return 'PublicStuff'44+45+46+class SandboxTestCase(JinjaTestCase):47+48+ 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')53+54+ 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)61+62+ 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 %}")68+69+ 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)74+75+ # string interpolations are safe to use too76+ assert Markup('<em>%s</em>') % '<bad user>' == \77+ '<em><bad user></em>'78+ assert Markup('<em>%(username)s</em>') % {79+ 'username': '<bad user>'80+ } == '<em><bad user></em>'81+82+ # an escaped object is markup too83+ assert type(Markup('foo') + 'bar') is Markup84+85+ # and it implements __html__ by returning itself86+ x = Markup("foo")87+ assert x.__html__() is x88+89+ # 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>'98+99+ # escaping and unescaping100+ assert escape('"<>&\'') == '"<>&''101+ assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"102+ assert Markup("<test>").unescape() == "<test>"103+104+105+ 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 <blink>foo</blink>!</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_out116+117+118+ 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)122+123+124+def suite():125+ suite = unittest.TestSuite()126+ suite.addTest(unittest.makeSuite(SandboxTestCase))127+ return suitemodified:setup.py+1 added-1 removed
74 unmodified lines75 '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