API
- class _jinja2.sandbox.SandboxedEnvironment([_options])
- The sandboxed environment. It works like the regular environment buttells the compiler to generate sandboxed code. Additionally subclasses ofthis environment may override the methods that tell the runtime whatattributes or functions are safe to access.
If the template tries to access insecure code a SecurityError israised. However also other exceptions may occour during the rendering sothe caller has to ensure that all exceptions are catched.
- callbinop(_context, operator, left, right)
- For intercepted binary operator calls (intercepted_binops())this function is executed instead of the builtin operator. This canbe used to fine tune the behavior of certain operators.
New in version 2.6.
- callunop(_context, operator, arg)
- For intercepted unary operator calls (intercepted_unops())this function is executed instead of the builtin operator. This canbe used to fine tune the behavior of certain operators.
New in version 2.6.
- defaultbinop_table = {'//':
, '%': , '+': , '': , '-': ':, '/': , '* }_ default callback table for the binary operators. A copy of this isavailable on each instance of a sandboxed environment asbinop_table
default callback table for the unary operators. A copy of this isavailable on each instance of a sandboxed environment asunop_table
- a set of binary operators that should be intercepted. Each operatorthat is added to this set (empty by default) is delegated to thecall_binop() method that will perform the operator. The defaultoperator callback is specified by binop_table.
The following binary operators are interceptable://, %, +, , -, /, and *
The default operation form the operator table corresponds to thebuiltin function. Intercepted calls are always slower than the nativeoperator call, so make sure only to intercept the ones you areinterested in.
New in version 2.6.
- interceptedunops = frozenset([])_
- a set of unary operators that should be intercepted. Each operatorthat is added to this set (empty by default) is delegated to thecall_unop() method that will perform the operator. The defaultoperator callback is specified by unop_table.
The following unary operators are interceptable: +, -
The default operation form the operator table corresponds to thebuiltin function. Intercepted calls are always slower than the nativeoperator call, so make sure only to intercept the ones you areinterested in.
New in version 2.6.
- issafe_attribute(_obj, attr, value)
The sandboxed environment will call this method to check if theattribute of an object is safe to access. Per default all attributesstarting with an underscore are considered private as well as thespecial attributes of internal python objects as returned by theis_internal_attribute() function.
- Check if an object is safely callable. Per default a function isconsidered safe unless the unsafe_callable attribute exists and isTrue. Override this method to alter the behavior, but this won’taffect the unsafe decorator from this module.
- class _jinja2.sandbox.ImmutableSandboxedEnvironment([_options])
- Works exactly like the regular SandboxedEnvironment but does notpermit modifications on the builtin mutable objects list, set, anddict by using the modifies_known_mutable() function.
- exception _jinja2.sandbox.SecurityError(_message=None)
- Raised if a template tries to do something insecure if thesandbox is enabled.
@unsafe
def delete(self):
pass
- jinja2.sandbox.isinternal_attribute(_obj, attr)
- Test if the attribute given is an internal python attribute. Forexample this function returns True for the func_code attribute ofpython objects. This is useful if the environment methodis_safe_attribute() is overridden.
>>> from jinja2.sandbox import is_internal_attribute
>>> is_internal_attribute(lambda: None, "func_code")
True
>>> is_internal_attribute((lambda x:x).func_code, 'co_code')
True
>>> is_internal_attribute(str, "upper")
False
- jinja2.sandbox.modifiesknown_mutable(_obj, attr)
- This function checks if an attribute on a builtin mutable object(list, dict, set or deque) would modify it if called. It also supportsthe “user”-versions of the objects (sets.Set, UserDict.* etc.) andwith Python 2.6 onwards the abstract base classes MutableSet,MutableMapping, and MutableSequence.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) False isreturned.
>>> modifies_known_mutable("foo", "upper")
False
提示
Jinja2 沙箱自己并没有彻底解决安全问题。特别是对 web 应用,你必须晓得用户可能用任意 HTML 来创建模板,所以保证他们不通过注入 JavaScript 或其它更多方法来互相损害至关重要(如果你在同一个服务器上运行多用户)。
同样,沙箱的好处取决于配置。我们强烈建议只向模板传递非共享资源,并且使用某种属性白名单。
也请记住,模板会抛出运行时或编译期错误,确保捕获它们。