4 The Jinja2 sandbox can be used to evaluate untrusted code. Access to unsafe
5 attributes and methods is prohibited.
7 Assuming `env` is a :class:`SandboxedEnvironment` in the default configuration
8 the following piece of code shows how it works:
10 >>> env.from_string("{{ func.func_code }}").render(func=lambda:None)
12 >>> env.from_string("{{ func.func_code.do_something }}").render(func=lambda:None)
13 Traceback (most recent call last):
15 SecurityError: access to attribute 'func_code' of 'function' object is unsafe.
20 .. module:: jinja2.sandbox
22 .. autoclass:: SandboxedEnvironment([options])
23 :members: is_safe_attribute, is_safe_callable, default_binop_table,
24 default_unop_table, intercepted_binops, intercepted_unops,
27 .. autoclass:: ImmutableSandboxedEnvironment([options])
29 .. autoexception:: SecurityError
31 .. autofunction:: unsafe
33 .. autofunction:: is_internal_attribute
35 .. autofunction:: modifies_known_mutable
39 The Jinja2 sandbox alone is no solution for perfect security. Especially
40 for web applications you have to keep in mind that users may create
41 templates with arbitrary HTML in so it's crucial to ensure that (if you
42 are running multiple users on the same server) they can't harm each other
43 via JavaScript insertions and much more.
45 Also the sandbox is only as good as the configuration. We stronly
46 recommend only passing non-shared resources to the template and use
47 some sort of whitelisting for attributes.
49 Also keep in mind that templates may raise runtime or compile time errors,
50 so make sure to catch them.
57 For maximum performace Jinja2 will let operators call directly the type
58 specific callback methods. This means that it's not possible to have this
59 intercepted by overriding :meth:`Environment.call`. Furthermore a
60 conversion from operator to special method is not always directly possible
61 due to how operators work. For instance for divisions more than one
64 With Jinja 2.6 there is now support for explicit operator intercepting.
65 This can be used to customize specific operators as necessary. In order
66 to intercept an operator one has to override the
67 :attr:`SandboxedEnvironment.intercepted_binops` attribute. Once the
68 operator that needs to be intercepted is added to that set Jinja2 will
69 generate bytecode that calls the :meth:`SandboxedEnvironment.call_binop`
70 function. For unary operators the `unary` attributes and methods have to
73 The default implementation of :attr:`SandboxedEnvironment.call_binop`
74 will use the :attr:`SandboxedEnvironment.binop_table` to translate
75 operator symbols into callbacks performing the default operator behavior.
77 This example shows how the power (``**``) operator can be disabled in
80 from jinja2.sandbox import SandboxedEnvironment
83 class MyEnvironment(SandboxedEnvironment):
84 intercepted_binops = frozenset(['**'])
86 def call_binop(self, context, operator, left, right):
88 return self.undefined('the power operator is unavailable')
89 return SandboxedEnvironment.call_binop(self, context,
90 operator, left, right)
92 Make sure to always call into the super method, even if you are not
93 intercepting the call. Jinja2 might internally call the method to