Skip to content
Myr

sandbox.rst

This file

Path

docs/sandbox.rst

46 lines · 1.5 KiB

Last changed by

Not known.

History of this file

Take it

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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 1–46why, 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_callable2223.. autoclass:: ImmutableSandboxedEnvironment([options])2425.. autoexception:: SecurityError2627.. autofunction:: unsafe2829.. autofunction:: is_internal_attribute3031.. autofunction:: modifies_known_mutable3233.. 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.