| 1 |
Sandbox |
| 2 |
======= |
| 3 |
|
| 4 |
The Jinja sandbox can be used to render untrusted templates. Access to |
| 5 |
attributes, method calls, operators, mutating data structures, and |
| 6 |
string formatting can be intercepted and prohibited. |
| 7 |
|
| 8 |
.. code-block:: pycon |
| 9 |
|
| 10 |
>>> from jinja2.sandbox import SandboxedEnvironment |
| 11 |
>>> env = SandboxedEnvironment() |
| 12 |
>>> func = lambda: "Hello, Sandbox!" |
| 13 |
>>> env.from_string("{{ func() }}").render(func=func) |
| 14 |
'Hello, Sandbox!' |
| 15 |
>>> env.from_string("{{ func.__code__.co_code }}").render(func=func) |
| 16 |
Traceback (most recent call last): |
| 17 |
... |
| 18 |
SecurityError: access to attribute '__code__' of 'function' object is unsafe. |
| 19 |
|
| 20 |
A sandboxed environment can be useful, for example, to allow users of an |
| 21 |
internal reporting system to create custom emails. You would document |
| 22 |
what data is available in the templates, then the user would write a |
| 23 |
template using that information. Your code would generate the report |
| 24 |
data and pass it to the user's sandboxed template to render. |
| 25 |
|
| 26 |
|
| 27 |
Security Considerations |
| 28 |
----------------------- |
| 29 |
|
| 30 |
The sandbox alone is not a solution for perfect security. Keep these |
| 31 |
things in mind when using the sandbox. |
| 32 |
|
| 33 |
Templates can still raise errors when compiled or rendered. Your code |
| 34 |
should attempt to catch errors instead of crashing. |
| 35 |
|
| 36 |
It is possible to construct a relatively small template that renders to |
| 37 |
a very large amount of output, which could correspond to a high use of |
| 38 |
CPU or memory. You should run your application with limits on resources |
| 39 |
such as CPU and memory to mitigate this. |
| 40 |
|
| 41 |
Jinja only renders text, it does not understand, for example, JavaScript |
| 42 |
code. Depending on how the rendered template will be used, you may need |
| 43 |
to do other postprocessing to restrict the output. |
| 44 |
|
| 45 |
Pass only the data that is relevant to the template. Avoid passing |
| 46 |
global data, or objects with methods that have side effects. By default |
| 47 |
the sandbox prevents private and internal attribute access. You can |
| 48 |
override :meth:`~SandboxedEnvironment.is_safe_attribute` to further |
| 49 |
restrict attributes access. Decorate methods with :func:`unsafe` to |
| 50 |
prevent calling them from templates when passing objects as data. Use |
| 51 |
:class:`ImmutableSandboxedEnvironment` to prevent modifying lists and |
| 52 |
dictionaries. |
| 53 |
|
| 54 |
|
| 55 |
API |
| 56 |
--- |
| 57 |
|
| 58 |
.. module:: jinja2.sandbox |
| 59 |
|
| 60 |
.. autoclass:: SandboxedEnvironment([options]) |
| 61 |
:members: is_safe_attribute, is_safe_callable, default_binop_table, |
| 62 |
default_unop_table, intercepted_binops, intercepted_unops, |
| 63 |
call_binop, call_unop |
| 64 |
|
| 65 |
.. autoclass:: ImmutableSandboxedEnvironment([options]) |
| 66 |
|
| 67 |
.. autoexception:: SecurityError |
| 68 |
|
| 69 |
.. autofunction:: unsafe |
| 70 |
|
| 71 |
.. autofunction:: is_internal_attribute |
| 72 |
|
| 73 |
.. autofunction:: modifies_known_mutable |
| 74 |
|
| 75 |
|
| 76 |
Operator Intercepting |
| 77 |
--------------------- |
| 78 |
|
| 79 |
For performance, Jinja outputs operators directly when compiling. This |
| 80 |
means it's not possible to intercept operator behavior by overriding |
| 81 |
:meth:`SandboxEnvironment.call <Environment.call>` by default, because |
| 82 |
operator special methods are handled by the Python interpreter, and |
| 83 |
might not correspond with exactly one method depending on the operator's |
| 84 |
use. |
| 85 |
|
| 86 |
The sandbox can instruct the compiler to output a function to intercept |
| 87 |
certain operators instead. Override |
| 88 |
:attr:`SandboxedEnvironment.intercepted_binops` and |
| 89 |
:attr:`SandboxedEnvironment.intercepted_unops` with the operator symbols |
| 90 |
you want to intercept. The compiler will replace the symbols with calls |
| 91 |
to :meth:`SandboxedEnvironment.call_binop` and |
| 92 |
:meth:`SandboxedEnvironment.call_unop` instead. The default |
| 93 |
implementation of those methods will use |
| 94 |
:attr:`SandboxedEnvironment.binop_table` and |
| 95 |
:attr:`SandboxedEnvironment.unop_table` to translate operator symbols |
| 96 |
into :mod:`operator` functions. |
| 97 |
|
| 98 |
For example, the power (``**``) operator can be disabled: |
| 99 |
|
| 100 |
.. code-block:: python |
| 101 |
|
| 102 |
from jinja2.sandbox import SandboxedEnvironment |
| 103 |
|
| 104 |
class MyEnvironment(SandboxedEnvironment): |
| 105 |
intercepted_binops = frozenset(["**"]) |
| 106 |
|
| 107 |
def call_binop(self, context, operator, left, right): |
| 108 |
if operator == "**": |
| 109 |
return self.undefined("The power (**) operator is unavailable.") |
| 110 |
|
| 111 |
return super().call_binop(self, context, operator, left, right) |