Skip to content
Myr

sandbox.rst

This file

Path

docs/sandbox.rst

46 lines · 1.5 KiB

Last changed by

some more documentation updates and minor code cleanups. Additionally True and true in the template are the same now, same for false/False and none/None.Armin Ronacherbefore adoption9bb7e47

History of this file

Take it

documentation updateArmin Ronacherbefore adoption5cdc1ac

lines 1–22why, one step back
1Sandbox
2=======34The Jinja2 sandbox can be used to evaluate untrusted code. Access to unsafe5attributes and methods is prohibited.67Assuming `env` is a :class:`SandboxedEnvironment` in the default configuration8the following piece of code shows how it works:910>>> env.from_string("{{ func.func_code }}").render(func=lambda:None)11u''12>>> env.from_string("{{ func.func_code.do_something }}").render(func=lambda:None)13Traceback (most recent call last):14 ...15SecurityError: access to attribute 'func_code' of 'function' object is unsafe.161718.. module:: jinja2.sandbox1920.. autoclass:: SandboxedEnvironment([options])21 :members: is_safe_attribute, is_safe_callable22

added `ImmutableSandboxedEnvironment`.Armin Ronacherbefore adoption522cad6

lines 23–24why, one step back
23.. autoclass:: ImmutableSandboxedEnvironment([options])
24

documentation updateArmin Ronacherbefore adoption5cdc1ac

lines 25–29why, one step back
25.. autoexception:: SecurityError
2627.. autofunction:: unsafe2829.. autofunction:: is_internal_attribute

added `ImmutableSandboxedEnvironment`.Armin Ronacherbefore adoption522cad6

line 30why, one step back
30

improved sandbox and updated setup.pyArmin Ronacherbefore adoptiond71fff0

line 31why, one step back
31.. autofunction:: modifies_known_mutable

some more documentation updates and minor code cleanups. Additionally True and true in the template are the same now, same for false/False and none/None.Armin Ronacherbefore adoption9bb7e47

lines 32–46why, one step back
32
33.. admonition:: Note3435 The Jinja2 sandbox alone is no solution for perfect security. Especially36 for web applications you have to keep in mind that users may create37 templates with arbitrary HTML in so it's crucial to ensure that (if you38 are running multiple users on the same server) they can't harm each other39 via JavaScript insertions and much more.4041 Also the sandbox is only as good as the configuration. We stronly42 recommend only passing non-shared resources to the template and use43 some sort of whitelisting for attributes.4445 Also keep in mind that templates may raise runtime or compile time errors,46 so make sure to catch them.